@orkestrel/contract 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/core/compilers.d.ts +3 -0
- package/dist/src/core/index.cjs +0 -67
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.js +0 -67
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/core/shapers.d.ts +2 -2
- package/dist/src/core/types.d.ts +49 -11
- package/package.json +1 -1
package/dist/src/core/index.js
CHANGED
|
@@ -1933,24 +1933,6 @@ function compileSchema(shape) {
|
|
|
1933
1933
|
case "raw": return shape.schema;
|
|
1934
1934
|
}
|
|
1935
1935
|
}
|
|
1936
|
-
/**
|
|
1937
|
-
* Compile a {@link ContractShape} into a runtime type guard.
|
|
1938
|
-
*
|
|
1939
|
-
* @remarks
|
|
1940
|
-
* Reuses the combinators: `literalOf` for literals, `arrayOf` for arrays,
|
|
1941
|
-
* `recordOf` for closed objects, `unionOf` for unions, `nullableOf` for nullable,
|
|
1942
|
-
* and `whereOf` for constraint refinement. Like every guard it is total — it
|
|
1943
|
-
* never throws (AGENTS §14).
|
|
1944
|
-
*
|
|
1945
|
-
* @param shape - The shape to compile
|
|
1946
|
-
* @returns A guard narrowing to the shape's inferred type
|
|
1947
|
-
*
|
|
1948
|
-
* @example
|
|
1949
|
-
* ```ts
|
|
1950
|
-
* const isUser = compileGuard(objectShape({ name: stringShape() }))
|
|
1951
|
-
* isUser({ name: 'Ada' }) // true
|
|
1952
|
-
* ```
|
|
1953
|
-
*/
|
|
1954
1936
|
function compileGuard(shape) {
|
|
1955
1937
|
switch (shape.type) {
|
|
1956
1938
|
case "string": return stringOf({
|
|
@@ -2009,31 +1991,6 @@ function compileGuard(shape) {
|
|
|
2009
1991
|
case "raw": return (_value) => true;
|
|
2010
1992
|
}
|
|
2011
1993
|
}
|
|
2012
|
-
/**
|
|
2013
|
-
* Compile a {@link ContractShape} into an input parser.
|
|
2014
|
-
*
|
|
2015
|
-
* @remarks
|
|
2016
|
-
* Reuses the leaf parsers (`parseString` / `parseInteger` / `parseNumber` /
|
|
2017
|
-
* `parseBoolean` / `parseRecord`) and coerces structurally. An object fails as a
|
|
2018
|
-
* whole on any required-field failure; a union returns a guard-valid value
|
|
2019
|
-
* unchanged, otherwise the first variant that both parses and guards wins.
|
|
2020
|
-
*
|
|
2021
|
-
* After coercing a leaf, it re-applies that leaf's REFINEMENTS through the same
|
|
2022
|
-
* combinators `compileGuard` uses — `stringOf` for a string's length/pattern and
|
|
2023
|
-
* `boundsOf` for a number's value and an array's length — so a value that coerces
|
|
2024
|
-
* but violates a bound parses to `undefined`. The result is full parse↔guard
|
|
2025
|
-
* soundness (AGENTS §14): a non-`undefined` parse always satisfies the contract's
|
|
2026
|
-
* `is`, refinements included.
|
|
2027
|
-
*
|
|
2028
|
-
* @param shape - The shape to compile
|
|
2029
|
-
* @returns A parser yielding the shape's inferred type or `undefined`
|
|
2030
|
-
*
|
|
2031
|
-
* @example
|
|
2032
|
-
* ```ts
|
|
2033
|
-
* const parseUser = compileParser(objectShape({ name: stringShape() }))
|
|
2034
|
-
* parseUser({ name: 'Ada' }) // { name: 'Ada' }
|
|
2035
|
-
* ```
|
|
2036
|
-
*/
|
|
2037
1994
|
function compileParser(shape) {
|
|
2038
1995
|
switch (shape.type) {
|
|
2039
1996
|
case "string": {
|
|
@@ -2154,30 +2111,6 @@ function compileParser(shape) {
|
|
|
2154
2111
|
case "raw": return (value) => value;
|
|
2155
2112
|
}
|
|
2156
2113
|
}
|
|
2157
|
-
/**
|
|
2158
|
-
* Compile a {@link ContractShape} into a deterministic seed value.
|
|
2159
|
-
*
|
|
2160
|
-
* @remarks
|
|
2161
|
-
* The same shape and the same `random` source always produce the same value, so
|
|
2162
|
-
* seed data is reproducible. Defaults to a {@link seededRandom} source seeded
|
|
2163
|
-
* from the wall clock when none is supplied. Throws on a degenerate empty
|
|
2164
|
-
* `literalShape` / `unionShape`, on a pattern-constrained `stringShape` whose
|
|
2165
|
-
* generated sample cannot satisfy the pattern, or on a `rawShape` (its embedded
|
|
2166
|
-
* schema is arbitrary and cannot be auto-generated) — a programmer error that
|
|
2167
|
-
* cannot generate a value (AGENTS §12). `createContract` runs
|
|
2168
|
-
* {@link validateShape} first, so a degenerate `literalShape` / `unionShape` /
|
|
2169
|
-
* bounded shape is normally caught there; these throws remain here as defense
|
|
2170
|
-
* for standalone `compileGenerator` use.
|
|
2171
|
-
*
|
|
2172
|
-
* @param shape - The shape to generate from
|
|
2173
|
-
* @param random - A seeded random source (defaults to `seededRandom(Date.now())`)
|
|
2174
|
-
* @returns A value matching the shape
|
|
2175
|
-
*
|
|
2176
|
-
* @example
|
|
2177
|
-
* ```ts
|
|
2178
|
-
* compileGenerator(stringShape({ min: 1, max: 4 })) // a random string of 1-4 characters (seed a RandomFunction for determinism)
|
|
2179
|
-
* ```
|
|
2180
|
-
*/
|
|
2181
2114
|
function compileGenerator(shape, random = seededRandom(Date.now())) {
|
|
2182
2115
|
switch (shape.type) {
|
|
2183
2116
|
case "string": {
|