@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
|
@@ -70,6 +70,7 @@ export declare function compileSchema(shape: ContractShape): JSONSchema;
|
|
|
70
70
|
* isUser({ name: 'Ada' }) // true
|
|
71
71
|
* ```
|
|
72
72
|
*/
|
|
73
|
+
export declare function compileGuard<S extends ContractShape>(shape: S): Guard<Infer<S>>;
|
|
73
74
|
export declare function compileGuard(shape: ContractShape): Guard<unknown>;
|
|
74
75
|
/**
|
|
75
76
|
* Compile a {@link ContractShape} into an input parser.
|
|
@@ -96,6 +97,7 @@ export declare function compileGuard(shape: ContractShape): Guard<unknown>;
|
|
|
96
97
|
* parseUser({ name: 'Ada' }) // { name: 'Ada' }
|
|
97
98
|
* ```
|
|
98
99
|
*/
|
|
100
|
+
export declare function compileParser<S extends ContractShape>(shape: S): Parser<Infer<S>>;
|
|
99
101
|
export declare function compileParser(shape: ContractShape): Parser<unknown>;
|
|
100
102
|
/**
|
|
101
103
|
* Compile a {@link ContractShape} into a deterministic seed value.
|
|
@@ -121,6 +123,7 @@ export declare function compileParser(shape: ContractShape): Parser<unknown>;
|
|
|
121
123
|
* compileGenerator(stringShape({ min: 1, max: 4 })) // a random string of 1-4 characters (seed a RandomFunction for determinism)
|
|
122
124
|
* ```
|
|
123
125
|
*/
|
|
126
|
+
export declare function compileGenerator<S extends ContractShape>(shape: S, random?: RandomFunction): Infer<S>;
|
|
124
127
|
export declare function compileGenerator(shape: ContractShape, random?: RandomFunction): unknown;
|
|
125
128
|
/**
|
|
126
129
|
* Compile a {@link ContractShape} into a {@link ContractInterface} — the four
|
package/dist/src/core/index.cjs
CHANGED
|
@@ -1934,24 +1934,6 @@ function compileSchema(shape) {
|
|
|
1934
1934
|
case "raw": return shape.schema;
|
|
1935
1935
|
}
|
|
1936
1936
|
}
|
|
1937
|
-
/**
|
|
1938
|
-
* Compile a {@link ContractShape} into a runtime type guard.
|
|
1939
|
-
*
|
|
1940
|
-
* @remarks
|
|
1941
|
-
* Reuses the combinators: `literalOf` for literals, `arrayOf` for arrays,
|
|
1942
|
-
* `recordOf` for closed objects, `unionOf` for unions, `nullableOf` for nullable,
|
|
1943
|
-
* and `whereOf` for constraint refinement. Like every guard it is total — it
|
|
1944
|
-
* never throws (AGENTS §14).
|
|
1945
|
-
*
|
|
1946
|
-
* @param shape - The shape to compile
|
|
1947
|
-
* @returns A guard narrowing to the shape's inferred type
|
|
1948
|
-
*
|
|
1949
|
-
* @example
|
|
1950
|
-
* ```ts
|
|
1951
|
-
* const isUser = compileGuard(objectShape({ name: stringShape() }))
|
|
1952
|
-
* isUser({ name: 'Ada' }) // true
|
|
1953
|
-
* ```
|
|
1954
|
-
*/
|
|
1955
1937
|
function compileGuard(shape) {
|
|
1956
1938
|
switch (shape.type) {
|
|
1957
1939
|
case "string": return stringOf({
|
|
@@ -2010,31 +1992,6 @@ function compileGuard(shape) {
|
|
|
2010
1992
|
case "raw": return (_value) => true;
|
|
2011
1993
|
}
|
|
2012
1994
|
}
|
|
2013
|
-
/**
|
|
2014
|
-
* Compile a {@link ContractShape} into an input parser.
|
|
2015
|
-
*
|
|
2016
|
-
* @remarks
|
|
2017
|
-
* Reuses the leaf parsers (`parseString` / `parseInteger` / `parseNumber` /
|
|
2018
|
-
* `parseBoolean` / `parseRecord`) and coerces structurally. An object fails as a
|
|
2019
|
-
* whole on any required-field failure; a union returns a guard-valid value
|
|
2020
|
-
* unchanged, otherwise the first variant that both parses and guards wins.
|
|
2021
|
-
*
|
|
2022
|
-
* After coercing a leaf, it re-applies that leaf's REFINEMENTS through the same
|
|
2023
|
-
* combinators `compileGuard` uses — `stringOf` for a string's length/pattern and
|
|
2024
|
-
* `boundsOf` for a number's value and an array's length — so a value that coerces
|
|
2025
|
-
* but violates a bound parses to `undefined`. The result is full parse↔guard
|
|
2026
|
-
* soundness (AGENTS §14): a non-`undefined` parse always satisfies the contract's
|
|
2027
|
-
* `is`, refinements included.
|
|
2028
|
-
*
|
|
2029
|
-
* @param shape - The shape to compile
|
|
2030
|
-
* @returns A parser yielding the shape's inferred type or `undefined`
|
|
2031
|
-
*
|
|
2032
|
-
* @example
|
|
2033
|
-
* ```ts
|
|
2034
|
-
* const parseUser = compileParser(objectShape({ name: stringShape() }))
|
|
2035
|
-
* parseUser({ name: 'Ada' }) // { name: 'Ada' }
|
|
2036
|
-
* ```
|
|
2037
|
-
*/
|
|
2038
1995
|
function compileParser(shape) {
|
|
2039
1996
|
switch (shape.type) {
|
|
2040
1997
|
case "string": {
|
|
@@ -2155,30 +2112,6 @@ function compileParser(shape) {
|
|
|
2155
2112
|
case "raw": return (value) => value;
|
|
2156
2113
|
}
|
|
2157
2114
|
}
|
|
2158
|
-
/**
|
|
2159
|
-
* Compile a {@link ContractShape} into a deterministic seed value.
|
|
2160
|
-
*
|
|
2161
|
-
* @remarks
|
|
2162
|
-
* The same shape and the same `random` source always produce the same value, so
|
|
2163
|
-
* seed data is reproducible. Defaults to a {@link seededRandom} source seeded
|
|
2164
|
-
* from the wall clock when none is supplied. Throws on a degenerate empty
|
|
2165
|
-
* `literalShape` / `unionShape`, on a pattern-constrained `stringShape` whose
|
|
2166
|
-
* generated sample cannot satisfy the pattern, or on a `rawShape` (its embedded
|
|
2167
|
-
* schema is arbitrary and cannot be auto-generated) — a programmer error that
|
|
2168
|
-
* cannot generate a value (AGENTS §12). `createContract` runs
|
|
2169
|
-
* {@link validateShape} first, so a degenerate `literalShape` / `unionShape` /
|
|
2170
|
-
* bounded shape is normally caught there; these throws remain here as defense
|
|
2171
|
-
* for standalone `compileGenerator` use.
|
|
2172
|
-
*
|
|
2173
|
-
* @param shape - The shape to generate from
|
|
2174
|
-
* @param random - A seeded random source (defaults to `seededRandom(Date.now())`)
|
|
2175
|
-
* @returns A value matching the shape
|
|
2176
|
-
*
|
|
2177
|
-
* @example
|
|
2178
|
-
* ```ts
|
|
2179
|
-
* compileGenerator(stringShape({ min: 1, max: 4 })) // a random string of 1-4 characters (seed a RandomFunction for determinism)
|
|
2180
|
-
* ```
|
|
2181
|
-
*/
|
|
2182
2115
|
function compileGenerator(shape, random = seededRandom(Date.now())) {
|
|
2183
2116
|
switch (shape.type) {
|
|
2184
2117
|
case "string": {
|