@orkestrel/contract 0.0.6 → 0.0.7
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/index.cjs +348 -0
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +245 -0
- package/dist/src/core/index.d.ts +245 -0
- package/dist/src/core/index.js +343 -1
- package/dist/src/core/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -150,6 +150,124 @@ export declare interface BooleanShapeOptions {
|
|
|
150
150
|
*/
|
|
151
151
|
export declare function boundsOf(min?: number, max?: number): Guard<number>;
|
|
152
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Build an {@link ObjectShape} from a JSON Schema object node's `properties` /
|
|
155
|
+
* `required` / `additionalProperties` keywords — the object-specialized branch
|
|
156
|
+
* of {@link buildShapeFromNode}.
|
|
157
|
+
*
|
|
158
|
+
* @remarks
|
|
159
|
+
* `properties` (when a record) contributes one child shape per own key, capped
|
|
160
|
+
* at {@link INFER_BREADTH_LIMIT}; a key is wrapped in {@link optionalShape}
|
|
161
|
+
* unless it appears as a string entry of `required`. A property whose value is
|
|
162
|
+
* not itself a record widens to {@link jsonShape}. `additionalProperties`:
|
|
163
|
+
* `false` closes the object; a record value recurses into it (`objectShape`
|
|
164
|
+
* validates extras against that shape); anything else — `true`, absent, or
|
|
165
|
+
* malformed — leaves the object OPEN (`true`), matching JSON Schema's own
|
|
166
|
+
* absent-means-open default and the fact that {@link valueToSchema} /
|
|
167
|
+
* {@link samplesToSchema} always emit the keyword explicitly, so an absent
|
|
168
|
+
* value only arises from a hand-written schema. When `properties` has MORE
|
|
169
|
+
* keys than {@link INFER_BREADTH_LIMIT}, the schema's own
|
|
170
|
+
* `additionalProperties` is OVERRIDDEN and forced to `true` (fully open) —
|
|
171
|
+
* a dropped key's value could otherwise fail a `false` or record-valued rest
|
|
172
|
+
* shape it was never checked against — mirroring `inferObject`'s
|
|
173
|
+
* `truncated ? true : !closed` rule (inferers.ts:485). The accumulator uses a
|
|
174
|
+
* null-prototype record so a property literally named `__proto__` becomes an
|
|
175
|
+
* own data key rather than mutating the prototype, mirroring
|
|
176
|
+
* {@link inferObject} (inferers.ts).
|
|
177
|
+
*
|
|
178
|
+
* @param schema - The object schema node
|
|
179
|
+
* @param depth - Remaining descent budget passed to child properties
|
|
180
|
+
* @param visited - The ancestor set guarding against cycles — recursion
|
|
181
|
+
* state owned by the {@link schemaToShape} entry point; passing a shared or
|
|
182
|
+
* pre-populated `WeakSet` changes cycle-detection behavior and is not
|
|
183
|
+
* supported usage
|
|
184
|
+
* @param memo - A per-call `(schema node, remaining depth) → shape` cache —
|
|
185
|
+
* recursion state owned by the {@link schemaToShape} entry point; passing
|
|
186
|
+
* a shared or pre-populated `WeakMap` changes caching behavior and is not
|
|
187
|
+
* supported usage
|
|
188
|
+
* @param description - The node's already-extracted `description`, if any
|
|
189
|
+
* @returns The built object shape
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* buildObjectShape(
|
|
194
|
+
* { type: 'object', properties: { id: { type: 'integer' } }, required: ['id'] },
|
|
195
|
+
* INFER_DEPTH_LIMIT,
|
|
196
|
+
* new WeakSet(),
|
|
197
|
+
* new WeakMap(),
|
|
198
|
+
* undefined,
|
|
199
|
+
* )
|
|
200
|
+
* // objectShape({ id: integerShape() }, { additionalProperties: true })
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
export declare function buildObjectShape(schema: JSONSchema, depth: number, visited: WeakSet<object>, memo: WeakMap<object, Map<number, ContractShape>>, description: string | undefined): ContractShape;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Build a {@link ContractShape} for one JSON Schema node — the recursive
|
|
207
|
+
* spine of {@link schemaToShape}, shared with per-child recursion via
|
|
208
|
+
* {@link schemaNodeToShape}.
|
|
209
|
+
*
|
|
210
|
+
* @remarks
|
|
211
|
+
* Every keyword is read defensively (the node's static `JSONSchema` type is
|
|
212
|
+
* NOT trusted at runtime — a caller-supplied node may be adversarial), so a
|
|
213
|
+
* malformed keyword falls through to the next rule instead of throwing.
|
|
214
|
+
* Precedence, top-down:
|
|
215
|
+
*
|
|
216
|
+
* 1. `enum` — an array with at least one string/number/boolean entry (finite
|
|
217
|
+
* numbers only) becomes a {@link literalShape} over the filtered entries.
|
|
218
|
+
* Non-primitive / non-finite entries are dropped; an empty result falls
|
|
219
|
+
* through.
|
|
220
|
+
* 2. `oneOf` — an array with at least one record entry becomes an
|
|
221
|
+
* {@link oneOfShape} over the recursively-built variants, provided the
|
|
222
|
+
* record-entry count is at or under {@link INFER_BREADTH_LIMIT}; OVER the
|
|
223
|
+
* limit, building a subset union would be strictly narrower than the
|
|
224
|
+
* schema's full union (a value matching only a dropped variant would be
|
|
225
|
+
* wrongly rejected), so the whole node widens to {@link jsonShape} instead
|
|
226
|
+
* of sampling a subset.
|
|
227
|
+
* 3. `anyOf` — identically, via {@link unionShape}.
|
|
228
|
+
* 4. `type: 'string'` / `'number'` / `'integer'` / `'boolean'` / `'null'` —
|
|
229
|
+
* the matching primitive shape, with length/range bounds derived via
|
|
230
|
+
* {@link deriveLengthBounds} / {@link deriveRangeBounds}. An integer node
|
|
231
|
+
* additionally drops its bounds when they describe an EMPTY integer range
|
|
232
|
+
* (e.g. `minimum: 1.5, maximum: 1.6`) — the same emptiness `validateShape`
|
|
233
|
+
* rejects — so the result is always a valid shape.
|
|
234
|
+
* 5. `type: 'array'` — an {@link arrayShape} whose element shape recurses into
|
|
235
|
+
* a record-valued `items` (widening to {@link jsonShape} otherwise), with
|
|
236
|
+
* bounds from `minItems` / `maxItems`.
|
|
237
|
+
* 6. `type: 'object'`, OR no `type` / `enum` / `oneOf` / `anyOf` but a
|
|
238
|
+
* record-valued `properties` — delegates to {@link buildObjectShape}.
|
|
239
|
+
* 7. Everything else — an empty schema, an unrecognized/absent `type`,
|
|
240
|
+
* exhausted depth/breadth, or an attempt failure — widens to
|
|
241
|
+
* {@link jsonShape} (the exact inverse of `compileSchema(jsonShape())`,
|
|
242
|
+
* and where the inferers themselves bottom out at their own limits).
|
|
243
|
+
*
|
|
244
|
+
* `format` and `pattern` are NEVER read into the compiled shape — `format` is
|
|
245
|
+
* annotation-only and `pattern` compiling an attacker-supplied string into a
|
|
246
|
+
* `RegExp` is a ReDoS vector. `description`, when a string, carries through to
|
|
247
|
+
* the produced shape's `description` option.
|
|
248
|
+
*
|
|
249
|
+
* @param schema - The schema node to convert
|
|
250
|
+
* @param depth - Remaining descent budget (0 halts recursion with `jsonShape()`)
|
|
251
|
+
* @param visited - The ancestor set guarding against cycles — recursion
|
|
252
|
+
* state owned by the {@link schemaToShape} entry point; passing a shared or
|
|
253
|
+
* pre-populated `WeakSet` changes cycle-detection behavior and is not
|
|
254
|
+
* supported usage
|
|
255
|
+
* @param memo - A per-call `(schema node, remaining depth) → shape` cache
|
|
256
|
+
* guarding against exponential re-conversion of a
|
|
257
|
+
* shared-reference schema DAG — recursion state owned by the
|
|
258
|
+
* {@link schemaToShape} entry point; passing a shared or
|
|
259
|
+
* pre-populated `WeakMap` changes caching behavior and is not
|
|
260
|
+
* supported usage
|
|
261
|
+
* @returns The built shape for `schema`
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```ts
|
|
265
|
+
* buildShapeFromNode({ type: 'string', minLength: 1 }, INFER_DEPTH_LIMIT, new WeakSet(), new WeakMap())
|
|
266
|
+
* // stringShape({ min: 1 })
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
export declare function buildShapeFromNode(schema: JSONSchema, depth: number, visited: WeakSet<object>, memo: WeakMap<object, Map<number, ContractShape>>): ContractShape;
|
|
270
|
+
|
|
153
271
|
/**
|
|
154
272
|
* Render a value as a deterministic, key-sorted JSON string.
|
|
155
273
|
*
|
|
@@ -390,6 +508,58 @@ export declare function createContract<S extends ContractShape>(shape: S): Contr
|
|
|
390
508
|
|
|
391
509
|
export declare function createContract(shape: ContractShape): ContractInterface<unknown>;
|
|
392
510
|
|
|
511
|
+
/**
|
|
512
|
+
* Derive `min`/`max` shape bounds from a pair of non-negative-integer JSON
|
|
513
|
+
* Schema length keywords (`minLength`/`maxLength`, `minItems`/`maxItems`).
|
|
514
|
+
*
|
|
515
|
+
* @remarks
|
|
516
|
+
* Total and pure. Either keyword is used only when it is a non-negative
|
|
517
|
+
* integer (`isInteger` + `>= 0`); a malformed value (a string, a negative
|
|
518
|
+
* number, `NaN`, `Infinity`, a fraction) is dropped as if absent. When both
|
|
519
|
+
* bounds are present and `min` exceeds `max`, the PAIR is dropped entirely
|
|
520
|
+
* (an unbounded shape is always a legal widening of a contradictory schema).
|
|
521
|
+
*
|
|
522
|
+
* @param min - The raw `minLength` / `minItems` keyword value
|
|
523
|
+
* @param max - The raw `maxLength` / `maxItems` keyword value
|
|
524
|
+
* @returns The derived `min` / `max` pair, either possibly `undefined`
|
|
525
|
+
*
|
|
526
|
+
* @example
|
|
527
|
+
* ```ts
|
|
528
|
+
* deriveLengthBounds(1, 10) // { min: 1, max: 10 }
|
|
529
|
+
* deriveLengthBounds(10, 1) // {} — contradictory, dropped
|
|
530
|
+
* deriveLengthBounds(-1, 10) // { max: 10 } — negative min dropped
|
|
531
|
+
* ```
|
|
532
|
+
*/
|
|
533
|
+
export declare function deriveLengthBounds(min: unknown, max: unknown): {
|
|
534
|
+
readonly min?: number;
|
|
535
|
+
readonly max?: number;
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Derive `min`/`max` shape bounds from a pair of finite-number JSON Schema
|
|
540
|
+
* range keywords (`minimum`/`maximum`).
|
|
541
|
+
*
|
|
542
|
+
* @remarks
|
|
543
|
+
* Total and pure. Either keyword is used only when it is a finite number
|
|
544
|
+
* ({@link isFiniteNumber} — rejects `NaN` / `±Infinity` / non-numbers); when
|
|
545
|
+
* both bounds are present and `min` exceeds `max`, the PAIR is dropped
|
|
546
|
+
* entirely, the same contradiction rule {@link deriveLengthBounds} applies.
|
|
547
|
+
*
|
|
548
|
+
* @param min - The raw `minimum` keyword value
|
|
549
|
+
* @param max - The raw `maximum` keyword value
|
|
550
|
+
* @returns The derived `min` / `max` pair, either possibly `undefined`
|
|
551
|
+
*
|
|
552
|
+
* @example
|
|
553
|
+
* ```ts
|
|
554
|
+
* deriveRangeBounds(0, 120) // { min: 0, max: 120 }
|
|
555
|
+
* deriveRangeBounds(5, 1) // {} — contradictory, dropped
|
|
556
|
+
* ```
|
|
557
|
+
*/
|
|
558
|
+
export declare function deriveRangeBounds(min: unknown, max: unknown): {
|
|
559
|
+
readonly min?: number;
|
|
560
|
+
readonly max?: number;
|
|
561
|
+
};
|
|
562
|
+
|
|
393
563
|
/**
|
|
394
564
|
* Count the enumerable own-symbol keys on a value.
|
|
395
565
|
*
|
|
@@ -2869,6 +3039,46 @@ export declare function sanitizeBudget(value: number | undefined, fallback: numb
|
|
|
2869
3039
|
*/
|
|
2870
3040
|
export declare type SchemaFormat = 'date-time' | 'date' | 'time' | 'uuid' | 'email' | 'uri';
|
|
2871
3041
|
|
|
3042
|
+
/**
|
|
3043
|
+
* Convert one JSON Schema node into a {@link ContractShape} — the recursion
|
|
3044
|
+
* entry point {@link schemaToShape} and {@link buildShapeFromNode} share for
|
|
3045
|
+
* every child (`items`, `properties` values, `additionalProperties`,
|
|
3046
|
+
* `oneOf` / `anyOf` variants).
|
|
3047
|
+
*
|
|
3048
|
+
* @remarks
|
|
3049
|
+
* Total: never throws. Guards depth exhaustion, a non-record node (the
|
|
3050
|
+
* node's static `JSONSchema` type is not trusted at runtime), and a cyclic
|
|
3051
|
+
* re-encounter of `schema` — all three widen to {@link jsonShape}. The
|
|
3052
|
+
* ancestor set is added to and removed from around the WHOLE subtree
|
|
3053
|
+
* conversion (not permanently), so a DAG-shaped schema reached twice via two
|
|
3054
|
+
* different, non-cyclic paths does not false-positive as a cycle. The
|
|
3055
|
+
* subtree conversion itself runs inside {@link attempt}, so a hostile
|
|
3056
|
+
* throwing getter/Proxy anywhere in `schema` cannot escape as a thrown error
|
|
3057
|
+
* — it degrades to {@link jsonShape} instead. A same-node re-conversion at
|
|
3058
|
+
* the same remaining `depth` is served from `memo` (guards a
|
|
3059
|
+
* shared-reference schema DAG against exponential blowup), mirroring
|
|
3060
|
+
* {@link inferObject} / {@link inferArray} (inferers.ts).
|
|
3061
|
+
*
|
|
3062
|
+
* @param schema - The schema node to convert
|
|
3063
|
+
* @param depth - Remaining descent budget
|
|
3064
|
+
* @param visited - The ancestor set guarding against cycles — recursion
|
|
3065
|
+
* state owned by the {@link schemaToShape} entry point; passing a shared or
|
|
3066
|
+
* pre-populated `WeakSet` changes cycle-detection behavior and is not
|
|
3067
|
+
* supported usage
|
|
3068
|
+
* @param memo - A per-call `(schema node, remaining depth) → shape` cache —
|
|
3069
|
+
* recursion state owned by the {@link schemaToShape} entry point; passing
|
|
3070
|
+
* a shared or pre-populated `WeakMap` changes caching behavior and is not
|
|
3071
|
+
* supported usage
|
|
3072
|
+
* @returns The built shape for `schema`, or {@link jsonShape} on any failure
|
|
3073
|
+
*
|
|
3074
|
+
* @example
|
|
3075
|
+
* ```ts
|
|
3076
|
+
* schemaNodeToShape({ type: 'boolean' }, INFER_DEPTH_LIMIT, new WeakSet(), new WeakMap())
|
|
3077
|
+
* // booleanShape()
|
|
3078
|
+
* ```
|
|
3079
|
+
*/
|
|
3080
|
+
export declare function schemaNodeToShape(schema: JSONSchema, depth: number, visited: WeakSet<object>, memo: WeakMap<object, Map<number, ContractShape>>): ContractShape;
|
|
3081
|
+
|
|
2872
3082
|
/**
|
|
2873
3083
|
* Wrap a non-object `JSONSchema` root in a single-property object schema, so
|
|
2874
3084
|
* an inferred primitive/array/union schema can flow into {@link schemaToParameters}
|
|
@@ -2921,6 +3131,41 @@ export declare function schemaToObject(schema: JSONSchema): JSONSchema;
|
|
|
2921
3131
|
*/
|
|
2922
3132
|
export declare function schemaToParameters(schema: JSONSchema): Readonly<Record<string, unknown>> | undefined;
|
|
2923
3133
|
|
|
3134
|
+
/**
|
|
3135
|
+
* Convert a runtime `JSONSchema` value into a validating {@link ContractShape}
|
|
3136
|
+
* — the inverse of {@link compileSchema}, and the validating sibling of
|
|
3137
|
+
* {@link rawShape} (which embeds a schema fragment WITHOUT validating it).
|
|
3138
|
+
*
|
|
3139
|
+
* @remarks
|
|
3140
|
+
* Total: NEVER throws, for any input — a malformed, cyclic, deeply-nested, or
|
|
3141
|
+
* outright hostile (throwing-getter Proxy) `schema` value all degrade to
|
|
3142
|
+
* {@link jsonShape} rather than escaping as an error, so
|
|
3143
|
+
* `createContract(schemaToShape(x))` is safe to call for any `x`. See
|
|
3144
|
+
* {@link buildShapeFromNode} for the exact per-keyword precedence.
|
|
3145
|
+
*
|
|
3146
|
+
* `format` and `pattern` are NEVER asserted by the compiled shape — `format`
|
|
3147
|
+
* is annotation-only (per the JSON Schema spec, it never narrows validation
|
|
3148
|
+
* on its own) and compiling an attacker-controlled `pattern` string into a
|
|
3149
|
+
* `RegExp` is a ReDoS vector; both keywords are read only far enough to be
|
|
3150
|
+
* ignored. Any node the walk cannot express — an empty `{}`, an
|
|
3151
|
+
* unrecognized `type`, a schema past {@link INFER_DEPTH_LIMIT} deep, or a
|
|
3152
|
+
* cyclic re-encounter — widens to {@link jsonShape} (accept any JSON), never
|
|
3153
|
+
* narrows: the round trip `compileGuard(schemaToShape(valueToSchema(v)))(v)`
|
|
3154
|
+
* holds for every `v`, with widening as the only source of looseness.
|
|
3155
|
+
*
|
|
3156
|
+
* @param schema - The JSON Schema value to convert
|
|
3157
|
+
* @returns The built {@link ContractShape}
|
|
3158
|
+
*
|
|
3159
|
+
* @example
|
|
3160
|
+
* ```ts
|
|
3161
|
+
* const schema = samplesToSchema([{ id: 1, name: 'Ada' }, { id: 2, name: 'Grace' }])
|
|
3162
|
+
* const contract = createContract(schemaToShape(schema))
|
|
3163
|
+
* contract.parse({ id: 3, name: 'Alan' }) // { id: 3, name: 'Alan' }
|
|
3164
|
+
* contract.parse({ id: 'nope' }) // undefined
|
|
3165
|
+
* ```
|
|
3166
|
+
*/
|
|
3167
|
+
export declare function schemaToShape(schema: JSONSchema): ContractShape;
|
|
3168
|
+
|
|
2924
3169
|
/**
|
|
2925
3170
|
* Build a deterministic pseudo-random source seeded from a single number.
|
|
2926
3171
|
*
|
package/dist/src/core/index.d.ts
CHANGED
|
@@ -150,6 +150,124 @@ export declare interface BooleanShapeOptions {
|
|
|
150
150
|
*/
|
|
151
151
|
export declare function boundsOf(min?: number, max?: number): Guard<number>;
|
|
152
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Build an {@link ObjectShape} from a JSON Schema object node's `properties` /
|
|
155
|
+
* `required` / `additionalProperties` keywords — the object-specialized branch
|
|
156
|
+
* of {@link buildShapeFromNode}.
|
|
157
|
+
*
|
|
158
|
+
* @remarks
|
|
159
|
+
* `properties` (when a record) contributes one child shape per own key, capped
|
|
160
|
+
* at {@link INFER_BREADTH_LIMIT}; a key is wrapped in {@link optionalShape}
|
|
161
|
+
* unless it appears as a string entry of `required`. A property whose value is
|
|
162
|
+
* not itself a record widens to {@link jsonShape}. `additionalProperties`:
|
|
163
|
+
* `false` closes the object; a record value recurses into it (`objectShape`
|
|
164
|
+
* validates extras against that shape); anything else — `true`, absent, or
|
|
165
|
+
* malformed — leaves the object OPEN (`true`), matching JSON Schema's own
|
|
166
|
+
* absent-means-open default and the fact that {@link valueToSchema} /
|
|
167
|
+
* {@link samplesToSchema} always emit the keyword explicitly, so an absent
|
|
168
|
+
* value only arises from a hand-written schema. When `properties` has MORE
|
|
169
|
+
* keys than {@link INFER_BREADTH_LIMIT}, the schema's own
|
|
170
|
+
* `additionalProperties` is OVERRIDDEN and forced to `true` (fully open) —
|
|
171
|
+
* a dropped key's value could otherwise fail a `false` or record-valued rest
|
|
172
|
+
* shape it was never checked against — mirroring `inferObject`'s
|
|
173
|
+
* `truncated ? true : !closed` rule (inferers.ts:485). The accumulator uses a
|
|
174
|
+
* null-prototype record so a property literally named `__proto__` becomes an
|
|
175
|
+
* own data key rather than mutating the prototype, mirroring
|
|
176
|
+
* {@link inferObject} (inferers.ts).
|
|
177
|
+
*
|
|
178
|
+
* @param schema - The object schema node
|
|
179
|
+
* @param depth - Remaining descent budget passed to child properties
|
|
180
|
+
* @param visited - The ancestor set guarding against cycles — recursion
|
|
181
|
+
* state owned by the {@link schemaToShape} entry point; passing a shared or
|
|
182
|
+
* pre-populated `WeakSet` changes cycle-detection behavior and is not
|
|
183
|
+
* supported usage
|
|
184
|
+
* @param memo - A per-call `(schema node, remaining depth) → shape` cache —
|
|
185
|
+
* recursion state owned by the {@link schemaToShape} entry point; passing
|
|
186
|
+
* a shared or pre-populated `WeakMap` changes caching behavior and is not
|
|
187
|
+
* supported usage
|
|
188
|
+
* @param description - The node's already-extracted `description`, if any
|
|
189
|
+
* @returns The built object shape
|
|
190
|
+
*
|
|
191
|
+
* @example
|
|
192
|
+
* ```ts
|
|
193
|
+
* buildObjectShape(
|
|
194
|
+
* { type: 'object', properties: { id: { type: 'integer' } }, required: ['id'] },
|
|
195
|
+
* INFER_DEPTH_LIMIT,
|
|
196
|
+
* new WeakSet(),
|
|
197
|
+
* new WeakMap(),
|
|
198
|
+
* undefined,
|
|
199
|
+
* )
|
|
200
|
+
* // objectShape({ id: integerShape() }, { additionalProperties: true })
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
export declare function buildObjectShape(schema: JSONSchema, depth: number, visited: WeakSet<object>, memo: WeakMap<object, Map<number, ContractShape>>, description: string | undefined): ContractShape;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Build a {@link ContractShape} for one JSON Schema node — the recursive
|
|
207
|
+
* spine of {@link schemaToShape}, shared with per-child recursion via
|
|
208
|
+
* {@link schemaNodeToShape}.
|
|
209
|
+
*
|
|
210
|
+
* @remarks
|
|
211
|
+
* Every keyword is read defensively (the node's static `JSONSchema` type is
|
|
212
|
+
* NOT trusted at runtime — a caller-supplied node may be adversarial), so a
|
|
213
|
+
* malformed keyword falls through to the next rule instead of throwing.
|
|
214
|
+
* Precedence, top-down:
|
|
215
|
+
*
|
|
216
|
+
* 1. `enum` — an array with at least one string/number/boolean entry (finite
|
|
217
|
+
* numbers only) becomes a {@link literalShape} over the filtered entries.
|
|
218
|
+
* Non-primitive / non-finite entries are dropped; an empty result falls
|
|
219
|
+
* through.
|
|
220
|
+
* 2. `oneOf` — an array with at least one record entry becomes an
|
|
221
|
+
* {@link oneOfShape} over the recursively-built variants, provided the
|
|
222
|
+
* record-entry count is at or under {@link INFER_BREADTH_LIMIT}; OVER the
|
|
223
|
+
* limit, building a subset union would be strictly narrower than the
|
|
224
|
+
* schema's full union (a value matching only a dropped variant would be
|
|
225
|
+
* wrongly rejected), so the whole node widens to {@link jsonShape} instead
|
|
226
|
+
* of sampling a subset.
|
|
227
|
+
* 3. `anyOf` — identically, via {@link unionShape}.
|
|
228
|
+
* 4. `type: 'string'` / `'number'` / `'integer'` / `'boolean'` / `'null'` —
|
|
229
|
+
* the matching primitive shape, with length/range bounds derived via
|
|
230
|
+
* {@link deriveLengthBounds} / {@link deriveRangeBounds}. An integer node
|
|
231
|
+
* additionally drops its bounds when they describe an EMPTY integer range
|
|
232
|
+
* (e.g. `minimum: 1.5, maximum: 1.6`) — the same emptiness `validateShape`
|
|
233
|
+
* rejects — so the result is always a valid shape.
|
|
234
|
+
* 5. `type: 'array'` — an {@link arrayShape} whose element shape recurses into
|
|
235
|
+
* a record-valued `items` (widening to {@link jsonShape} otherwise), with
|
|
236
|
+
* bounds from `minItems` / `maxItems`.
|
|
237
|
+
* 6. `type: 'object'`, OR no `type` / `enum` / `oneOf` / `anyOf` but a
|
|
238
|
+
* record-valued `properties` — delegates to {@link buildObjectShape}.
|
|
239
|
+
* 7. Everything else — an empty schema, an unrecognized/absent `type`,
|
|
240
|
+
* exhausted depth/breadth, or an attempt failure — widens to
|
|
241
|
+
* {@link jsonShape} (the exact inverse of `compileSchema(jsonShape())`,
|
|
242
|
+
* and where the inferers themselves bottom out at their own limits).
|
|
243
|
+
*
|
|
244
|
+
* `format` and `pattern` are NEVER read into the compiled shape — `format` is
|
|
245
|
+
* annotation-only and `pattern` compiling an attacker-supplied string into a
|
|
246
|
+
* `RegExp` is a ReDoS vector. `description`, when a string, carries through to
|
|
247
|
+
* the produced shape's `description` option.
|
|
248
|
+
*
|
|
249
|
+
* @param schema - The schema node to convert
|
|
250
|
+
* @param depth - Remaining descent budget (0 halts recursion with `jsonShape()`)
|
|
251
|
+
* @param visited - The ancestor set guarding against cycles — recursion
|
|
252
|
+
* state owned by the {@link schemaToShape} entry point; passing a shared or
|
|
253
|
+
* pre-populated `WeakSet` changes cycle-detection behavior and is not
|
|
254
|
+
* supported usage
|
|
255
|
+
* @param memo - A per-call `(schema node, remaining depth) → shape` cache
|
|
256
|
+
* guarding against exponential re-conversion of a
|
|
257
|
+
* shared-reference schema DAG — recursion state owned by the
|
|
258
|
+
* {@link schemaToShape} entry point; passing a shared or
|
|
259
|
+
* pre-populated `WeakMap` changes caching behavior and is not
|
|
260
|
+
* supported usage
|
|
261
|
+
* @returns The built shape for `schema`
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```ts
|
|
265
|
+
* buildShapeFromNode({ type: 'string', minLength: 1 }, INFER_DEPTH_LIMIT, new WeakSet(), new WeakMap())
|
|
266
|
+
* // stringShape({ min: 1 })
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
export declare function buildShapeFromNode(schema: JSONSchema, depth: number, visited: WeakSet<object>, memo: WeakMap<object, Map<number, ContractShape>>): ContractShape;
|
|
270
|
+
|
|
153
271
|
/**
|
|
154
272
|
* Render a value as a deterministic, key-sorted JSON string.
|
|
155
273
|
*
|
|
@@ -390,6 +508,58 @@ export declare function createContract<S extends ContractShape>(shape: S): Contr
|
|
|
390
508
|
|
|
391
509
|
export declare function createContract(shape: ContractShape): ContractInterface<unknown>;
|
|
392
510
|
|
|
511
|
+
/**
|
|
512
|
+
* Derive `min`/`max` shape bounds from a pair of non-negative-integer JSON
|
|
513
|
+
* Schema length keywords (`minLength`/`maxLength`, `minItems`/`maxItems`).
|
|
514
|
+
*
|
|
515
|
+
* @remarks
|
|
516
|
+
* Total and pure. Either keyword is used only when it is a non-negative
|
|
517
|
+
* integer (`isInteger` + `>= 0`); a malformed value (a string, a negative
|
|
518
|
+
* number, `NaN`, `Infinity`, a fraction) is dropped as if absent. When both
|
|
519
|
+
* bounds are present and `min` exceeds `max`, the PAIR is dropped entirely
|
|
520
|
+
* (an unbounded shape is always a legal widening of a contradictory schema).
|
|
521
|
+
*
|
|
522
|
+
* @param min - The raw `minLength` / `minItems` keyword value
|
|
523
|
+
* @param max - The raw `maxLength` / `maxItems` keyword value
|
|
524
|
+
* @returns The derived `min` / `max` pair, either possibly `undefined`
|
|
525
|
+
*
|
|
526
|
+
* @example
|
|
527
|
+
* ```ts
|
|
528
|
+
* deriveLengthBounds(1, 10) // { min: 1, max: 10 }
|
|
529
|
+
* deriveLengthBounds(10, 1) // {} — contradictory, dropped
|
|
530
|
+
* deriveLengthBounds(-1, 10) // { max: 10 } — negative min dropped
|
|
531
|
+
* ```
|
|
532
|
+
*/
|
|
533
|
+
export declare function deriveLengthBounds(min: unknown, max: unknown): {
|
|
534
|
+
readonly min?: number;
|
|
535
|
+
readonly max?: number;
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Derive `min`/`max` shape bounds from a pair of finite-number JSON Schema
|
|
540
|
+
* range keywords (`minimum`/`maximum`).
|
|
541
|
+
*
|
|
542
|
+
* @remarks
|
|
543
|
+
* Total and pure. Either keyword is used only when it is a finite number
|
|
544
|
+
* ({@link isFiniteNumber} — rejects `NaN` / `±Infinity` / non-numbers); when
|
|
545
|
+
* both bounds are present and `min` exceeds `max`, the PAIR is dropped
|
|
546
|
+
* entirely, the same contradiction rule {@link deriveLengthBounds} applies.
|
|
547
|
+
*
|
|
548
|
+
* @param min - The raw `minimum` keyword value
|
|
549
|
+
* @param max - The raw `maximum` keyword value
|
|
550
|
+
* @returns The derived `min` / `max` pair, either possibly `undefined`
|
|
551
|
+
*
|
|
552
|
+
* @example
|
|
553
|
+
* ```ts
|
|
554
|
+
* deriveRangeBounds(0, 120) // { min: 0, max: 120 }
|
|
555
|
+
* deriveRangeBounds(5, 1) // {} — contradictory, dropped
|
|
556
|
+
* ```
|
|
557
|
+
*/
|
|
558
|
+
export declare function deriveRangeBounds(min: unknown, max: unknown): {
|
|
559
|
+
readonly min?: number;
|
|
560
|
+
readonly max?: number;
|
|
561
|
+
};
|
|
562
|
+
|
|
393
563
|
/**
|
|
394
564
|
* Count the enumerable own-symbol keys on a value.
|
|
395
565
|
*
|
|
@@ -2869,6 +3039,46 @@ export declare function sanitizeBudget(value: number | undefined, fallback: numb
|
|
|
2869
3039
|
*/
|
|
2870
3040
|
export declare type SchemaFormat = 'date-time' | 'date' | 'time' | 'uuid' | 'email' | 'uri';
|
|
2871
3041
|
|
|
3042
|
+
/**
|
|
3043
|
+
* Convert one JSON Schema node into a {@link ContractShape} — the recursion
|
|
3044
|
+
* entry point {@link schemaToShape} and {@link buildShapeFromNode} share for
|
|
3045
|
+
* every child (`items`, `properties` values, `additionalProperties`,
|
|
3046
|
+
* `oneOf` / `anyOf` variants).
|
|
3047
|
+
*
|
|
3048
|
+
* @remarks
|
|
3049
|
+
* Total: never throws. Guards depth exhaustion, a non-record node (the
|
|
3050
|
+
* node's static `JSONSchema` type is not trusted at runtime), and a cyclic
|
|
3051
|
+
* re-encounter of `schema` — all three widen to {@link jsonShape}. The
|
|
3052
|
+
* ancestor set is added to and removed from around the WHOLE subtree
|
|
3053
|
+
* conversion (not permanently), so a DAG-shaped schema reached twice via two
|
|
3054
|
+
* different, non-cyclic paths does not false-positive as a cycle. The
|
|
3055
|
+
* subtree conversion itself runs inside {@link attempt}, so a hostile
|
|
3056
|
+
* throwing getter/Proxy anywhere in `schema` cannot escape as a thrown error
|
|
3057
|
+
* — it degrades to {@link jsonShape} instead. A same-node re-conversion at
|
|
3058
|
+
* the same remaining `depth` is served from `memo` (guards a
|
|
3059
|
+
* shared-reference schema DAG against exponential blowup), mirroring
|
|
3060
|
+
* {@link inferObject} / {@link inferArray} (inferers.ts).
|
|
3061
|
+
*
|
|
3062
|
+
* @param schema - The schema node to convert
|
|
3063
|
+
* @param depth - Remaining descent budget
|
|
3064
|
+
* @param visited - The ancestor set guarding against cycles — recursion
|
|
3065
|
+
* state owned by the {@link schemaToShape} entry point; passing a shared or
|
|
3066
|
+
* pre-populated `WeakSet` changes cycle-detection behavior and is not
|
|
3067
|
+
* supported usage
|
|
3068
|
+
* @param memo - A per-call `(schema node, remaining depth) → shape` cache —
|
|
3069
|
+
* recursion state owned by the {@link schemaToShape} entry point; passing
|
|
3070
|
+
* a shared or pre-populated `WeakMap` changes caching behavior and is not
|
|
3071
|
+
* supported usage
|
|
3072
|
+
* @returns The built shape for `schema`, or {@link jsonShape} on any failure
|
|
3073
|
+
*
|
|
3074
|
+
* @example
|
|
3075
|
+
* ```ts
|
|
3076
|
+
* schemaNodeToShape({ type: 'boolean' }, INFER_DEPTH_LIMIT, new WeakSet(), new WeakMap())
|
|
3077
|
+
* // booleanShape()
|
|
3078
|
+
* ```
|
|
3079
|
+
*/
|
|
3080
|
+
export declare function schemaNodeToShape(schema: JSONSchema, depth: number, visited: WeakSet<object>, memo: WeakMap<object, Map<number, ContractShape>>): ContractShape;
|
|
3081
|
+
|
|
2872
3082
|
/**
|
|
2873
3083
|
* Wrap a non-object `JSONSchema` root in a single-property object schema, so
|
|
2874
3084
|
* an inferred primitive/array/union schema can flow into {@link schemaToParameters}
|
|
@@ -2921,6 +3131,41 @@ export declare function schemaToObject(schema: JSONSchema): JSONSchema;
|
|
|
2921
3131
|
*/
|
|
2922
3132
|
export declare function schemaToParameters(schema: JSONSchema): Readonly<Record<string, unknown>> | undefined;
|
|
2923
3133
|
|
|
3134
|
+
/**
|
|
3135
|
+
* Convert a runtime `JSONSchema` value into a validating {@link ContractShape}
|
|
3136
|
+
* — the inverse of {@link compileSchema}, and the validating sibling of
|
|
3137
|
+
* {@link rawShape} (which embeds a schema fragment WITHOUT validating it).
|
|
3138
|
+
*
|
|
3139
|
+
* @remarks
|
|
3140
|
+
* Total: NEVER throws, for any input — a malformed, cyclic, deeply-nested, or
|
|
3141
|
+
* outright hostile (throwing-getter Proxy) `schema` value all degrade to
|
|
3142
|
+
* {@link jsonShape} rather than escaping as an error, so
|
|
3143
|
+
* `createContract(schemaToShape(x))` is safe to call for any `x`. See
|
|
3144
|
+
* {@link buildShapeFromNode} for the exact per-keyword precedence.
|
|
3145
|
+
*
|
|
3146
|
+
* `format` and `pattern` are NEVER asserted by the compiled shape — `format`
|
|
3147
|
+
* is annotation-only (per the JSON Schema spec, it never narrows validation
|
|
3148
|
+
* on its own) and compiling an attacker-controlled `pattern` string into a
|
|
3149
|
+
* `RegExp` is a ReDoS vector; both keywords are read only far enough to be
|
|
3150
|
+
* ignored. Any node the walk cannot express — an empty `{}`, an
|
|
3151
|
+
* unrecognized `type`, a schema past {@link INFER_DEPTH_LIMIT} deep, or a
|
|
3152
|
+
* cyclic re-encounter — widens to {@link jsonShape} (accept any JSON), never
|
|
3153
|
+
* narrows: the round trip `compileGuard(schemaToShape(valueToSchema(v)))(v)`
|
|
3154
|
+
* holds for every `v`, with widening as the only source of looseness.
|
|
3155
|
+
*
|
|
3156
|
+
* @param schema - The JSON Schema value to convert
|
|
3157
|
+
* @returns The built {@link ContractShape}
|
|
3158
|
+
*
|
|
3159
|
+
* @example
|
|
3160
|
+
* ```ts
|
|
3161
|
+
* const schema = samplesToSchema([{ id: 1, name: 'Ada' }, { id: 2, name: 'Grace' }])
|
|
3162
|
+
* const contract = createContract(schemaToShape(schema))
|
|
3163
|
+
* contract.parse({ id: 3, name: 'Alan' }) // { id: 3, name: 'Alan' }
|
|
3164
|
+
* contract.parse({ id: 'nope' }) // undefined
|
|
3165
|
+
* ```
|
|
3166
|
+
*/
|
|
3167
|
+
export declare function schemaToShape(schema: JSONSchema): ContractShape;
|
|
3168
|
+
|
|
2924
3169
|
/**
|
|
2925
3170
|
* Build a deterministic pseudo-random source seeded from a single number.
|
|
2926
3171
|
*
|