@orkestrel/contract 0.0.7 → 0.0.8
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/README.md +13 -5
- package/dist/src/core/index.cjs +1653 -611
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +626 -137
- package/dist/src/core/index.d.ts +626 -137
- package/dist/src/core/index.js +1640 -611
- package/dist/src/core/index.js.map +1 -1
- package/package.json +13 -15
|
@@ -62,6 +62,7 @@ export declare interface ArrayShape<S extends ContractShape = ContractShape> {
|
|
|
62
62
|
* @param items - The element shape
|
|
63
63
|
* @param options - Optional length bounds and `description`
|
|
64
64
|
* @returns An array shape
|
|
65
|
+
* @throws {ContractError} When a present bound is not a non-negative safe integer
|
|
65
66
|
*
|
|
66
67
|
* @example
|
|
67
68
|
* ```ts
|
|
@@ -82,13 +83,14 @@ export declare interface ArrayShapeOptions {
|
|
|
82
83
|
* a throw escape.
|
|
83
84
|
*
|
|
84
85
|
* @remarks
|
|
85
|
-
* The
|
|
86
|
+
* The sanctioned never-throw boundary for the guards (AGENTS §14). The
|
|
86
87
|
* `whereOf`, `lazyOf`, and `transformOf` combinators invoke caller-supplied
|
|
87
88
|
* callbacks *inside* a guard body, yet a guard must NEVER throw — it returns a
|
|
88
89
|
* `boolean`. This converts a throwing callback into a `Failure` so the
|
|
89
90
|
* surrounding guard can treat it as a non-match instead of propagating the
|
|
90
91
|
* exception, written once and shared rather than copy-pasted as ad-hoc
|
|
91
|
-
* `try`/`catch`.
|
|
92
|
+
* `try`/`catch`. The sole hand-written exception is {@link isContractError},
|
|
93
|
+
* whose local boundary avoids an `errors` → `helpers` dependency inversion.
|
|
92
94
|
*
|
|
93
95
|
* @param callback - The callback to invoke with no arguments
|
|
94
96
|
* @returns A `Success` carrying the return value, or a `Failure` carrying the
|
|
@@ -159,7 +161,7 @@ export declare function boundsOf(min?: number, max?: number): Guard<number>;
|
|
|
159
161
|
* `properties` (when a record) contributes one child shape per own key, capped
|
|
160
162
|
* at {@link INFER_BREADTH_LIMIT}; a key is wrapped in {@link optionalShape}
|
|
161
163
|
* unless it appears as a string entry of `required`. A property whose value is
|
|
162
|
-
* not itself a record widens to {@link
|
|
164
|
+
* not itself a record widens to {@link rawShape}. `additionalProperties`:
|
|
163
165
|
* `false` closes the object; a record value recurses into it (`objectShape`
|
|
164
166
|
* validates extras against that shape); anything else — `true`, absent, or
|
|
165
167
|
* malformed — leaves the object OPEN (`true`), matching JSON Schema's own
|
|
@@ -169,8 +171,8 @@ export declare function boundsOf(min?: number, max?: number): Guard<number>;
|
|
|
169
171
|
* keys than {@link INFER_BREADTH_LIMIT}, the schema's own
|
|
170
172
|
* `additionalProperties` is OVERRIDDEN and forced to `true` (fully open) —
|
|
171
173
|
* a dropped key's value could otherwise fail a `false` or record-valued rest
|
|
172
|
-
* shape it was never checked against — mirroring
|
|
173
|
-
*
|
|
174
|
+
* shape it was never checked against — mirroring {@link inferObject}'s
|
|
175
|
+
* partial-key handling in `inferers.ts`. The accumulator uses a
|
|
174
176
|
* null-prototype record so a property literally named `__proto__` becomes an
|
|
175
177
|
* own data key rather than mutating the prototype, mirroring
|
|
176
178
|
* {@link inferObject} (inferers.ts).
|
|
@@ -222,7 +224,7 @@ export declare function buildObjectShape(schema: JSONSchema, depth: number, visi
|
|
|
222
224
|
* record-entry count is at or under {@link INFER_BREADTH_LIMIT}; OVER the
|
|
223
225
|
* limit, building a subset union would be strictly narrower than the
|
|
224
226
|
* schema's full union (a value matching only a dropped variant would be
|
|
225
|
-
* wrongly rejected), so the whole node widens to {@link
|
|
227
|
+
* wrongly rejected), so the whole node widens to {@link rawShape} instead
|
|
226
228
|
* of sampling a subset.
|
|
227
229
|
* 3. `anyOf` — identically, via {@link unionShape}.
|
|
228
230
|
* 4. `type: 'string'` / `'number'` / `'integer'` / `'boolean'` / `'null'` —
|
|
@@ -232,14 +234,16 @@ export declare function buildObjectShape(schema: JSONSchema, depth: number, visi
|
|
|
232
234
|
* (e.g. `minimum: 1.5, maximum: 1.6`) — the same emptiness `validateShape`
|
|
233
235
|
* rejects — so the result is always a valid shape.
|
|
234
236
|
* 5. `type: 'array'` — an {@link arrayShape} whose element shape recurses into
|
|
235
|
-
* a record-valued `items` (widening to {@link
|
|
237
|
+
* a record-valued `items` (widening to {@link rawShape} otherwise), with
|
|
236
238
|
* bounds from `minItems` / `maxItems`.
|
|
237
239
|
* 6. `type: 'object'`, OR no `type` / `enum` / `oneOf` / `anyOf` but a
|
|
238
240
|
* record-valued `properties` — delegates to {@link buildObjectShape}.
|
|
239
241
|
* 7. Everything else — an empty schema, an unrecognized/absent `type`,
|
|
240
242
|
* exhausted depth/breadth, or an attempt failure — widens to
|
|
241
|
-
* {@link
|
|
242
|
-
*
|
|
243
|
+
* {@link rawShape}, whose guard accepts every defined value and whose
|
|
244
|
+
* emitted schema is the same `{}` (plus `description`) the node carried.
|
|
245
|
+
* This is the exact inverse of `{}`, JSON Schema's accept-anything schema,
|
|
246
|
+
* and where the inferers themselves bottom out at their own limits.
|
|
243
247
|
*
|
|
244
248
|
* `format` and `pattern` are NEVER read into the compiled shape — `format` is
|
|
245
249
|
* annotation-only and `pattern` compiling an attacker-supplied string into a
|
|
@@ -247,7 +251,7 @@ export declare function buildObjectShape(schema: JSONSchema, depth: number, visi
|
|
|
247
251
|
* the produced shape's `description` option.
|
|
248
252
|
*
|
|
249
253
|
* @param schema - The schema node to convert
|
|
250
|
-
* @param depth - Remaining descent budget (0 halts recursion with `
|
|
254
|
+
* @param depth - Remaining descent budget (0 halts recursion with `rawShape({})`)
|
|
251
255
|
* @param visited - The ancestor set guarding against cycles — recursion
|
|
252
256
|
* state owned by the {@link schemaToShape} entry point; passing a shared or
|
|
253
257
|
* pre-populated `WeakSet` changes cycle-detection behavior and is not
|
|
@@ -269,26 +273,148 @@ export declare function buildObjectShape(schema: JSONSchema, depth: number, visi
|
|
|
269
273
|
export declare function buildShapeFromNode(schema: JSONSchema, depth: number, visited: WeakSet<object>, memo: WeakMap<object, Map<number, ContractShape>>): ContractShape;
|
|
270
274
|
|
|
271
275
|
/**
|
|
272
|
-
*
|
|
276
|
+
* Encode one value as a deterministic, key-sorted JSON string — the recursive
|
|
277
|
+
* spine of {@link canonicalStringify}.
|
|
278
|
+
*
|
|
279
|
+
* @remarks
|
|
280
|
+
* Arrays keep their element order; records sort their own keys before encoding,
|
|
281
|
+
* recursively at every nesting level. Every other value is encoded by
|
|
282
|
+
* `JSON.stringify`, so `NaN` / `±Infinity` collapse to `'null'` and `-0`
|
|
283
|
+
* encodes as `'0'` — the same lossy-but-deterministic mapping real JSON makes.
|
|
284
|
+
*
|
|
285
|
+
* Returns `undefined` for anything JSON cannot encode: `undefined` itself, a
|
|
286
|
+
* function, a symbol, an array hole, or a cyclic back-edge to an ancestor. A
|
|
287
|
+
* container carrying such a member is itself un-encodable and returns
|
|
288
|
+
* `undefined` too, so the result is either a faithful encoding of the WHOLE
|
|
289
|
+
* value or nothing — a partially-encoded key is never emitted. Traversal is NOT
|
|
290
|
+
* contained here: a hostile getter or `Proxy` trap throws through to
|
|
291
|
+
* {@link canonicalStringify}, which owns the single {@link attempt} boundary
|
|
292
|
+
* for the walk.
|
|
293
|
+
*
|
|
294
|
+
* @param value - The value to encode
|
|
295
|
+
* @param ancestors - Objects on the active traversal path, guarding cycles
|
|
296
|
+
* @returns The deterministic encoding, or `undefined` when JSON cannot encode
|
|
297
|
+
* `value`
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* ```ts
|
|
301
|
+
* canonicalizeValue({ b: 1, a: 2 }, new WeakSet()) // '{"a":2,"b":1}'
|
|
302
|
+
* canonicalizeValue(undefined, new WeakSet()) // undefined
|
|
303
|
+
* ```
|
|
304
|
+
*/
|
|
305
|
+
export declare function canonicalizeValue(value: unknown, ancestors: WeakSet<object>): string | undefined;
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Render a value as a deterministic, key-sorted JSON string — or `undefined`
|
|
309
|
+
* when it has no faithful JSON encoding.
|
|
273
310
|
*
|
|
274
311
|
* @remarks
|
|
275
312
|
* The stable-stringify backing {@link unifySchemas}'s de-duplication and
|
|
276
313
|
* ordering: unlike `JSON.stringify`, object keys are sorted before encoding
|
|
277
314
|
* (recursively, at every nesting level), so two structurally-equal
|
|
278
315
|
* `JSONSchema` fragments built independently always canonicalize to the same
|
|
279
|
-
* string. Pure ECMAScript
|
|
316
|
+
* string. Pure host-independent ECMAScript with no environment-specific imports.
|
|
317
|
+
*
|
|
318
|
+
* TOTAL: it NEVER throws, for any input. It returns `undefined` — never a
|
|
319
|
+
* partial or invalid encoding — for every value JSON cannot faithfully encode:
|
|
320
|
+
*
|
|
321
|
+
* - `undefined` itself, a function, a symbol, or an array hole (JSON encodes
|
|
322
|
+
* none of them), at the top level or anywhere inside a container;
|
|
323
|
+
* - a bigint (`JSON.stringify` throws on one);
|
|
324
|
+
* - cyclic input, tracked with the same ancestor-{@link WeakSet} discipline
|
|
325
|
+
* {@link inferArray} / {@link inferObject} use, so a shared (non-cyclic)
|
|
326
|
+
* reference reached twice through different paths still encodes;
|
|
327
|
+
* - hostile traversal — a throwing own-getter, a hostile `ownKeys` trap, or a
|
|
328
|
+
* revoked `Proxy` anywhere in the value — contained via {@link attempt}.
|
|
329
|
+
*
|
|
330
|
+
* A caller therefore treats `undefined` as "this value has no canonical key",
|
|
331
|
+
* never as an encoding: see {@link unifySchemas} (an un-keyed member cannot
|
|
332
|
+
* participate in de-duplication or ordering) and {@link inferPrimitiveEnum} (an
|
|
333
|
+
* un-keyed member makes the slot enum-ineligible).
|
|
280
334
|
*
|
|
281
335
|
* @param value - The value to canonicalize (a `JSONSchema` fragment, or any
|
|
282
336
|
* nested piece of one)
|
|
283
|
-
* @returns A deterministic string encoding of `value`
|
|
337
|
+
* @returns A deterministic string encoding of `value`, or `undefined` when JSON
|
|
338
|
+
* cannot encode it
|
|
284
339
|
*
|
|
285
340
|
* @example
|
|
286
341
|
* ```ts
|
|
287
342
|
* canonicalStringify({ type: 'object', properties: {} }) ===
|
|
288
343
|
* canonicalStringify({ properties: {}, type: 'object' }) // true
|
|
344
|
+
* canonicalStringify(Number.NaN) // 'null' — JSON.stringify semantics
|
|
345
|
+
* canonicalStringify(undefined) // undefined
|
|
346
|
+
* canonicalStringify(cyclicValue) // undefined
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
349
|
+
export declare function canonicalStringify(value: unknown): string | undefined;
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Deep-clone a JSON Schema graph into an owned frozen snapshot.
|
|
353
|
+
*
|
|
354
|
+
* @remarks
|
|
355
|
+
* Walks arrays and records iteratively with a memo, preserving shared
|
|
356
|
+
* references and closing cyclic edges onto their cloned nodes. Primitive
|
|
357
|
+
* values and own enumerable string-keyed edges are copied; record nodes use
|
|
358
|
+
* null prototypes, arrays retain only their intrinsic array prototype, and
|
|
359
|
+
* every produced object is frozen after its edges are wired. Hostile traversal
|
|
360
|
+
* throws a clone-coded {@link ContractError}, never a caller-owned raw error.
|
|
361
|
+
*
|
|
362
|
+
* @param schema - The JSON Schema graph to snapshot
|
|
363
|
+
* @returns A deeply cloned and frozen JSON Schema graph
|
|
364
|
+
* @throws {ContractError} When hostile schema traversal prevents ownership
|
|
365
|
+
*
|
|
366
|
+
* @example
|
|
367
|
+
* ```ts
|
|
368
|
+
* const child = { type: 'string' }
|
|
369
|
+
* const clone = cloneSchema({ anyOf: [child, child] })
|
|
370
|
+
* clone.anyOf?.[0] === clone.anyOf?.[1] // true
|
|
371
|
+
* ```
|
|
372
|
+
*/
|
|
373
|
+
export declare function cloneSchema(schema: JSONSchema): JSONSchema;
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Deep-clone a contract shape graph into an owned frozen snapshot.
|
|
377
|
+
*
|
|
378
|
+
* @remarks
|
|
379
|
+
* Uses an explicit memo to preserve shared-child identity and close cyclic
|
|
380
|
+
* edges onto their cloned nodes. Shape-node shells are created iteratively,
|
|
381
|
+
* then their structural edges are wired and their copied collections frozen.
|
|
382
|
+
* String patterns are captured by source and flags behind an enumerable
|
|
383
|
+
* accessor that returns a fresh frozen zero-state `RegExp` on every read.
|
|
384
|
+
* A raw schema delegates to {@link cloneSchema}; every shape-graph read is
|
|
385
|
+
* contained, so hostile traversal throws only a clone-coded
|
|
386
|
+
* {@link ContractError}.
|
|
387
|
+
*
|
|
388
|
+
* @param shape - The contract shape graph to snapshot
|
|
389
|
+
* @returns A deeply cloned and frozen shape graph
|
|
390
|
+
* @throws {ContractError} When hostile shape or raw-schema traversal prevents ownership
|
|
391
|
+
*
|
|
392
|
+
* @example
|
|
393
|
+
* ```ts
|
|
394
|
+
* const child = arrayShape(stringShape())
|
|
395
|
+
* const clone = cloneShape(objectShape({ first: child, second: child }))
|
|
396
|
+
* clone.type === 'object' && clone.properties.first === clone.properties.second // true
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
399
|
+
export declare function cloneShape(shape: StringShape): StringShape;
|
|
400
|
+
|
|
401
|
+
export declare function cloneShape(shape: ContractShape): ContractShape;
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* The maximum supported nesting depth of a compiled contract shape, frozen.
|
|
405
|
+
*
|
|
406
|
+
* @remarks
|
|
407
|
+
* {@link validateShape} rejects the next level with a depth-coded
|
|
408
|
+
* {@link ContractError} before recursive artifact compilation begins, so a
|
|
409
|
+
* finite but pathologically deep developer-authored shape fails predictably
|
|
410
|
+
* instead of reaching the JavaScript call-stack limit.
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* ```ts
|
|
414
|
+
* COMPILE_DEPTH_LIMIT // 512
|
|
289
415
|
* ```
|
|
290
416
|
*/
|
|
291
|
-
export declare
|
|
417
|
+
export declare const COMPILE_DEPTH_LIMIT = 512;
|
|
292
418
|
|
|
293
419
|
/**
|
|
294
420
|
* Compile a {@link ContractShape} into a deterministic seed value.
|
|
@@ -296,18 +422,25 @@ export declare function canonicalStringify(value: unknown): string;
|
|
|
296
422
|
* @remarks
|
|
297
423
|
* The same shape and the same `random` source always produce the same value, so
|
|
298
424
|
* seed data is reproducible. Defaults to a {@link seededRandom} source seeded
|
|
299
|
-
* from the wall clock when none is supplied.
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
425
|
+
* from the wall clock when none is supplied. Shape-generation failures throw a
|
|
426
|
+
* {@link ContractError} with code `'generate'`; {@link drawRandom} failures use
|
|
427
|
+
* code `'random'` and retain the consuming shape category in context.
|
|
428
|
+
* Failures include a degenerate empty `literalShape` / `unionShape`, a
|
|
429
|
+
* pattern-constrained `stringShape` whose generated sample cannot satisfy the
|
|
430
|
+
* pattern, an invalid random sample, and a `rawShape` whose arbitrary embedded
|
|
431
|
+
* schema cannot be auto-generated. {@link validateShapeDepth} iteratively
|
|
432
|
+
* rejects excessive nesting or cycles before recursive generation begins.
|
|
433
|
+
* `createContract` runs
|
|
304
434
|
* {@link validateShape} first, so a degenerate `literalShape` / `unionShape` /
|
|
305
435
|
* bounded shape is normally caught there; these throws remain here as defense
|
|
306
|
-
* for standalone `compileGenerator` use.
|
|
436
|
+
* for standalone `compileGenerator` use. Union candidates are bounded by
|
|
437
|
+
* {@link GENERATION_ATTEMPT_LIMIT} and accepted only when they satisfy the
|
|
438
|
+
* union's compiled guard.
|
|
307
439
|
*
|
|
308
440
|
* @param shape - The shape to generate from
|
|
309
441
|
* @param random - A seeded random source (defaults to `seededRandom(Date.now())`)
|
|
310
442
|
* @returns A value matching the shape
|
|
443
|
+
* @throws {ContractError} When the shape or random source cannot produce a valid value
|
|
311
444
|
*
|
|
312
445
|
* @example
|
|
313
446
|
* ```ts
|
|
@@ -322,10 +455,14 @@ export declare function compileGenerator(shape: ContractShape, random?: RandomFu
|
|
|
322
455
|
* Compile a {@link ContractShape} into a runtime type guard.
|
|
323
456
|
*
|
|
324
457
|
* @remarks
|
|
325
|
-
* Reuses the combinators
|
|
326
|
-
*
|
|
327
|
-
*
|
|
328
|
-
*
|
|
458
|
+
* Reuses the combinators for structural and refined shapes, including
|
|
459
|
+
* {@link literalOf} for SameValueZero literal matching. Compiled object shapes
|
|
460
|
+
* observe own enumerable string keys through {@link enumerableKeys}, matching
|
|
461
|
+
* their parser, reporter, and inference view for both open and closed objects.
|
|
462
|
+
* Like every guard it is total — it never throws (AGENTS §14). The shape is
|
|
463
|
+
* taken through {@link ownShape} first, so an unfrozen caller-owned graph
|
|
464
|
+
* compiles from a snapshot. {@link validateShapeDepth} iteratively rejects
|
|
465
|
+
* excessive nesting or cycles before recursive guard compilation begins.
|
|
329
466
|
*
|
|
330
467
|
* @param shape - The shape to compile
|
|
331
468
|
* @returns A guard narrowing to the shape's inferred type
|
|
@@ -350,11 +487,16 @@ export declare function compileGuard(shape: ContractShape): Guard<unknown>;
|
|
|
350
487
|
* unchanged, otherwise the first variant that both parses and guards wins.
|
|
351
488
|
*
|
|
352
489
|
* After coercing a leaf, it re-applies that leaf's REFINEMENTS through the same
|
|
353
|
-
* combinators `compileGuard` uses — `stringOf` for a string's length/pattern
|
|
354
|
-
* `boundsOf` for a number's value and an array's length
|
|
355
|
-
*
|
|
356
|
-
* soundness (AGENTS §14): a
|
|
357
|
-
* `is`, refinements
|
|
490
|
+
* combinators `compileGuard` uses — `stringOf` for a string's length/pattern,
|
|
491
|
+
* `boundsOf` for a number's value and an array's length, and {@link literalOf}
|
|
492
|
+
* for literal membership — so a value that coerces but violates a bound parses
|
|
493
|
+
* to `undefined`. The result is full parse↔guard soundness (AGENTS §14): a
|
|
494
|
+
* non-`undefined` parse always satisfies the contract's `is`, refinements
|
|
495
|
+
* included. Object presence and extra-key processing use the same
|
|
496
|
+
* own-enumerable-string snapshot as the guard, reporter, and inference. The
|
|
497
|
+
* shape is taken through {@link ownShape} first, so an unfrozen caller-owned
|
|
498
|
+
* graph compiles from a snapshot. {@link validateShapeDepth} iteratively
|
|
499
|
+
* rejects excessive nesting or cycles before recursive parser compilation.
|
|
358
500
|
*
|
|
359
501
|
* @param shape - The shape to compile
|
|
360
502
|
* @returns A parser yielding the shape's inferred type or `undefined`
|
|
@@ -393,7 +535,8 @@ export declare function compileParser(shape: ContractShape): Parser<unknown>;
|
|
|
393
535
|
* open object with a constraining `additionalProperties` shape recurses extras
|
|
394
536
|
* against it instead. A hostile getter or throwing `Proxy` trap is contained
|
|
395
537
|
* via {@link attempt} and surfaces as a single top-level type fault, never a
|
|
396
|
-
* throw (AGENTS §14).
|
|
538
|
+
* throw (AGENTS §14). {@link validateShapeDepth} iteratively rejects excessive
|
|
539
|
+
* shape nesting or cycles before recursive reporting begins.
|
|
397
540
|
*
|
|
398
541
|
* @param shape - The shape to report against
|
|
399
542
|
* @param value - The value to check
|
|
@@ -416,7 +559,10 @@ export declare function compileReporter(shape: ContractShape, value: unknown, pa
|
|
|
416
559
|
* @remarks
|
|
417
560
|
* Object shapes emit `additionalProperties: false` (unless opened) and list only
|
|
418
561
|
* required keys in `required`; nullable shapes emit an `anyOf` with `{ type:
|
|
419
|
-
* 'null' }`.
|
|
562
|
+
* 'null' }`. The result is an owned deeply frozen graph; raw schemas are cloned
|
|
563
|
+
* rather than retained by reference. Emission only — it never inspects a
|
|
564
|
+
* runtime value. {@link validateShapeDepth} iteratively rejects excessive
|
|
565
|
+
* nesting or cycles before recursive emission begins.
|
|
420
566
|
*
|
|
421
567
|
* @param shape - The shape to compile
|
|
422
568
|
* @returns The emitted JSON Schema
|
|
@@ -441,14 +587,86 @@ export declare function compileSchema(shape: ContractShape): JSONSchema;
|
|
|
441
587
|
*/
|
|
442
588
|
export declare function complementOf<TBase, TExcluded extends TBase>(base: Guard<TBase>, excluded: Guard<TExcluded> | ((value: TBase) => value is TExcluded)): Guard<Exclude<TBase, TExcluded>>;
|
|
443
589
|
|
|
590
|
+
/** Machine-readable category carried by a {@link ContractError}. */
|
|
591
|
+
export declare type ContractCode =
|
|
592
|
+
/** Identifies a bound contract error. */
|
|
593
|
+
'bound'
|
|
594
|
+
/** Identifies a range contract error. */
|
|
595
|
+
| 'range'
|
|
596
|
+
/** Identifies an empty-value contract error. */
|
|
597
|
+
| 'empty'
|
|
598
|
+
/** Identifies a placement contract error. */
|
|
599
|
+
| 'placement'
|
|
600
|
+
/** Identifies a literal contract error. */
|
|
601
|
+
| 'literal'
|
|
602
|
+
/** Identifies a cycle contract error. */
|
|
603
|
+
| 'cycle'
|
|
604
|
+
/** Identifies a pattern contract error. */
|
|
605
|
+
| 'pattern'
|
|
606
|
+
/** Identifies a generation contract error. */
|
|
607
|
+
| 'generate'
|
|
608
|
+
/** Identifies a random-source contract error. */
|
|
609
|
+
| 'random'
|
|
610
|
+
/** Identifies an owned-clone contract error. */
|
|
611
|
+
| 'clone'
|
|
612
|
+
/** Identifies a compilation-depth contract error. */
|
|
613
|
+
| 'depth';
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Error carrying a machine-readable contract category and optional context.
|
|
617
|
+
*
|
|
618
|
+
* @example
|
|
619
|
+
* ```ts
|
|
620
|
+
* const error = new ContractError('Minimum exceeds maximum', {
|
|
621
|
+
* code: 'range',
|
|
622
|
+
* context: { path: ['properties', 'age'] },
|
|
623
|
+
* })
|
|
624
|
+
* ```
|
|
625
|
+
*/
|
|
626
|
+
export declare class ContractError extends Error {
|
|
627
|
+
readonly name = "ContractError";
|
|
628
|
+
readonly code: ContractCode;
|
|
629
|
+
readonly context: ContractErrorContext | undefined;
|
|
630
|
+
/**
|
|
631
|
+
* Create a contract error.
|
|
632
|
+
*
|
|
633
|
+
* @param message - Human-readable error description
|
|
634
|
+
* @param options - Machine-readable category, optional context, and optional cause
|
|
635
|
+
*/
|
|
636
|
+
constructor(message: string, options: ContractErrorOptions);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
/** Optional structured details carried by a {@link ContractError}. */
|
|
640
|
+
export declare interface ContractErrorContext {
|
|
641
|
+
/** Location associated with the error. */
|
|
642
|
+
readonly path?: FieldPath;
|
|
643
|
+
/** Shape label associated with the error. */
|
|
644
|
+
readonly shape?: string;
|
|
645
|
+
/** Numeric or textual limit associated with the error. */
|
|
646
|
+
readonly limit?: number | string;
|
|
647
|
+
/** Received-value description associated with the error. */
|
|
648
|
+
readonly received?: string;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/** Construction options for a {@link ContractError}. */
|
|
652
|
+
export declare interface ContractErrorOptions {
|
|
653
|
+
/** Machine-readable error category. */
|
|
654
|
+
readonly code: ContractCode;
|
|
655
|
+
/** Optional structured error details. */
|
|
656
|
+
readonly context?: ContractErrorContext;
|
|
657
|
+
/** Optional originating thrown value. */
|
|
658
|
+
readonly cause?: unknown;
|
|
659
|
+
}
|
|
660
|
+
|
|
444
661
|
/**
|
|
445
|
-
* A compiled contract — the
|
|
662
|
+
* A compiled contract — the five lockstep outputs derived from one shape.
|
|
446
663
|
*
|
|
447
664
|
* @remarks
|
|
448
665
|
* Built by `createContract`: `is` narrows, `parse` coerces (returning the typed
|
|
449
|
-
* value or `undefined`), `schema` is
|
|
450
|
-
*
|
|
451
|
-
*
|
|
666
|
+
* value or `undefined`), `schema` is an owned deeply frozen emitted JSON
|
|
667
|
+
* Schema, `explain` reports the structured faults behind a failed `parse`, and
|
|
668
|
+
* `generate` produces deterministic seed data from a {@link RandomFunction}
|
|
669
|
+
* (defaulting to a wall-clock-seeded source when none is supplied).
|
|
452
670
|
*/
|
|
453
671
|
export declare interface ContractInterface<T> {
|
|
454
672
|
readonly schema: JSONSchema;
|
|
@@ -476,22 +694,25 @@ export declare interface ContractInterface<T> {
|
|
|
476
694
|
*
|
|
477
695
|
* @remarks
|
|
478
696
|
* A discriminated union keyed on `type`. Shapes nest (an `ArrayShape` holds an
|
|
479
|
-
* element shape, an `ObjectShape` a map of them)
|
|
480
|
-
*
|
|
697
|
+
* element shape, an `ObjectShape` a map of them). {@link validateShape}
|
|
698
|
+
* enforces an acyclic graph within {@link COMPILE_DEPTH_LIMIT}.
|
|
481
699
|
*/
|
|
482
700
|
export declare type ContractShape = StringShape | NumberShape | BooleanShape | NullShape | LiteralShape | ArrayShape | ObjectShape | UnionShape | OptionalShape | NullableShape | JSONShape | RawShape;
|
|
483
701
|
|
|
484
702
|
/**
|
|
485
|
-
* Compile a {@link ContractShape} into a {@link ContractInterface} — the
|
|
703
|
+
* Compile a {@link ContractShape} into a {@link ContractInterface} — the five
|
|
486
704
|
* lockstep outputs from one declaration.
|
|
487
705
|
*
|
|
488
706
|
* @remarks
|
|
489
707
|
* Runs {@link validateShape} first — a malformed shape throws immediately
|
|
490
|
-
* rather than compiling into a silently-wrong contract (AGENTS §12).
|
|
491
|
-
*
|
|
492
|
-
*
|
|
708
|
+
* rather than compiling into a silently-wrong contract (AGENTS §12). It always
|
|
709
|
+
* takes its own {@link cloneShape} snapshot — never merely a frozen node's word
|
|
710
|
+
* — and hands that same graph to every artifact compiler.
|
|
711
|
+
* Then it precompiles the deeply frozen owned schema, guard, and parser once;
|
|
712
|
+
* `generate` walks the
|
|
713
|
+
* snapshot per call with the supplied random source, and `explain` compiles the
|
|
493
714
|
* diagnostic report via {@link compileReporter} at zero added compile-time cost
|
|
494
|
-
* (it re-walks the
|
|
715
|
+
* (it re-walks the snapshot per call, exactly like `generate`).
|
|
495
716
|
*
|
|
496
717
|
* @param shape - The shape to compile
|
|
497
718
|
* @returns A contract bundling `schema` / `is` / `parse` / `explain` / `generate`
|
|
@@ -514,10 +735,11 @@ export declare function createContract(shape: ContractShape): ContractInterface<
|
|
|
514
735
|
*
|
|
515
736
|
* @remarks
|
|
516
737
|
* Total and pure. Either keyword is used only when it is a non-negative
|
|
517
|
-
* integer (`
|
|
518
|
-
* number, `NaN`, `Infinity`, a fraction
|
|
519
|
-
* bounds are present and `min` exceeds `max`,
|
|
520
|
-
* (an unbounded shape is always a legal widening
|
|
738
|
+
* safe integer (`Number.isSafeInteger` + `>= 0`); a malformed value (a string,
|
|
739
|
+
* a negative number, `NaN`, `Infinity`, a fraction, or an unsafe integer) is
|
|
740
|
+
* dropped as if absent. When both bounds are present and `min` exceeds `max`,
|
|
741
|
+
* the PAIR is dropped entirely (an unbounded shape is always a legal widening
|
|
742
|
+
* of a contradictory schema).
|
|
521
743
|
*
|
|
522
744
|
* @param min - The raw `minLength` / `minItems` keyword value
|
|
523
745
|
* @param max - The raw `maxLength` / `maxItems` keyword value
|
|
@@ -560,6 +782,41 @@ export declare function deriveRangeBounds(min: unknown, max: unknown): {
|
|
|
560
782
|
readonly max?: number;
|
|
561
783
|
};
|
|
562
784
|
|
|
785
|
+
/**
|
|
786
|
+
* Draw and validate one generator random sample.
|
|
787
|
+
*
|
|
788
|
+
* @param random - The caller-supplied random source
|
|
789
|
+
* @param shape - The shape category consuming the sample
|
|
790
|
+
* @returns A finite sample in `[0, 1)`
|
|
791
|
+
* @throws {ContractError} When the source throws or returns outside `[0, 1)`
|
|
792
|
+
*
|
|
793
|
+
* @example
|
|
794
|
+
* ```ts
|
|
795
|
+
* drawRandom(() => 0.5, 'number') // 0.5
|
|
796
|
+
* ```
|
|
797
|
+
*/
|
|
798
|
+
export declare function drawRandom(random: RandomFunction, shape: string): number;
|
|
799
|
+
|
|
800
|
+
/**
|
|
801
|
+
* Snapshot an object's own enumerable string keys through a total boundary.
|
|
802
|
+
*
|
|
803
|
+
* @remarks
|
|
804
|
+
* This is the package-wide runtime property view used by compiled object
|
|
805
|
+
* guards, parsers, reporters, schema inference, and owned schema cloning. It
|
|
806
|
+
* matches the object-key view serialized by `JSON.stringify`: inherited,
|
|
807
|
+
* symbol, and non-enumerable properties are excluded. A hostile Proxy trap
|
|
808
|
+
* returns `undefined` rather than escaping.
|
|
809
|
+
*
|
|
810
|
+
* @param value - The object whose keys to snapshot
|
|
811
|
+
* @returns A frozen owned key list, or `undefined` when enumeration throws
|
|
812
|
+
*
|
|
813
|
+
* @example
|
|
814
|
+
* ```ts
|
|
815
|
+
* enumerableKeys({ visible: 1 }) // ['visible']
|
|
816
|
+
* ```
|
|
817
|
+
*/
|
|
818
|
+
export declare function enumerableKeys(value: object): readonly string[] | undefined;
|
|
819
|
+
|
|
563
820
|
/**
|
|
564
821
|
* Count the enumerable own-symbol keys on a value.
|
|
565
822
|
*
|
|
@@ -706,9 +963,40 @@ export declare type FromGuards<G extends GuardsShape> = Readonly<{
|
|
|
706
963
|
[K in keyof G]: GuardType<G[K]>;
|
|
707
964
|
}>;
|
|
708
965
|
|
|
966
|
+
/**
|
|
967
|
+
* The maximum number of candidate-generation attempts for a constrained
|
|
968
|
+
* generated value, frozen.
|
|
969
|
+
*
|
|
970
|
+
* @remarks
|
|
971
|
+
* Provides one deterministic work bound for generators that must retry a
|
|
972
|
+
* candidate against a contract constraint.
|
|
973
|
+
*
|
|
974
|
+
* @example
|
|
975
|
+
* ```ts
|
|
976
|
+
* GENERATION_ATTEMPT_LIMIT // 32
|
|
977
|
+
* ```
|
|
978
|
+
*/
|
|
979
|
+
export declare const GENERATION_ATTEMPT_LIMIT = 32;
|
|
980
|
+
|
|
709
981
|
/** A runtime type guard: returns `true` when `value` satisfies `T` and narrows it. */
|
|
710
982
|
export declare type Guard<T> = (value: unknown) => value is T;
|
|
711
983
|
|
|
984
|
+
/**
|
|
985
|
+
* The maximum number of active {@link lazyOf} invocations for one lazy guard,
|
|
986
|
+
* frozen.
|
|
987
|
+
*
|
|
988
|
+
* @remarks
|
|
989
|
+
* Bounds explicitly recursive guards before the JavaScript call stack becomes
|
|
990
|
+
* the limiting mechanism. An invocation beyond this depth returns `false`, and
|
|
991
|
+
* the counter unwinds after every contained guard call.
|
|
992
|
+
*
|
|
993
|
+
* @example
|
|
994
|
+
* ```ts
|
|
995
|
+
* GUARD_DEPTH_LIMIT // 512
|
|
996
|
+
* ```
|
|
997
|
+
*/
|
|
998
|
+
export declare const GUARD_DEPTH_LIMIT = 512;
|
|
999
|
+
|
|
712
1000
|
/**
|
|
713
1001
|
* A mapping of string keys to guards.
|
|
714
1002
|
*
|
|
@@ -720,6 +1008,19 @@ export declare type GuardsShape = Readonly<Record<string, Guard<unknown>>>;
|
|
|
720
1008
|
/** Extract the guarded type `T` from a `Guard<T>`. */
|
|
721
1009
|
export declare type GuardType<G> = G extends Guard<infer T> ? T : never;
|
|
722
1010
|
|
|
1011
|
+
/**
|
|
1012
|
+
* Invoke a predicate through the sanctioned never-throw boundary.
|
|
1013
|
+
*
|
|
1014
|
+
* @param callback - The predicate to invoke with no arguments
|
|
1015
|
+
* @returns `true` only when the callback returns the boolean value `true`
|
|
1016
|
+
*
|
|
1017
|
+
* @example
|
|
1018
|
+
* ```ts
|
|
1019
|
+
* holds(() => value instanceof Widget) // false when inspection throws
|
|
1020
|
+
* ```
|
|
1021
|
+
*/
|
|
1022
|
+
export declare function holds(callback: () => boolean): boolean;
|
|
1023
|
+
|
|
723
1024
|
/**
|
|
724
1025
|
* Infer the static TypeScript type a {@link ContractShape} describes.
|
|
725
1026
|
*
|
|
@@ -904,22 +1205,28 @@ export declare type InferObject<P extends Readonly<Record<string, ContractShape>
|
|
|
904
1205
|
* properties.
|
|
905
1206
|
*
|
|
906
1207
|
* @remarks
|
|
907
|
-
* Own enumerable string keys via
|
|
908
|
-
* deterministic output, capped at `breadth`.
|
|
909
|
-
*
|
|
910
|
-
*
|
|
911
|
-
*
|
|
912
|
-
*
|
|
913
|
-
*
|
|
914
|
-
*
|
|
915
|
-
*
|
|
916
|
-
*
|
|
917
|
-
*
|
|
918
|
-
*
|
|
919
|
-
*
|
|
920
|
-
*
|
|
921
|
-
*
|
|
922
|
-
*
|
|
1208
|
+
* Own enumerable string keys via {@link enumerableKeys}, sorted
|
|
1209
|
+
* lexicographically for deterministic output, capped at `breadth`. This is the
|
|
1210
|
+
* same property view compiled object guards, parsers, and reporters use. Each
|
|
1211
|
+
* property value is read through {@link attempt} so a hostile getter cannot
|
|
1212
|
+
* escape as a thrown error; a property whose value is `undefined` is DROPPED
|
|
1213
|
+
* — JSON encodes no such property (`JSON.stringify({ a: undefined })` is
|
|
1214
|
+
* `'{}'`), so it contributes neither a `properties` entry nor a `required`
|
|
1215
|
+
* entry. Every other present key is required (single-value mode).
|
|
1216
|
+
*
|
|
1217
|
+
* Emits `additionalProperties: false` when `closed`, `true` otherwise —
|
|
1218
|
+
* mirroring {@link compileSchema}'s object-emission convention — EXCEPT when
|
|
1219
|
+
* the sampled key list no longer describes every key `value` actually carries,
|
|
1220
|
+
* which happens two ways: the own-key list exceeds `breadth` (truncation), or
|
|
1221
|
+
* a key was dropped for holding `undefined`. Either way `additionalProperties`
|
|
1222
|
+
* is forced to `true` regardless of `closed`, because a CLOSED schema built
|
|
1223
|
+
* from an incomplete key list would reject the very object it was inferred
|
|
1224
|
+
* from (`recordOf` rejects any own key the shape does not declare).
|
|
1225
|
+
*
|
|
1226
|
+
* Depth exhaustion or a cyclic re-encounter of `value` both yield `{}`. A
|
|
1227
|
+
* same-object re-inference at the same remaining `depth` is served from `memo`
|
|
1228
|
+
* instead of recomputing (guards a shared-reference DAG against exponential
|
|
1229
|
+
* blowup).
|
|
923
1230
|
*
|
|
924
1231
|
* @param value - The record to infer from
|
|
925
1232
|
* @param depth - Remaining descent budget
|
|
@@ -984,6 +1291,12 @@ export declare type InferOpenIndex<A extends boolean | ContractShape> = [A] exte
|
|
|
984
1291
|
* `literalShape` emission. Members are sorted by canonical key for
|
|
985
1292
|
* deterministic output.
|
|
986
1293
|
*
|
|
1294
|
+
* A member {@link canonicalStringify} cannot key has no identity to
|
|
1295
|
+
* de-duplicate against, so the whole slot is enum-INELIGIBLE and returns
|
|
1296
|
+
* `undefined` — widening to the caller's bare `type` rather than emitting an
|
|
1297
|
+
* `enum` that might silently omit a value. (A string or finite number always
|
|
1298
|
+
* canonicalizes, so this only guards the total contract.)
|
|
1299
|
+
*
|
|
987
1300
|
* @param values - The collected slot values
|
|
988
1301
|
* @param limit - The maximum distinct-value count before giving up
|
|
989
1302
|
* @returns The `{ enum: [...] }` fragment, or `undefined` when ineligible
|
|
@@ -1013,14 +1326,20 @@ export declare function inferPrimitiveEnum(values: readonly unknown[], limit: nu
|
|
|
1013
1326
|
* {@link inferArray}, this path carries no `visited` `WeakSet` — a value
|
|
1014
1327
|
* shared by reference across multiple sample rows is legitimate (not a
|
|
1015
1328
|
* cycle back to an ancestor), so termination on cyclic row data relies on
|
|
1016
|
-
* the decrementing `depth` budget alone.
|
|
1017
|
-
*
|
|
1018
|
-
* forced to `true` regardless of `closed`
|
|
1019
|
-
*
|
|
1020
|
-
*
|
|
1021
|
-
*
|
|
1022
|
-
*
|
|
1023
|
-
*
|
|
1329
|
+
* the decrementing `depth` budget alone.
|
|
1330
|
+
*
|
|
1331
|
+
* `additionalProperties` is forced to `true` regardless of `closed` whenever
|
|
1332
|
+
* the declared key list stops describing every key the rows carry — the same
|
|
1333
|
+
* incomplete-sample-opens-the-schema rule {@link inferObject} applies, so a
|
|
1334
|
+
* closed schema never rejects the very rows it was inferred from. That happens
|
|
1335
|
+
* three ways: the union of sample keys exceeds `breadth` (truncation); a
|
|
1336
|
+
* hostile getter defeats a key's value walk (which drops that key for ALL
|
|
1337
|
+
* rows — a deliberate all-or-nothing scope, not a per-row partial failure);
|
|
1338
|
+
* or some row carries the key as an own property holding `undefined`, which
|
|
1339
|
+
* JSON encodes as no property at all, so the key is dropped rather than
|
|
1340
|
+
* declared with a shape that row would fail. When the KEY walk itself fails
|
|
1341
|
+
* (a hostile `ownKeys` trap on any row), nothing is known and the whole slot
|
|
1342
|
+
* widens to `{}`.
|
|
1024
1343
|
*
|
|
1025
1344
|
* @param samples - The plain-record samples
|
|
1026
1345
|
* @param depth - Remaining descent budget
|
|
@@ -1083,13 +1402,21 @@ export declare type InferUnion<V extends readonly ContractShape[]> = V extends r
|
|
|
1083
1402
|
* @remarks
|
|
1084
1403
|
* Total: never throws, and terminates on cyclic input via `visited`. Leaf
|
|
1085
1404
|
* classification order: `null`, boolean, integer (`Number.isInteger`
|
|
1086
|
-
* semantics — `-0` counts), finite non-integer number,
|
|
1087
|
-
* (`NaN` / `±Infinity`, widened to `{ type: 'number' }`), string (gaining a
|
|
1405
|
+
* semantics — `-0` counts), finite non-integer number, string (gaining a
|
|
1088
1406
|
* `format` keyword when `format` is on and {@link stringToFormat} matches),
|
|
1089
1407
|
* array (recurse), plain record (recurse), `Date` (`{ type: 'string' }`,
|
|
1090
|
-
* plus `format: 'date-time'` when `format` is on); everything else
|
|
1091
|
-
* (
|
|
1092
|
-
* `Map` / `Set`
|
|
1408
|
+
* plus `format: 'date-time'` when `format` is on); everything else — a
|
|
1409
|
+
* NON-FINITE number (`NaN` / `±Infinity`), a function, a symbol, a bigint,
|
|
1410
|
+
* `undefined`, and other non-plain objects such as `Map` / `Set` — is the
|
|
1411
|
+
* empty accept-anything schema `{}`.
|
|
1412
|
+
*
|
|
1413
|
+
* A non-finite number bottoms out with the other JSON-inexpressible values on
|
|
1414
|
+
* purpose: JSON carries no `NaN` / `±Infinity` (`JSON.stringify(Number.NaN)`
|
|
1415
|
+
* is `'null'`), so `{ type: 'number' }` would ASSERT something a JSON Schema
|
|
1416
|
+
* validator rejects — and the shape {@link schemaToShape} builds from it would
|
|
1417
|
+
* reject the very sample it was inferred from. `{}` is the truthful
|
|
1418
|
+
* description, and it inverts to an accept-anything shape, keeping
|
|
1419
|
+
* `compileGuard(schemaToShape(valueToSchema(v)))(v)` true.
|
|
1093
1420
|
*
|
|
1094
1421
|
* @param value - The value to classify
|
|
1095
1422
|
* @param depth - Remaining descent budget (0 halts recursion with `{}`)
|
|
@@ -1133,6 +1460,7 @@ export declare function instanceOf<C extends abstract new (...args: never) => ob
|
|
|
1133
1460
|
*
|
|
1134
1461
|
* @param options - Optional bounds and `description` (no `integer` key)
|
|
1135
1462
|
* @returns An integer number shape
|
|
1463
|
+
* @throws {ContractError} When a present bound is not finite
|
|
1136
1464
|
*/
|
|
1137
1465
|
export declare function integerShape(options?: Omit<NumberShapeOptions, 'integer'>): NumberShape;
|
|
1138
1466
|
|
|
@@ -1284,6 +1612,20 @@ export declare function isBoolean(value: unknown): value is boolean;
|
|
|
1284
1612
|
*/
|
|
1285
1613
|
export declare function isConstructor(value: unknown): value is AnyConstructor<object>;
|
|
1286
1614
|
|
|
1615
|
+
/**
|
|
1616
|
+
* Determine whether an unknown value is a {@link ContractError}.
|
|
1617
|
+
*
|
|
1618
|
+
* @param value - The value to inspect
|
|
1619
|
+
* @returns `true` only when the value is a `ContractError`
|
|
1620
|
+
*
|
|
1621
|
+
* @example
|
|
1622
|
+
* ```ts
|
|
1623
|
+
* isContractError(new ContractError('Invalid shape', { code: 'placement' })) // true
|
|
1624
|
+
* isContractError(new Error('Invalid shape')) // false
|
|
1625
|
+
* ```
|
|
1626
|
+
*/
|
|
1627
|
+
export declare function isContractError(value: unknown): value is ContractError;
|
|
1628
|
+
|
|
1287
1629
|
/** Determine whether a value is a `DataView`.
|
|
1288
1630
|
*
|
|
1289
1631
|
* @example
|
|
@@ -1446,7 +1788,7 @@ export declare function isGeneratorFunction(value: unknown): value is (...args:
|
|
|
1446
1788
|
* NOT total (AGENTS §14): it invokes `getPrototypeOf` on `value` — which a
|
|
1447
1789
|
* revoked `Proxy` or a `getPrototypeOf`-trap `Proxy` throws from — and, when
|
|
1448
1790
|
* `X[Symbol.hasInstance]` is user-defined, can throw from arbitrary code. This
|
|
1449
|
-
* wraps the check in {@link
|
|
1791
|
+
* wraps the check in {@link holds} (see ./helpers.js) so any such throw
|
|
1450
1792
|
* yields `false` instead of escaping.
|
|
1451
1793
|
*
|
|
1452
1794
|
* @param value - The value to test
|
|
@@ -1552,7 +1894,7 @@ export declare function isJSONPrimitive(value: unknown): value is JSONPrimitive;
|
|
|
1552
1894
|
* Total guard: never throws, returns `false` for cycles, functions, `Date`
|
|
1553
1895
|
* instances, class instances, `NaN`, and `±Infinity`. Arrays and plain records
|
|
1554
1896
|
* are walked with an ancestor set so recursive input fails instead of hanging.
|
|
1555
|
-
* The whole walk runs inside `
|
|
1897
|
+
* The whole walk runs inside `holds` (AGENTS §14): a hostile getter on a
|
|
1556
1898
|
* record property, or a revoked `Proxy` anywhere in the structure, is caught
|
|
1557
1899
|
* and yields `false` instead of escaping as a thrown error.
|
|
1558
1900
|
*
|
|
@@ -1746,7 +2088,7 @@ export declare function isPromiseLike<T = unknown>(value: unknown): value is Pro
|
|
|
1746
2088
|
* above `null`. Arrays and class instances are still rejected: an array's
|
|
1747
2089
|
* prototype chain runs through `Array.prototype` before `null`, and a class
|
|
1748
2090
|
* instance's runs through the class's own prototype. The whole body runs
|
|
1749
|
-
* inside `
|
|
2091
|
+
* inside `holds` (AGENTS §14) so a revoked `Proxy` or a hostile
|
|
1750
2092
|
* `getPrototypeOf` trap cannot escape as a thrown error.
|
|
1751
2093
|
*
|
|
1752
2094
|
* @example
|
|
@@ -1955,22 +2297,6 @@ export declare function isZeroArgAsyncGenerator(value: unknown): value is () =>
|
|
|
1955
2297
|
*/
|
|
1956
2298
|
export declare function isZeroArgGenerator(value: unknown): value is () => Generator<unknown, unknown, unknown>;
|
|
1957
2299
|
|
|
1958
|
-
/**
|
|
1959
|
-
* Build a guard that accepts any iterable whose every element satisfies
|
|
1960
|
-
* `elementGuard`.
|
|
1961
|
-
*
|
|
1962
|
-
* @example
|
|
1963
|
-
* ```ts
|
|
1964
|
-
* const isNumberIterable = iterableOf(isNumber)
|
|
1965
|
-
* isNumberIterable([1, 2, 3]) // true
|
|
1966
|
-
* isNumberIterable(new Set([1, 2])) // true
|
|
1967
|
-
* isNumberIterable([1, 'two']) // false
|
|
1968
|
-
* ```
|
|
1969
|
-
*/
|
|
1970
|
-
export declare function iterableOf<T>(elementGuard: Guard<T>): Guard<Iterable<T>>;
|
|
1971
|
-
|
|
1972
|
-
export declare function iterableOf(elementGuard: (value: unknown) => boolean): Guard<Iterable<unknown>>;
|
|
1973
|
-
|
|
1974
2300
|
/**
|
|
1975
2301
|
* The seven standard JSON Schema `type` names, frozen.
|
|
1976
2302
|
*
|
|
@@ -2012,8 +2338,9 @@ export declare type JSONPrimitive = string | number | boolean | null;
|
|
|
2012
2338
|
* emitted by the {@link stringToFormat} / {@link samplesToFormat} inference
|
|
2013
2339
|
* heuristics (`valueToSchema` / `samplesToSchema`), never by `compileSchema`.
|
|
2014
2340
|
* Recursive via `items` / `properties` / `additionalProperties` / `anyOf` /
|
|
2015
|
-
* `oneOf
|
|
2016
|
-
*
|
|
2341
|
+
* `oneOf`. {@link createContract} owns and validates developer-authored shape
|
|
2342
|
+
* graphs; cycles and nesting past {@link COMPILE_DEPTH_LIMIT} fail with a coded
|
|
2343
|
+
* {@link ContractError} before artifact compilation.
|
|
2017
2344
|
*/
|
|
2018
2345
|
export declare interface JSONSchema {
|
|
2019
2346
|
readonly type?: JSONSchemaType;
|
|
@@ -2046,7 +2373,7 @@ export declare type JSONSchemaType = 'null' | 'boolean' | 'object' | 'array' | '
|
|
|
2046
2373
|
* functions, `NaN`, and `±Infinity`); the parser gates through that guard; the
|
|
2047
2374
|
* schema is the empty schema `{}` (matches any JSON instance); the generator
|
|
2048
2375
|
* emits a small deterministic {@link JSONValue}. Unlike {@link RawShape}, whose
|
|
2049
|
-
* guard accepts
|
|
2376
|
+
* guard accepts every defined value, this shape validates that a value is real JSON.
|
|
2050
2377
|
*/
|
|
2051
2378
|
export declare interface JSONShape {
|
|
2052
2379
|
readonly type: 'json';
|
|
@@ -2058,8 +2385,8 @@ export declare interface JSONShape {
|
|
|
2058
2385
|
*
|
|
2059
2386
|
* @remarks
|
|
2060
2387
|
* The sound counterpart of {@link rawShape}: `rawShape` embeds an arbitrary
|
|
2061
|
-
* schema fragment and accepts
|
|
2062
|
-
* that a value is real JSON (via {@link isJSONValue}).
|
|
2388
|
+
* schema fragment and accepts every defined value at runtime, while `jsonShape`
|
|
2389
|
+
* validates that a value is real JSON (via {@link isJSONValue}).
|
|
2063
2390
|
*
|
|
2064
2391
|
* @param options - Optional `description`
|
|
2065
2392
|
* @returns A JSON passthrough shape
|
|
@@ -2122,10 +2449,10 @@ export declare function keyOf<const O extends Readonly<Record<PropertyKey, unkno
|
|
|
2122
2449
|
* self-referential recursive guards. Per §14 a throw from `thunk` (or the guard
|
|
2123
2450
|
* it resolves to) is contained and reported as a non-match.
|
|
2124
2451
|
*
|
|
2125
|
-
*
|
|
2126
|
-
*
|
|
2127
|
-
*
|
|
2128
|
-
*
|
|
2452
|
+
* Each lazy guard tracks its active invocation depth. An invocation that would
|
|
2453
|
+
* exceed {@link GUARD_DEPTH_LIMIT} returns `false` before resolving `thunk`; the
|
|
2454
|
+
* counter always unwinds after the contained call, so one deep or cyclic input
|
|
2455
|
+
* cannot poison later guard calls.
|
|
2129
2456
|
*
|
|
2130
2457
|
* @example
|
|
2131
2458
|
* ```ts
|
|
@@ -2137,16 +2464,32 @@ export declare function keyOf<const O extends Readonly<Record<PropertyKey, unkno
|
|
|
2137
2464
|
export declare function lazyOf<T>(thunk: () => Guard<T>): Guard<T>;
|
|
2138
2465
|
|
|
2139
2466
|
/**
|
|
2140
|
-
* Build a guard that accepts
|
|
2141
|
-
*
|
|
2467
|
+
* Build a guard that accepts a provided literal primitive using SameValueZero
|
|
2468
|
+
* comparison.
|
|
2469
|
+
*
|
|
2470
|
+
* @remarks
|
|
2471
|
+
* Signed zero compares equal, while `NaN` compares equal to `NaN`. Two call
|
|
2472
|
+
* forms, one meaning: list the literals inline, or hand in one array of them.
|
|
2473
|
+
* The array form exists for a vocabulary that is machine-generated rather than
|
|
2474
|
+
* hand-written — a `literalShape` built from an untrusted schema's `enum`, say
|
|
2475
|
+
* — where spreading a list of tens of thousands of entries would exhaust the
|
|
2476
|
+
* engine's argument limit. `compileGuard` / `compileParser` / `compileReporter`
|
|
2477
|
+
* take the array form for exactly that reason.
|
|
2478
|
+
*
|
|
2479
|
+
* @param literals - The permitted literal primitives, listed inline or as one array
|
|
2480
|
+
* @returns A guard narrowing to the provided literal union
|
|
2142
2481
|
*
|
|
2143
2482
|
* @example
|
|
2144
2483
|
* ```ts
|
|
2145
2484
|
* const isRole = literalOf('admin', 'member', 'guest')
|
|
2146
2485
|
* isRole('admin') // true
|
|
2147
2486
|
* isRole('owner') // false
|
|
2487
|
+
*
|
|
2488
|
+
* const isSameRole = literalOf(['admin', 'member', 'guest']) // the same guard, from an array
|
|
2148
2489
|
* ```
|
|
2149
2490
|
*/
|
|
2491
|
+
export declare function literalOf<const Literals extends ReadonlyArray<string | number | boolean>>(literals: Literals): Guard<Literals[number]>;
|
|
2492
|
+
|
|
2150
2493
|
export declare function literalOf<const Literals extends ReadonlyArray<string | number | boolean>>(...literals: Literals): Guard<Literals[number]>;
|
|
2151
2494
|
|
|
2152
2495
|
/** A literal shape — accepts exactly one of a fixed set of primitive values. */
|
|
@@ -2171,7 +2514,7 @@ export declare interface LiteralShape<T extends readonly (string | number | bool
|
|
|
2171
2514
|
* const via = literalShape(['function', 'tool', 'agent'], { description: 'How to run the step.' })
|
|
2172
2515
|
* ```
|
|
2173
2516
|
*/
|
|
2174
|
-
export declare function literalShape<const T extends readonly (string | number | boolean)[]>(values: T, options?: LiteralShapeOptions): LiteralShape<T
|
|
2517
|
+
export declare function literalShape<const T extends readonly (string | number | boolean)[]>(values: T, options?: LiteralShapeOptions): LiteralShape<Readonly<T>>;
|
|
2175
2518
|
|
|
2176
2519
|
/** Options for {@link LiteralShape} (via `literalShape`). */
|
|
2177
2520
|
export declare interface LiteralShapeOptions {
|
|
@@ -2193,9 +2536,38 @@ export declare function mapOf<K, V>(keyGuard: Guard<K>, valueGuard: Guard<V>): G
|
|
|
2193
2536
|
|
|
2194
2537
|
export declare function mapOf(keyPredicate: (value: unknown) => boolean, valuePredicate: (value: unknown) => boolean): Guard<ReadonlyMap<unknown, unknown>>;
|
|
2195
2538
|
|
|
2539
|
+
/**
|
|
2540
|
+
* Match an unknown value against the recursive JSON value structure.
|
|
2541
|
+
*
|
|
2542
|
+
* @remarks
|
|
2543
|
+
* The caller-owned ancestor set tracks only the active traversal path, so
|
|
2544
|
+
* cycles fail while shared references across sibling branches remain valid.
|
|
2545
|
+
* The set belongs to one traversal from one entry point; passing a shared or
|
|
2546
|
+
* pre-populated set is unsupported. Arrays and plain records recurse through
|
|
2547
|
+
* this helper; class instances and non-finite numbers are rejected.
|
|
2548
|
+
*
|
|
2549
|
+
* @param entry - The value to inspect
|
|
2550
|
+
* @param ancestors - Objects on the active traversal path
|
|
2551
|
+
* @returns `true` when the value is a cycle-free JSON value
|
|
2552
|
+
*
|
|
2553
|
+
* @example
|
|
2554
|
+
* ```ts
|
|
2555
|
+
* matchesJSONValue({ nested: [1, 'x', null] }, new WeakSet()) // true
|
|
2556
|
+
* matchesJSONValue(Number.NaN, new WeakSet()) // false
|
|
2557
|
+
* ```
|
|
2558
|
+
*/
|
|
2559
|
+
export declare function matchesJSONValue(entry: unknown, ancestors: WeakSet<object>): boolean;
|
|
2560
|
+
|
|
2196
2561
|
/**
|
|
2197
2562
|
* Build a guard that accepts strings matching a regular expression.
|
|
2198
2563
|
*
|
|
2564
|
+
* @remarks
|
|
2565
|
+
* Clones the pattern for the guard and strips the stateful `g` / `y` flags, so
|
|
2566
|
+
* repeated checks are stable and never change the caller's `lastIndex`.
|
|
2567
|
+
*
|
|
2568
|
+
* @param pattern - The regular expression to own and apply
|
|
2569
|
+
* @returns A stateless string guard
|
|
2570
|
+
*
|
|
2199
2571
|
* @example
|
|
2200
2572
|
* ```ts
|
|
2201
2573
|
* const isHex = matchOf(/^[0-9a-f]+$/)
|
|
@@ -2290,6 +2662,7 @@ export declare interface NumberShape {
|
|
|
2290
2662
|
*
|
|
2291
2663
|
* @param options - Optional bounds (`min` / `max`), `integer`, and `description`
|
|
2292
2664
|
* @returns A number shape
|
|
2665
|
+
* @throws {ContractError} When a present bound is not finite
|
|
2293
2666
|
*
|
|
2294
2667
|
* @example
|
|
2295
2668
|
* ```ts
|
|
@@ -2396,7 +2769,7 @@ export declare function omitOf<S extends GuardsShape, K extends ReadonlyArray<ke
|
|
|
2396
2769
|
* // 3.5 passes — matches numberShape only
|
|
2397
2770
|
* ```
|
|
2398
2771
|
*/
|
|
2399
|
-
export declare function oneOfShape<V extends readonly ContractShape[]>(...variants: V): UnionShape<V
|
|
2772
|
+
export declare function oneOfShape<V extends readonly ContractShape[]>(...variants: V): UnionShape<Readonly<V>>;
|
|
2400
2773
|
|
|
2401
2774
|
/**
|
|
2402
2775
|
* Like {@link FromGuards}, but every key listed in `K` becomes a true optional
|
|
@@ -2461,6 +2834,38 @@ export declare function orOf<A, B>(left: Guard<A>, right: Guard<B>): Guard<A | B
|
|
|
2461
2834
|
|
|
2462
2835
|
export declare function orOf(left: (value: unknown) => boolean, right: (value: unknown) => boolean): Guard<unknown>;
|
|
2463
2836
|
|
|
2837
|
+
/**
|
|
2838
|
+
* Take ownership of a contract shape node — the node itself when it is already
|
|
2839
|
+
* frozen, otherwise a {@link cloneShape} snapshot of its graph.
|
|
2840
|
+
*
|
|
2841
|
+
* @remarks
|
|
2842
|
+
* Builder- and `cloneShape`-produced frozen means owned: those nodes have frozen
|
|
2843
|
+
* `properties` / `variants` / `values` collections and pattern accessors that
|
|
2844
|
+
* return fresh frozen snapshots, so they cannot drift under a caller who still
|
|
2845
|
+
* holds an input reference or a previously read pattern. `Object.freeze` on a
|
|
2846
|
+
* hand-authored pattern shape is not an ownership marker because it cannot
|
|
2847
|
+
* protect `RegExp` internal slots; leave that shape unfrozen for this function
|
|
2848
|
+
* to snapshot, or pass it through {@link cloneShape} first.
|
|
2849
|
+
*
|
|
2850
|
+
* Every compiler entry point (`compileSchema` / `compileGuard` /
|
|
2851
|
+
* `compileParser` / `compileGenerator` / `compileReporter`) opens with this
|
|
2852
|
+
* call, and the check applies per node as the recursion descends: a frozen
|
|
2853
|
+
* parent is trusted for its own fields while each child is owned again at its
|
|
2854
|
+
* own level, so a hand-assembled graph that froze only part of itself is still
|
|
2855
|
+
* compiled from owned data.
|
|
2856
|
+
*
|
|
2857
|
+
* @param shape - The contract shape to own
|
|
2858
|
+
* @returns The shape itself when frozen, otherwise a deeply cloned frozen snapshot
|
|
2859
|
+
*
|
|
2860
|
+
* @example
|
|
2861
|
+
* ```ts
|
|
2862
|
+
* const authored = stringShape() // builders freeze
|
|
2863
|
+
* ownShape(authored) === authored // true
|
|
2864
|
+
* ownShape({ type: 'string' }) // a frozen snapshot — the literal is caller-owned
|
|
2865
|
+
* ```
|
|
2866
|
+
*/
|
|
2867
|
+
export declare function ownShape(shape: ContractShape): ContractShape;
|
|
2868
|
+
|
|
2464
2869
|
/**
|
|
2465
2870
|
* Parse an unknown value to an array — the input reference, never cloned —
|
|
2466
2871
|
* optionally guarding every element.
|
|
@@ -2537,14 +2942,16 @@ export declare function parseBooleanField(record: Record<string, unknown>, path:
|
|
|
2537
2942
|
* Parse an unknown value as one of the allowed literal primitives.
|
|
2538
2943
|
*
|
|
2539
2944
|
* @remarks
|
|
2540
|
-
* Pairs with {@link literalOf} — both match by
|
|
2945
|
+
* Pairs with {@link literalOf} — both match by SameValueZero, so the
|
|
2541
2946
|
* `parseEnum ↔ literalOf(...allowed)` pairing covers every literal primitive
|
|
2542
2947
|
* (string, number, or boolean), not only strings. Matching is identity, never
|
|
2543
|
-
* cross-type coercion: `parseEnum('1', [1])` stays `undefined`.
|
|
2948
|
+
* cross-type coercion: `parseEnum('1', [1])` stays `undefined`. The allowed
|
|
2949
|
+
* values are copied into an owned frozen `Set`; hostile iteration returns
|
|
2950
|
+
* `undefined` rather than escaping.
|
|
2544
2951
|
*
|
|
2545
2952
|
* @param value - The value to parse
|
|
2546
2953
|
* @param allowed - The permitted literal values
|
|
2547
|
-
* @returns The
|
|
2954
|
+
* @returns The input when it matches an allowed literal by SameValueZero, or `undefined`
|
|
2548
2955
|
*
|
|
2549
2956
|
* @example
|
|
2550
2957
|
* ```ts
|
|
@@ -2554,6 +2961,8 @@ export declare function parseBooleanField(record: Record<string, unknown>, path:
|
|
|
2554
2961
|
*/
|
|
2555
2962
|
export declare function parseEnum<const T extends string | number | boolean>(value: unknown, allowed: readonly T[]): T | undefined;
|
|
2556
2963
|
|
|
2964
|
+
export declare function parseEnum(value: unknown, allowed: readonly (string | number | boolean)[]): string | number | boolean | undefined;
|
|
2965
|
+
|
|
2557
2966
|
/**
|
|
2558
2967
|
* Read and parse an enum field from a record by key or nested key path.
|
|
2559
2968
|
*
|
|
@@ -2857,8 +3266,11 @@ export declare type RandomFunction = () => number;
|
|
|
2857
3266
|
* A raw JSON Schema passthrough — embeds a schema fragment directly.
|
|
2858
3267
|
*
|
|
2859
3268
|
* @remarks
|
|
2860
|
-
* For values the shape DSL can't express. The compiled guard accepts
|
|
2861
|
-
*
|
|
3269
|
+
* For values the shape DSL can't express. The compiled guard accepts every
|
|
3270
|
+
* top-level value except `undefined`, which is reserved as the parser failure
|
|
3271
|
+
* sentinel. Wrap the shape with {@link OptionalShape} to admit absence. Defined
|
|
3272
|
+
* values pass through unchanged, and the schema is emitted structurally
|
|
3273
|
+
* verbatim as an owned deeply frozen copy.
|
|
2862
3274
|
*/
|
|
2863
3275
|
export declare interface RawShape {
|
|
2864
3276
|
readonly type: 'raw';
|
|
@@ -2869,11 +3281,18 @@ export declare interface RawShape {
|
|
|
2869
3281
|
* Build a {@link RawShape} from a JSON Schema fragment.
|
|
2870
3282
|
*
|
|
2871
3283
|
* @remarks
|
|
2872
|
-
* For values the shape DSL can't express. The compiled guard accepts
|
|
2873
|
-
*
|
|
3284
|
+
* For values the shape DSL can't express. The compiled guard accepts every
|
|
3285
|
+
* DEFINED value — `undefined` alone fails, because it is the parser's failure
|
|
3286
|
+
* sentinel; wrap the shape in {@link optionalShape} to admit absence. The
|
|
3287
|
+
* parser passes a defined value through unchanged, and the fragment is
|
|
3288
|
+
* deep-cloned into an owned frozen snapshot ({@link cloneSchema}), so
|
|
3289
|
+
* `rawShape(fragment).schema !== fragment` and later edits to the caller's
|
|
3290
|
+
* fragment cannot reach the shape. `compileSchema` re-emits that snapshot
|
|
3291
|
+
* structurally verbatim; `compileGenerator` throws, since an arbitrary embedded
|
|
3292
|
+
* schema has no auto-generatable sample.
|
|
2874
3293
|
*
|
|
2875
3294
|
* @param schema - The JSON Schema fragment to embed
|
|
2876
|
-
* @returns A raw shape
|
|
3295
|
+
* @returns A raw shape owning a frozen copy of the fragment
|
|
2877
3296
|
*
|
|
2878
3297
|
* @example
|
|
2879
3298
|
* ```ts
|
|
@@ -3048,13 +3467,13 @@ export declare type SchemaFormat = 'date-time' | 'date' | 'time' | 'uuid' | 'ema
|
|
|
3048
3467
|
* @remarks
|
|
3049
3468
|
* Total: never throws. Guards depth exhaustion, a non-record node (the
|
|
3050
3469
|
* node's static `JSONSchema` type is not trusted at runtime), and a cyclic
|
|
3051
|
-
* re-encounter of `schema` — all three widen to {@link
|
|
3470
|
+
* re-encounter of `schema` — all three widen to {@link rawShape}. The
|
|
3052
3471
|
* ancestor set is added to and removed from around the WHOLE subtree
|
|
3053
3472
|
* conversion (not permanently), so a DAG-shaped schema reached twice via two
|
|
3054
3473
|
* different, non-cyclic paths does not false-positive as a cycle. The
|
|
3055
3474
|
* subtree conversion itself runs inside {@link attempt}, so a hostile
|
|
3056
3475
|
* throwing getter/Proxy anywhere in `schema` cannot escape as a thrown error
|
|
3057
|
-
* — it degrades to {@link
|
|
3476
|
+
* — it degrades to {@link rawShape} instead. A same-node re-conversion at
|
|
3058
3477
|
* the same remaining `depth` is served from `memo` (guards a
|
|
3059
3478
|
* shared-reference schema DAG against exponential blowup), mirroring
|
|
3060
3479
|
* {@link inferObject} / {@link inferArray} (inferers.ts).
|
|
@@ -3069,7 +3488,7 @@ export declare type SchemaFormat = 'date-time' | 'date' | 'time' | 'uuid' | 'ema
|
|
|
3069
3488
|
* recursion state owned by the {@link schemaToShape} entry point; passing
|
|
3070
3489
|
* a shared or pre-populated `WeakMap` changes caching behavior and is not
|
|
3071
3490
|
* supported usage
|
|
3072
|
-
* @returns The built shape for `schema`, or {@link
|
|
3491
|
+
* @returns The built shape for `schema`, or {@link rawShape} on any failure
|
|
3073
3492
|
*
|
|
3074
3493
|
* @example
|
|
3075
3494
|
* ```ts
|
|
@@ -3139,7 +3558,7 @@ export declare function schemaToParameters(schema: JSONSchema): Readonly<Record<
|
|
|
3139
3558
|
* @remarks
|
|
3140
3559
|
* Total: NEVER throws, for any input — a malformed, cyclic, deeply-nested, or
|
|
3141
3560
|
* outright hostile (throwing-getter Proxy) `schema` value all degrade to
|
|
3142
|
-
* {@link
|
|
3561
|
+
* {@link rawShape} rather than escaping as an error, so
|
|
3143
3562
|
* `createContract(schemaToShape(x))` is safe to call for any `x`. See
|
|
3144
3563
|
* {@link buildShapeFromNode} for the exact per-keyword precedence.
|
|
3145
3564
|
*
|
|
@@ -3149,9 +3568,36 @@ export declare function schemaToParameters(schema: JSONSchema): Readonly<Record<
|
|
|
3149
3568
|
* `RegExp` is a ReDoS vector; both keywords are read only far enough to be
|
|
3150
3569
|
* ignored. Any node the walk cannot express — an empty `{}`, an
|
|
3151
3570
|
* unrecognized `type`, a schema past {@link INFER_DEPTH_LIMIT} deep, or a
|
|
3152
|
-
* cyclic re-encounter — widens to {@link
|
|
3153
|
-
*
|
|
3154
|
-
*
|
|
3571
|
+
* cyclic re-encounter — widens to {@link rawShape} (accept any defined
|
|
3572
|
+
* value), never narrows.
|
|
3573
|
+
*
|
|
3574
|
+
* ROUND TRIP: `compileGuard(schemaToShape(valueToSchema(v)))(v)` is `true` for
|
|
3575
|
+
* every `v` — including the values no JSON Schema keyword describes (`NaN`,
|
|
3576
|
+
* `±Infinity`, a `Map`, a `Set`, a class instance, a function, a symbol, a
|
|
3577
|
+
* bigint, a cyclic or hostile host), which infer `{}` and widen back to an
|
|
3578
|
+
* accept-anything {@link rawShape}. Widening is the only source of looseness.
|
|
3579
|
+
* The law has three explicit host limits:
|
|
3580
|
+
*
|
|
3581
|
+
* - **Absence.** `undefined` is not a value: no compiled guard accepts it
|
|
3582
|
+
* (`rawShape` reserves it as the parser failure sentinel). So `undefined`
|
|
3583
|
+
* itself, an array element holding it, and an array HOLE (`arrayOf` requires
|
|
3584
|
+
* every index to be an own property, the same rule `isJSONValue` applies)
|
|
3585
|
+
* all fall outside the law. An OBJECT property holding `undefined` does not:
|
|
3586
|
+
* `valueToSchema` drops the key and opens the object, so the source object
|
|
3587
|
+
* is still accepted.
|
|
3588
|
+
* - **`Date` serialization.** A `Date` infers the schema of its JSON form
|
|
3589
|
+
* (`{ type: 'string' }`, plus `format: 'date-time'` when `format` is on), so
|
|
3590
|
+
* the law applies to `date.toISOString()` rather than the runtime instance.
|
|
3591
|
+
* - **Stateful access.** A getter whose result changes between inference and
|
|
3592
|
+
* guard evaluation can invalidate the sampled fact. Likewise, an array that
|
|
3593
|
+
* overrides iteration behavior can present different elements to the two
|
|
3594
|
+
* phases. The law applies only while the sampled host's observable own
|
|
3595
|
+
* enumerable string properties and array iteration remain stable.
|
|
3596
|
+
*
|
|
3597
|
+
* A widened node cannot be auto-generated: `rawShape` embeds an arbitrary
|
|
3598
|
+
* schema fragment, so `createContract(schemaToShape(x)).generate()` throws
|
|
3599
|
+
* when the conversion widened anywhere — `schema` / `is` / `parse` / `explain`
|
|
3600
|
+
* stay total.
|
|
3155
3601
|
*
|
|
3156
3602
|
* @param schema - The JSON Schema value to convert
|
|
3157
3603
|
* @returns The built {@link ContractShape}
|
|
@@ -3229,10 +3675,14 @@ export declare function shapeToKind(shape: ContractShape): FaultKind;
|
|
|
3229
3675
|
*
|
|
3230
3676
|
* @remarks
|
|
3231
3677
|
* Composes {@link isString} with {@link boundsOf} on the string's `.length` and
|
|
3232
|
-
* an
|
|
3233
|
-
* the bare {@link isString} guard
|
|
3234
|
-
* unrefined string leaf pays no wrapping
|
|
3235
|
-
* refinement shared by the compiled guard
|
|
3678
|
+
* an owned stateless pattern (the same refinement {@link matchOf} performs).
|
|
3679
|
+
* When all three options are absent it returns the bare {@link isString} guard
|
|
3680
|
+
* (the unconstrained fast path), so an unrefined string leaf pays no wrapping
|
|
3681
|
+
* cost. The single source of the string refinement shared by the compiled guard
|
|
3682
|
+
* and parser (compilers.ts).
|
|
3683
|
+
*
|
|
3684
|
+
* @param options - Optional length bounds and regular expression refinement
|
|
3685
|
+
* @returns A string guard enforcing the requested refinements
|
|
3236
3686
|
*
|
|
3237
3687
|
* @example
|
|
3238
3688
|
* ```ts
|
|
@@ -3255,6 +3705,7 @@ export declare interface StringShape {
|
|
|
3255
3705
|
readonly type: 'string';
|
|
3256
3706
|
readonly min?: number;
|
|
3257
3707
|
readonly max?: number;
|
|
3708
|
+
/** A pattern constraint; builders and cloners expose an owned fresh frozen zero-state copy per read. */
|
|
3258
3709
|
readonly pattern?: RegExp;
|
|
3259
3710
|
readonly description?: string;
|
|
3260
3711
|
}
|
|
@@ -3262,8 +3713,14 @@ export declare interface StringShape {
|
|
|
3262
3713
|
/**
|
|
3263
3714
|
* Build a string {@link StringShape}.
|
|
3264
3715
|
*
|
|
3716
|
+
* @remarks
|
|
3717
|
+
* A supplied pattern is captured by source and flags. The shape exposes a fresh
|
|
3718
|
+
* frozen zero-state `RegExp` on every `pattern` read, so neither the caller's
|
|
3719
|
+
* original nor a value read from the shape can drift later compiled artifacts.
|
|
3720
|
+
*
|
|
3265
3721
|
* @param options - Optional length (`min` / `max`), `pattern`, and `description`
|
|
3266
3722
|
* @returns A string shape
|
|
3723
|
+
* @throws {ContractError} When a present bound is invalid or `pattern` has flags
|
|
3267
3724
|
*
|
|
3268
3725
|
* @example
|
|
3269
3726
|
* ```ts
|
|
@@ -3324,9 +3781,8 @@ export declare interface Success<T> {
|
|
|
3324
3781
|
*
|
|
3325
3782
|
* @remarks
|
|
3326
3783
|
* `project` is a plain `(value: T) => U`. Per §14 the returned guard never
|
|
3327
|
-
* throws: a throw from `project` is contained and reported as a
|
|
3328
|
-
*
|
|
3329
|
-
* contained region. (Unlike the reference implementation, there is no
|
|
3784
|
+
* throws: a throw from `project` or `target` is contained and reported as a
|
|
3785
|
+
* non-match. (Unlike the reference implementation, there is no
|
|
3330
3786
|
* "curried projector" branch — a projection that legitimately returns a function
|
|
3331
3787
|
* would be double-invoked under that scheme. Compose explicitly if you need it.)
|
|
3332
3788
|
*
|
|
@@ -3377,6 +3833,14 @@ export declare function tupleOf(...predicates: ReadonlyArray<(value: unknown) =>
|
|
|
3377
3833
|
* `{ anyOf: [...] }`, sorted by their canonical key for deterministic output.
|
|
3378
3834
|
* An empty input list returns the empty accept-anything schema `{}`.
|
|
3379
3835
|
*
|
|
3836
|
+
* A member {@link canonicalStringify} cannot key — a cyclic, hostile, or
|
|
3837
|
+
* otherwise JSON-inexpressible fragment, which only a direct caller can supply
|
|
3838
|
+
* since the inferers always build plain encodable fragments — has NO
|
|
3839
|
+
* de-duplication key, so it can participate in neither de-duplication nor the
|
|
3840
|
+
* canonical-key ordering. It is KEPT (dropping a variant would narrow the
|
|
3841
|
+
* union, and unification only ever widens), appended in input order after the
|
|
3842
|
+
* sorted keyed members, so the result stays total and deterministic.
|
|
3843
|
+
*
|
|
3380
3844
|
* @param schemas - The schemas to unify
|
|
3381
3845
|
* @returns The unified schema
|
|
3382
3846
|
*
|
|
@@ -3406,8 +3870,9 @@ export declare function unionOf(...predicates: ReadonlyArray<(value: unknown) =>
|
|
|
3406
3870
|
* A union shape — accepts a value matching any one variant (first match wins).
|
|
3407
3871
|
*
|
|
3408
3872
|
* @remarks
|
|
3409
|
-
* `mode` selects the emitted JSON Schema keyword
|
|
3410
|
-
* `'
|
|
3873
|
+
* `mode` selects the emitted JSON Schema keyword and runtime matching rule:
|
|
3874
|
+
* `'anyOf'` (default) accepts the first matching variant, while `'oneOf'`
|
|
3875
|
+
* requires exactly one variant to match.
|
|
3411
3876
|
*/
|
|
3412
3877
|
export declare interface UnionShape<V extends readonly ContractShape[] = readonly ContractShape[]> {
|
|
3413
3878
|
readonly type: 'union';
|
|
@@ -3428,19 +3893,21 @@ export declare interface UnionShape<V extends readonly ContractShape[] = readonl
|
|
|
3428
3893
|
* // Infer<typeof id> = string | number
|
|
3429
3894
|
* ```
|
|
3430
3895
|
*/
|
|
3431
|
-
export declare function unionShape<V extends readonly ContractShape[]>(...variants: V): UnionShape<V
|
|
3896
|
+
export declare function unionShape<V extends readonly ContractShape[]>(...variants: V): UnionShape<Readonly<V>>;
|
|
3432
3897
|
|
|
3433
3898
|
/** Convert a union type to an intersection type. */
|
|
3434
3899
|
export declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
3435
3900
|
|
|
3436
3901
|
/**
|
|
3437
|
-
* Validate that a {@link ContractShape}
|
|
3438
|
-
*
|
|
3902
|
+
* Validate that a {@link ContractShape} graph is well-formed before
|
|
3903
|
+
* compilation.
|
|
3439
3904
|
*
|
|
3440
3905
|
* @remarks
|
|
3441
3906
|
* Fail-fast, per AGENTS §12: a malformed shape is a programmer error, so this
|
|
3442
|
-
* throws a
|
|
3443
|
-
* guard, parser, schema, or generator later.
|
|
3907
|
+
* throws a coded {@link ContractError} immediately rather than surfacing as a
|
|
3908
|
+
* silently-wrong guard, parser, schema, or generator later. The iterative walk
|
|
3909
|
+
* tracks active ancestors, so a structural cycle reports its precise path while
|
|
3910
|
+
* a shared child reached through separate paths remains legal. Checks:
|
|
3444
3911
|
*
|
|
3445
3912
|
* - An {@link OptionalShape} is only legal as a direct object-property value —
|
|
3446
3913
|
* `optionalShape` wrapping an array item, a union variant, another
|
|
@@ -3453,14 +3920,19 @@ export declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void
|
|
|
3453
3920
|
* `-Infinity`) number values.
|
|
3454
3921
|
* - A bounded {@link StringShape} / {@link NumberShape} / {@link ArrayShape}
|
|
3455
3922
|
* needs `min <= max` when both are set.
|
|
3923
|
+
* - Every present number bound is finite.
|
|
3456
3924
|
* - An integer {@link NumberShape} (`integer: true`) needs a non-empty integer
|
|
3457
3925
|
* range: `Math.ceil(min ?? -Infinity) <= Math.floor(max ?? Infinity)`.
|
|
3926
|
+
* - Shape nesting may not exceed {@link COMPILE_DEPTH_LIMIT}; excessive depth
|
|
3927
|
+
* fails before recursive artifact compilation begins. Because the depth check
|
|
3928
|
+
* runs before the active-ancestor check, a back-edge first reached beyond the
|
|
3929
|
+
* limit reports `depth`; a cycle reached within the limit reports `cycle`.
|
|
3458
3930
|
* - `null` / `json` / `raw` / `boolean` are always-valid leaves. Recursion
|
|
3459
3931
|
* continues into array items, object properties (and `additionalProperties`
|
|
3460
3932
|
* when it is a shape), union variants, and optional/nullable inner shapes.
|
|
3461
3933
|
*
|
|
3462
3934
|
* @param shape - The shape to validate
|
|
3463
|
-
* @throws {
|
|
3935
|
+
* @throws {ContractError} When the shape is malformed or cyclic
|
|
3464
3936
|
*
|
|
3465
3937
|
* @example
|
|
3466
3938
|
* ```ts
|
|
@@ -3470,6 +3942,23 @@ export declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void
|
|
|
3470
3942
|
*/
|
|
3471
3943
|
export declare function validateShape(shape: ContractShape): void;
|
|
3472
3944
|
|
|
3945
|
+
/**
|
|
3946
|
+
* Gate recursive compiler work on shape depth and structural cycles.
|
|
3947
|
+
*
|
|
3948
|
+
* @remarks
|
|
3949
|
+
* Walks the shape graph iteratively in linear time and stack space, tracking
|
|
3950
|
+
* active ancestors so shared children remain legal. Every standalone compiler
|
|
3951
|
+
* calls this gate before its recursive branch begins. The depth check runs
|
|
3952
|
+
* before the active-ancestor check, so a back-edge first reached beyond
|
|
3953
|
+
* {@link COMPILE_DEPTH_LIMIT} reports `depth`; a shallower cycle reports
|
|
3954
|
+
* `cycle`.
|
|
3955
|
+
*
|
|
3956
|
+
* @param shape - The shape graph to gate
|
|
3957
|
+
* @returns Nothing; successful return means recursive compilation is depth-safe
|
|
3958
|
+
* @throws {ContractError} When the shape is cyclic or exceeds the compilation depth limit
|
|
3959
|
+
*/
|
|
3960
|
+
export declare function validateShapeDepth(shape: ContractShape): void;
|
|
3961
|
+
|
|
3473
3962
|
/**
|
|
3474
3963
|
* Infer a `JSONSchema` for one unknown value — the reverse direction of
|
|
3475
3964
|
* {@link compileSchema}.
|