@orkestrel/contract 0.0.8 → 0.0.10
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 +7 -3
- package/dist/src/core/index.cjs +7922 -2600
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +2524 -323
- package/dist/src/core/index.d.ts +2524 -323
- package/dist/src/core/index.js +7872 -2600
- package/dist/src/core/index.js.map +1 -1
- package/package.json +8 -7
package/dist/src/core/index.d.ts
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Collect one more member into a vocabulary that grows as a walk proceeds.
|
|
3
|
+
*
|
|
4
|
+
* @param members - The vocabulary to extend
|
|
5
|
+
* @param value - The value to admit
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const seen = collectMembers([])
|
|
10
|
+
* admitMember(seen, 'a')
|
|
11
|
+
* matchesMember(seen, 'a') // true
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare function admitMember(members: Set<unknown>, value: unknown): void;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Record an object as entered on a traversal's active path.
|
|
18
|
+
*
|
|
19
|
+
* @param visited - The active-path set this traversal owns
|
|
20
|
+
* @param value - The object being entered
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* admitVisited(active, node)
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export declare function admitVisited(visited: WeakSet<object>, value: object): void;
|
|
28
|
+
|
|
1
29
|
/**
|
|
2
30
|
* Combine two guards with logical AND — passes only when both pass.
|
|
3
31
|
*
|
|
@@ -34,19 +62,67 @@ export declare type AnyConstructor<T = unknown> = new (...args: unknown[]) => T;
|
|
|
34
62
|
export declare type AnyFunction = (...args: unknown[]) => unknown;
|
|
35
63
|
|
|
36
64
|
/**
|
|
37
|
-
*
|
|
65
|
+
* Append every element of one array onto another, by index.
|
|
66
|
+
*
|
|
67
|
+
* @remarks
|
|
68
|
+
* The sibling of {@link pathOf}, for the other shape the same defect takes.
|
|
69
|
+
* `target[target.length] = ...source` and `[summary, ...rest]` both dispatch through
|
|
70
|
+
* `Array.prototype[Symbol.iterator]`, and a caller-installed iterator that
|
|
71
|
+
* yields one extra value writes the caller's text into a diagnostic this
|
|
72
|
+
* package publishes as its own. Both operands here are arrays this package
|
|
73
|
+
* built, and an indexed read of an own index property dispatches through
|
|
74
|
+
* nothing.
|
|
75
|
+
*
|
|
76
|
+
* @param target - The array to extend in place
|
|
77
|
+
* @param source - The elements to append, read by index
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* appendEntries(faults, compileReporter(inner, raw, pathOf(path, key)))
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare function appendEntries<T>(target: T[], source: readonly T[]): void;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Build a guard that accepts DENSE arrays whose every element satisfies
|
|
88
|
+
* `elementGuard`.
|
|
89
|
+
*
|
|
90
|
+
* @remarks
|
|
91
|
+
* Density is part of the contract, not an accident: the guard reads the array
|
|
92
|
+
* through the shared reflected own-index lens {@link readArrayEntries}, which is
|
|
93
|
+
* what makes it immune to caller-defined iteration and what `parseArray` reads
|
|
94
|
+
* too. A SPARSE array is therefore refused outright — `[1, , 3]` fails even when
|
|
95
|
+
* the element guard accepts `undefined` — because a hole is an absent own
|
|
96
|
+
* property rather than a present `undefined`, and the two are different facts.
|
|
97
|
+
* Pass `[1, undefined, 3]` when the middle slot is meant to exist.
|
|
38
98
|
*
|
|
39
99
|
* @example
|
|
40
100
|
* ```ts
|
|
41
101
|
* const isStringArray = arrayOf(isString)
|
|
42
102
|
* isStringArray(['a', 'b']) // true
|
|
43
103
|
* isStringArray(['a', 1]) // false
|
|
104
|
+
* isStringArray(['a', , 'b']) // false — sparse
|
|
44
105
|
* ```
|
|
45
106
|
*/
|
|
46
107
|
export declare function arrayOf<T>(elementGuard: Guard<T>): Guard<readonly T[]>;
|
|
47
108
|
|
|
48
109
|
export declare function arrayOf(elementGuard: (value: unknown) => boolean): Guard<readonly unknown[]>;
|
|
49
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Owned result of reading one array through its reflected own-index lens.
|
|
113
|
+
*
|
|
114
|
+
* @remarks
|
|
115
|
+
* {@link entries} is a frozen native array with actual holes: reading a hole
|
|
116
|
+
* yields `undefined`, while own membership remains absent. A length-driven
|
|
117
|
+
* consumer must first require {@link dense} or carry an independent work bound.
|
|
118
|
+
*/
|
|
119
|
+
export declare interface ArrayRead<T = unknown> {
|
|
120
|
+
/** Frozen native entries in index order, retaining sparse positions as holes. */
|
|
121
|
+
readonly entries: readonly (T | undefined)[];
|
|
122
|
+
/** Whether every index from zero through length minus one was reflected. */
|
|
123
|
+
readonly dense: boolean;
|
|
124
|
+
}
|
|
125
|
+
|
|
50
126
|
/** An array shape with an element shape and optional length bounds. */
|
|
51
127
|
export declare interface ArrayShape<S extends ContractShape = ContractShape> {
|
|
52
128
|
readonly type: 'array';
|
|
@@ -79,8 +155,8 @@ export declare interface ArrayShapeOptions {
|
|
|
79
155
|
}
|
|
80
156
|
|
|
81
157
|
/**
|
|
82
|
-
* Invoke a callback and capture its outcome as a
|
|
83
|
-
*
|
|
158
|
+
* Invoke a callback once and synchronously capture its exact outcome as a
|
|
159
|
+
* {@link Result}.
|
|
84
160
|
*
|
|
85
161
|
* @remarks
|
|
86
162
|
* The sanctioned never-throw boundary for the guards (AGENTS §14). The
|
|
@@ -89,12 +165,20 @@ export declare interface ArrayShapeOptions {
|
|
|
89
165
|
* `boolean`. This converts a throwing callback into a `Failure` so the
|
|
90
166
|
* surrounding guard can treat it as a non-match instead of propagating the
|
|
91
167
|
* exception, written once and shared rather than copy-pasted as ad-hoc
|
|
92
|
-
* `try`/`catch`. The
|
|
93
|
-
*
|
|
168
|
+
* `try`/`catch`. The return or thrown value is retained exactly and is never
|
|
169
|
+
* inspected, coerced, cloned, frozen, or mutated. A returned Promise or
|
|
170
|
+
* thenable is an ordinary successful value; later settlement is outside this
|
|
171
|
+
* synchronous boundary. {@link isContractError} does not use this boundary and
|
|
172
|
+
* is not an exception to it: it carries its own `try`/`catch` inside the class
|
|
173
|
+
* body, because `errors.ts` cannot import this module without inverting the
|
|
174
|
+
* dependency. The earlier claim here — that it was total BY CONSTRUCTION and had
|
|
175
|
+
* nothing to contain — was false, and it is what justified deleting the
|
|
176
|
+
* containment the committed baseline had. A guard whose totality rests on an
|
|
177
|
+
* argument that nothing inside it can throw is one refactor away from throwing.
|
|
94
178
|
*
|
|
95
179
|
* @param callback - The callback to invoke with no arguments
|
|
96
|
-
* @returns A `Success` carrying the return value, or a `Failure` carrying
|
|
97
|
-
*
|
|
180
|
+
* @returns A `Success` carrying the exact return value, or a `Failure` carrying
|
|
181
|
+
* the exact thrown value as `unknown`
|
|
98
182
|
*
|
|
99
183
|
* @example
|
|
100
184
|
* ```ts
|
|
@@ -104,6 +188,22 @@ export declare interface ArrayShapeOptions {
|
|
|
104
188
|
*/
|
|
105
189
|
export declare function attempt<T>(callback: () => T): Result<T>;
|
|
106
190
|
|
|
191
|
+
/** Every fault an audit reports — the parse faults plus undeclared keys. */
|
|
192
|
+
export declare type AuditFault = Fault | ExtraFault;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* A compiled strict-domain diagnostic — the shape of `compileAuditor` bound to
|
|
196
|
+
* one shape.
|
|
197
|
+
*
|
|
198
|
+
* @remarks
|
|
199
|
+
* The optional `path` is the prefix every fault this call reports is rooted at,
|
|
200
|
+
* so a nested walk can name where it started. A contract's
|
|
201
|
+
* {@link ContractInterface.audit} takes only the value, because no contract
|
|
202
|
+
* consumer injects a root path; a function of this type is assignable to that
|
|
203
|
+
* property, so one compiled function can serve both surfaces.
|
|
204
|
+
*/
|
|
205
|
+
export declare type AuditorFunction = (value: unknown, path?: readonly string[]) => readonly AuditFault[];
|
|
206
|
+
|
|
107
207
|
/** A boolean shape — accepts only `true` or `false`. */
|
|
108
208
|
export declare interface BooleanShape {
|
|
109
209
|
readonly type: 'boolean';
|
|
@@ -204,6 +304,26 @@ export declare function boundsOf(min?: number, max?: number): Guard<number>;
|
|
|
204
304
|
*/
|
|
205
305
|
export declare function buildObjectShape(schema: JSONSchema, depth: number, visited: WeakSet<object>, memo: WeakMap<object, Map<number, ContractShape>>, description: string | undefined): ContractShape;
|
|
206
306
|
|
|
307
|
+
/**
|
|
308
|
+
* Build one empty {@link SampleMemo} node.
|
|
309
|
+
*
|
|
310
|
+
* @remarks
|
|
311
|
+
* The root a multi-sample walk starts from and the node every further row
|
|
312
|
+
* prefix is grown into, built from the CAPTURED `WeakMap` and `Map` so a
|
|
313
|
+
* replaced global cannot decide what a published schema is served from. One
|
|
314
|
+
* memo belongs to one walk: {@link samplesToSchema} builds it at the door, and
|
|
315
|
+
* a direct caller of {@link inferSamples} / {@link inferRecordSamples} builds a
|
|
316
|
+
* fresh one per call.
|
|
317
|
+
*
|
|
318
|
+
* @returns An empty memo node with no recorded rows and no recorded schemas
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* ```ts
|
|
322
|
+
* inferRecordSamples([{ id: 1 }], 32, 256, true, false, false, buildSampleMemo())
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
export declare function buildSampleMemo(): SampleMemo;
|
|
326
|
+
|
|
207
327
|
/**
|
|
208
328
|
* Build a {@link ContractShape} for one JSON Schema node — the recursive
|
|
209
329
|
* spine of {@link schemaToShape}, shared with per-child recursion via
|
|
@@ -231,15 +351,15 @@ export declare function buildObjectShape(schema: JSONSchema, depth: number, visi
|
|
|
231
351
|
* the matching primitive shape, with length/range bounds derived via
|
|
232
352
|
* {@link deriveLengthBounds} / {@link deriveRangeBounds}. An integer node
|
|
233
353
|
* additionally drops its bounds when they describe an EMPTY integer range
|
|
234
|
-
* (e.g. `minimum: 1.5, maximum: 1.6`) — the same emptiness `
|
|
354
|
+
* (e.g. `minimum: 1.5, maximum: 1.6`) — the same emptiness `validateShapeDepth`
|
|
235
355
|
* rejects — so the result is always a valid shape.
|
|
236
356
|
* 5. `type: 'array'` — an {@link arrayShape} whose element shape recurses into
|
|
237
357
|
* a record-valued `items` (widening to {@link rawShape} otherwise), with
|
|
238
358
|
* bounds from `minItems` / `maxItems`.
|
|
239
359
|
* 6. `type: 'object'`, OR no `type` / `enum` / `oneOf` / `anyOf` but a
|
|
240
360
|
* record-valued `properties` — delegates to {@link buildObjectShape}.
|
|
241
|
-
* 7. Everything else — an empty schema, an unrecognized/absent `type`,
|
|
242
|
-
* exhausted depth/breadth
|
|
361
|
+
* 7. Everything else — an empty schema, an unrecognized/absent `type`, or
|
|
362
|
+
* exhausted depth/breadth — widens to
|
|
243
363
|
* {@link rawShape}, whose guard accepts every defined value and whose
|
|
244
364
|
* emitted schema is the same `{}` (plus `description`) the node carried.
|
|
245
365
|
* This is the exact inverse of `{}`, JSON Schema's accept-anything schema,
|
|
@@ -273,12 +393,13 @@ export declare function buildObjectShape(schema: JSONSchema, depth: number, visi
|
|
|
273
393
|
export declare function buildShapeFromNode(schema: JSONSchema, depth: number, visited: WeakSet<object>, memo: WeakMap<object, Map<number, ContractShape>>): ContractShape;
|
|
274
394
|
|
|
275
395
|
/**
|
|
276
|
-
* Encode one value as a deterministic, key-sorted JSON string — the
|
|
396
|
+
* Encode one value as a deterministic, key-sorted JSON string — the traversal
|
|
277
397
|
* spine of {@link canonicalStringify}.
|
|
278
398
|
*
|
|
279
399
|
* @remarks
|
|
280
|
-
* Arrays keep their element order
|
|
281
|
-
*
|
|
400
|
+
* Arrays keep their element order through the shared dense own-index lens;
|
|
401
|
+
* records sort their own keys before encoding, at every nesting level. Every
|
|
402
|
+
* other value is encoded by
|
|
282
403
|
* `JSON.stringify`, so `NaN` / `±Infinity` collapse to `'null'` and `-0`
|
|
283
404
|
* encodes as `'0'` — the same lossy-but-deterministic mapping real JSON makes.
|
|
284
405
|
*
|
|
@@ -286,10 +407,20 @@ export declare function buildShapeFromNode(schema: JSONSchema, depth: number, vi
|
|
|
286
407
|
* function, a symbol, an array hole, or a cyclic back-edge to an ancestor. A
|
|
287
408
|
* container carrying such a member is itself un-encodable and returns
|
|
288
409
|
* `undefined` too, so the result is either a faithful encoding of the WHOLE
|
|
289
|
-
* value or nothing — a partially-encoded key is never emitted.
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
*
|
|
410
|
+
* value or nothing — a partially-encoded key is never emitted. A hostile
|
|
411
|
+
* getter or `Proxy` trap is refused through this function's own required-read
|
|
412
|
+
* boundary, including when this spine is called directly.
|
|
413
|
+
*
|
|
414
|
+
* The walk is ITERATIVE over an explicit enter/exit stack and keeps a
|
|
415
|
+
* walk-local encoding memo, so a container reached through several paths is
|
|
416
|
+
* encoded ONCE. That memo is sound precisely because a partial answer does not
|
|
417
|
+
* exist here: any cycle or JSON-inexpressible member abandons the whole call,
|
|
418
|
+
* so a recorded encoding is the container's complete encoding on every path.
|
|
419
|
+
* The earlier recursion re-encoded per path, which made thirty ordinary shared
|
|
420
|
+
* aliases cost `2^30` encodings through the public {@link canonicalStringify}
|
|
421
|
+
* and {@link unifySchemas} doors. Each container's members are read ONCE, at
|
|
422
|
+
* the moment it is entered, and the ancestor set is restored on every exit
|
|
423
|
+
* including the abandoning ones.
|
|
293
424
|
*
|
|
294
425
|
* @param value - The value to encode
|
|
295
426
|
* @param ancestors - Objects on the active traversal path, guarding cycles
|
|
@@ -315,8 +446,8 @@ export declare function canonicalizeValue(value: unknown, ancestors: WeakSet<obj
|
|
|
315
446
|
* `JSONSchema` fragments built independently always canonicalize to the same
|
|
316
447
|
* string. Pure host-independent ECMAScript with no environment-specific imports.
|
|
317
448
|
*
|
|
318
|
-
*
|
|
319
|
-
*
|
|
449
|
+
* For READABLE input it returns `undefined` — never a partial or invalid
|
|
450
|
+
* encoding — for every value JSON cannot faithfully encode:
|
|
320
451
|
*
|
|
321
452
|
* - `undefined` itself, a function, a symbol, or an array hole (JSON encodes
|
|
322
453
|
* none of them), at the top level or anywhere inside a container;
|
|
@@ -324,8 +455,10 @@ export declare function canonicalizeValue(value: unknown, ancestors: WeakSet<obj
|
|
|
324
455
|
* - cyclic input, tracked with the same ancestor-{@link WeakSet} discipline
|
|
325
456
|
* {@link inferArray} / {@link inferObject} use, so a shared (non-cyclic)
|
|
326
457
|
* reference reached twice through different paths still encodes;
|
|
327
|
-
*
|
|
328
|
-
*
|
|
458
|
+
* A hostile traversal is categorically different: a throwing own-getter,
|
|
459
|
+
* hostile `ownKeys` trap, or revoked `Proxy` throws a `structure`
|
|
460
|
+
* {@link ContractError} through {@link readValue}. A caller can therefore
|
|
461
|
+
* distinguish "not JSON-encodable" from "could not be read".
|
|
329
462
|
*
|
|
330
463
|
* A caller therefore treats `undefined` as "this value has no canonical key",
|
|
331
464
|
* never as an encoding: see {@link unifySchemas} (an un-keyed member cannot
|
|
@@ -336,6 +469,7 @@ export declare function canonicalizeValue(value: unknown, ancestors: WeakSet<obj
|
|
|
336
469
|
* nested piece of one)
|
|
337
470
|
* @returns A deterministic string encoding of `value`, or `undefined` when JSON
|
|
338
471
|
* cannot encode it
|
|
472
|
+
* @throws {ContractError} When the value cannot be read
|
|
339
473
|
*
|
|
340
474
|
* @example
|
|
341
475
|
* ```ts
|
|
@@ -348,6 +482,105 @@ export declare function canonicalizeValue(value: unknown, ancestors: WeakSet<obj
|
|
|
348
482
|
*/
|
|
349
483
|
export declare function canonicalStringify(value: unknown): string | undefined;
|
|
350
484
|
|
|
485
|
+
/**
|
|
486
|
+
* Classify an already-bounded string against the pattern-only and
|
|
487
|
+
* calendar-checked {@link SchemaFormat} vocabulary.
|
|
488
|
+
*
|
|
489
|
+
* @remarks
|
|
490
|
+
* The pure leaf behind {@link stringToFormat}'s total boundary: it performs the
|
|
491
|
+
* pattern dispatch, and the door decides what a failed dispatch answers. Order
|
|
492
|
+
* is significant — `date-time` is tried before `date`, and both require the
|
|
493
|
+
* calendar check, so `2020-13-45` matches the pattern and is still refused.
|
|
494
|
+
*
|
|
495
|
+
* @param value - The candidate string, already length-bounded
|
|
496
|
+
* @returns The matched {@link SchemaFormat}, or `undefined`
|
|
497
|
+
* @throws The exact value thrown by a redirected pattern dispatch
|
|
498
|
+
*
|
|
499
|
+
* @example
|
|
500
|
+
* ```ts
|
|
501
|
+
* classifyFormat('ada@example.com') // 'email'
|
|
502
|
+
* ```
|
|
503
|
+
*/
|
|
504
|
+
export declare function classifyFormat(value: string): SchemaFormat | undefined;
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* The maximum number of nodes one JSON snapshot may produce, frozen.
|
|
508
|
+
*
|
|
509
|
+
* @remarks
|
|
510
|
+
* JSON persistence is a TREE, so `cloneJSONValue` / `cloneJSONRecord`
|
|
511
|
+
* deliberately duplicate a repeated noncyclic alias into distinct equal
|
|
512
|
+
* branches — `clone.primary !== clone.fallback` is a documented guarantee, not
|
|
513
|
+
* an accident, and a memo would silently take it away. The price of that
|
|
514
|
+
* guarantee is that output size is exponential in the number of shared aliases:
|
|
515
|
+
* an ordinary in-memory graph of twenty-one objects, a few hundred bytes,
|
|
516
|
+
* produced two million nodes and took seconds, and thirty aliases took hours.
|
|
517
|
+
* No attacker is needed — shared references are normal data.
|
|
518
|
+
*
|
|
519
|
+
* So the cost is BOUNDED rather than paid: the walk counts the nodes it
|
|
520
|
+
* produces and refuses past this cap with the ordinary cause-free `clone`
|
|
521
|
+
* refusal, which makes the door's worst case a function of this constant
|
|
522
|
+
* instead of the caller's input. Size a snapshot against it: a document with
|
|
523
|
+
* more than this many nodes — counting every array, record, and leaf the
|
|
524
|
+
* snapshot would contain AFTER alias duplication — is refused rather than
|
|
525
|
+
* cloned.
|
|
526
|
+
*
|
|
527
|
+
* @example
|
|
528
|
+
* ```ts
|
|
529
|
+
* CLONE_NODE_LIMIT // 262144
|
|
530
|
+
* ```
|
|
531
|
+
*/
|
|
532
|
+
export declare const CLONE_NODE_LIMIT = 262144;
|
|
533
|
+
|
|
534
|
+
/**
|
|
535
|
+
* Deep-clone an exact JSON object record into an owned frozen snapshot.
|
|
536
|
+
*
|
|
537
|
+
* @remarks
|
|
538
|
+
* Adds a record-root boundary to {@link cloneJSONValue}. The output is a
|
|
539
|
+
* deeply frozen null-prototype record, and repeated noncyclic aliases are
|
|
540
|
+
* duplicated as independent JSON tree branches.
|
|
541
|
+
*
|
|
542
|
+
* @param value - The unknown record value to validate and snapshot
|
|
543
|
+
* @returns A deeply cloned and frozen JSON record
|
|
544
|
+
* @throws {ContractError} When the root is not a record, nested data is inexact, or traversal fails
|
|
545
|
+
*
|
|
546
|
+
* @example
|
|
547
|
+
* ```ts
|
|
548
|
+
* cloneJSONRecord({ attempt: 1 }) // frozen null-prototype record
|
|
549
|
+
* ```
|
|
550
|
+
*/
|
|
551
|
+
export declare function cloneJSONRecord(value: unknown): JSONRecord;
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* Deep-clone exact JSON data into an owned frozen snapshot.
|
|
555
|
+
*
|
|
556
|
+
* @remarks
|
|
557
|
+
* Traverses iteratively so deeply nested input cannot exhaust the call stack.
|
|
558
|
+
* Repeated noncyclic aliases are duplicated because JSON persistence represents
|
|
559
|
+
* a tree, while a structural back-edge on the active path is rejected as a
|
|
560
|
+
* cycle. Arrays are rebuilt as standard dense arrays and records as
|
|
561
|
+
* null-prototype objects; every produced node is frozen after its children are
|
|
562
|
+
* wired. Array keys must be exactly `length` plus every canonical index.
|
|
563
|
+
* Writable and configurable index flags are normalized rather than treated as
|
|
564
|
+
* JSON data, so frozen arrays remain valid. Property descriptors are inspected
|
|
565
|
+
* without reading values through accessors, and every hostile reflective
|
|
566
|
+
* operation is contained before a new clone-coded error is exposed.
|
|
567
|
+
* Each call creates a fresh {@link JSONCloner}; use the class directly when
|
|
568
|
+
* terminal success or failure identity must be replayed without another read.
|
|
569
|
+
*
|
|
570
|
+
* @param value - The unknown value to validate and snapshot
|
|
571
|
+
* @returns The primitive unchanged, or a deeply cloned and frozen JSON graph
|
|
572
|
+
* @throws {ContractError} When the value is not exact acyclic JSON data or traversal fails
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
575
|
+
* ```ts
|
|
576
|
+
* const source = { settings: { enabled: true } }
|
|
577
|
+
* const clone = cloneJSONValue(source)
|
|
578
|
+
* source.settings.enabled = false
|
|
579
|
+
* clone // { settings: { enabled: true } }
|
|
580
|
+
* ```
|
|
581
|
+
*/
|
|
582
|
+
export declare function cloneJSONValue(value: unknown): JSONValue;
|
|
583
|
+
|
|
351
584
|
/**
|
|
352
585
|
* Deep-clone a JSON Schema graph into an owned frozen snapshot.
|
|
353
586
|
*
|
|
@@ -358,6 +591,8 @@ export declare function canonicalStringify(value: unknown): string | undefined;
|
|
|
358
591
|
* null prototypes, arrays retain only their intrinsic array prototype, and
|
|
359
592
|
* every produced object is frozen after its edges are wired. Hostile traversal
|
|
360
593
|
* throws a clone-coded {@link ContractError}, never a caller-owned raw error.
|
|
594
|
+
* Each call creates a fresh {@link SchemaCloner}; use the class directly when
|
|
595
|
+
* terminal success or failure identity must be replayed without another read.
|
|
361
596
|
*
|
|
362
597
|
* @param schema - The JSON Schema graph to snapshot
|
|
363
598
|
* @returns A deeply cloned and frozen JSON Schema graph
|
|
@@ -376,18 +611,30 @@ export declare function cloneSchema(schema: JSONSchema): JSONSchema;
|
|
|
376
611
|
* Deep-clone a contract shape graph into an owned frozen snapshot.
|
|
377
612
|
*
|
|
378
613
|
* @remarks
|
|
379
|
-
*
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
*
|
|
384
|
-
*
|
|
385
|
-
*
|
|
386
|
-
*
|
|
614
|
+
* A fresh public {@link ShapeCloner} preserves shared-child identity while
|
|
615
|
+
* building the candidate snapshot, owns raw schemas, validates the exact
|
|
616
|
+
* completed root, applies deferred fidelity, and translates unexpected hostile
|
|
617
|
+
* failures. Inside the engine, each node's own data discriminant and every
|
|
618
|
+
* declared field are captured descriptor-first from two agreeing reads, and the
|
|
619
|
+
* carried population alone supplies its shallow shell, semantic checks, child
|
|
620
|
+
* scheduling, and later edge wiring. An inherited field is refused through a
|
|
621
|
+
* non-invoking presence check, and a revealed accessor is refused without
|
|
622
|
+
* invocation except for the builder's `pattern` contract: two fresh frozen
|
|
623
|
+
* genuine `RegExp` values with equal source and flags are captured as owned
|
|
624
|
+
* source/flags semantics.
|
|
625
|
+
*
|
|
626
|
+
* Literal and union members retain their own-index descriptor/repeated-read
|
|
627
|
+
* checks; property entries retain descriptor/first/second agreement plus two
|
|
628
|
+
* exactly equal ordered key populations; raw schemas delegate to
|
|
629
|
+
* {@link cloneSchema}. Before return, a fresh {@link ShapeValidator} independently
|
|
630
|
+
* validates the exact cloned root. Ownership never erases a structural edge or
|
|
631
|
+
* normalizes an invalid scalar/bound into a plausible declaration: those
|
|
632
|
+
* inputs throw their coded structure/bound error. Hostile traversal remains
|
|
633
|
+
* distinct and throws a clone-coded {@link ContractError}.
|
|
387
634
|
*
|
|
388
635
|
* @param shape - The contract shape graph to snapshot
|
|
389
636
|
* @returns A deeply cloned and frozen shape graph
|
|
390
|
-
* @throws {ContractError} When hostile shape or raw-schema traversal prevents ownership
|
|
637
|
+
* @throws {ContractError} When the declaration cannot be copied faithfully or hostile shape or raw-schema traversal prevents ownership
|
|
391
638
|
*
|
|
392
639
|
* @example
|
|
393
640
|
* ```ts
|
|
@@ -400,14 +647,94 @@ export declare function cloneShape(shape: StringShape): StringShape;
|
|
|
400
647
|
|
|
401
648
|
export declare function cloneShape(shape: ContractShape): ContractShape;
|
|
402
649
|
|
|
650
|
+
/**
|
|
651
|
+
* Build the collector a captured `forEach` sweep appends through.
|
|
652
|
+
*
|
|
653
|
+
* @remarks
|
|
654
|
+
* Extracted rather than written inline for the same reason
|
|
655
|
+
* {@link compareValues} is: `Reflect.apply` takes its arguments as a LIST, so an
|
|
656
|
+
* inline collector is a function expression inside an array literal rather than
|
|
657
|
+
* one passed directly as an argument — a hidden function assignment this
|
|
658
|
+
* repository forbids wherever it appears. Returned directly from a factory it is
|
|
659
|
+
* both named and testable. Both sweeps hand the callback `(value, key)`; a `Set`
|
|
660
|
+
* passes its entry in both positions, so one collector serves both.
|
|
661
|
+
*
|
|
662
|
+
* @param target - The pair list to append each swept entry onto
|
|
663
|
+
* @returns The collector a captured `forEach` invokes per entry
|
|
664
|
+
*
|
|
665
|
+
* @example
|
|
666
|
+
* ```ts
|
|
667
|
+
* const collected: unknown[][] = []
|
|
668
|
+
* new Set(['a']).forEach(collectEntries(collected)) // [['a', 'a']]
|
|
669
|
+
* ```
|
|
670
|
+
*/
|
|
671
|
+
export declare function collectEntries(target: unknown[][]): (value: unknown, key: unknown) => void;
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Collect an array's entries into a membership collection this package owns.
|
|
675
|
+
*
|
|
676
|
+
* @remarks
|
|
677
|
+
* THE builder behind every declared vocabulary, and the first half of the answer
|
|
678
|
+
* to a defect that survived three rounds by moving rather than closing. A guard
|
|
679
|
+
* deciding membership with `set.has(value)` answers whatever the caller most
|
|
680
|
+
* recently installed on `Set.prototype`, and answering it instead through the
|
|
681
|
+
* `has` method of an exported class only moved the writable member one prototype
|
|
682
|
+
* up: `Vocabulary.prototype.has = () => true` reproduced the whole defect at
|
|
683
|
+
* nineteen door groups. There is no property lookup here to redirect —
|
|
684
|
+
* membership is asked of a MODULE BINDING, which the specification makes
|
|
685
|
+
* immutable to every importer, over operations {@link INTRINSICS} captured while
|
|
686
|
+
* it evaluated.
|
|
687
|
+
*
|
|
688
|
+
* Collection is by INDEX rather than from an iterable on purpose:
|
|
689
|
+
* `new Set(values)` reads `Symbol.iterator` off the argument and `add` off the
|
|
690
|
+
* instance, so building from an iterable would reintroduce two replaceable
|
|
691
|
+
* dispatches to remove one.
|
|
692
|
+
*
|
|
693
|
+
* @param values - The members to collect, read by index
|
|
694
|
+
* @returns A collection no caller holds a reference to
|
|
695
|
+
*
|
|
696
|
+
* @example
|
|
697
|
+
* ```ts
|
|
698
|
+
* const allowed = collectMembers(['admin', 'guest'])
|
|
699
|
+
* matchesMember(allowed, 'admin') // true
|
|
700
|
+
* ```
|
|
701
|
+
*/
|
|
702
|
+
export declare function collectMembers(values: readonly unknown[]): Set<unknown>;
|
|
703
|
+
|
|
704
|
+
/**
|
|
705
|
+
* Order two primitive keys or indices ascending.
|
|
706
|
+
*
|
|
707
|
+
* @remarks
|
|
708
|
+
* The comparison {@link sortValues} hands to the captured sort, extracted rather
|
|
709
|
+
* than written inline. `Reflect.apply` takes its arguments as a LIST, so an
|
|
710
|
+
* inline comparator is a function expression inside an array literal rather than
|
|
711
|
+
* one passed directly as an argument — a hidden function assignment, which this
|
|
712
|
+
* repository forbids wherever it appears because a function that is not a named
|
|
713
|
+
* declaration is a function no caller can reach and no test can exercise. As a
|
|
714
|
+
* declaration it is both. The comparison is `<` / `>` on primitives, which
|
|
715
|
+
* dispatches through nothing: no `valueOf`, no `toString`, and no member a
|
|
716
|
+
* caller can replace, so the order a published schema is emitted in is decided
|
|
717
|
+
* by the values and not by the environment.
|
|
718
|
+
*
|
|
719
|
+
* @param left - The value ordered first when it compares lower
|
|
720
|
+
* @param right - The value compared against
|
|
721
|
+
* @returns `-1`, `1`, or `0` as `left` sorts before, after, or with `right`
|
|
722
|
+
*
|
|
723
|
+
* @example
|
|
724
|
+
* ```ts
|
|
725
|
+
* compareValues('a', 'b') // -1
|
|
726
|
+
* ```
|
|
727
|
+
*/
|
|
728
|
+
export declare function compareValues<T extends string | number>(left: T, right: T): number;
|
|
729
|
+
|
|
403
730
|
/**
|
|
404
731
|
* The maximum supported nesting depth of a compiled contract shape, frozen.
|
|
405
732
|
*
|
|
406
733
|
* @remarks
|
|
407
|
-
* {@link
|
|
408
|
-
* {@link ContractError} before recursive artifact
|
|
409
|
-
* finite but pathologically deep developer-authored
|
|
410
|
-
* instead of reaching the JavaScript call-stack limit.
|
|
734
|
+
* {@link validateShapeDepth} rejects the next level
|
|
735
|
+
* with a depth-coded {@link ContractError} before recursive artifact
|
|
736
|
+
* compilation begins, so a finite but pathologically deep developer-authored
|
|
737
|
+
* shape fails predictably instead of reaching the JavaScript call-stack limit.
|
|
411
738
|
*
|
|
412
739
|
* @example
|
|
413
740
|
* ```ts
|
|
@@ -416,6 +743,81 @@ export declare function cloneShape(shape: ContractShape): ContractShape;
|
|
|
416
743
|
*/
|
|
417
744
|
export declare const COMPILE_DEPTH_LIMIT = 512;
|
|
418
745
|
|
|
746
|
+
/**
|
|
747
|
+
* The maximum number of nodes a compiled artifact may expand a shape into,
|
|
748
|
+
* frozen.
|
|
749
|
+
*
|
|
750
|
+
* @remarks
|
|
751
|
+
* A shape graph is a DAG; every compiled artifact is a TREE. A declaration may
|
|
752
|
+
* therefore be tiny and its schema, guard, parser, reporter, auditor and
|
|
753
|
+
* generated value enormous: `objectShape({ left: node, right: node })` nested
|
|
754
|
+
* thirty times is thirty-one authored nodes that expand into more than a
|
|
755
|
+
* billion emitted ones. Sharing one child is ordinary authoring, not an attack,
|
|
756
|
+
* and the compilers cannot fold the expansion away without publishing a schema
|
|
757
|
+
* whose members alias each other.
|
|
758
|
+
*
|
|
759
|
+
* So the cost is BOUNDED rather than paid. {@link validateShapeDepth} and
|
|
760
|
+
* {@link ContractCompiler} preparation both count the nodes the declaration
|
|
761
|
+
* expands into (one per node, summed over every incoming edge) and refuse past
|
|
762
|
+
* this cap through {@link refuseExpansion}, with an `expansion`-coded
|
|
763
|
+
* {@link ContractError}. Ownership is deliberately NOT bounded by it:
|
|
764
|
+
* {@link cloneShape} and {@link ownShape} preserve shared-child identity, so
|
|
765
|
+
* they answer a shared-child graph in time proportional to its authored nodes
|
|
766
|
+
* and keep working above this cap.
|
|
767
|
+
*
|
|
768
|
+
* What this cap bounds MOVED when the compilers stopped expanding a DAG. Every
|
|
769
|
+
* artifact family is now one entry per unique node, so the boundary declaration
|
|
770
|
+
* above compiles fourteen nodes in about a millisecond rather than sixteen
|
|
771
|
+
* thousand in 1,342 ms, and the cap is no longer what keeps compilation finite.
|
|
772
|
+
* What still expands is what a CONSUMER materializes from the artifacts: the
|
|
773
|
+
* value `generate` builds, and the document a compiled schema serializes to,
|
|
774
|
+
* are trees of exactly this size. The cap survives as a bound on what a caller
|
|
775
|
+
* can be handed, not on what the compiler pays.
|
|
776
|
+
*
|
|
777
|
+
* @example
|
|
778
|
+
* ```ts
|
|
779
|
+
* COMPILE_NODE_LIMIT // 16384
|
|
780
|
+
* ```
|
|
781
|
+
*/
|
|
782
|
+
export declare const COMPILE_NODE_LIMIT = 16384;
|
|
783
|
+
|
|
784
|
+
/**
|
|
785
|
+
* Audit a value against the strict acceptance domain of a {@link ContractShape}.
|
|
786
|
+
*
|
|
787
|
+
* @remarks
|
|
788
|
+
* The diagnostic for the domain {@link compileGuard} and {@link compileSchema}
|
|
789
|
+
* describe, where {@link compileReporter} diagnoses the wider preimage
|
|
790
|
+
* {@link compileParser} maps into it. This walk therefore mirrors the guard:
|
|
791
|
+
* leaf coercions are faults, closed-object extras are faults, and union
|
|
792
|
+
* acceptance is decided from each variant's strict audit emptiness, so the
|
|
793
|
+
* soundness invariant
|
|
794
|
+
* `compileAuditor(shape, v).length === 0 ⟺ compileGuard(shape)(v)` holds
|
|
795
|
+
* structurally. The shared declaration gate rejects structural and bound-domain
|
|
796
|
+
* malformations before either artifact is built, so the invariant is only
|
|
797
|
+
* evaluated for a valid declaration. The invariant relates two
|
|
798
|
+
* separate calls, so it holds for a value whose reads are stable across calls;
|
|
799
|
+
* see the read-stability precondition on {@link ContractInterface}. Every
|
|
800
|
+
* recursive call returns at most {@link FAULT_LIMIT} entries. Hostile property
|
|
801
|
+
* access raises the shared coded read refusal with the current container path
|
|
802
|
+
* and shape, so unreadability never masquerades as a type mismatch.
|
|
803
|
+
* It requests exactly the `auditor` root of a fresh {@link ContractCompiler}
|
|
804
|
+
* and applies it once; reuse the compiler (or a contract's `audit`) when many
|
|
805
|
+
* values are audited against one shape.
|
|
806
|
+
*
|
|
807
|
+
* @param shape - The shape to audit against
|
|
808
|
+
* @param value - The value to check
|
|
809
|
+
* @param path - The path prefix for faults produced at this call
|
|
810
|
+
* @returns The strict faults found, empty exactly when the compiled guard accepts a stably-read value
|
|
811
|
+
*
|
|
812
|
+
* @example
|
|
813
|
+
* ```ts
|
|
814
|
+
* const user = objectShape({ name: stringShape() })
|
|
815
|
+
* compileAuditor(user, { name: 'Ada', extra: true })
|
|
816
|
+
* // [{ reason: 'extra', path: ['extra'] }]
|
|
817
|
+
* ```
|
|
818
|
+
*/
|
|
819
|
+
export declare function compileAuditor(shape: ContractShape, value: unknown, path?: readonly string[]): readonly AuditFault[];
|
|
820
|
+
|
|
419
821
|
/**
|
|
420
822
|
* Compile a {@link ContractShape} into a deterministic seed value.
|
|
421
823
|
*
|
|
@@ -425,20 +827,19 @@ export declare const COMPILE_DEPTH_LIMIT = 512;
|
|
|
425
827
|
* from the wall clock when none is supplied. Shape-generation failures throw a
|
|
426
828
|
* {@link ContractError} with code `'generate'`; {@link drawRandom} failures use
|
|
427
829
|
* code `'random'` and retain the consuming shape category in context.
|
|
428
|
-
* Failures include a
|
|
429
|
-
* pattern
|
|
430
|
-
*
|
|
431
|
-
*
|
|
432
|
-
*
|
|
433
|
-
*
|
|
434
|
-
*
|
|
435
|
-
* bounded shape is normally caught there; these throws remain here as defense
|
|
436
|
-
* for standalone `compileGenerator` use. Union candidates are bounded by
|
|
830
|
+
* Failures include a pattern-constrained `stringShape` whose generated sample
|
|
831
|
+
* cannot satisfy the pattern, an invalid random sample, and a `rawShape` whose
|
|
832
|
+
* arbitrary embedded schema cannot be auto-generated. Degenerate empty
|
|
833
|
+
* literal/union vocabularies fail earlier with the shared gate's `empty` code.
|
|
834
|
+
* It requests exactly the `generator` root of a fresh
|
|
835
|
+
* {@link ContractCompiler} and invokes it once, so the declaration is owned and
|
|
836
|
+
* validated once before any draw. Union candidates are bounded by
|
|
437
837
|
* {@link GENERATION_ATTEMPT_LIMIT} and accepted only when they satisfy the
|
|
438
|
-
* union's compiled guard
|
|
838
|
+
* union's compiled guard, which is why a declaration containing a union also
|
|
839
|
+
* builds the compiler's guard plan.
|
|
439
840
|
*
|
|
440
841
|
* @param shape - The shape to generate from
|
|
441
|
-
* @param random - A seeded random source (defaults to
|
|
842
|
+
* @param random - A seeded random source (defaults to a wall-clock seed drawn inside the door's boundary)
|
|
442
843
|
* @returns A value matching the shape
|
|
443
844
|
* @throws {ContractError} When the shape or random source cannot produce a valid value
|
|
444
845
|
*
|
|
@@ -457,12 +858,16 @@ export declare function compileGenerator(shape: ContractShape, random?: RandomFu
|
|
|
457
858
|
* @remarks
|
|
458
859
|
* Reuses the combinators for structural and refined shapes, including
|
|
459
860
|
* {@link literalOf} for SameValueZero literal matching. Compiled object shapes
|
|
460
|
-
* observe own enumerable string keys through {@link enumerableKeys},
|
|
461
|
-
* their parser, reporter, and inference
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
*
|
|
861
|
+
* observe own enumerable string keys through {@link enumerableKeys}, the same
|
|
862
|
+
* key view their parser, reporter, auditor, and inference use for both open and
|
|
863
|
+
* closed objects — the view is shared, the verdict on an undeclared key is not
|
|
864
|
+
* (this guard rejects the object; the parser drops the key).
|
|
865
|
+
* Like every guard it is total — it never throws (AGENTS §14). It requests
|
|
866
|
+
* exactly the `guard` root of a fresh {@link ContractCompiler}, which owns the
|
|
867
|
+
* declaration once through {@link ownShape} and validates that snapshot once,
|
|
868
|
+
* so an unfrozen caller-owned graph compiles from a snapshot and excessive
|
|
869
|
+
* nesting, cycles, and excessive expansion are rejected before any guard is
|
|
870
|
+
* built.
|
|
466
871
|
*
|
|
467
872
|
* @param shape - The shape to compile
|
|
468
873
|
* @returns A guard narrowing to the shape's inferred type
|
|
@@ -482,21 +887,26 @@ export declare function compileGuard(shape: ContractShape): Guard<unknown>;
|
|
|
482
887
|
*
|
|
483
888
|
* @remarks
|
|
484
889
|
* Reuses the leaf parsers (`parseString` / `parseInteger` / `parseNumber` /
|
|
485
|
-
* `parseBoolean`
|
|
890
|
+
* `parseBoolean`) and coerces structurally. An object fails as a
|
|
486
891
|
* whole on any required-field failure; a union returns a guard-valid value
|
|
487
892
|
* unchanged, otherwise the first variant that both parses and guards wins.
|
|
488
893
|
*
|
|
489
894
|
* After coercing a leaf, it re-applies that leaf's REFINEMENTS through the same
|
|
490
|
-
* combinators
|
|
491
|
-
* `boundsOf` for a number's value and an array's length, and
|
|
492
|
-
* for literal membership — so a value that coerces but
|
|
493
|
-
* to `undefined`. The result is full parse↔guard
|
|
895
|
+
* combinators {@link compileGuard} uses — `stringOf` for a string's
|
|
896
|
+
* length/pattern, `boundsOf` for a number's value and an array's length, and
|
|
897
|
+
* {@link literalOf} for literal membership — so a value that coerces but
|
|
898
|
+
* violates a bound parses to `undefined`. The result is full parse↔guard
|
|
899
|
+
* soundness (AGENTS §14): a
|
|
494
900
|
* non-`undefined` parse always satisfies the contract's `is`, refinements
|
|
495
|
-
* included
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
*
|
|
901
|
+
* included — a statement about the OUTPUT, never about the input, which may be
|
|
902
|
+
* a value `is` rejects. Object presence and extra-key processing read the same
|
|
903
|
+
* own-enumerable-string key view as the guard, reporter, auditor, and inference
|
|
904
|
+
* — one key set, deliberately different verdicts: this parser drops an
|
|
905
|
+
* undeclared key that {@link compileGuard} rejects and {@link compileAuditor}
|
|
906
|
+
* faults. It requests exactly the `parser` root of a fresh
|
|
907
|
+
* {@link ContractCompiler}, which owns and validates the declaration once; a
|
|
908
|
+
* union's variant selection is a guard question, so a declaration containing one
|
|
909
|
+
* also builds the compiler's guard plan.
|
|
500
910
|
*
|
|
501
911
|
* @param shape - The shape to compile
|
|
502
912
|
* @returns A parser yielding the shape's inferred type or `undefined`
|
|
@@ -523,6 +933,11 @@ export declare function compileParser(shape: ContractShape): Parser<unknown>;
|
|
|
523
933
|
* holds structurally — `explain` mirrors `parse`, not the stricter `is` (a
|
|
524
934
|
* coercible value like `'42'` against a `numberShape` reports no fault, the
|
|
525
935
|
* same leniency `parse` grants, even though the strict guard would reject it).
|
|
936
|
+
* The invariant relates two separate calls, so it holds for a value whose reads
|
|
937
|
+
* are stable across calls; see the read-stability precondition on
|
|
938
|
+
* {@link ContractInterface}. {@link compileAuditor} is the counterpart report
|
|
939
|
+
* for the strict domain, mirroring {@link compileGuard} the way this one
|
|
940
|
+
* mirrors {@link compileParser}.
|
|
526
941
|
*
|
|
527
942
|
* Faults are collected in stable pre-order (declared key/index order); every
|
|
528
943
|
* call — object, array, and union alike, including a union's `oneOf` "no
|
|
@@ -531,12 +946,15 @@ export declare function compileParser(shape: ContractShape): Parser<unknown>;
|
|
|
531
946
|
* bound holds at every level of nesting, not just the outermost container, on
|
|
532
947
|
* adversarial input (a huge array, a wide record, a wide union of wide
|
|
533
948
|
* records). A closed object's extra keys never
|
|
534
|
-
* fault — `parse` silently drops them, so `explain` mirrors that leniency
|
|
535
|
-
*
|
|
536
|
-
*
|
|
537
|
-
*
|
|
538
|
-
* throw (AGENTS §14).
|
|
539
|
-
*
|
|
949
|
+
* fault — `parse` silently drops them, so `explain` mirrors that leniency, and
|
|
950
|
+
* {@link compileAuditor} is where they do fault; an open object with a
|
|
951
|
+
* constraining `additionalProperties` shape recurses extras against it instead.
|
|
952
|
+
* A hostile getter or throwing `Proxy` trap is contained via {@link attempt}
|
|
953
|
+
* and surfaces as a single top-level type fault, never a throw (AGENTS §14).
|
|
954
|
+
* It requests exactly the `reporter` root of a fresh {@link ContractCompiler}
|
|
955
|
+
* and applies it once, so one call owns and validates the declaration once
|
|
956
|
+
* rather than re-gating every node it descends into. Reuse the compiler (or a
|
|
957
|
+
* contract's `explain`) when many values are reported against one shape.
|
|
540
958
|
*
|
|
541
959
|
* @param shape - The shape to report against
|
|
542
960
|
* @param value - The value to check
|
|
@@ -551,7 +969,7 @@ export declare function compileParser(shape: ContractShape): Parser<unknown>;
|
|
|
551
969
|
* compileReporter(user, { name: 'Ada' }) // []
|
|
552
970
|
* ```
|
|
553
971
|
*/
|
|
554
|
-
export declare function compileReporter(shape: ContractShape, value: unknown, path?: string[]): readonly Fault[];
|
|
972
|
+
export declare function compileReporter(shape: ContractShape, value: unknown, path?: readonly string[]): readonly Fault[];
|
|
555
973
|
|
|
556
974
|
/**
|
|
557
975
|
* Compile a {@link ContractShape} into a JSON Schema document.
|
|
@@ -561,8 +979,11 @@ export declare function compileReporter(shape: ContractShape, value: unknown, pa
|
|
|
561
979
|
* required keys in `required`; nullable shapes emit an `anyOf` with `{ type:
|
|
562
980
|
* 'null' }`. The result is an owned deeply frozen graph; raw schemas are cloned
|
|
563
981
|
* rather than retained by reference. Emission only — it never inspects a
|
|
564
|
-
* runtime value.
|
|
565
|
-
*
|
|
982
|
+
* runtime value. It requests exactly the `schema` root of a fresh
|
|
983
|
+
* {@link ContractCompiler}, so the declaration is owned once and validated once
|
|
984
|
+
* and no other artifact family is built. Shared declaration identity survives
|
|
985
|
+
* into the emitted document: two slots holding one authored node hold one
|
|
986
|
+
* emitted subschema, while structurally equal distinct nodes stay distinct.
|
|
566
987
|
*
|
|
567
988
|
* @param shape - The shape to compile
|
|
568
989
|
* @returns The emitted JSON Schema
|
|
@@ -587,6 +1008,68 @@ export declare function compileSchema(shape: ContractShape): JSONSchema;
|
|
|
587
1008
|
*/
|
|
588
1009
|
export declare function complementOf<TBase, TExcluded extends TBase>(base: Guard<TBase>, excluded: Guard<TExcluded> | ((value: TBase) => value is TExcluded)): Guard<Exclude<TBase, TExcluded>>;
|
|
589
1010
|
|
|
1011
|
+
/**
|
|
1012
|
+
* Run a public door's whole body and publish only this package's error class.
|
|
1013
|
+
*
|
|
1014
|
+
* @remarks
|
|
1015
|
+
* The other half of the answer {@link INTRINSICS} gives, and the half that does
|
|
1016
|
+
* not depend on anyone enumerating anything. Capture removes a named dispatch;
|
|
1017
|
+
* this removes the CONSEQUENCE of every dispatch a door's path still makes,
|
|
1018
|
+
* named or not. Four consecutive rounds fixed the statements they were shown
|
|
1019
|
+
* and were defeated by a statement one line later, because a boundary placed
|
|
1020
|
+
* per statement is only ever as complete as the last sweep. A boundary at the
|
|
1021
|
+
* door composes: whatever the body reaches, and whatever a caller installs
|
|
1022
|
+
* under it, the door publishes a {@link ContractError} or the value it
|
|
1023
|
+
* promised.
|
|
1024
|
+
*
|
|
1025
|
+
* A {@link ContractError} reaching this boundary passes through by identity —
|
|
1026
|
+
* the diagnosis a door spent its whole body computing is the point of the door,
|
|
1027
|
+
* and rewrapping it would demote it to a cause. The mechanism is
|
|
1028
|
+
* {@link isContractError}, which establishes CLASS MEMBERSHIP; it does not and
|
|
1029
|
+
* cannot establish that this package authored the error, and the passthrough is
|
|
1030
|
+
* described by what it tests rather than by what it intends. Anything else is a
|
|
1031
|
+
* host failure the caller arranged, so it is republished under the door's own
|
|
1032
|
+
* name with the exact thrown value retained as `cause`.
|
|
1033
|
+
*
|
|
1034
|
+
* Its population is exactly the public doors that can refuse — every door whose
|
|
1035
|
+
* TSDoc carries `@throws {ContractError}`, and no door whose body cannot throw
|
|
1036
|
+
* at all. A wrapper around a body that only allocates a closure buys nothing
|
|
1037
|
+
* and misreports where the refusals are.
|
|
1038
|
+
*
|
|
1039
|
+
* Use {@link readValue} instead where a single read has its own subject and
|
|
1040
|
+
* deserves its own diagnostic; use this where the subject is the door.
|
|
1041
|
+
*
|
|
1042
|
+
* @param callback - The door body to run
|
|
1043
|
+
* @param door - The public door name used in the diagnostic
|
|
1044
|
+
* @param options - Optional code and structured context for the published refusal
|
|
1045
|
+
* @returns The body's exact return value
|
|
1046
|
+
* @throws {ContractError} The body's own refusal, or a coded translation of a host failure
|
|
1047
|
+
*
|
|
1048
|
+
* @example
|
|
1049
|
+
* ```ts
|
|
1050
|
+
* export function nullShape(options?: NullShapeOptions): NullShape {
|
|
1051
|
+
* return contain(() => buildNullShape(options), 'nullShape')
|
|
1052
|
+
* }
|
|
1053
|
+
* ```
|
|
1054
|
+
*/
|
|
1055
|
+
export declare function contain<T>(callback: () => T, door: string, options?: ContainOptions): T;
|
|
1056
|
+
|
|
1057
|
+
/**
|
|
1058
|
+
* Optional diagnostic metadata for a public door's containment boundary.
|
|
1059
|
+
*
|
|
1060
|
+
* @remarks
|
|
1061
|
+
* Deliberately narrower than {@link ReadValueOptions}: a contained door's
|
|
1062
|
+
* subject IS the door, so there is no `subject` to name. The two options types
|
|
1063
|
+
* are separate because a signature that accepts a key it silently ignores tells
|
|
1064
|
+
* the caller a lie the type checker will not catch.
|
|
1065
|
+
*/
|
|
1066
|
+
export declare interface ContainOptions {
|
|
1067
|
+
/** Machine-readable refusal category. */
|
|
1068
|
+
readonly code?: ContractCode;
|
|
1069
|
+
/** Structured location and domain details retained by the refusal. */
|
|
1070
|
+
readonly context?: ContractErrorContext;
|
|
1071
|
+
}
|
|
1072
|
+
|
|
590
1073
|
/** Machine-readable category carried by a {@link ContractError}. */
|
|
591
1074
|
export declare type ContractCode =
|
|
592
1075
|
/** Identifies a bound contract error. */
|
|
@@ -595,8 +1078,10 @@ export declare type ContractCode =
|
|
|
595
1078
|
| 'range'
|
|
596
1079
|
/** Identifies an empty-value contract error. */
|
|
597
1080
|
| 'empty'
|
|
598
|
-
/** Identifies a
|
|
1081
|
+
/** Identifies a valid optional shape used in a forbidden position. */
|
|
599
1082
|
| 'placement'
|
|
1083
|
+
/** Identifies a corrupt shape node or structural slot. */
|
|
1084
|
+
| 'structure'
|
|
600
1085
|
/** Identifies a literal contract error. */
|
|
601
1086
|
| 'literal'
|
|
602
1087
|
/** Identifies a cycle contract error. */
|
|
@@ -610,10 +1095,142 @@ export declare type ContractCode =
|
|
|
610
1095
|
/** Identifies an owned-clone contract error. */
|
|
611
1096
|
| 'clone'
|
|
612
1097
|
/** Identifies a compilation-depth contract error. */
|
|
613
|
-
| 'depth'
|
|
1098
|
+
| 'depth'
|
|
1099
|
+
/** Identifies a shape whose compiled expansion exceeds the emitted-node limit. */
|
|
1100
|
+
| 'expansion';
|
|
1101
|
+
|
|
1102
|
+
/**
|
|
1103
|
+
* Lazy compiler owning one contract shape's six artifacts and their bundle.
|
|
1104
|
+
*
|
|
1105
|
+
* @remarks
|
|
1106
|
+
* The engine every standalone `compile*` function and `createContract` now runs
|
|
1107
|
+
* on. Its reason for existing is that the recursive compilers used to re-own and
|
|
1108
|
+
* re-validate the SUBGRAPH at every node they descended into, so a depth-100
|
|
1109
|
+
* chain paid a hundred clones of shrinking graphs and a hundred validations —
|
|
1110
|
+
* quadratic work for a linear declaration, measured at 640 ms for one guard and
|
|
1111
|
+
* 1.87 s for one contract. Here ownership runs once, validation runs once over
|
|
1112
|
+
* that owned result, and each unique node and structural edge is indexed once
|
|
1113
|
+
* into children-before-parent order. Every artifact family is then a single
|
|
1114
|
+
* postorder pass whose entries are keyed by node identity, so a shared child is
|
|
1115
|
+
* compiled once however many parents point at it. The same asymmetry exists on
|
|
1116
|
+
* the VALUE side and is answered the same way: `guard`, `auditor` and `reporter`
|
|
1117
|
+
* carry a call-scoped ledger, so a shared object costs one visit per compiled
|
|
1118
|
+
* node rather than one per path through the graph.
|
|
1119
|
+
*
|
|
1120
|
+
* Construction observes nothing at all: no read, no validation, no clone, no
|
|
1121
|
+
* graph-sized allocation, no clock, no draw. The first getter read prepares; the
|
|
1122
|
+
* getters after it replay. A getter builds its own family and no other, except
|
|
1123
|
+
* that `parser`, `reporter` and `generator` build the guard plan when the graph
|
|
1124
|
+
* holds a union, because a union's membership question IS a guard question in
|
|
1125
|
+
* all three.
|
|
1126
|
+
*
|
|
1127
|
+
* One terminal lifecycle covers preparation and every family. A failure settles
|
|
1128
|
+
* the compiler: later getters rethrow that exact error, while an artifact
|
|
1129
|
+
* already handed out keeps working, because each compiled artifact is
|
|
1130
|
+
* self-contained. Reentry — reachable only through a caller accessor the
|
|
1131
|
+
* declaration itself exposes, since a `pattern` getter is the one accessor
|
|
1132
|
+
* ownership invokes — poisons the nested read, the interrupted outer read, and
|
|
1133
|
+
* every later read with one shared cause-free error.
|
|
1134
|
+
*
|
|
1135
|
+
* @typeParam S - The declaration's shape type, which types the published artifacts
|
|
1136
|
+
*
|
|
1137
|
+
* @example
|
|
1138
|
+
* ```ts
|
|
1139
|
+
* const compiler = new ContractCompiler({ type: 'string', min: 1 })
|
|
1140
|
+
* compiler.guard('Ada') // true
|
|
1141
|
+
* compiler.guard === compiler.guard // true — every getter replays its exact root
|
|
1142
|
+
* ```
|
|
1143
|
+
*/
|
|
1144
|
+
export declare class ContractCompiler<S extends ContractShape = ContractShape> implements ContractCompilerInterface<S> {
|
|
1145
|
+
#private;
|
|
1146
|
+
/**
|
|
1147
|
+
* Retain a shape declaration without observing it.
|
|
1148
|
+
*
|
|
1149
|
+
* @param shape - The live declaration the first getter read will own
|
|
1150
|
+
*/
|
|
1151
|
+
constructor(shape: S);
|
|
1152
|
+
/**
|
|
1153
|
+
* The emitted JSON Schema for the owned declaration.
|
|
1154
|
+
*
|
|
1155
|
+
* @remarks
|
|
1156
|
+
* A deeply frozen graph that preserves shared declaration identity: two
|
|
1157
|
+
* property slots holding the same authored node hold the same emitted
|
|
1158
|
+
* subschema, while structurally equal distinct nodes stay distinct objects.
|
|
1159
|
+
*/
|
|
1160
|
+
get schema(): JSONSchema;
|
|
1161
|
+
/** The compiled strict guard for the owned declaration. */
|
|
1162
|
+
get guard(): Guard<Infer<S>>;
|
|
1163
|
+
/** The compiled coercive parser for the owned declaration. */
|
|
1164
|
+
get parser(): Parser<Infer<S>>;
|
|
1165
|
+
/** The compiled strict-domain diagnostic for the owned declaration. */
|
|
1166
|
+
get auditor(): AuditorFunction;
|
|
1167
|
+
/** The compiled coercive-domain diagnostic for the owned declaration. */
|
|
1168
|
+
get reporter(): ReporterFunction;
|
|
1169
|
+
/** The compiled seed-data source for the owned declaration. */
|
|
1170
|
+
get generator(): SeederFunction<Infer<S>>;
|
|
1171
|
+
/**
|
|
1172
|
+
* The frozen six-member bundle of this compiler's artifacts.
|
|
1173
|
+
*
|
|
1174
|
+
* @remarks
|
|
1175
|
+
* Own enumerable keys `schema`, `is`, `parse`, `audit`, `explain`,
|
|
1176
|
+
* `generate`, in that order, each holding the exact value the corresponding
|
|
1177
|
+
* getter publishes.
|
|
1178
|
+
*/
|
|
1179
|
+
get contract(): ContractInterface<Infer<S>>;
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
/**
|
|
1183
|
+
* Lazy owner of one contract shape's six compiled artifacts plus their bundle.
|
|
1184
|
+
*
|
|
1185
|
+
* @remarks
|
|
1186
|
+
* Construction observes nothing. The FIRST getter read owns the declaration
|
|
1187
|
+
* once, validates that owned graph once, and indexes each unique node and
|
|
1188
|
+
* structural edge once into children-before-parent order; every artifact family
|
|
1189
|
+
* is then one postorder pass over that index, so a shared child costs its
|
|
1190
|
+
* authored nodes rather than its paths. A getter builds its own family and no
|
|
1191
|
+
* other, except where an artifact genuinely consumes the compiled guard —
|
|
1192
|
+
* `parser`, `reporter` and `generator` resolve union membership through it, so
|
|
1193
|
+
* they build it too. `contract` requests all six roots in getter order.
|
|
1194
|
+
*
|
|
1195
|
+
* Every getter REPLAYS its exact artifact: reading one twice returns the same
|
|
1196
|
+
* function or graph by identity, and {@link contract}'s six members are those
|
|
1197
|
+
* exact values. One terminal lifecycle covers preparation and every family, so
|
|
1198
|
+
* a failure anywhere settles the compiler permanently — later getters rethrow
|
|
1199
|
+
* that exact error while an artifact already handed out stays usable, because
|
|
1200
|
+
* each one is self-contained. Cross-getter reentry (only reachable through a
|
|
1201
|
+
* caller accessor the declaration exposes) poisons the nested read, the
|
|
1202
|
+
* interrupted outer read, and every later read with one shared cause-free
|
|
1203
|
+
* error.
|
|
1204
|
+
*
|
|
1205
|
+
* @example
|
|
1206
|
+
* ```ts
|
|
1207
|
+
* const compiler = new ContractCompiler(stringShape({ min: 1 }))
|
|
1208
|
+
* compiler.guard('Ada') // true
|
|
1209
|
+
* ```
|
|
1210
|
+
*/
|
|
1211
|
+
export declare interface ContractCompilerInterface<S extends ContractShape> {
|
|
1212
|
+
/** The emitted JSON Schema, deeply frozen and shared-identity preserving. */
|
|
1213
|
+
readonly schema: JSONSchema;
|
|
1214
|
+
/** The compiled strict guard. */
|
|
1215
|
+
readonly guard: Guard<Infer<S>>;
|
|
1216
|
+
/** The compiled coercive parser. */
|
|
1217
|
+
readonly parser: Parser<Infer<S>>;
|
|
1218
|
+
/** The compiled strict-domain diagnostic. */
|
|
1219
|
+
readonly auditor: AuditorFunction;
|
|
1220
|
+
/** The compiled coercive-domain diagnostic. */
|
|
1221
|
+
readonly reporter: ReporterFunction;
|
|
1222
|
+
/** The compiled seed-data source. */
|
|
1223
|
+
readonly generator: SeederFunction<Infer<S>>;
|
|
1224
|
+
/** The frozen six-member bundle whose values are the six artifacts above. */
|
|
1225
|
+
readonly contract: ContractInterface<Infer<S>>;
|
|
1226
|
+
}
|
|
614
1227
|
|
|
615
1228
|
/**
|
|
616
|
-
* Error carrying a machine-readable contract category
|
|
1229
|
+
* Error carrying a machine-readable contract category, optional context, and
|
|
1230
|
+
* an exact optional cause. Omitting `cause` omits the own property; explicitly
|
|
1231
|
+
* supplying `cause: undefined` retains an own property with that value. Both
|
|
1232
|
+
* optional options are read as OWN properties, so a construction never consults
|
|
1233
|
+
* the caller-writable prototype chain of the container it was handed.
|
|
617
1234
|
*
|
|
618
1235
|
* @example
|
|
619
1236
|
* ```ts
|
|
@@ -624,6 +1241,7 @@ export declare type ContractCode =
|
|
|
624
1241
|
* ```
|
|
625
1242
|
*/
|
|
626
1243
|
export declare class ContractError extends Error {
|
|
1244
|
+
#private;
|
|
627
1245
|
readonly name = "ContractError";
|
|
628
1246
|
readonly code: ContractCode;
|
|
629
1247
|
readonly context: ContractErrorContext | undefined;
|
|
@@ -634,6 +1252,52 @@ export declare class ContractError extends Error {
|
|
|
634
1252
|
* @param options - Machine-readable category, optional context, and optional cause
|
|
635
1253
|
*/
|
|
636
1254
|
constructor(message: string, options: ContractErrorOptions);
|
|
1255
|
+
/**
|
|
1256
|
+
* Hand the module the branded recognition test.
|
|
1257
|
+
*
|
|
1258
|
+
* @remarks
|
|
1259
|
+
* The bootstrap seam, and deliberately NOT a spelling of the recognition
|
|
1260
|
+
* question. Private-name membership is legal only inside this class body, so
|
|
1261
|
+
* something must bridge the class to the module — and the previous bridge was
|
|
1262
|
+
* a static that ANSWERED the question, `ContractError.owns(value)`. That
|
|
1263
|
+
* reintroduced, verbatim, the poisonable surface the private brand was chosen
|
|
1264
|
+
* to remove: one assignment made the package fail to recognize its own errors,
|
|
1265
|
+
* so an engine rewrapped an error it had authored as an unreadable failure,
|
|
1266
|
+
* and a throwing assignment put the caller's raw value through fifteen public
|
|
1267
|
+
* doors.
|
|
1268
|
+
*
|
|
1269
|
+
* This hands over the CLOSURE instead: {@link isContractError} calls it and
|
|
1270
|
+
* applies the result, so the only member on the recognition path hands the
|
|
1271
|
+
* test over rather than answering with it. That per-call read is not a seam,
|
|
1272
|
+
* because the static block below pins this member as a non-writable,
|
|
1273
|
+
* non-configurable own data property.
|
|
1274
|
+
*
|
|
1275
|
+
* An earlier round argued the pin needed no further defence, because a static
|
|
1276
|
+
* block runs during class definition and no order — this module's, an
|
|
1277
|
+
* importer's, or a module that evaluated first — reaches `ContractError.guard`
|
|
1278
|
+
* before the block runs. That is true and it answers the wrong question.
|
|
1279
|
+
* INSTALLING IS NOT READING. The concern was never that something reads the
|
|
1280
|
+
* member unpinned; it was whether the pin installs at all, and the pin is one
|
|
1281
|
+
* live dispatch through `Object.defineProperty`. A module that evaluated
|
|
1282
|
+
* first can replace it with a selective no-op, after which the block runs,
|
|
1283
|
+
* returns, installs nothing, and every later read finds a writable member
|
|
1284
|
+
* again — the whole `owns` defect restored, invisibly, in a realm where every
|
|
1285
|
+
* shipped test still passes.
|
|
1286
|
+
*
|
|
1287
|
+
* So the block VERIFIES its own work and refuses to define the class when the
|
|
1288
|
+
* pin did not take. A silent failure becomes a loud one at import. The
|
|
1289
|
+
* residual is named rather than denied: an adversary who also answers the
|
|
1290
|
+
* verifying descriptor read defeats it, and that adversary already chose what
|
|
1291
|
+
* every capture in this package holds.
|
|
1292
|
+
*
|
|
1293
|
+
* @returns The total recognition guard `isContractError` publishes
|
|
1294
|
+
*
|
|
1295
|
+
* @example
|
|
1296
|
+
* ```ts
|
|
1297
|
+
* ContractError.guard()(new ContractError('x', { code: 'clone' })) // true
|
|
1298
|
+
* ```
|
|
1299
|
+
*/
|
|
1300
|
+
static guard(): Guard<ContractError>;
|
|
637
1301
|
}
|
|
638
1302
|
|
|
639
1303
|
/** Optional structured details carried by a {@link ContractError}. */
|
|
@@ -659,27 +1323,64 @@ export declare interface ContractErrorOptions {
|
|
|
659
1323
|
}
|
|
660
1324
|
|
|
661
1325
|
/**
|
|
662
|
-
* A compiled contract — the
|
|
1326
|
+
* A compiled contract — the six lockstep outputs derived from one shape.
|
|
663
1327
|
*
|
|
664
1328
|
* @remarks
|
|
665
|
-
* Built by `createContract`: `is` narrows, `
|
|
666
|
-
* value or `undefined`), `schema` is an
|
|
667
|
-
* Schema, `explain` reports the structured
|
|
668
|
-
* `generate` produces deterministic seed
|
|
669
|
-
* (defaulting to a wall-clock-seeded source
|
|
1329
|
+
* Built by `createContract`: `is` narrows, `audit` diagnoses strict rejection,
|
|
1330
|
+
* `parse` coerces (returning the typed value or `undefined`), `schema` is an
|
|
1331
|
+
* owned deeply frozen emitted JSON Schema, `explain` reports the structured
|
|
1332
|
+
* faults behind a failed `parse`, and `generate` produces deterministic seed
|
|
1333
|
+
* data from a {@link RandomFunction} (defaulting to a wall-clock-seeded source
|
|
1334
|
+
* when none is supplied).
|
|
1335
|
+
*
|
|
1336
|
+
* LOCKSTEP means derived from one owned snapshot of the shape, not agreeing on
|
|
1337
|
+
* which values to accept: `is` and `schema` describe the contract's canonical
|
|
1338
|
+
* domain, while `parse` is a map into that domain whose preimage is
|
|
1339
|
+
* deliberately larger (it coerces leaves and drops a closed object's undeclared
|
|
1340
|
+
* keys). `audit` diagnoses the domain; `explain` diagnoses the map.
|
|
1341
|
+
*
|
|
1342
|
+
* READ STABILITY is the precondition on both soundness laws — `audit` against
|
|
1343
|
+
* `is`, `explain` against `parse`. Each law relates two separate calls, and
|
|
1344
|
+
* every call reads the value it is handed, so both hold for a STABLE value: one
|
|
1345
|
+
* whose observable reads do not change between calls. A getter that answers a
|
|
1346
|
+
* declared string on its first read and a number on its second, or a `Proxy`
|
|
1347
|
+
* whose traps change behavior mid-flight, can leave `audit` empty and still
|
|
1348
|
+
* fail `is`; no law spanning two calls can promise otherwise, and no artifact
|
|
1349
|
+
* re-reads a value to close the gap. This is a statement of scope, not a hedge:
|
|
1350
|
+
* for primitives and data-only structures whose entire observable read surface
|
|
1351
|
+
* stays stable across both calls, both laws hold exactly as written.
|
|
1352
|
+
* `Object.freeze` alone does not establish that condition because it is shallow
|
|
1353
|
+
* and does not stabilize accessors.
|
|
670
1354
|
*/
|
|
671
1355
|
export declare interface ContractInterface<T> {
|
|
672
1356
|
readonly schema: JSONSchema;
|
|
673
1357
|
readonly is: Guard<T>;
|
|
674
1358
|
parse(value: unknown): T | undefined;
|
|
1359
|
+
/**
|
|
1360
|
+
* Report every strict fault a value has against this contract.
|
|
1361
|
+
*
|
|
1362
|
+
* @remarks
|
|
1363
|
+
* An empty report means the value is strictly valid. Soundness invariant:
|
|
1364
|
+
* `audit(v).length === 0` if and only if `is(v)`, for a value whose reads are
|
|
1365
|
+
* stable across calls (see the read-stability precondition on
|
|
1366
|
+
* {@link ContractInterface}). It is the report for the stricter of the two
|
|
1367
|
+
* domains, so a coercible leaf and a closed object's undeclared key both
|
|
1368
|
+
* fault here and neither faults in `explain`.
|
|
1369
|
+
*
|
|
1370
|
+
* @param value - The value to check
|
|
1371
|
+
* @returns The faults found, empty when the value is strictly valid
|
|
1372
|
+
*/
|
|
1373
|
+
audit(value: unknown): readonly AuditFault[];
|
|
675
1374
|
/**
|
|
676
1375
|
* Report every structured parse fault a value has against this contract.
|
|
677
1376
|
*
|
|
678
1377
|
* @remarks
|
|
679
1378
|
* An empty report means the value is valid. Soundness invariant:
|
|
680
|
-
* `explain(v).length === 0` if and only if `parse(v) !== undefined
|
|
681
|
-
*
|
|
682
|
-
*
|
|
1379
|
+
* `explain(v).length === 0` if and only if `parse(v) !== undefined`, for a
|
|
1380
|
+
* value whose reads are stable across calls (see the read-stability
|
|
1381
|
+
* precondition on {@link ContractInterface}) — explain mirrors `parse`'s
|
|
1382
|
+
* coercion, not the stricter `is`, and `audit` is the report that mirrors
|
|
1383
|
+
* `is`. Faults are listed in stable pre-order (declared key/index order).
|
|
683
1384
|
*
|
|
684
1385
|
* @param value - The value to check
|
|
685
1386
|
* @returns The faults found, empty when the value parses successfully
|
|
@@ -694,28 +1395,59 @@ export declare interface ContractInterface<T> {
|
|
|
694
1395
|
*
|
|
695
1396
|
* @remarks
|
|
696
1397
|
* A discriminated union keyed on `type`. Shapes nest (an `ArrayShape` holds an
|
|
697
|
-
* element shape, an `ObjectShape` a map of them). {@link
|
|
1398
|
+
* element shape, an `ObjectShape` a map of them). {@link validateShapeDepth}
|
|
698
1399
|
* enforces an acyclic graph within {@link COMPILE_DEPTH_LIMIT}.
|
|
699
1400
|
*/
|
|
700
1401
|
export declare type ContractShape = StringShape | NumberShape | BooleanShape | NullShape | LiteralShape | ArrayShape | ObjectShape | UnionShape | OptionalShape | NullableShape | JSONShape | RawShape;
|
|
701
1402
|
|
|
702
1403
|
/**
|
|
703
|
-
*
|
|
704
|
-
*
|
|
1404
|
+
* Build the length faults an array has against an {@link ArrayShape}.
|
|
1405
|
+
*
|
|
1406
|
+
* @remarks
|
|
1407
|
+
* Takes the LENGTH rather than the array, because both doors have already read
|
|
1408
|
+
* their entries through {@link readArrayEntries} and must report the length that
|
|
1409
|
+
* read observed rather than re-asking the caller's value for it. `received` is
|
|
1410
|
+
* that count rendered through the captured `String`, matching the other length
|
|
1411
|
+
* diagnostics in the package. Order is `min`, then `max`. It refuses an
|
|
1412
|
+
* unreadable shape through the same boundary and for the same reason
|
|
1413
|
+
* {@link createStringFaults} does.
|
|
1414
|
+
*
|
|
1415
|
+
* @param shape - The array shape whose bounds are checked
|
|
1416
|
+
* @param length - The entry count the door already observed
|
|
1417
|
+
* @param path - The path every produced fault is rooted at
|
|
1418
|
+
* @returns A fresh array of faults, empty when the length satisfies both bounds
|
|
1419
|
+
* @throws {ContractError} When the shape's bound fields cannot be read
|
|
1420
|
+
*
|
|
1421
|
+
* @example
|
|
1422
|
+
* ```ts
|
|
1423
|
+
* createArrayFaults({ type: 'array', items: { type: 'string' }, min: 2 }, 1, [])
|
|
1424
|
+
* // [{ reason: 'constraint', path: [], expected: 'array', constraint: 'min', limit: 2, received: '1' }]
|
|
1425
|
+
* ```
|
|
1426
|
+
*/
|
|
1427
|
+
export declare function createArrayFaults(shape: ArrayShape, length: number, path: readonly string[]): readonly Fault[];
|
|
1428
|
+
|
|
1429
|
+
/**
|
|
1430
|
+
* Compile a {@link ContractShape} into a {@link ContractInterface} — the six
|
|
1431
|
+
* lockstep outputs from one declaration, lockstep meaning derived from one
|
|
1432
|
+
* owned snapshot rather than accepting the same values.
|
|
705
1433
|
*
|
|
706
1434
|
* @remarks
|
|
707
|
-
*
|
|
708
|
-
*
|
|
709
|
-
*
|
|
710
|
-
*
|
|
711
|
-
*
|
|
712
|
-
*
|
|
713
|
-
*
|
|
714
|
-
*
|
|
715
|
-
*
|
|
1435
|
+
* Creates ONE {@link ContractCompiler} and returns that compiler's exact
|
|
1436
|
+
* `contract` bundle. One ownership population governs the whole contract: the
|
|
1437
|
+
* declaration is owned once through {@link ownShape}, that owned graph is
|
|
1438
|
+
* validated once, and the six artifacts are compiled from it. There is no
|
|
1439
|
+
* discarded pre-ownership pass over the caller's declaration and no second
|
|
1440
|
+
* snapshot — ownership already refuses a malformed structural slot, scalar
|
|
1441
|
+
* field, or bound rather than normalizing it, so a second walk of the caller's
|
|
1442
|
+
* live source only added a population that could disagree with the one the
|
|
1443
|
+
* artifacts actually use.
|
|
1444
|
+
* All six artifacts are precompiled, so `audit`, `explain` and `generate` no
|
|
1445
|
+
* longer re-walk and re-gate the declaration on every call the way they used
|
|
1446
|
+
* to; `contract.audit` and `compileAuditor` are the same compiled function
|
|
1447
|
+
* reached two ways.
|
|
716
1448
|
*
|
|
717
1449
|
* @param shape - The shape to compile
|
|
718
|
-
* @returns A contract bundling `schema` / `is` / `parse` / `explain` / `generate`
|
|
1450
|
+
* @returns A contract bundling `schema` / `is` / `parse` / `audit` / `explain` / `generate`
|
|
719
1451
|
*
|
|
720
1452
|
* @example
|
|
721
1453
|
* ```ts
|
|
@@ -729,6 +1461,72 @@ export declare function createContract<S extends ContractShape>(shape: S): Contr
|
|
|
729
1461
|
|
|
730
1462
|
export declare function createContract(shape: ContractShape): ContractInterface<unknown>;
|
|
731
1463
|
|
|
1464
|
+
/**
|
|
1465
|
+
* Build the refinement faults a number value has against a {@link NumberShape}.
|
|
1466
|
+
*
|
|
1467
|
+
* @remarks
|
|
1468
|
+
* The numeric sibling of {@link createStringFaults}, shared by the same two
|
|
1469
|
+
* doors for the same reason. `expected` is the shape's own kind — `'integer'`
|
|
1470
|
+
* when `integer: true`, otherwise `'number'` — so a caller reading the report
|
|
1471
|
+
* sees the declaration's vocabulary rather than the value's. Order is
|
|
1472
|
+
* `integer`, then `min`, then `max`. It refuses an unreadable shape through the
|
|
1473
|
+
* same boundary and for the same reason {@link createStringFaults} does.
|
|
1474
|
+
*
|
|
1475
|
+
* @param shape - The number shape whose refinements are checked
|
|
1476
|
+
* @param value - The already-obtained number to check
|
|
1477
|
+
* @param path - The path every produced fault is rooted at
|
|
1478
|
+
* @returns A fresh array of faults, empty when the value satisfies every refinement
|
|
1479
|
+
* @throws {ContractError} When the shape's refinement fields cannot be read
|
|
1480
|
+
*
|
|
1481
|
+
* @example
|
|
1482
|
+
* ```ts
|
|
1483
|
+
* createNumberFaults({ type: 'number', integer: true }, 1.5, [])
|
|
1484
|
+
* // [{ reason: 'constraint', path: [], expected: 'integer', constraint: 'integer', received: '1.5' }]
|
|
1485
|
+
* ```
|
|
1486
|
+
*/
|
|
1487
|
+
export declare function createNumberFaults(shape: NumberShape, value: number, path: readonly string[]): readonly Fault[];
|
|
1488
|
+
|
|
1489
|
+
/**
|
|
1490
|
+
* Build the refinement faults a string value has against a {@link StringShape}.
|
|
1491
|
+
*
|
|
1492
|
+
* @remarks
|
|
1493
|
+
* The single source of the string refinement report, shared by
|
|
1494
|
+
* `compileReporter` and `compileAuditor`. The two doors differ only in how they
|
|
1495
|
+
* OBTAIN the string — the reporter coerces through `parseString`, the auditor
|
|
1496
|
+
* demands a primitive string — and agreed on every constraint afterwards by
|
|
1497
|
+
* carrying two copies of the same twenty-one lines, which is one edit away from
|
|
1498
|
+
* two contracts. Faults come out in declaration order — `min`, then `max`, then
|
|
1499
|
+
* `pattern` — because a report is read top to bottom and its order is public.
|
|
1500
|
+
*
|
|
1501
|
+
* The pattern is applied through an OWNED stateless rebuild
|
|
1502
|
+
* ({@link readPattern}) and asked through {@link matchesPattern}, so a caller's
|
|
1503
|
+
* `lastIndex` never moves and no caller-writable member decides whether the
|
|
1504
|
+
* value matched.
|
|
1505
|
+
*
|
|
1506
|
+
* The whole body reads the caller's SHAPE, so it runs through the same
|
|
1507
|
+
* {@link readValue} boundary {@link shapeToKind} uses and refuses an
|
|
1508
|
+
* out-of-domain declaration with the same diagnostic. The compiled doors gate a
|
|
1509
|
+
* non-`RegExp` `pattern` and a non-finite bound long before this helper sees
|
|
1510
|
+
* them, so the package's own path never arrives here off-domain — but the door
|
|
1511
|
+
* is PUBLISHED, and a shape a `StringShape` annotation merely vouched for
|
|
1512
|
+
* (parsed out of a document, say) reaches it unchecked. Publishing the host's
|
|
1513
|
+
* own `TypeError` from such a shape would falsify the promise this module makes
|
|
1514
|
+
* for every one of its doors.
|
|
1515
|
+
*
|
|
1516
|
+
* @param shape - The string shape whose refinements are checked
|
|
1517
|
+
* @param value - The already-obtained string to check
|
|
1518
|
+
* @param path - The path every produced fault is rooted at
|
|
1519
|
+
* @returns A fresh array of faults, empty when the value satisfies every refinement
|
|
1520
|
+
* @throws {ContractError} When the shape's refinement fields cannot be read
|
|
1521
|
+
*
|
|
1522
|
+
* @example
|
|
1523
|
+
* ```ts
|
|
1524
|
+
* createStringFaults({ type: 'string', min: 3 }, 'ab', [])
|
|
1525
|
+
* // [{ reason: 'constraint', path: [], expected: 'string', constraint: 'min', limit: 3, received: '"ab"' }]
|
|
1526
|
+
* ```
|
|
1527
|
+
*/
|
|
1528
|
+
export declare function createStringFaults(shape: StringShape, value: string, path: readonly string[]): readonly Fault[];
|
|
1529
|
+
|
|
732
1530
|
/**
|
|
733
1531
|
* Derive `min`/`max` shape bounds from a pair of non-negative-integer JSON
|
|
734
1532
|
* Schema length keywords (`minLength`/`maxLength`, `minItems`/`maxItems`).
|
|
@@ -788,7 +1586,8 @@ export declare function deriveRangeBounds(min: unknown, max: unknown): {
|
|
|
788
1586
|
* @param random - The caller-supplied random source
|
|
789
1587
|
* @param shape - The shape category consuming the sample
|
|
790
1588
|
* @returns A finite sample in `[0, 1)`
|
|
791
|
-
* @throws {ContractError} When the source throws or returns outside `[0, 1)
|
|
1589
|
+
* @throws {ContractError} When the source throws or returns outside `[0, 1)`;
|
|
1590
|
+
* a thrown value is retained exactly as the cause
|
|
792
1591
|
*
|
|
793
1592
|
* @example
|
|
794
1593
|
* ```ts
|
|
@@ -797,6 +1596,29 @@ export declare function deriveRangeBounds(min: unknown, max: unknown): {
|
|
|
797
1596
|
*/
|
|
798
1597
|
export declare function drawRandom(random: RandomFunction, shape: string): number;
|
|
799
1598
|
|
|
1599
|
+
/**
|
|
1600
|
+
* Encode one non-container value the way JSON encodes it, or `undefined` when
|
|
1601
|
+
* JSON cannot encode it at all.
|
|
1602
|
+
*
|
|
1603
|
+
* @remarks
|
|
1604
|
+
* The leaf half of {@link canonicalizeValue}: `JSON.stringify` returns
|
|
1605
|
+
* `undefined` (never a string) for `undefined`, a function, and a symbol —
|
|
1606
|
+
* exactly the values with no JSON encoding — and THROWS on a bigint, which is
|
|
1607
|
+
* refused before the call rather than through it. A `Date` and any other
|
|
1608
|
+
* non-record object encode through the same captured `JSON.stringify`, so a
|
|
1609
|
+
* `toJSON` member keeps its ordinary meaning.
|
|
1610
|
+
*
|
|
1611
|
+
* @param value - The non-container value to encode
|
|
1612
|
+
* @returns The JSON encoding, or `undefined` when JSON cannot encode `value`
|
|
1613
|
+
*
|
|
1614
|
+
* @example
|
|
1615
|
+
* ```ts
|
|
1616
|
+
* encodeLeaf(Number.NaN) // 'null'
|
|
1617
|
+
* encodeLeaf(undefined) // undefined
|
|
1618
|
+
* ```
|
|
1619
|
+
*/
|
|
1620
|
+
export declare function encodeLeaf(value: unknown): string | undefined;
|
|
1621
|
+
|
|
800
1622
|
/**
|
|
801
1623
|
* Snapshot an object's own enumerable string keys through a total boundary.
|
|
802
1624
|
*
|
|
@@ -822,9 +1644,13 @@ export declare function enumerableKeys(value: object): readonly string[] | undef
|
|
|
822
1644
|
*
|
|
823
1645
|
* @remarks
|
|
824
1646
|
* String keys are ignored — only `Object.getOwnPropertySymbols` entries whose
|
|
825
|
-
* descriptor is `enumerable` are counted
|
|
826
|
-
*
|
|
827
|
-
*
|
|
1647
|
+
* descriptor is `enumerable` are counted, and each descriptor is read through
|
|
1648
|
+
* the captured observation so no accessor runs. It answers exactly the
|
|
1649
|
+
* `JSON.stringify`-invisible half of a record's own-symbol population, for a
|
|
1650
|
+
* consumer that needs that count directly. It does NOT back `isEmptyObject` /
|
|
1651
|
+
* `isNonEmptyObject` — those ask the complete own-key question `recordOf` asks,
|
|
1652
|
+
* because an enumerable-only count made their `never` narrowing unsound for an
|
|
1653
|
+
* own non-enumerable key.
|
|
828
1654
|
*
|
|
829
1655
|
* @param value - The object to inspect
|
|
830
1656
|
* @returns The number of enumerable own-symbol keys
|
|
@@ -841,6 +1667,10 @@ export declare function enumerableSymbolCount(value: object): number;
|
|
|
841
1667
|
/**
|
|
842
1668
|
* Build a guard from a native `enum` or any object whose values are strings or
|
|
843
1669
|
* numbers.
|
|
1670
|
+
|
|
1671
|
+
* @param enumeration - The readable enumeration whose values the guard accepts
|
|
1672
|
+
* @returns A guard accepting one enumeration value
|
|
1673
|
+
* @throws {ContractError} When the enumeration cannot be read
|
|
844
1674
|
*
|
|
845
1675
|
* @example
|
|
846
1676
|
* ```ts
|
|
@@ -852,6 +1682,12 @@ export declare function enumerableSymbolCount(value: object): number;
|
|
|
852
1682
|
*/
|
|
853
1683
|
export declare function enumOf<const E extends Record<string, string | number>>(enumeration: E): Guard<E[keyof E]>;
|
|
854
1684
|
|
|
1685
|
+
/** A key present on a value that its closed object shape does not declare. */
|
|
1686
|
+
export declare interface ExtraFault {
|
|
1687
|
+
readonly reason: 'extra';
|
|
1688
|
+
readonly path: FieldPath;
|
|
1689
|
+
}
|
|
1690
|
+
|
|
855
1691
|
/**
|
|
856
1692
|
* Discriminated failure branch of a {@link Result}.
|
|
857
1693
|
*
|
|
@@ -906,14 +1742,17 @@ export declare type Fault = {
|
|
|
906
1742
|
};
|
|
907
1743
|
|
|
908
1744
|
/**
|
|
909
|
-
* The maximum number of {@link Fault} entries a single
|
|
910
|
-
* returns, frozen.
|
|
1745
|
+
* The maximum number of {@link Fault} / {@link AuditFault} entries a single
|
|
1746
|
+
* `explain` or `audit` report ever returns, frozen.
|
|
911
1747
|
*
|
|
912
1748
|
* @remarks
|
|
913
|
-
* Bounds
|
|
914
|
-
* `compileReporter`
|
|
915
|
-
*
|
|
916
|
-
*
|
|
1749
|
+
* Bounds BOTH reports against adversarial input (a giant array, a wide record)
|
|
1750
|
+
* — `compileReporter` and `compileAuditor` each collect faults in stable
|
|
1751
|
+
* pre-order and stop once this cap is reached, and every recursive call slices
|
|
1752
|
+
* to it, so the report size (and the work to build it) stays finite and
|
|
1753
|
+
* deterministic at every nesting level regardless of the input's size. Size a
|
|
1754
|
+
* diagnostic surface off this constant and it bounds `audit` exactly as it
|
|
1755
|
+
* bounds `explain`.
|
|
917
1756
|
*/
|
|
918
1757
|
export declare const FAULT_LIMIT = 64;
|
|
919
1758
|
|
|
@@ -982,13 +1821,15 @@ export declare const GENERATION_ATTEMPT_LIMIT = 32;
|
|
|
982
1821
|
export declare type Guard<T> = (value: unknown) => value is T;
|
|
983
1822
|
|
|
984
1823
|
/**
|
|
985
|
-
* The maximum
|
|
1824
|
+
* The maximum active recursion or JSON container depth for runtime guards,
|
|
986
1825
|
* frozen.
|
|
987
1826
|
*
|
|
988
1827
|
* @remarks
|
|
989
1828
|
* Bounds explicitly recursive guards before the JavaScript call stack becomes
|
|
990
|
-
* the limiting mechanism.
|
|
991
|
-
*
|
|
1829
|
+
* the limiting mechanism. It also caps array/plain-record containers on each
|
|
1830
|
+
* active path inspected by {@link matchesJSONDepth}: noncontainers are depth
|
|
1831
|
+
* zero, an empty container is depth one, 512 containers pass, and the 513th
|
|
1832
|
+
* fails. Active cycle edges do not add a level.
|
|
992
1833
|
*
|
|
993
1834
|
* @example
|
|
994
1835
|
* ```ts
|
|
@@ -1108,31 +1949,34 @@ export declare const INFER_ENUM_LIMIT = 12;
|
|
|
1108
1949
|
* depth) and unified with {@link unifySchemas}: a single distinct element
|
|
1109
1950
|
* schema becomes `items` directly; multiple distinct schemas become
|
|
1110
1951
|
* `items: { anyOf: [...] }`. Depth exhaustion or a cyclic re-encounter of
|
|
1111
|
-
* `value` both yield the empty schema `{}` instead of descending. A
|
|
1112
|
-
* array (holes, e.g. `[1, , 3]`)
|
|
1113
|
-
*
|
|
1114
|
-
* `
|
|
1115
|
-
*
|
|
1952
|
+
* `value` both yield the empty schema `{}` instead of descending. A SPARSE
|
|
1953
|
+
* array (holes, e.g. `[1, , 3]`) has no JSON expression and widens to the
|
|
1954
|
+
* accept-anything `{}`, the same treatment `NaN`, a function, a symbol, a
|
|
1955
|
+
* `Map` and a `Set` receive — it is not read as a list of present `undefined`
|
|
1956
|
+
* leaves, because the array schema that reading produced was rejected by its
|
|
1957
|
+
* own compiled guard, which is the one direction the round-trip law forbids.
|
|
1958
|
+
* Invalid direct depth and breadth budgets use {@link sanitizeBudget} with the
|
|
1959
|
+
* package defaults, matching the higher-level inference boundaries.
|
|
1116
1960
|
*
|
|
1117
1961
|
* ALL reads of `value` — including its `length` — happen inside
|
|
1118
|
-
* {@link attempt}: a hostile `length` getter
|
|
1119
|
-
*
|
|
1120
|
-
*
|
|
1121
|
-
*
|
|
1122
|
-
*
|
|
1123
|
-
*
|
|
1124
|
-
*
|
|
1125
|
-
* of recomputing (guards a shared-reference DAG against exponential blowup).
|
|
1962
|
+
* {@link attempt}, then cross {@link readValue}: a hostile `length` getter,
|
|
1963
|
+
* throwing own-getter element, or hostile element access raises the shared
|
|
1964
|
+
* coded refusal instead of returning the empty-array schema. A genuinely
|
|
1965
|
+
* empty sampled/classified list still returns `{ type: 'array' }` with no
|
|
1966
|
+
* `items`. A same-object re-inference at the same remaining `depth` is served
|
|
1967
|
+
* from `memo` instead of recomputing (guards a shared-reference DAG against
|
|
1968
|
+
* exponential blowup).
|
|
1126
1969
|
*
|
|
1127
1970
|
* @param value - The array to infer from
|
|
1128
|
-
* @param depth - Remaining descent budget
|
|
1129
|
-
* @param breadth -
|
|
1971
|
+
* @param depth - Remaining descent budget; invalid values use the package default
|
|
1972
|
+
* @param breadth - Maximum elements sampled; invalid values use the package default
|
|
1130
1973
|
* @param closed - Threaded through to nested object elements
|
|
1131
1974
|
* @param format - Threaded through to nested string/`Date` elements
|
|
1132
1975
|
* @param visited - The ancestor set guarding against cycles
|
|
1133
1976
|
* @param memo - A per-call `(object, remaining depth) → schema` cache guarding
|
|
1134
1977
|
* against exponential re-inference of a shared-reference DAG
|
|
1135
1978
|
* @returns The inferred array schema
|
|
1979
|
+
* @throws {ContractError} When the array cannot be read
|
|
1136
1980
|
*
|
|
1137
1981
|
* @example
|
|
1138
1982
|
* ```ts
|
|
@@ -1208,8 +2052,9 @@ export declare type InferObject<P extends Readonly<Record<string, ContractShape>
|
|
|
1208
2052
|
* Own enumerable string keys via {@link enumerableKeys}, sorted
|
|
1209
2053
|
* lexicographically for deterministic output, capped at `breadth`. This is the
|
|
1210
2054
|
* same property view compiled object guards, parsers, and reporters use. Each
|
|
1211
|
-
* property value is read through {@link attempt}
|
|
1212
|
-
*
|
|
2055
|
+
* property value is read through {@link attempt} and {@link readValue}; a
|
|
2056
|
+
* hostile getter raises the shared coded refusal. A readable property whose
|
|
2057
|
+
* value is `undefined` is DROPPED
|
|
1213
2058
|
* — JSON encodes no such property (`JSON.stringify({ a: undefined })` is
|
|
1214
2059
|
* `'{}'`), so it contributes neither a `properties` entry nor a `required`
|
|
1215
2060
|
* entry. Every other present key is required (single-value mode).
|
|
@@ -1237,6 +2082,7 @@ export declare type InferObject<P extends Readonly<Record<string, ContractShape>
|
|
|
1237
2082
|
* @param memo - A per-call `(object, remaining depth) → schema` cache guarding
|
|
1238
2083
|
* against exponential re-inference of a shared-reference DAG
|
|
1239
2084
|
* @returns The inferred object schema
|
|
2085
|
+
* @throws {ContractError} When the record cannot be read
|
|
1240
2086
|
*
|
|
1241
2087
|
* @example
|
|
1242
2088
|
* ```ts
|
|
@@ -1326,20 +2172,19 @@ export declare function inferPrimitiveEnum(values: readonly unknown[], limit: nu
|
|
|
1326
2172
|
* {@link inferArray}, this path carries no `visited` `WeakSet` — a value
|
|
1327
2173
|
* shared by reference across multiple sample rows is legitimate (not a
|
|
1328
2174
|
* cycle back to an ancestor), so termination on cyclic row data relies on
|
|
1329
|
-
* the decrementing `depth` budget
|
|
1330
|
-
*
|
|
1331
|
-
* `additionalProperties` is forced to `true` regardless of `closed`
|
|
1332
|
-
*
|
|
1333
|
-
*
|
|
1334
|
-
*
|
|
1335
|
-
*
|
|
1336
|
-
*
|
|
1337
|
-
*
|
|
1338
|
-
*
|
|
1339
|
-
*
|
|
1340
|
-
*
|
|
1341
|
-
*
|
|
1342
|
-
* widens to `{}`.
|
|
2175
|
+
* the decrementing `depth` budget and the shared {@link SampleMemo}.
|
|
2176
|
+
*
|
|
2177
|
+
* `additionalProperties` is forced to `true` regardless of `closed` when the
|
|
2178
|
+
* key union exceeds `breadth`, or a readable row carries a key as an own
|
|
2179
|
+
* property holding `undefined`. A hostile getter or failed KEY walk throws the
|
|
2180
|
+
* shared coded refusal instead of dropping a key or widening the whole slot.
|
|
2181
|
+
*
|
|
2182
|
+
* The memo is keyed by the slot's ORDERED row identities, not by a single row.
|
|
2183
|
+
* Keying only the one-row slot left every MULTI-row slot — the shape this door
|
|
2184
|
+
* exists for — re-inferring a shared child once per path: two rows sharing one
|
|
2185
|
+
* `{ a: child, b: child }` detail cost `2^depth` inferences, the identical
|
|
2186
|
+
* denial of service the one-row memo was added to remove, through the same
|
|
2187
|
+
* public door.
|
|
1343
2188
|
*
|
|
1344
2189
|
* @param samples - The plain-record samples
|
|
1345
2190
|
* @param depth - Remaining descent budget
|
|
@@ -1347,16 +2192,19 @@ export declare function inferPrimitiveEnum(values: readonly unknown[], limit: nu
|
|
|
1347
2192
|
* @param closed - Whether the emitted schema closes to unknown keys
|
|
1348
2193
|
* @param format - Whether a unanimous string column gains a `format` keyword
|
|
1349
2194
|
* @param enumOn - Whether a low-cardinality column may emit `enum`
|
|
2195
|
+
* @param memo - The walk's {@link SampleMemo}, shared with
|
|
2196
|
+
* {@link inferSamples}; build one with {@link buildSampleMemo}
|
|
1350
2197
|
* @returns The inferred object schema
|
|
2198
|
+
* @throws {ContractError} When a sample row or the memo cannot be read
|
|
1351
2199
|
*
|
|
1352
2200
|
* @example
|
|
1353
2201
|
* ```ts
|
|
1354
|
-
* inferRecordSamples([{ id: 1 }, { id: 2, name: 'Ada' }], 32, 256, true, false, false)
|
|
2202
|
+
* inferRecordSamples([{ id: 1 }, { id: 2, name: 'Ada' }], 32, 256, true, false, false, buildSampleMemo())
|
|
1355
2203
|
* // { type: 'object', properties: { id: { type: 'integer' }, name: { type: 'string' } },
|
|
1356
2204
|
* // required: ['id'], additionalProperties: false }
|
|
1357
2205
|
* ```
|
|
1358
2206
|
*/
|
|
1359
|
-
export declare function inferRecordSamples(samples: readonly Record<string, unknown>[], depth: number, breadth: number, closed: boolean, format: boolean, enumOn: boolean): JSONSchema;
|
|
2207
|
+
export declare function inferRecordSamples(samples: readonly Record<string, unknown>[], depth: number, breadth: number, closed: boolean, format: boolean, enumOn: boolean, memo: SampleMemo): JSONSchema;
|
|
1360
2208
|
|
|
1361
2209
|
/**
|
|
1362
2210
|
* Infer a `JSONSchema` for a collected slot of sample values — the shared
|
|
@@ -1381,15 +2229,20 @@ export declare function inferRecordSamples(samples: readonly Record<string, unkn
|
|
|
1381
2229
|
* @param closed - Whether nested objects close to unknown keys
|
|
1382
2230
|
* @param format - Whether a unanimous string slot gains a `format` keyword
|
|
1383
2231
|
* @param enumOn - Whether low-cardinality primitive slots may emit `enum`
|
|
2232
|
+
* @param memo - The walk's {@link SampleMemo}, shared with
|
|
2233
|
+
* {@link inferRecordSamples} so a row list reached through
|
|
2234
|
+
* several slots is inferred once; build one with
|
|
2235
|
+
* {@link buildSampleMemo}
|
|
1384
2236
|
* @returns The inferred schema for the slot
|
|
2237
|
+
* @throws {ContractError} When the samples or the memo cannot be read
|
|
1385
2238
|
*
|
|
1386
2239
|
* @example
|
|
1387
2240
|
* ```ts
|
|
1388
|
-
* inferSamples(['2024-01-01', '2024-02-02'], 32, 256, true, true, false)
|
|
2241
|
+
* inferSamples(['2024-01-01', '2024-02-02'], 32, 256, true, true, false, buildSampleMemo())
|
|
1389
2242
|
* // { type: 'string', format: 'date' }
|
|
1390
2243
|
* ```
|
|
1391
2244
|
*/
|
|
1392
|
-
export declare function inferSamples(samples: readonly unknown[], depth: number, breadth: number, closed: boolean, format: boolean, enumOn: boolean): JSONSchema;
|
|
2245
|
+
export declare function inferSamples(samples: readonly unknown[], depth: number, breadth: number, closed: boolean, format: boolean, enumOn: boolean, memo: SampleMemo): JSONSchema;
|
|
1393
2246
|
|
|
1394
2247
|
/** {@link Infer} of a union shape's `variants` — the union of each variant's inferred type. */
|
|
1395
2248
|
export declare type InferUnion<V extends readonly ContractShape[]> = V extends readonly (infer U)[] ? U extends ContractShape ? Infer<U> : never : never;
|
|
@@ -1400,7 +2253,8 @@ export declare type InferUnion<V extends readonly ContractShape[]> = V extends r
|
|
|
1400
2253
|
* {@link inferArray} / {@link inferObject}.
|
|
1401
2254
|
*
|
|
1402
2255
|
* @remarks
|
|
1403
|
-
*
|
|
2256
|
+
* Terminates on cyclic readable input via `visited`; failed traversal is
|
|
2257
|
+
* refused by the containing public reader. Leaf
|
|
1404
2258
|
* classification order: `null`, boolean, integer (`Number.isInteger`
|
|
1405
2259
|
* semantics — `-0` counts), finite non-integer number, string (gaining a
|
|
1406
2260
|
* `format` keyword when `format` is on and {@link stringToFormat} matches),
|
|
@@ -1427,6 +2281,7 @@ export declare type InferUnion<V extends readonly ContractShape[]> = V extends r
|
|
|
1427
2281
|
* @param memo - A per-call `(object, remaining depth) → schema` cache guarding
|
|
1428
2282
|
* against exponential re-inference of a shared-reference DAG
|
|
1429
2283
|
* @returns The inferred schema fragment for `value`
|
|
2284
|
+
* @throws {ContractError} When a traversed container cannot be read
|
|
1430
2285
|
*
|
|
1431
2286
|
* @example
|
|
1432
2287
|
* ```ts
|
|
@@ -1480,7 +2335,230 @@ export declare function intersectionOf<const Gs extends ReadonlyArray<Guard<unkn
|
|
|
1480
2335
|
|
|
1481
2336
|
export declare function intersectionOf(...predicates: ReadonlyArray<(value: unknown) => boolean>): Guard<unknown>;
|
|
1482
2337
|
|
|
1483
|
-
/**
|
|
2338
|
+
/**
|
|
2339
|
+
* Every host operation this package dispatches through, captured while this
|
|
2340
|
+
* module evaluates.
|
|
2341
|
+
*
|
|
2342
|
+
* @remarks
|
|
2343
|
+
* THE answer to a defect class that mutated four times before anyone stated it
|
|
2344
|
+
* as a class. A caller can replace a global constructor, a static, a prototype
|
|
2345
|
+
* member, or a symbol-keyed hook, and each replacement can fail in two ways: it
|
|
2346
|
+
* can THROW, which a boundary contains, or it can LIE, which no boundary can
|
|
2347
|
+
* see. `Object.freeze = (value) => value` is the second kind and it is the
|
|
2348
|
+
* worse one: every cloner succeeds, publishes a mutable graph, and the caller
|
|
2349
|
+
* cannot tell that the package's central guarantee evaporated.
|
|
2350
|
+
*
|
|
2351
|
+
* Containment cannot close that, because there is nothing to contain — only
|
|
2352
|
+
* capture can, and only capture taken while this module evaluates. A module's
|
|
2353
|
+
* initializers run at import, so a reference read here is whatever was
|
|
2354
|
+
* installed at the moment THIS module evaluated, and reading it later, at the
|
|
2355
|
+
* call site, is reading whatever the caller most recently installed. "Before any
|
|
2356
|
+
* caller code runs" is the tempting phrasing and it is false in exactly the case
|
|
2357
|
+
* the limit below names, so it is not used.
|
|
2358
|
+
*
|
|
2359
|
+
* The limit that follows, stated as a limit rather than as a guarantee: capture
|
|
2360
|
+
* is only as early as this package's own evaluation. A consumer module that
|
|
2361
|
+
* evaluates BEFORE `@orkestrel/contract` — ESM evaluates imports in source
|
|
2362
|
+
* order — chooses what this table captures, and no mechanism inside the package
|
|
2363
|
+
* can reach code that ran before the package existed. That precondition is
|
|
2364
|
+
* outside this package's control, and an adversary who holds it can replace the
|
|
2365
|
+
* package wholesale rather than bother with the table, so it is named here
|
|
2366
|
+
* instead of defended.
|
|
2367
|
+
*
|
|
2368
|
+
* Membership rule, stated so a reviewer can apply it and a new call site knows
|
|
2369
|
+
* where to go: **every host operation this package dispatches by name whose
|
|
2370
|
+
* result a published answer depends on.** That admits statics, constructors and
|
|
2371
|
+
* namespaces, and — this is the part the earlier wording got wrong by writing
|
|
2372
|
+
* the rule from the rows instead of the rows from the rule — it admits a
|
|
2373
|
+
* PROTOTYPE member on the same terms, including an ACCESSOR's getter,
|
|
2374
|
+
* dispatched onto the package's own receiver through {@link INTRINSICS.apply}.
|
|
2375
|
+
* A round that read `Object.getOwnPropertyDescriptor(RegExp.prototype, 'source')`
|
|
2376
|
+
* per call had captured nothing: capture is decided by WHEN the reference is
|
|
2377
|
+
* taken, not by which reflective spelling takes it.
|
|
2378
|
+
*
|
|
2379
|
+
* Collection membership was previously excluded on the grounds that it needs a
|
|
2380
|
+
* data structure rather than one operation, and the exclusion was answered with
|
|
2381
|
+
* an exported class whose `has` method every consumer could rewrite — which
|
|
2382
|
+
* reproduced the whole defect one prototype higher. The rule has no exception
|
|
2383
|
+
* for it: `Set.prototype.has` / `.add` / `.forEach`,
|
|
2384
|
+
* `Map.prototype.has` / `.forEach`, and
|
|
2385
|
+
* `WeakSet.prototype.has` / `.add` / `.delete` are ordinary rows here, dispatched
|
|
2386
|
+
* onto collections this package built and no caller holds, and every membership
|
|
2387
|
+
* and visitation answer in the package is asked through the module-scope
|
|
2388
|
+
* functions {@link matchesMember} / {@link admitMember} /
|
|
2389
|
+
* {@link matchesVisited} / {@link admitVisited} / {@link omitVisited}. A module
|
|
2390
|
+
* binding is not a property, so there is no member on that path to replace.
|
|
2391
|
+
*
|
|
2392
|
+
* The walk-collection exclusion the earlier wording carried — "a redirect
|
|
2393
|
+
* corrupts it inside a boundary and the door refuses, which is loud" — was
|
|
2394
|
+
* false in the direction the corpus itself installs. `WeakSet.prototype.has`
|
|
2395
|
+
* answering `false` does not make a cyclic clone refuse; it removes the
|
|
2396
|
+
* termination bound, and a door that never returns is the one failure no
|
|
2397
|
+
* boundary can report. Visitation state is captured here for that reason.
|
|
2398
|
+
*
|
|
2399
|
+
* @example
|
|
2400
|
+
* ```ts
|
|
2401
|
+
* INTRINSICS.freeze(snapshot) // the genuine Object.freeze, whatever the caller installed
|
|
2402
|
+
* ```
|
|
2403
|
+
*/
|
|
2404
|
+
export declare const INTRINSICS: Readonly<{
|
|
2405
|
+
/** `Object.freeze` — the operation the ownership guarantee is made of. */
|
|
2406
|
+
freeze: {
|
|
2407
|
+
<T extends Function>(f: T): T;
|
|
2408
|
+
<T extends {
|
|
2409
|
+
[idx: string]: U | null | undefined | object;
|
|
2410
|
+
}, U extends string | bigint | number | boolean | symbol>(o: T): Readonly<T>;
|
|
2411
|
+
<T>(o: T): Readonly<T>;
|
|
2412
|
+
};
|
|
2413
|
+
/** `Object.isFrozen` — the independent check that the guarantee actually held. */
|
|
2414
|
+
frozen: (o: any) => boolean;
|
|
2415
|
+
/** `Object.keys` — the own enumerable string-key population of a snapshot. */
|
|
2416
|
+
keys: {
|
|
2417
|
+
(o: object): string[];
|
|
2418
|
+
(o: {}): string[];
|
|
2419
|
+
};
|
|
2420
|
+
/** `Object.values` — the own enumerable value population of a snapshot. */
|
|
2421
|
+
values: {
|
|
2422
|
+
<T>(o: {
|
|
2423
|
+
[s: string]: T;
|
|
2424
|
+
} | ArrayLike<T>): T[];
|
|
2425
|
+
(o: {}): any[];
|
|
2426
|
+
};
|
|
2427
|
+
/** `Object.hasOwn` — own presence, so no read leaves a container for its prototype. */
|
|
2428
|
+
own: (o: object, v: PropertyKey) => boolean;
|
|
2429
|
+
/** `Object.is` — `SameValue`, so a `NaN` or signed-zero comparison stays exact. */
|
|
2430
|
+
same: (value1: any, value2: any) => boolean;
|
|
2431
|
+
/** `Object.create` — the null-prototype and prototype-pinned accumulators. */
|
|
2432
|
+
create: {
|
|
2433
|
+
(o: object | null): any;
|
|
2434
|
+
(o: object | null, properties: PropertyDescriptorMap & ThisType<any>): any;
|
|
2435
|
+
};
|
|
2436
|
+
/** `Object.getOwnPropertyDescriptor` — value observation that runs no accessor. */
|
|
2437
|
+
describe: (o: any, p: PropertyKey) => PropertyDescriptor | undefined;
|
|
2438
|
+
/** `Object.defineProperty` — exact placement of an own data property. */
|
|
2439
|
+
define: <T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => T;
|
|
2440
|
+
/** `Object.getPrototypeOf` — the record-brand observation. */
|
|
2441
|
+
prototype: (o: any) => any;
|
|
2442
|
+
/** `Object.getOwnPropertySymbols` — the own-symbol population. */
|
|
2443
|
+
symbols: (o: any) => symbol[];
|
|
2444
|
+
/** `Object.prototype` — the realm-local plain-record prototype identity. */
|
|
2445
|
+
base: Object;
|
|
2446
|
+
/** `Reflect.get` — a proxy-visible read that reports the trap's exact answer. */
|
|
2447
|
+
read: typeof Reflect.get;
|
|
2448
|
+
/** `Reflect.set` — a proxy-visible write. */
|
|
2449
|
+
write: typeof Reflect.set;
|
|
2450
|
+
/** `Reflect.ownKeys` — the complete own-key population, strings and symbols. */
|
|
2451
|
+
members: typeof Reflect.ownKeys;
|
|
2452
|
+
/** `Reflect.has` — a proxy-visible presence observation. */
|
|
2453
|
+
present: typeof Reflect.has;
|
|
2454
|
+
/** `Reflect.getOwnPropertyDescriptor` — the reflective descriptor observation. */
|
|
2455
|
+
reveal: typeof Reflect.getOwnPropertyDescriptor;
|
|
2456
|
+
/** `Reflect.defineProperty` — placement that answers instead of throwing. */
|
|
2457
|
+
declare: typeof Reflect.defineProperty;
|
|
2458
|
+
/** `Reflect.getPrototypeOf` — the reflective prototype observation. */
|
|
2459
|
+
parent: typeof Reflect.getPrototypeOf;
|
|
2460
|
+
/** `Reflect.apply` — dispatch of a captured method onto its receiver. */
|
|
2461
|
+
apply: typeof Reflect.apply;
|
|
2462
|
+
/** `Reflect.construct` — construction with an explicit new target. */
|
|
2463
|
+
construct: typeof Reflect.construct;
|
|
2464
|
+
/** `Number.isFinite` — the finite-bound test every numeric shape refuses on. */
|
|
2465
|
+
finite: (number: unknown) => boolean;
|
|
2466
|
+
/** `Number.isInteger` — the integer-budget test the inference caps refuse on. */
|
|
2467
|
+
integer: (number: unknown) => boolean;
|
|
2468
|
+
/** `Number.isSafeInteger` — the safe-integer test every length bound refuses on. */
|
|
2469
|
+
safe: (number: unknown) => boolean;
|
|
2470
|
+
/** `Number.isNaN` — the calendar-validity test for a parsed instant. */
|
|
2471
|
+
nan: (number: unknown) => boolean;
|
|
2472
|
+
/** `Array.isArray` — array identity across realms. */
|
|
2473
|
+
array: (arg: any) => arg is any[];
|
|
2474
|
+
/** `JSON.stringify` — the escaping used by previews and canonical text. */
|
|
2475
|
+
stringify: {
|
|
2476
|
+
(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
|
|
2477
|
+
(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
|
|
2478
|
+
};
|
|
2479
|
+
/** `JSON.parse` — document decoding. */
|
|
2480
|
+
decode: (text: string, reviver?: (this: any, key: string, value: any) => any) => any;
|
|
2481
|
+
/** `Math.floor` — index and quantity flooring. */
|
|
2482
|
+
floor: (x: number) => number;
|
|
2483
|
+
/** `Math.ceil` — index and quantity ceiling. */
|
|
2484
|
+
ceil: (x: number) => number;
|
|
2485
|
+
/** `Math.max` — bound selection. */
|
|
2486
|
+
max: (...values: number[]) => number;
|
|
2487
|
+
/** `Math.min` — bound selection. */
|
|
2488
|
+
min: (...values: number[]) => number;
|
|
2489
|
+
/** `Math.imul` — the seeded generator's mixing step. */
|
|
2490
|
+
imul: (x: number, y: number) => number;
|
|
2491
|
+
/** `String` — primitive text coercion. */
|
|
2492
|
+
text: StringConstructor;
|
|
2493
|
+
/** `Number` — primitive numeric coercion. */
|
|
2494
|
+
numeric: NumberConstructor;
|
|
2495
|
+
/** `RegExp` — pattern construction from captured source and flags. */
|
|
2496
|
+
pattern: RegExpConstructor;
|
|
2497
|
+
/**
|
|
2498
|
+
* `RegExp.prototype.exec` — THE pattern membership answer, dispatched through
|
|
2499
|
+
* `apply`.
|
|
2500
|
+
*
|
|
2501
|
+
* @remarks
|
|
2502
|
+
* `test` is deliberately absent. `RegExp.prototype.test` is spec-defined in
|
|
2503
|
+
* terms of `RegExpExec`, which re-reads `exec` OFF THE RECEIVER and calls it
|
|
2504
|
+
* when it is callable, so capturing `test` and dispatching it still asks
|
|
2505
|
+
* whatever the caller installed on `RegExp.prototype.exec`. Only
|
|
2506
|
+
* `RegExp.prototype.exec` itself is `RegExpBuiltinExec`, which reads the
|
|
2507
|
+
* pattern's internal slots and no member at all. A capture that still routes
|
|
2508
|
+
* through the replaced member is not a capture.
|
|
2509
|
+
*/
|
|
2510
|
+
captures: (string: string) => RegExpExecArray | null;
|
|
2511
|
+
/** The `RegExp.prototype.source` getter — the pattern text a published schema embeds, dispatched through `apply`. */
|
|
2512
|
+
expression: (() => any) | undefined;
|
|
2513
|
+
/** The `RegExp.prototype.flags` getter — the flag text an owned pattern is rebuilt from, dispatched through `apply`. */
|
|
2514
|
+
modifiers: (() => any) | undefined;
|
|
2515
|
+
/** `Array` — array construction. */
|
|
2516
|
+
list: ArrayConstructor;
|
|
2517
|
+
/** `Array.prototype.sort` — the deterministic ordering every published schema is emitted in, dispatched through `apply`. */
|
|
2518
|
+
order: (compareFn?: ((a: any, b: any) => number) | undefined) => any[];
|
|
2519
|
+
/** `Map` — keyed working state. */
|
|
2520
|
+
map: MapConstructor;
|
|
2521
|
+
/** `Map.prototype.get` — a memo read whose answer a published graph embeds, dispatched through `apply`. */
|
|
2522
|
+
fetch: (key: any) => any;
|
|
2523
|
+
/** `Map.prototype.set` — a memo write a published graph is later assembled from, dispatched through `apply`. */
|
|
2524
|
+
store: (key: any, value: any) => Map<any, any>;
|
|
2525
|
+
/** `Map.prototype.has` — a memo presence answer that decides whether a node is captured, dispatched through `apply`. */
|
|
2526
|
+
keyed: (key: any) => boolean;
|
|
2527
|
+
/** `Map.prototype.forEach` — the only full view of a caller's `Map` that runs no iterator, dispatched through `apply`. */
|
|
2528
|
+
pairs: (callbackfn: (value: any, key: any, map: Map<any, any>) => void, thisArg?: any) => void;
|
|
2529
|
+
/** `Set` — membership working state. */
|
|
2530
|
+
set: SetConstructor;
|
|
2531
|
+
/** `Set.prototype.has` — THE membership answer every published verdict rests on, dispatched through `apply`. */
|
|
2532
|
+
member: (value: any) => boolean;
|
|
2533
|
+
/** `Set.prototype.add` — collection of one more member, dispatched through `apply`. */
|
|
2534
|
+
admit: (value: any) => Set<any>;
|
|
2535
|
+
/** `Set.prototype.forEach` — the only full view of a caller's `Set` that runs no iterator, dispatched through `apply`. */
|
|
2536
|
+
sweep: (callbackfn: (value: any, value2: any, set: Set<any>) => void, thisArg?: any) => void;
|
|
2537
|
+
/** `WeakMap` — object-keyed working state. */
|
|
2538
|
+
weakMap: WeakMapConstructor;
|
|
2539
|
+
/** `WeakMap.prototype.get` — an object-keyed memo read a published graph embeds, dispatched through `apply`. */
|
|
2540
|
+
recall: (key: WeakKey) => any;
|
|
2541
|
+
/** `WeakMap.prototype.set` — an object-keyed memo write, dispatched through `apply`. */
|
|
2542
|
+
retain: (key: WeakKey, value: any) => WeakMap<WeakKey, any>;
|
|
2543
|
+
/** `WeakSet` — object-membership working state. */
|
|
2544
|
+
weakSet: WeakSetConstructor;
|
|
2545
|
+
/** `WeakSet.prototype.has` — the visitation answer every traversal's termination rests on, dispatched through `apply`. */
|
|
2546
|
+
tracked: (value: WeakKey) => boolean;
|
|
2547
|
+
/** `WeakSet.prototype.add` — entry onto the active path, dispatched through `apply`. */
|
|
2548
|
+
track: (value: WeakKey) => WeakSet<WeakKey>;
|
|
2549
|
+
/** `WeakSet.prototype.delete` — exit from the active path, dispatched through `apply`. */
|
|
2550
|
+
untrack: (value: WeakKey) => boolean;
|
|
2551
|
+
/** `Error` — the internal marker an engine throws into its own contained walk. */
|
|
2552
|
+
error: ErrorConstructor;
|
|
2553
|
+
/** `Date` — calendar validation of an ISO instant. */
|
|
2554
|
+
date: DateConstructor;
|
|
2555
|
+
/** `Date.prototype.getTime` — the calendar verdict a published `format` rests on, dispatched through `apply`. */
|
|
2556
|
+
instant: () => number;
|
|
2557
|
+
/** `Date.now` — the wall-clock reading a default generator seed is drawn from. */
|
|
2558
|
+
now: () => number;
|
|
2559
|
+
}>;
|
|
2560
|
+
|
|
2561
|
+
/** Determine whether a value is an array.
|
|
1484
2562
|
*
|
|
1485
2563
|
* @example
|
|
1486
2564
|
* ```ts
|
|
@@ -1596,6 +2674,43 @@ export declare function isBigUint64Array(value: unknown): value is BigUint64Arra
|
|
|
1596
2674
|
*/
|
|
1597
2675
|
export declare function isBoolean(value: unknown): value is boolean;
|
|
1598
2676
|
|
|
2677
|
+
/**
|
|
2678
|
+
* Determine whether a value is a depth-bounded JSON record.
|
|
2679
|
+
*
|
|
2680
|
+
* @remarks
|
|
2681
|
+
* Requires the existing plain-record root invariant before applying
|
|
2682
|
+
* {@link isBoundedJSONValue}. Arrays therefore remain valid bounded JSON
|
|
2683
|
+
* values but never bounded JSON records.
|
|
2684
|
+
*
|
|
2685
|
+
* @param value - The value to inspect
|
|
2686
|
+
* @returns `true` when the value is a plain-record-rooted bounded JSON value
|
|
2687
|
+
*
|
|
2688
|
+
* @example
|
|
2689
|
+
* ```ts
|
|
2690
|
+
* isBoundedJSONRecord({ value: 1 }) // true
|
|
2691
|
+
* isBoundedJSONRecord([1]) // false
|
|
2692
|
+
* ```
|
|
2693
|
+
*/
|
|
2694
|
+
export declare function isBoundedJSONRecord(value: unknown): value is JSONRecord;
|
|
2695
|
+
|
|
2696
|
+
/**
|
|
2697
|
+
* Determine whether a value is JSON-valid within the fixed container-depth limit.
|
|
2698
|
+
*
|
|
2699
|
+
* @remarks
|
|
2700
|
+
* Runs the total depth predicate before the existing JSON guard. These are
|
|
2701
|
+
* sequential observations of caller-owned input, not an atomic snapshot.
|
|
2702
|
+
*
|
|
2703
|
+
* @param value - The value to inspect
|
|
2704
|
+
* @returns `true` when the value is both depth-bounded and a valid {@link JSONValue}
|
|
2705
|
+
*
|
|
2706
|
+
* @example
|
|
2707
|
+
* ```ts
|
|
2708
|
+
* isBoundedJSONValue({ nested: [1] }) // true
|
|
2709
|
+
* isBoundedJSONValue(new Date()) // false
|
|
2710
|
+
* ```
|
|
2711
|
+
*/
|
|
2712
|
+
export declare function isBoundedJSONValue(value: unknown): value is JSONValue;
|
|
2713
|
+
|
|
1599
2714
|
/**
|
|
1600
2715
|
* Determine whether a value can be used as a `new`-target constructor.
|
|
1601
2716
|
*
|
|
@@ -1615,8 +2730,34 @@ export declare function isConstructor(value: unknown): value is AnyConstructor<o
|
|
|
1615
2730
|
/**
|
|
1616
2731
|
* Determine whether an unknown value is a {@link ContractError}.
|
|
1617
2732
|
*
|
|
2733
|
+
* @remarks
|
|
2734
|
+
* The ONLY spelling of the recognition question this package publishes. The test
|
|
2735
|
+
* itself is a private static of the class, because private-name membership is
|
|
2736
|
+
* legal only inside a class body; the one member on the path,
|
|
2737
|
+
* {@link ContractError.guard}, hands that test over and is pinned non-writable
|
|
2738
|
+
* and non-configurable while the class is defined. So there is no property a
|
|
2739
|
+
* caller can point somewhere else, and no member that ANSWERS the recognition
|
|
2740
|
+
* question — which is exactly what the static that preceded it did.
|
|
2741
|
+
*
|
|
2742
|
+
* A function declaration rather than a `const` holding the retrieved closure.
|
|
2743
|
+
* The retrieved form was tried and was wrong twice over: a module-scope binding
|
|
2744
|
+
* bound to a computed value is module DATA, which belongs in a data kind file
|
|
2745
|
+
* and not in the file that owns the error classes and their guards; and the
|
|
2746
|
+
* single read it bought guarded a window that does not exist, since the pin is
|
|
2747
|
+
* installed during class definition and no code can observe this member before
|
|
2748
|
+
* it. Reading a permanently pinned own data property per call buys the caller
|
|
2749
|
+
* nothing, so the guard is spelled as what it is.
|
|
2750
|
+
*
|
|
2751
|
+
* Recognition is realm-local and identity-based: a value from another copy of
|
|
2752
|
+
* this module is not this class's error, exactly as `instanceof` reported; a
|
|
2753
|
+
* `Proxy` over a genuine error is refused, because a proxy holds no private
|
|
2754
|
+
* field of its target; and `Reflect.construct(ContractError, args, Foreign)` IS
|
|
2755
|
+
* recognized, because that construction genuinely ran this constructor and only
|
|
2756
|
+
* the prototype was the caller's choice. That last consequence is a widening
|
|
2757
|
+
* rather than a narrowing, and it is named rather than left to be discovered.
|
|
2758
|
+
*
|
|
1618
2759
|
* @param value - The value to inspect
|
|
1619
|
-
* @returns `true` only
|
|
2760
|
+
* @returns `true` only for a value this class's constructor branded
|
|
1620
2761
|
*
|
|
1621
2762
|
* @example
|
|
1622
2763
|
* ```ts
|
|
@@ -1677,12 +2818,22 @@ export declare function isEmptyArray(value: unknown): value is readonly [];
|
|
|
1677
2818
|
*/
|
|
1678
2819
|
export declare function isEmptyMap(value: unknown): value is ReadonlyMap<never, never>;
|
|
1679
2820
|
|
|
1680
|
-
/** Determine whether a value is an empty plain object
|
|
2821
|
+
/** Determine whether a value is an empty plain object — no OWN keys at all, of any
|
|
2822
|
+
* kind: string or symbol, enumerable or not.
|
|
2823
|
+
*
|
|
2824
|
+
* @remarks
|
|
2825
|
+
* The own-key population is the one `recordOf` inspects (`Reflect.ownKeys`), and
|
|
2826
|
+
* it has to be: this guard narrows to `Record<string | symbol, never>`, so
|
|
2827
|
+
* counting only ENUMERABLE keys made the narrowing unsound — a record carrying
|
|
2828
|
+
* an own non-enumerable `hidden: 1` answered `true` here while `recordOf({})`
|
|
2829
|
+
* saw the key and rejected the same value, and the enumerable-symbol and
|
|
2830
|
+
* non-enumerable-string cases were treated differently for no stated reason.
|
|
1681
2831
|
*
|
|
1682
2832
|
* @example
|
|
1683
2833
|
* ```ts
|
|
1684
2834
|
* isEmptyObject({}) // true
|
|
1685
2835
|
* isEmptyObject({ a: 1 }) // false
|
|
2836
|
+
* isEmptyObject(Object.defineProperty({}, 'hidden', { value: 1 })) // false
|
|
1686
2837
|
* ```
|
|
1687
2838
|
*/
|
|
1688
2839
|
export declare function isEmptyObject(value: unknown): value is Record<string | symbol, never>;
|
|
@@ -1909,6 +3060,25 @@ export declare function isJSONPrimitive(value: unknown): value is JSONPrimitive;
|
|
|
1909
3060
|
*/
|
|
1910
3061
|
export declare function isJSONValue(value: unknown): value is JSONValue;
|
|
1911
3062
|
|
|
3063
|
+
/**
|
|
3064
|
+
* Determine whether a value belongs to the string, number, or boolean literal domain.
|
|
3065
|
+
*
|
|
3066
|
+
* @remarks
|
|
3067
|
+
* Every JavaScript number belongs to this structural domain, including `NaN`,
|
|
3068
|
+
* `±Infinity`, and signed zero. Declaration and schema policies apply finiteness
|
|
3069
|
+
* separately where their contracts require it.
|
|
3070
|
+
*
|
|
3071
|
+
* @param value - The value to inspect
|
|
3072
|
+
* @returns `true` for a string, number, or boolean
|
|
3073
|
+
*
|
|
3074
|
+
* @example
|
|
3075
|
+
* ```ts
|
|
3076
|
+
* isLiteralValue(Number.NaN) // true
|
|
3077
|
+
* isLiteralValue(null) // false
|
|
3078
|
+
* ```
|
|
3079
|
+
*/
|
|
3080
|
+
export declare function isLiteralValue(value: unknown): value is LiteralValue;
|
|
3081
|
+
|
|
1912
3082
|
/** Determine whether a value is a `Map`.
|
|
1913
3083
|
*
|
|
1914
3084
|
* @example
|
|
@@ -1939,7 +3109,12 @@ export declare function isNonEmptyArray<T = unknown>(value: unknown): value is r
|
|
|
1939
3109
|
*/
|
|
1940
3110
|
export declare function isNonEmptyMap<K = unknown, V = unknown>(value: unknown): value is ReadonlyMap<K, V>;
|
|
1941
3111
|
|
|
1942
|
-
/** Determine whether a value is a non-empty plain object
|
|
3112
|
+
/** Determine whether a value is a non-empty plain object — at least one own key of
|
|
3113
|
+
* any kind: string or symbol, enumerable or not.
|
|
3114
|
+
*
|
|
3115
|
+
* @remarks
|
|
3116
|
+
* The exact negation of {@link isEmptyObject} over the same own-key population;
|
|
3117
|
+
* see there for why enumerability is not part of the rule.
|
|
1943
3118
|
*
|
|
1944
3119
|
* @example
|
|
1945
3120
|
* ```ts
|
|
@@ -1969,6 +3144,42 @@ export declare function isNonEmptySet<T = unknown>(value: unknown): value is Rea
|
|
|
1969
3144
|
*/
|
|
1970
3145
|
export declare function isNonEmptyString(value: unknown): value is string;
|
|
1971
3146
|
|
|
3147
|
+
/**
|
|
3148
|
+
* Determine whether a value is a non-negative finite primitive integer.
|
|
3149
|
+
*
|
|
3150
|
+
* @remarks
|
|
3151
|
+
* Composes {@link isNonNegativeNumber} with {@link isInteger}. Representable
|
|
3152
|
+
* unsafe integers remain integers; safe-integer policy is a separate domain.
|
|
3153
|
+
*
|
|
3154
|
+
* @param value - The value to inspect
|
|
3155
|
+
* @returns `true` for non-negative integers other than `-0`
|
|
3156
|
+
*
|
|
3157
|
+
* @example
|
|
3158
|
+
* ```ts
|
|
3159
|
+
* isNonNegativeInteger(3) // true
|
|
3160
|
+
* isNonNegativeInteger(3.5) // false
|
|
3161
|
+
* ```
|
|
3162
|
+
*/
|
|
3163
|
+
export declare function isNonNegativeInteger(value: unknown): value is number;
|
|
3164
|
+
|
|
3165
|
+
/**
|
|
3166
|
+
* Determine whether a value is a finite primitive number at or above positive zero.
|
|
3167
|
+
*
|
|
3168
|
+
* @remarks
|
|
3169
|
+
* Positive fractions pass. Negative zero is rejected explicitly, and no
|
|
3170
|
+
* coercion or property access is performed for non-number inputs.
|
|
3171
|
+
*
|
|
3172
|
+
* @param value - The value to inspect
|
|
3173
|
+
* @returns `true` for finite primitive numbers greater than or equal to zero, except `-0`
|
|
3174
|
+
*
|
|
3175
|
+
* @example
|
|
3176
|
+
* ```ts
|
|
3177
|
+
* isNonNegativeNumber(0.5) // true
|
|
3178
|
+
* isNonNegativeNumber(-0) // false
|
|
3179
|
+
* ```
|
|
3180
|
+
*/
|
|
3181
|
+
export declare function isNonNegativeNumber(value: unknown): value is number;
|
|
3182
|
+
|
|
1972
3183
|
/** Determine whether a value is `null`.
|
|
1973
3184
|
*
|
|
1974
3185
|
* @example
|
|
@@ -2078,18 +3289,14 @@ export declare function isPromiseLike<T = unknown>(value: unknown): value is Pro
|
|
|
2078
3289
|
* not an array or class instance.
|
|
2079
3290
|
*
|
|
2080
3291
|
* @remarks
|
|
2081
|
-
*
|
|
2082
|
-
*
|
|
2083
|
-
*
|
|
2084
|
-
*
|
|
2085
|
-
*
|
|
2086
|
-
* whose prototype
|
|
2087
|
-
*
|
|
2088
|
-
*
|
|
2089
|
-
* prototype chain runs through `Array.prototype` before `null`, and a class
|
|
2090
|
-
* instance's runs through the class's own prototype. The whole body runs
|
|
2091
|
-
* inside `holds` (AGENTS §14) so a revoked `Proxy` or a hostile
|
|
2092
|
-
* `getPrototypeOf` trap cannot escape as a thrown error.
|
|
3292
|
+
* The total form of the shared {@link matchesRecordBrand} rule, and the only
|
|
3293
|
+
* one a guard may use: the whole brand runs inside `holds` (AGENTS §14) so a
|
|
3294
|
+
* revoked `Proxy` or a hostile `getPrototypeOf` trap answers `false` instead of
|
|
3295
|
+
* escaping as a thrown error. Use instead of {@link isObject} to distinguish a
|
|
3296
|
+
* plain `{}` / `Object.create(null)` — or a plain object from another realm,
|
|
3297
|
+
* whose prototype is that realm's `Object.prototype` — from arrays, `Date`,
|
|
3298
|
+
* `Map`, and class instances, including a class whose prototype a caller
|
|
3299
|
+
* reparented to `null`.
|
|
2093
3300
|
*
|
|
2094
3301
|
* @example
|
|
2095
3302
|
* ```ts
|
|
@@ -2217,22 +3424,23 @@ export declare function isUint8ClampedArray(value: unknown): value is Uint8Clamp
|
|
|
2217
3424
|
export declare function isUndefined(value: unknown): value is undefined;
|
|
2218
3425
|
|
|
2219
3426
|
/**
|
|
2220
|
-
* Determine whether
|
|
3427
|
+
* Determine whether a supported ISO-8601 date or date-time is valid.
|
|
2221
3428
|
*
|
|
2222
3429
|
* @remarks
|
|
2223
|
-
*
|
|
2224
|
-
*
|
|
2225
|
-
*
|
|
2226
|
-
*
|
|
2227
|
-
*
|
|
3430
|
+
* Accepts exactly `YYYY-MM-DD`, or `YYYY-MM-DDTHH:MM:SS` with optional
|
|
3431
|
+
* fractional seconds followed by `Z` or a numeric offset. Inside the
|
|
3432
|
+
* {@link attempt} boundary, captured components receive explicit Gregorian
|
|
3433
|
+
* month/leap/day and clock validation before `Date#getTime` performs the final
|
|
3434
|
+
* offset/instant refusal. Backs {@link stringToFormat}'s `date`, `date-time`,
|
|
3435
|
+
* and prefixed `time` validation.
|
|
2228
3436
|
*
|
|
2229
3437
|
* @param value - The candidate ISO-8601 string
|
|
2230
3438
|
* @returns `true` when `value` parses to a real instant
|
|
2231
3439
|
*
|
|
2232
3440
|
* @example
|
|
2233
3441
|
* ```ts
|
|
2234
|
-
* isValidISOInstant('
|
|
2235
|
-
* isValidISOInstant('
|
|
3442
|
+
* isValidISOInstant('2024-02-29') // true
|
|
3443
|
+
* isValidISOInstant('2024-01-01T24:00Z') // false — incomplete normalized clock
|
|
2236
3444
|
* ```
|
|
2237
3445
|
*/
|
|
2238
3446
|
export declare function isValidISOInstant(value: string): boolean;
|
|
@@ -2309,7 +3517,7 @@ export declare function isZeroArgGenerator(value: unknown): value is () => Gener
|
|
|
2309
3517
|
*
|
|
2310
3518
|
* @example
|
|
2311
3519
|
* ```ts
|
|
2312
|
-
* import { JSON_SCHEMA_TYPES, literalOf, parseEnumField } from '@
|
|
3520
|
+
* import { JSON_SCHEMA_TYPES, literalOf, parseEnumField } from '@orkestrel/contract'
|
|
2313
3521
|
*
|
|
2314
3522
|
* const isSchemaType = literalOf(...JSON_SCHEMA_TYPES) // Guard<JSONSchemaType>
|
|
2315
3523
|
* parseEnumField(schema, 'type', JSON_SCHEMA_TYPES) // JSONSchemaType | undefined
|
|
@@ -2317,20 +3525,92 @@ export declare function isZeroArgGenerator(value: unknown): value is () => Gener
|
|
|
2317
3525
|
*/
|
|
2318
3526
|
export declare const JSON_SCHEMA_TYPES: readonly JSONSchemaType[];
|
|
2319
3527
|
|
|
3528
|
+
/**
|
|
3529
|
+
* Stateful owner of one exact JSON snapshot operation.
|
|
3530
|
+
*
|
|
3531
|
+
* @remarks
|
|
3532
|
+
* Construction retains the source without observing it. The first
|
|
3533
|
+
* {@link clone} call performs one iterative descriptor walk and settles
|
|
3534
|
+
* permanently. Success replays the exact frozen root; failure rethrows the
|
|
3535
|
+
* exact class-owned error. Nonredirectable terminal failure releases partial
|
|
3536
|
+
* traversal working state while retaining the source and exact error. Reentry
|
|
3537
|
+
* poisons the active operation and every later call with one shared cause-free
|
|
3538
|
+
* error.
|
|
3539
|
+
*
|
|
3540
|
+
* @param value - The unknown value to retain for cloning
|
|
3541
|
+
*
|
|
3542
|
+
* @example
|
|
3543
|
+
* ```ts
|
|
3544
|
+
* const source = { settings: { enabled: true } }
|
|
3545
|
+
* const cloner = new JSONCloner(source)
|
|
3546
|
+
* const clone = cloner.clone()
|
|
3547
|
+
* source.settings.enabled = false
|
|
3548
|
+
* cloner.clone() === clone // true
|
|
3549
|
+
* ```
|
|
3550
|
+
*/
|
|
3551
|
+
export declare class JSONCloner implements JSONClonerInterface {
|
|
3552
|
+
#private;
|
|
3553
|
+
constructor(value: unknown);
|
|
3554
|
+
/**
|
|
3555
|
+
* Clone the retained source into exact, deeply frozen JSON data.
|
|
3556
|
+
*
|
|
3557
|
+
* @returns The settled JSON snapshot
|
|
3558
|
+
* @throws {ContractError} When the source is inexact, cyclic, unreadable, or cloning is reentered
|
|
3559
|
+
*/
|
|
3560
|
+
clone(): JSONValue;
|
|
3561
|
+
}
|
|
3562
|
+
|
|
3563
|
+
/**
|
|
3564
|
+
* Stateful owner of one exact JSON snapshot operation.
|
|
3565
|
+
*
|
|
3566
|
+
* @remarks
|
|
3567
|
+
* Construction retains the source without observing it. The first
|
|
3568
|
+
* {@link clone} call settles once; later calls replay the exact same frozen
|
|
3569
|
+
* value or exact same owned {@link ContractError}. Terminal failure releases
|
|
3570
|
+
* partial traversal working state while retaining the source and exact error.
|
|
3571
|
+
*/
|
|
3572
|
+
export declare interface JSONClonerInterface {
|
|
3573
|
+
/**
|
|
3574
|
+
* Clone the retained source into exact, deeply frozen JSON data.
|
|
3575
|
+
*
|
|
3576
|
+
* @returns The settled JSON snapshot
|
|
3577
|
+
* @throws {ContractError} When the source is inexact, cyclic, unreadable, or cloning is reentered
|
|
3578
|
+
*/
|
|
3579
|
+
clone(): JSONValue;
|
|
3580
|
+
}
|
|
3581
|
+
|
|
2320
3582
|
/**
|
|
2321
3583
|
* A primitive JSON value — the flat leaf of any JSON document.
|
|
2322
3584
|
*
|
|
2323
3585
|
* @remarks
|
|
2324
3586
|
* The recursive {@link JSONValue} tree type is shipped for consumers that need a
|
|
2325
|
-
* reusable JSON metadata contract.
|
|
2326
|
-
*
|
|
2327
|
-
*
|
|
3587
|
+
* reusable JSON metadata contract. {@link JSONRecord} supplies the record-root
|
|
3588
|
+
* contract needed by persistence and metadata consumers; a dedicated
|
|
3589
|
+
* `JSONArray` alias remains unnecessary because `readonly JSONValue[]` already
|
|
3590
|
+
* expresses that branch directly.
|
|
2328
3591
|
*/
|
|
2329
3592
|
export declare type JSONPrimitive = string | number | boolean | null;
|
|
2330
3593
|
|
|
2331
3594
|
/**
|
|
2332
|
-
* A
|
|
2333
|
-
*
|
|
3595
|
+
* A readonly string-keyed JSON object record.
|
|
3596
|
+
*
|
|
3597
|
+
* @remarks
|
|
3598
|
+
* Runtime ownership through
|
|
3599
|
+
* {@link import('./cloners.js').cloneJSONRecord} normalizes records to a frozen
|
|
3600
|
+
* null-prototype object after exact descriptor validation.
|
|
3601
|
+
*
|
|
3602
|
+
* @example
|
|
3603
|
+
* ```ts
|
|
3604
|
+
* const metadata: JSONRecord = { attempt: 1, labels: ['ready'] }
|
|
3605
|
+
* ```
|
|
3606
|
+
*/
|
|
3607
|
+
export declare type JSONRecord = {
|
|
3608
|
+
readonly [key: string]: JSONValue;
|
|
3609
|
+
};
|
|
3610
|
+
|
|
3611
|
+
/**
|
|
3612
|
+
* A JSON Schema fragment — the supported keyword vocabulary the contract
|
|
3613
|
+
* compiler emits and {@link RawShape} validates before embedding.
|
|
2334
3614
|
*
|
|
2335
3615
|
* @remarks
|
|
2336
3616
|
* Intentionally lean (not the full ~50-keyword vocabulary): it carries only the
|
|
@@ -2345,7 +3625,7 @@ export declare type JSONPrimitive = string | number | boolean | null;
|
|
|
2345
3625
|
export declare interface JSONSchema {
|
|
2346
3626
|
readonly type?: JSONSchemaType;
|
|
2347
3627
|
readonly description?: string;
|
|
2348
|
-
readonly enum?: readonly
|
|
3628
|
+
readonly enum?: readonly LiteralValue[];
|
|
2349
3629
|
readonly minLength?: number;
|
|
2350
3630
|
readonly maxLength?: number;
|
|
2351
3631
|
readonly pattern?: string;
|
|
@@ -2386,7 +3666,10 @@ export declare interface JSONShape {
|
|
|
2386
3666
|
* @remarks
|
|
2387
3667
|
* The sound counterpart of {@link rawShape}: `rawShape` embeds an arbitrary
|
|
2388
3668
|
* schema fragment and accepts every defined value at runtime, while `jsonShape`
|
|
2389
|
-
* validates that a value is real JSON (via {@link isJSONValue}).
|
|
3669
|
+
* validates that a value is real JSON (via {@link isJSONValue}). Its emitted
|
|
3670
|
+
* schema is the empty accept-anything `{}`, so here the schema claims MORE than
|
|
3671
|
+
* the compiled guard accepts — `NaN`, a `Map`, and a class instance all satisfy
|
|
3672
|
+
* `{}` and all fail `isJSONValue`.
|
|
2390
3673
|
*
|
|
2391
3674
|
* @param options - Optional `description`
|
|
2392
3675
|
* @returns A JSON passthrough shape
|
|
@@ -2416,9 +3699,7 @@ export declare interface JSONShapeOptions {
|
|
|
2416
3699
|
* const value: JSONValue = { nested: [1, 'x', null] }
|
|
2417
3700
|
* ```
|
|
2418
3701
|
*/
|
|
2419
|
-
export declare type JSONValue = JSONPrimitive | readonly JSONValue[] |
|
|
2420
|
-
readonly [key: string]: JSONValue;
|
|
2421
|
-
};
|
|
3702
|
+
export declare type JSONValue = JSONPrimitive | readonly JSONValue[] | JSONRecord;
|
|
2422
3703
|
|
|
2423
3704
|
/**
|
|
2424
3705
|
* Build a guard that accepts values that are own keys of the provided object.
|
|
@@ -2463,6 +3744,26 @@ export declare function keyOf<const O extends Readonly<Record<PropertyKey, unkno
|
|
|
2463
3744
|
*/
|
|
2464
3745
|
export declare function lazyOf<T>(thunk: () => Guard<T>): Guard<T>;
|
|
2465
3746
|
|
|
3747
|
+
/**
|
|
3748
|
+
* Take at most `limit` leading elements of an array, by index.
|
|
3749
|
+
*
|
|
3750
|
+
* @remarks
|
|
3751
|
+
* `Array.prototype.slice` is a caller-writable member on every path that bounds
|
|
3752
|
+
* a published report, so a substitute decides how much of a diagnostic the
|
|
3753
|
+
* caller sees. Returns the input untouched when it already fits, so a bounded
|
|
3754
|
+
* report allocates nothing in the ordinary case.
|
|
3755
|
+
*
|
|
3756
|
+
* @param entries - The entries to bound
|
|
3757
|
+
* @param limit - The maximum number of leading entries to retain
|
|
3758
|
+
* @returns The input when it already fits, otherwise a fresh bounded copy
|
|
3759
|
+
*
|
|
3760
|
+
* @example
|
|
3761
|
+
* ```ts
|
|
3762
|
+
* limitEntries(faults, FAULT_LIMIT)
|
|
3763
|
+
* ```
|
|
3764
|
+
*/
|
|
3765
|
+
export declare function limitEntries<T>(entries: readonly T[], limit: number): readonly T[];
|
|
3766
|
+
|
|
2466
3767
|
/**
|
|
2467
3768
|
* Build a guard that accepts a provided literal primitive using SameValueZero
|
|
2468
3769
|
* comparison.
|
|
@@ -2488,12 +3789,12 @@ export declare function lazyOf<T>(thunk: () => Guard<T>): Guard<T>;
|
|
|
2488
3789
|
* const isSameRole = literalOf(['admin', 'member', 'guest']) // the same guard, from an array
|
|
2489
3790
|
* ```
|
|
2490
3791
|
*/
|
|
2491
|
-
export declare function literalOf<const Literals extends ReadonlyArray<
|
|
3792
|
+
export declare function literalOf<const Literals extends ReadonlyArray<LiteralValue>>(literals: Literals): Guard<Literals[number]>;
|
|
2492
3793
|
|
|
2493
|
-
export declare function literalOf<const Literals extends ReadonlyArray<
|
|
3794
|
+
export declare function literalOf<const Literals extends ReadonlyArray<LiteralValue>>(...literals: Literals): Guard<Literals[number]>;
|
|
2494
3795
|
|
|
2495
3796
|
/** A literal shape — accepts exactly one of a fixed set of primitive values. */
|
|
2496
|
-
export declare interface LiteralShape<T extends readonly
|
|
3797
|
+
export declare interface LiteralShape<T extends readonly LiteralValue[] = readonly LiteralValue[]> {
|
|
2497
3798
|
readonly type: 'literal';
|
|
2498
3799
|
readonly values: T;
|
|
2499
3800
|
readonly description?: string;
|
|
@@ -2514,13 +3815,16 @@ export declare interface LiteralShape<T extends readonly (string | number | bool
|
|
|
2514
3815
|
* const via = literalShape(['function', 'tool', 'agent'], { description: 'How to run the step.' })
|
|
2515
3816
|
* ```
|
|
2516
3817
|
*/
|
|
2517
|
-
export declare function literalShape<const T extends readonly
|
|
3818
|
+
export declare function literalShape<const T extends readonly LiteralValue[]>(values: T, options?: LiteralShapeOptions): LiteralShape<Readonly<T>>;
|
|
2518
3819
|
|
|
2519
3820
|
/** Options for {@link LiteralShape} (via `literalShape`). */
|
|
2520
3821
|
export declare interface LiteralShapeOptions {
|
|
2521
3822
|
readonly description?: string;
|
|
2522
3823
|
}
|
|
2523
3824
|
|
|
3825
|
+
/** A string, number, or boolean literal value. */
|
|
3826
|
+
export declare type LiteralValue = string | number | boolean;
|
|
3827
|
+
|
|
2524
3828
|
/**
|
|
2525
3829
|
* Build a guard that accepts `Map` instances where every key satisfies
|
|
2526
3830
|
* `keyGuard` and every value satisfies `valueGuard`.
|
|
@@ -2536,6 +3840,35 @@ export declare function mapOf<K, V>(keyGuard: Guard<K>, valueGuard: Guard<V>): G
|
|
|
2536
3840
|
|
|
2537
3841
|
export declare function mapOf(keyPredicate: (value: unknown) => boolean, valuePredicate: (value: unknown) => boolean): Guard<ReadonlyMap<unknown, unknown>>;
|
|
2538
3842
|
|
|
3843
|
+
/**
|
|
3844
|
+
* Determine whether a readable value stays within the fixed JSON container-depth limit.
|
|
3845
|
+
*
|
|
3846
|
+
* @remarks
|
|
3847
|
+
* Counts array and plain-record containers on each active root-to-value path.
|
|
3848
|
+
* Primitive and readable non-record objects are leaves, active cycles add no
|
|
3849
|
+
* level, and shared aliases are answered from the shallowest depth at which
|
|
3850
|
+
* the alias already fit. Arrays are traversed through their reflected
|
|
3851
|
+
* own-index population, so sparse work is proportional to populated entries
|
|
3852
|
+
* rather than advertised length. Every observable operation is contained;
|
|
3853
|
+
* hostile or contradictory reads return `false`.
|
|
3854
|
+
*
|
|
3855
|
+
* The walk carries a settled-depth memo beside its active-path set: a
|
|
3856
|
+
* container that fit at depth `d` also fits at any depth `<= d`, because every
|
|
3857
|
+
* path below it is then shallower than the one already measured. Without it a
|
|
3858
|
+
* node reachable by `k` distinct paths was re-walked `k` times, so an ORDINARY
|
|
3859
|
+
* record graph with thirty shared aliases — thirty-one nodes — cost `2^30`
|
|
3860
|
+
* visits through a public guard.
|
|
3861
|
+
*
|
|
3862
|
+
* @param value - The value whose readable container depth to inspect
|
|
3863
|
+
* @returns `true` when no active path exceeds {@link GUARD_DEPTH_LIMIT}
|
|
3864
|
+
*
|
|
3865
|
+
* @example
|
|
3866
|
+
* ```ts
|
|
3867
|
+
* matchesJSONDepth({ nested: [1] }) // true
|
|
3868
|
+
* ```
|
|
3869
|
+
*/
|
|
3870
|
+
export declare function matchesJSONDepth(value: unknown): boolean;
|
|
3871
|
+
|
|
2539
3872
|
/**
|
|
2540
3873
|
* Match an unknown value against the recursive JSON value structure.
|
|
2541
3874
|
*
|
|
@@ -2543,8 +3876,28 @@ export declare function mapOf(keyPredicate: (value: unknown) => boolean, valuePr
|
|
|
2543
3876
|
* The caller-owned ancestor set tracks only the active traversal path, so
|
|
2544
3877
|
* cycles fail while shared references across sibling branches remain valid.
|
|
2545
3878
|
* The set belongs to one traversal from one entry point; passing a shared or
|
|
2546
|
-
* pre-populated set is unsupported. Arrays
|
|
2547
|
-
*
|
|
3879
|
+
* pre-populated set is unsupported. Arrays descend through the shared dense
|
|
3880
|
+
* own-index lens and plain records descend by values; class instances and
|
|
3881
|
+
* non-finite numbers are rejected.
|
|
3882
|
+
*
|
|
3883
|
+
* The walk is ITERATIVE, over an explicit enter/exit stack, exactly as
|
|
3884
|
+
* {@link matchesJSONDepth} already was. It used to recurse, and that made the
|
|
3885
|
+
* verdict a function of the REMAINING CALL STACK rather than of the value: the
|
|
3886
|
+
* same readable 4,000-deep document answered `true` at a root call site and
|
|
3887
|
+
* `false` a few frames down, and `parseJSONValue` republished the resulting
|
|
3888
|
+
* `RangeError` as `value could not be read` for a value every read of which
|
|
3889
|
+
* succeeded. A cap enforced by the JavaScript stack is not a cap. This walk
|
|
3890
|
+
* carries no depth cap of its own — `isJSONValue` is deliberately the unbounded
|
|
3891
|
+
* deep gate and {@link matchesJSONDepth} / `isBoundedJSONValue` are the bounded
|
|
3892
|
+
* pair beside it — so the answer now depends only on the value, at every depth
|
|
3893
|
+
* and from every call site.
|
|
3894
|
+
*
|
|
3895
|
+
* Beside the ancestor set the walk keeps a walk-local PROVED set, so a node
|
|
3896
|
+
* whose whole subtree already matched is not re-walked when a second path
|
|
3897
|
+
* reaches it. Removing the recursion alone left the work exponential in shared
|
|
3898
|
+
* aliases: thirty aliases — thirty-one ordinary records — cost `2^30` visits
|
|
3899
|
+
* through `isJSONValue`, `parseJSONValue`, `canonicalStringify` and every
|
|
3900
|
+
* `jsonShape` contract.
|
|
2548
3901
|
*
|
|
2549
3902
|
* @param entry - The value to inspect
|
|
2550
3903
|
* @param ancestors - Objects on the active traversal path
|
|
@@ -2556,7 +3909,138 @@ export declare function mapOf(keyPredicate: (value: unknown) => boolean, valuePr
|
|
|
2556
3909
|
* matchesJSONValue(Number.NaN, new WeakSet()) // false
|
|
2557
3910
|
* ```
|
|
2558
3911
|
*/
|
|
2559
|
-
export declare function matchesJSONValue(entry: unknown, ancestors: WeakSet<object>):
|
|
3912
|
+
export declare function matchesJSONValue(entry: unknown, ancestors: WeakSet<object>): entry is JSONValue;
|
|
3913
|
+
|
|
3914
|
+
/**
|
|
3915
|
+
* Determine whether a value is a member of a collected vocabulary, by
|
|
3916
|
+
* SameValueZero.
|
|
3917
|
+
*
|
|
3918
|
+
* @param members - The vocabulary to ask, built by {@link collectMembers}
|
|
3919
|
+
* @param value - The value to test for membership
|
|
3920
|
+
* @returns `true` only when the value was collected
|
|
3921
|
+
*
|
|
3922
|
+
* @example
|
|
3923
|
+
* ```ts
|
|
3924
|
+
* matchesMember(collectMembers([Number.NaN]), Number.NaN) // true — SameValueZero
|
|
3925
|
+
* ```
|
|
3926
|
+
*/
|
|
3927
|
+
export declare function matchesMember(members: Set<unknown>, value: unknown): boolean;
|
|
3928
|
+
|
|
3929
|
+
/**
|
|
3930
|
+
* Determine whether a string is in the language of a pattern this package owns.
|
|
3931
|
+
*
|
|
3932
|
+
* @remarks
|
|
3933
|
+
* The pattern-membership answer, asked exactly as {@link matchesMember} asks the
|
|
3934
|
+
* literal one, and asked through `exec` rather than `test` on purpose:
|
|
3935
|
+
* `RegExp.prototype.test` is spec-defined in terms of `RegExpExec`, which
|
|
3936
|
+
* re-reads `exec` off the receiver, so even a CAPTURED `test` still answers
|
|
3937
|
+
* whatever the caller installed. Replacing either member decided what `matchOf`,
|
|
3938
|
+
* `stringOf`, `contract.is`, `contract.parse`, `audit`, `explain` and the format
|
|
3939
|
+
* inferers published — a wrong yes for a non-member and a wrong no for a member,
|
|
3940
|
+
* silently.
|
|
3941
|
+
*
|
|
3942
|
+
* @param pattern - The owned pattern to apply
|
|
3943
|
+
* @param value - The string to test
|
|
3944
|
+
* @returns `true` only when the pattern genuinely matches
|
|
3945
|
+
*
|
|
3946
|
+
* @example
|
|
3947
|
+
* ```ts
|
|
3948
|
+
* matchesPattern(/^[0-9a-f]+$/, '1a2f') // true
|
|
3949
|
+
* ```
|
|
3950
|
+
*/
|
|
3951
|
+
export declare function matchesPattern(pattern: RegExp, value: string): boolean;
|
|
3952
|
+
|
|
3953
|
+
/**
|
|
3954
|
+
* Determine whether a value carries the plain-record brand, raising a hostile
|
|
3955
|
+
* prototype observation instead of answering it.
|
|
3956
|
+
*
|
|
3957
|
+
* @remarks
|
|
3958
|
+
* THE single record-brand rule: a plain record is a non-array object whose
|
|
3959
|
+
* prototype is `null`, or is a realm's `Object.prototype`. Realm-agnosticism is
|
|
3960
|
+
* why the second arm cannot simply compare against this realm's
|
|
3961
|
+
* `Object.prototype` — a plain object from another `vm.Context`, iframe, or
|
|
3962
|
+
* worker inherits from THAT realm's `Object.prototype`, which is a different
|
|
3963
|
+
* object. The earlier rule accepted any prototype that itself had a `null`
|
|
3964
|
+
* prototype, which every realm's `Object.prototype` satisfies — and so does a
|
|
3965
|
+
* class prototype a caller reparented to `null`, which is how a class instance
|
|
3966
|
+
* laundered through every ownership door. A foreign `Object.prototype` is
|
|
3967
|
+
* therefore identified by the own members ECMAScript requires every realm to
|
|
3968
|
+
* put on it (`constructor`, `hasOwnProperty`, `isPrototypeOf`,
|
|
3969
|
+
* `propertyIsEnumerable`, `toLocaleString`, `toString`, `valueOf`), each read
|
|
3970
|
+
* through its own DESCRIPTOR so no accessor on a hostile prototype ever runs.
|
|
3971
|
+
* Each must be an own DATA property whose value is a FUNCTION — true of every
|
|
3972
|
+
* conformant realm, so the requirement costs a genuine foreign record nothing,
|
|
3973
|
+
* and it refuses the cheapest forgery (stamping the seven names with
|
|
3974
|
+
* `undefined`) for free.
|
|
3975
|
+
*
|
|
3976
|
+
* That is a structural test, not a provenance one, and the residual is stated
|
|
3977
|
+
* as exactly what it is: a FUNCTION-VALUED forgery passes, and this realm's own
|
|
3978
|
+
* `Object.prototype` supplies the seven functions to stamp, so the price is a
|
|
3979
|
+
* few lines rather than nothing. Reparenting a class prototype to `null` and
|
|
3980
|
+
* stamping the seven names with real functions passes; so does leaving the
|
|
3981
|
+
* class untouched and putting a `Proxy` in prototype position that reports
|
|
3982
|
+
* `null` as its own prototype and answers those seven descriptor reads with
|
|
3983
|
+
* functions. In both cases the value is a live class instance whose methods are
|
|
3984
|
+
* still reachable on it. A further own-key SUBSET rule buys even less: it
|
|
3985
|
+
* refuses a forgery that left methods on its prototype and accepts the same
|
|
3986
|
+
* class with those methods moved onto the instance, where they are
|
|
3987
|
+
* indistinguishable from a plain record's function
|
|
3988
|
+
* properties — so it raises the forgery's price and narrows the realm-agnostic
|
|
3989
|
+
* arm this rule exists to keep open. What the pass buys is acceptance at
|
|
3990
|
+
* brand-governed doors and nothing after it: every ownership engine publishes a
|
|
3991
|
+
* frozen plain record built only from captured data, so no class instance,
|
|
3992
|
+
* class behavior, or forged prototype survives into a snapshot.
|
|
3993
|
+
*
|
|
3994
|
+
* `Object.create(<null-prototype object>)` is refused, and NOT because it is
|
|
3995
|
+
* structurally identical to a reparented class instance — it is not, since a
|
|
3996
|
+
* class prototype always owns `constructor` and a bare `Object.create(null)`
|
|
3997
|
+
* owns nothing. It is refused as policy: no realm produces that chain for a
|
|
3998
|
+
* plain object, no consumer of it has been named, and a caller erases the
|
|
3999
|
+
* difference by deleting `constructor`.
|
|
4000
|
+
*
|
|
4001
|
+
* This is the diagnosing form, deliberately NOT total: a revoked `Proxy` or a
|
|
4002
|
+
* hostile `getPrototypeOf` trap throws out of it, so an ownership engine can
|
|
4003
|
+
* report an unreadable value as a failed read with the exact cause rather than
|
|
4004
|
+
* as a well-formed structural refusal. {@link isRecord} is the total form for
|
|
4005
|
+
* every guard consumer and contains that throw as `false`.
|
|
4006
|
+
*
|
|
4007
|
+
* @param value - The value whose record brand to inspect
|
|
4008
|
+
* @returns `true` only when the value is a plain record
|
|
4009
|
+
* @throws The exact value thrown by a hostile brand observation
|
|
4010
|
+
*
|
|
4011
|
+
* @example
|
|
4012
|
+
* ```ts
|
|
4013
|
+
* matchesRecordBrand({}) // true
|
|
4014
|
+
* matchesRecordBrand(Object.create(null)) // true
|
|
4015
|
+
* matchesRecordBrand(new Date()) // false
|
|
4016
|
+
* ```
|
|
4017
|
+
*/
|
|
4018
|
+
export declare function matchesRecordBrand(value: unknown): boolean;
|
|
4019
|
+
|
|
4020
|
+
/**
|
|
4021
|
+
* Determine whether an object is already on a traversal's active path.
|
|
4022
|
+
*
|
|
4023
|
+
* @remarks
|
|
4024
|
+
* The visitation half, and the one an earlier ruling wrongly excused as safe
|
|
4025
|
+
* because "a redirect corrupts it inside a boundary and the door refuses, which
|
|
4026
|
+
* is loud". It is not loud in the direction that matters:
|
|
4027
|
+
* `WeakSet.prototype.has` answering `false` does not make a cyclic clone refuse,
|
|
4028
|
+
* it removes the door's termination bound, and a door that never returns is the
|
|
4029
|
+
* one failure a containment boundary cannot report. Every walk's termination
|
|
4030
|
+
* therefore rests on a captured operation rather than on a caller-writable one.
|
|
4031
|
+
*
|
|
4032
|
+
* @param visited - The active-path set this traversal owns
|
|
4033
|
+
* @param value - The object to test
|
|
4034
|
+
* @returns `true` only when the object is already on the active path
|
|
4035
|
+
*
|
|
4036
|
+
* @example
|
|
4037
|
+
* ```ts
|
|
4038
|
+
* const active = new WeakSet<object>()
|
|
4039
|
+
* admitVisited(active, node)
|
|
4040
|
+
* matchesVisited(active, node) // true
|
|
4041
|
+
* ```
|
|
4042
|
+
*/
|
|
4043
|
+
export declare function matchesVisited(visited: WeakSet<object>, value: object): boolean;
|
|
2560
4044
|
|
|
2561
4045
|
/**
|
|
2562
4046
|
* Build a guard that accepts strings matching a regular expression.
|
|
@@ -2684,9 +4168,13 @@ export declare interface NumberShapeOptions {
|
|
|
2684
4168
|
*
|
|
2685
4169
|
* @remarks
|
|
2686
4170
|
* A property whose shape is an {@link OptionalShape} may be absent; all others
|
|
2687
|
-
* are required. `additionalProperties` controls unknown keys
|
|
2688
|
-
*
|
|
2689
|
-
*
|
|
4171
|
+
* are required. `additionalProperties` controls unknown keys, and each compiled
|
|
4172
|
+
* artifact acts on that setting in its own way. Closed (`undefined` / `false`):
|
|
4173
|
+
* the compiled guard rejects the object, the compiled auditor reports one
|
|
4174
|
+
* `'extra'` fault at the offending key, and the emitted schema sets
|
|
4175
|
+
* `additionalProperties: false` — while the compiled parser drops the key and
|
|
4176
|
+
* the compiled reporter stays silent, mirroring that parser. `true` accepts
|
|
4177
|
+
* unknown keys as-is, and a `ContractShape` validates them, in every artifact.
|
|
2690
4178
|
*/
|
|
2691
4179
|
export declare interface ObjectShape<P extends Readonly<Record<string, ContractShape>> = Readonly<Record<string, ContractShape>>, A extends boolean | ContractShape = boolean | ContractShape> {
|
|
2692
4180
|
readonly type: 'object';
|
|
@@ -2737,6 +4225,19 @@ export declare interface ObjectShapeOptions<A extends boolean | ContractShape =
|
|
|
2737
4225
|
*/
|
|
2738
4226
|
export declare function omitOf<S extends GuardsShape, K extends ReadonlyArray<keyof S & string>>(shape: S, keys: K): Omit<S, K[number]>;
|
|
2739
4227
|
|
|
4228
|
+
/**
|
|
4229
|
+
* Record an object as exited from a traversal's active path.
|
|
4230
|
+
*
|
|
4231
|
+
* @param visited - The active-path set this traversal owns
|
|
4232
|
+
* @param value - The object being exited
|
|
4233
|
+
*
|
|
4234
|
+
* @example
|
|
4235
|
+
* ```ts
|
|
4236
|
+
* omitVisited(active, node)
|
|
4237
|
+
* ```
|
|
4238
|
+
*/
|
|
4239
|
+
export declare function omitVisited(visited: WeakSet<object>, value: object): void;
|
|
4240
|
+
|
|
2740
4241
|
/**
|
|
2741
4242
|
* Build a {@link UnionShape} that emits `oneOf` (exactly one match) in JSON Schema.
|
|
2742
4243
|
*
|
|
@@ -2835,33 +4336,27 @@ export declare function orOf<A, B>(left: Guard<A>, right: Guard<B>): Guard<A | B
|
|
|
2835
4336
|
export declare function orOf(left: (value: unknown) => boolean, right: (value: unknown) => boolean): Guard<unknown>;
|
|
2836
4337
|
|
|
2837
4338
|
/**
|
|
2838
|
-
* Take ownership of a contract shape node
|
|
2839
|
-
*
|
|
4339
|
+
* Take ownership of a contract shape node as an independent {@link cloneShape}
|
|
4340
|
+
* snapshot of its graph.
|
|
2840
4341
|
*
|
|
2841
4342
|
* @remarks
|
|
2842
|
-
*
|
|
2843
|
-
*
|
|
2844
|
-
*
|
|
2845
|
-
*
|
|
2846
|
-
*
|
|
2847
|
-
*
|
|
2848
|
-
*
|
|
2849
|
-
*
|
|
2850
|
-
*
|
|
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.
|
|
4343
|
+
* Every successful return is a deeply frozen caller-independent graph, and
|
|
4344
|
+
* "frozen" here means frozen by the `Object.freeze` this package captured while
|
|
4345
|
+
* it loaded, not by whatever `Object.freeze` names when the call is made. The
|
|
4346
|
+
* distinction is the whole guarantee: under `Object.freeze = (value) => value`
|
|
4347
|
+
* this door used to SUCCEED and publish a mutable graph, with no throw and no
|
|
4348
|
+
* signal a caller could read. A frozen caller root receives no identity
|
|
4349
|
+
* exception: shallow freezing cannot establish ownership of nested collections,
|
|
4350
|
+
* child nodes, raw schemas, or `RegExp` internal state, and the fidelity clone
|
|
4351
|
+
* has already paid the traversal cost needed to prove and carry those values.
|
|
2856
4352
|
*
|
|
2857
4353
|
* @param shape - The contract shape to own
|
|
2858
|
-
* @returns
|
|
4354
|
+
* @returns A deeply cloned frozen snapshot
|
|
2859
4355
|
*
|
|
2860
4356
|
* @example
|
|
2861
4357
|
* ```ts
|
|
2862
|
-
* const authored = stringShape()
|
|
2863
|
-
* ownShape(authored) === authored //
|
|
2864
|
-
* ownShape({ type: 'string' }) // a frozen snapshot — the literal is caller-owned
|
|
4358
|
+
* const authored = stringShape()
|
|
4359
|
+
* ownShape(authored) === authored // false
|
|
2865
4360
|
* ```
|
|
2866
4361
|
*/
|
|
2867
4362
|
export declare function ownShape(shape: ContractShape): ContractShape;
|
|
@@ -2946,7 +4441,8 @@ export declare function parseBooleanField(record: Record<string, unknown>, path:
|
|
|
2946
4441
|
* `parseEnum ↔ literalOf(...allowed)` pairing covers every literal primitive
|
|
2947
4442
|
* (string, number, or boolean), not only strings. Matching is identity, never
|
|
2948
4443
|
* cross-type coercion: `parseEnum('1', [1])` stays `undefined`. The allowed
|
|
2949
|
-
* values are
|
|
4444
|
+
* values are captured through their dense own-index view and copied into an
|
|
4445
|
+
* owned `Set`; caller-defined iteration is ignored and unreadability returns
|
|
2950
4446
|
* `undefined` rather than escaping.
|
|
2951
4447
|
*
|
|
2952
4448
|
* @param value - The value to parse
|
|
@@ -2959,9 +4455,9 @@ export declare function parseBooleanField(record: Record<string, unknown>, path:
|
|
|
2959
4455
|
* parseEnum('z', ['a', 'b', 'c']) // undefined
|
|
2960
4456
|
* ```
|
|
2961
4457
|
*/
|
|
2962
|
-
export declare function parseEnum<const T extends
|
|
4458
|
+
export declare function parseEnum<const T extends LiteralValue>(value: unknown, allowed: readonly T[]): T | undefined;
|
|
2963
4459
|
|
|
2964
|
-
export declare function parseEnum(value: unknown, allowed: readonly
|
|
4460
|
+
export declare function parseEnum(value: unknown, allowed: readonly LiteralValue[]): LiteralValue | undefined;
|
|
2965
4461
|
|
|
2966
4462
|
/**
|
|
2967
4463
|
* Read and parse an enum field from a record by key or nested key path.
|
|
@@ -2971,7 +4467,7 @@ export declare function parseEnum(value: unknown, allowed: readonly (string | nu
|
|
|
2971
4467
|
* @param allowed - The permitted literal values
|
|
2972
4468
|
* @returns The matched literal, or `undefined`
|
|
2973
4469
|
*/
|
|
2974
|
-
export declare function parseEnumField<const T extends
|
|
4470
|
+
export declare function parseEnumField<const T extends LiteralValue>(record: Record<string, unknown>, path: FieldPath, allowed: readonly T[]): T | undefined;
|
|
2975
4471
|
|
|
2976
4472
|
/**
|
|
2977
4473
|
* Parse an unknown value to a finite integer.
|
|
@@ -3045,13 +4541,14 @@ export declare function parseJSONAs<T>(value: string, guard: Guard<T>): T | unde
|
|
|
3045
4541
|
* @remarks
|
|
3046
4542
|
* Unlike {@link parseRecord} / {@link parseArray}, this is a DEEP gate: it
|
|
3047
4543
|
* walks the entire tree via {@link isJSONValue} rather than checking only the
|
|
3048
|
-
* top-level shape. That walk is cycle-safe
|
|
3049
|
-
*
|
|
3050
|
-
* structure
|
|
3051
|
-
*
|
|
4544
|
+
* top-level shape. That walk is cycle-safe. A readable non-JSON structure,
|
|
4545
|
+
* including a cycle, returns `undefined`; a failed property read throws a
|
|
4546
|
+
* `structure` {@link ContractError}, keeping unreadability distinct from an
|
|
4547
|
+
* honest invalid result.
|
|
3052
4548
|
*
|
|
3053
4549
|
* @param value - The value to parse
|
|
3054
4550
|
* @returns The value, or `undefined` when it is not a valid JSON value
|
|
4551
|
+
* @throws {ContractError} When the JSON tree cannot be read
|
|
3055
4552
|
*
|
|
3056
4553
|
* @example
|
|
3057
4554
|
* ```ts
|
|
@@ -3169,6 +4666,7 @@ export declare type Parser<T> = (value: unknown) => T | undefined;
|
|
|
3169
4666
|
*
|
|
3170
4667
|
* @param value - The value to parse
|
|
3171
4668
|
* @returns The record, or `undefined`
|
|
4669
|
+
* @throws {ContractError} When an object value cannot be read
|
|
3172
4670
|
*/
|
|
3173
4671
|
export declare function parseRecord(value: unknown): Record<string, unknown> | undefined;
|
|
3174
4672
|
|
|
@@ -3209,6 +4707,34 @@ export declare function parseString(value: unknown): string | undefined;
|
|
|
3209
4707
|
*/
|
|
3210
4708
|
export declare function parseStringField(record: Record<string, unknown>, path: FieldPath): string | undefined;
|
|
3211
4709
|
|
|
4710
|
+
/**
|
|
4711
|
+
* Build a diagnostic path from an existing path and further segments, without
|
|
4712
|
+
* dispatching through array iteration.
|
|
4713
|
+
*
|
|
4714
|
+
* @remarks
|
|
4715
|
+
* `[...path, key]` reads well and dispatches through
|
|
4716
|
+
* `Array.prototype[Symbol.iterator]`, a member every caller can write — and the
|
|
4717
|
+
* damaging installation is not a thrower but a LIAR. An iterator yielding one
|
|
4718
|
+
* extra value before the array's real contents turns a refusal this package
|
|
4719
|
+
* authored into `path: ['INJECTED', 'properties', 'INJECTED']`, so the caller
|
|
4720
|
+
* writes their own text into a diagnostic this package published. An indexed
|
|
4721
|
+
* walk reads only own index properties of an array this package owns, and a
|
|
4722
|
+
* rest parameter collects its arguments without an iterator either, so nothing
|
|
4723
|
+
* on the path is caller-reachable.
|
|
4724
|
+
*
|
|
4725
|
+
* @param path - The path segments accumulated so far
|
|
4726
|
+
* @param segments - Further segments to append in order; an absent segment is
|
|
4727
|
+
* omitted, so an optional level needs no branch at the call site
|
|
4728
|
+
* @returns A fresh path carrying every existing segment and each new one
|
|
4729
|
+
*
|
|
4730
|
+
* @example
|
|
4731
|
+
* ```ts
|
|
4732
|
+
* pathOf(['properties'], 'age') // ['properties', 'age']
|
|
4733
|
+
* pathOf(path) // an owned copy
|
|
4734
|
+
* ```
|
|
4735
|
+
*/
|
|
4736
|
+
export declare function pathOf(path: readonly string[], ...segments: readonly (string | undefined)[]): readonly string[];
|
|
4737
|
+
|
|
3212
4738
|
/**
|
|
3213
4739
|
* Build a new guard shape by keeping only the listed keys — the structural
|
|
3214
4740
|
* equivalent of `Pick<T, K>`. Produces a shape for {@link recordOf}, not a guard.
|
|
@@ -3222,18 +4748,61 @@ export declare function parseStringField(record: Record<string, unknown>, path:
|
|
|
3222
4748
|
*/
|
|
3223
4749
|
export declare function pickOf<S extends GuardsShape, K extends ReadonlyArray<keyof S & string>>(shape: S, keys: K): Pick<S, K[number]>;
|
|
3224
4750
|
|
|
4751
|
+
/**
|
|
4752
|
+
* Pin every own member of a class prototype as a non-configurable member —
|
|
4753
|
+
* non-writable too when it is a data property — and verify the pin took.
|
|
4754
|
+
*
|
|
4755
|
+
* @remarks
|
|
4756
|
+
* The other half of the structural answer. Membership answers moved off class
|
|
4757
|
+
* methods entirely, but a class this package EXPORTS still has methods its own
|
|
4758
|
+
* modules dispatch through — `cloneShape` reaches `ShapeCloner.prototype.clone`,
|
|
4759
|
+
* and one assignment there made `compileSchema` publish whatever the caller
|
|
4760
|
+
* chose while `compileSchema` itself was never touched. That is the same defect
|
|
4761
|
+
* as a replaced host member with the package's own name on it, so every exported
|
|
4762
|
+
* class pins its prototype while it is DEFINED.
|
|
4763
|
+
*
|
|
4764
|
+
* The qualification that phrase used to carry — "before any importer's code can
|
|
4765
|
+
* run" — was FALSE, in exactly the case {@link INTRINSICS} already states and
|
|
4766
|
+
* does not defend: ESM evaluates imports in source order, so a module that
|
|
4767
|
+
* evaluates before this package has already run. What is true is narrower and is
|
|
4768
|
+
* what the pin buys: no code that runs AFTER this class is defined can replace a
|
|
4769
|
+
* member on its prototype.
|
|
4770
|
+
*
|
|
4771
|
+
* Placement goes through the captured `Reflect.defineProperty`, which ANSWERS
|
|
4772
|
+
* instead of throwing, and the answer is then corroborated by reading the
|
|
4773
|
+
* descriptor back. Installing is not reading: a pin that silently did not happen
|
|
4774
|
+
* is indistinguishable from one that did until something asks, so this asks. The
|
|
4775
|
+
* residual is named rather than denied: an adversary who also answers the
|
|
4776
|
+
* verifying descriptor read defeats this, and that adversary already chose what
|
|
4777
|
+
* {@link INTRINSICS} captured.
|
|
4778
|
+
*
|
|
4779
|
+
* @param prototype - The class prototype to pin
|
|
4780
|
+
* @param owner - The class name used in the refusal
|
|
4781
|
+
* @throws {ContractError} When a member cannot be pinned or the pin cannot be verified
|
|
4782
|
+
*
|
|
4783
|
+
* @example
|
|
4784
|
+
* ```ts
|
|
4785
|
+
* class Widget { static { pinMembers(Widget.prototype, 'Widget') } }
|
|
4786
|
+
* ```
|
|
4787
|
+
*/
|
|
4788
|
+
export declare function pinMembers(prototype: object, owner: string): void;
|
|
4789
|
+
|
|
3225
4790
|
/**
|
|
3226
4791
|
* Render a short, safe, TOTAL preview of an unknown value for a {@link Fault}'s
|
|
3227
4792
|
* `received` field.
|
|
3228
4793
|
*
|
|
3229
4794
|
* @remarks
|
|
3230
|
-
* A primitive renders as
|
|
3231
|
-
*
|
|
3232
|
-
*
|
|
3233
|
-
*
|
|
3234
|
-
*
|
|
3235
|
-
*
|
|
3236
|
-
*
|
|
4795
|
+
* A primitive renders as printable text: a string retains its quoted JSON
|
|
4796
|
+
* representation, while a narrowed symbol renders through intrinsic `String`
|
|
4797
|
+
* and receives the same escaping without outer quotes. One bounded indexed
|
|
4798
|
+
* encoder appends only complete escaped code-point tokens within
|
|
4799
|
+
* {@link PREVIEW_LIMIT}; clipping therefore never retrieves the mutable string
|
|
4800
|
+
* iterator or splits an escape/surrogate pair before its trailing `…`, and
|
|
4801
|
+
* enormous primitive text is not fully traversed. A number / boolean / bigint
|
|
4802
|
+
* renders via `String`; `null` and `undefined` render as their own name. An
|
|
4803
|
+
* array renders as `'array'`. Every other host — a plain object, a function, a
|
|
4804
|
+
* class instance, a `Map` — is NEVER traversed or stringified; it renders as
|
|
4805
|
+
* its bare `typeof` tag (`'object'` / `'function'`).
|
|
3237
4806
|
*
|
|
3238
4807
|
* @param value - The value to preview
|
|
3239
4808
|
* @returns A short descriptive string, always safe to embed in a diagnostic
|
|
@@ -3244,7 +4813,7 @@ export declare function pickOf<S extends GuardsShape, K extends ReadonlyArray<ke
|
|
|
3244
4813
|
* preview(42) // '42'
|
|
3245
4814
|
* preview(null) // 'null'
|
|
3246
4815
|
* preview({ a: 1 }) // 'object'
|
|
3247
|
-
* preview([1, 2, 3]) // '
|
|
4816
|
+
* preview([1, 2, 3]) // 'array'
|
|
3248
4817
|
* ```
|
|
3249
4818
|
*/
|
|
3250
4819
|
export declare function preview(value: unknown): string;
|
|
@@ -3263,10 +4832,16 @@ export declare const PREVIEW_LIMIT = 64;
|
|
|
3263
4832
|
export declare type RandomFunction = () => number;
|
|
3264
4833
|
|
|
3265
4834
|
/**
|
|
3266
|
-
* A raw JSON Schema passthrough — embeds a schema fragment directly.
|
|
4835
|
+
* A validated raw JSON Schema passthrough — embeds a supported schema fragment directly.
|
|
3267
4836
|
*
|
|
3268
4837
|
* @remarks
|
|
3269
|
-
* For values the shape DSL can't express. The
|
|
4838
|
+
* For values the shape DSL can't express. The fragment is checked recursively
|
|
4839
|
+
* against the lean {@link JSONSchema} vocabulary before it is accepted or
|
|
4840
|
+
* emitted; unsupported keywords and malformed keyword values throw a coded
|
|
4841
|
+
* {@link ContractError}. This is structural and keyword-domain validation,
|
|
4842
|
+
* not a full JSON Schema solver: it does not resolve cross-keyword
|
|
4843
|
+
* contradictions, `required`/`properties` membership, keyword/type coherence,
|
|
4844
|
+
* `enum`/`type` compatibility, or a closed `format` vocabulary. The compiled guard accepts every
|
|
3270
4845
|
* top-level value except `undefined`, which is reserved as the parser failure
|
|
3271
4846
|
* sentinel. Wrap the shape with {@link OptionalShape} to admit absence. Defined
|
|
3272
4847
|
* values pass through unchanged, and the schema is emitted structurally
|
|
@@ -3278,18 +4853,24 @@ export declare interface RawShape {
|
|
|
3278
4853
|
}
|
|
3279
4854
|
|
|
3280
4855
|
/**
|
|
3281
|
-
* Build a {@link RawShape} from a JSON Schema fragment.
|
|
4856
|
+
* Build a {@link RawShape} from a supported JSON Schema fragment.
|
|
3282
4857
|
*
|
|
3283
4858
|
* @remarks
|
|
3284
|
-
* For values the shape DSL can't express. The
|
|
4859
|
+
* For values the shape DSL can't express. The fragment is recursively checked
|
|
4860
|
+
* against the lean {@link JSONSchema} vocabulary before ownership is taken;
|
|
4861
|
+
* malformed or unsupported keywords throw a coded {@link ContractError}. The
|
|
4862
|
+
* compiled guard accepts every
|
|
3285
4863
|
* DEFINED value — `undefined` alone fails, because it is the parser's failure
|
|
3286
4864
|
* sentinel; wrap the shape in {@link optionalShape} to admit absence. The
|
|
3287
4865
|
* parser passes a defined value through unchanged, and the fragment is
|
|
3288
4866
|
* deep-cloned into an owned frozen snapshot ({@link cloneSchema}), so
|
|
3289
4867
|
* `rawShape(fragment).schema !== fragment` and later edits to the caller's
|
|
3290
4868
|
* fragment cannot reach the shape. `compileSchema` re-emits that snapshot
|
|
3291
|
-
* structurally verbatim
|
|
3292
|
-
*
|
|
4869
|
+
* structurally verbatim, so here the schema claims LESS than the compiled guard
|
|
4870
|
+
* accepts — `rawShape({ type: 'string' })` emits `{ type: 'string' }` and its
|
|
4871
|
+
* guard still accepts `42`, the mirror of {@link jsonShape}'s looseness.
|
|
4872
|
+
* `compileGenerator` throws, since an arbitrary embedded schema has no
|
|
4873
|
+
* auto-generatable sample.
|
|
3293
4874
|
*
|
|
3294
4875
|
* @param schema - The JSON Schema fragment to embed
|
|
3295
4876
|
* @returns A raw shape owning a frozen copy of the fragment
|
|
@@ -3301,6 +4882,224 @@ export declare interface RawShape {
|
|
|
3301
4882
|
*/
|
|
3302
4883
|
export declare function rawShape(schema: JSONSchema): RawShape;
|
|
3303
4884
|
|
|
4885
|
+
/**
|
|
4886
|
+
* Snapshot an array through its reflected own-index population.
|
|
4887
|
+
*
|
|
4888
|
+
* @remarks
|
|
4889
|
+
* Reads `length` once and one reflected own-key population, then corroborates
|
|
4890
|
+
* and reads only those reflected canonical indices in ascending order. The
|
|
4891
|
+
* frozen native snapshot retains actual holes: reading one yields `undefined`,
|
|
4892
|
+
* while own membership remains absent. Its work is proportional to the
|
|
4893
|
+
* reflected population, so a length-driven consumer must require `dense` or
|
|
4894
|
+
* carry an independent bound. Caller-defined iteration is ignored. A
|
|
4895
|
+
* descriptor-only index omitted from reflection is deliberately outside this
|
|
4896
|
+
* lens and remains a hole. Failure retains the exact thrown value when length,
|
|
4897
|
+
* reflection, membership, or indexed value observation throws; a non-native
|
|
4898
|
+
* length or view disagreement is also failure. `4294967295` is metadata rather
|
|
4899
|
+
* than an array index.
|
|
4900
|
+
*
|
|
4901
|
+
* @param value - The array whose reflected indexed entries to read
|
|
4902
|
+
* @returns A successful frozen entry snapshot with its dense fact, or a
|
|
4903
|
+
* failure carrying the exact thrown value as `unknown`
|
|
4904
|
+
*
|
|
4905
|
+
* @example
|
|
4906
|
+
* ```ts
|
|
4907
|
+
* readArrayEntries([1, 2]) // { success: true, value: { entries: [1, 2], dense: true } }
|
|
4908
|
+
* ```
|
|
4909
|
+
*/
|
|
4910
|
+
export declare function readArrayEntries<T>(value: readonly T[]): Result<ArrayRead<T>>;
|
|
4911
|
+
|
|
4912
|
+
/**
|
|
4913
|
+
* Snapshot the genuine entries of a caller's `Map` without running an iterator.
|
|
4914
|
+
*
|
|
4915
|
+
* @remarks
|
|
4916
|
+
* The `Map` half of {@link readSetEntries}, with one further replaceable
|
|
4917
|
+
* dispatch removed: destructuring `for (const [key, value] of map)` reads
|
|
4918
|
+
* `Map.prototype[Symbol.iterator]` AND `Array.prototype[Symbol.iterator]` for
|
|
4919
|
+
* every pair, so a substituting iterator could rename a key or replace a value
|
|
4920
|
+
* while every downstream structural check still passed. Each pair is read
|
|
4921
|
+
* positionally from a list this package built.
|
|
4922
|
+
*
|
|
4923
|
+
* @param value - The map whose genuine entries to snapshot
|
|
4924
|
+
* @returns A frozen `[key, value]` pair snapshot, or a failure carrying the exact thrown value
|
|
4925
|
+
*
|
|
4926
|
+
* @example
|
|
4927
|
+
* ```ts
|
|
4928
|
+
* readMapEntries(new Map([['a', 1]])) // { success: true, value: [['a', 1]] }
|
|
4929
|
+
* ```
|
|
4930
|
+
*/
|
|
4931
|
+
export declare function readMapEntries(value: ReadonlyMap<unknown, unknown>): Result<readonly (readonly unknown[])[]>;
|
|
4932
|
+
|
|
4933
|
+
/**
|
|
4934
|
+
* Validate and snapshot a shape-builder options record through every reflective
|
|
4935
|
+
* operation the builder relies on.
|
|
4936
|
+
*
|
|
4937
|
+
* @remarks
|
|
4938
|
+
* Primitive inputs are rejected before reflection so ordinary caller mistakes
|
|
4939
|
+
* retain the reader's precise plain-record diagnostic. For an object, every
|
|
4940
|
+
* consumed key is read exactly once, checked for presence, and inspected for an
|
|
4941
|
+
* own descriptor while the container is enumerated once. Every successfully
|
|
4942
|
+
* read non-`undefined` consumed value enters the fresh own-enumerable snapshot,
|
|
4943
|
+
* including an inherited or non-enumerable option. A hostile host is reported
|
|
4944
|
+
* uniformly as an unreadable options record, while a readable array or class
|
|
4945
|
+
* instance retains the plain-record diagnostic.
|
|
4946
|
+
*
|
|
4947
|
+
* @param source - The optional builder options value
|
|
4948
|
+
* @param keys - Every option key consumed by that builder
|
|
4949
|
+
* @param builder - The builder name used in diagnostics
|
|
4950
|
+
* @param shape - The shape category used in structured error context
|
|
4951
|
+
* @returns An owned options snapshot, or `undefined` when options are absent
|
|
4952
|
+
* @throws {ContractError} When the value is not a plain record or reflection fails
|
|
4953
|
+
*
|
|
4954
|
+
* @example
|
|
4955
|
+
* ```ts
|
|
4956
|
+
* const options = readOptions(source, ['min', 'max'], 'numberShape', 'number')
|
|
4957
|
+
* ```
|
|
4958
|
+
*/
|
|
4959
|
+
export declare function readOptions<T extends object>(source: T | undefined, keys: readonly (keyof T & string)[], builder: string, shape: string): T | undefined;
|
|
4960
|
+
|
|
4961
|
+
/**
|
|
4962
|
+
* Rebuild a caller's regular expression as a stateless pattern this package
|
|
4963
|
+
* owns.
|
|
4964
|
+
*
|
|
4965
|
+
* @remarks
|
|
4966
|
+
* Strips the stateful `g` / `y` flags so repeated checks are stable and the
|
|
4967
|
+
* caller's `lastIndex` never moves. The strip is an INDEXED character filter
|
|
4968
|
+
* rather than `String.prototype.replaceAll`, which is itself a caller-writable
|
|
4969
|
+
* member: a substitute answering `'i'` made `matchOf(/^abc$/)` accept `'ABC'` —
|
|
4970
|
+
* the package building a case-insensitive pattern the developer never wrote.
|
|
4971
|
+
*
|
|
4972
|
+
* @param pattern - The caller's regular expression
|
|
4973
|
+
* @returns An owned, stateless equivalent
|
|
4974
|
+
* @throws The exact value thrown when the pattern's source or flags cannot be read
|
|
4975
|
+
*
|
|
4976
|
+
* @example
|
|
4977
|
+
* ```ts
|
|
4978
|
+
* readPattern(/^a+$/gy) // /^a+$/
|
|
4979
|
+
* ```
|
|
4980
|
+
*/
|
|
4981
|
+
export declare function readPattern(pattern: RegExp): RegExp;
|
|
4982
|
+
|
|
4983
|
+
/**
|
|
4984
|
+
* Read a regular expression's flag text through the captured accessor.
|
|
4985
|
+
*
|
|
4986
|
+
* @param pattern - The candidate regular expression to read
|
|
4987
|
+
* @returns The pattern's flag text, or `undefined` when it cannot be read as a string
|
|
4988
|
+
* @throws The exact value the captured accessor throws for a receiver that is not a pattern
|
|
4989
|
+
*
|
|
4990
|
+
* @example
|
|
4991
|
+
* ```ts
|
|
4992
|
+
* readPatternFlags(/^a+$/giu) // 'giu'
|
|
4993
|
+
* ```
|
|
4994
|
+
*/
|
|
4995
|
+
export declare function readPatternFlags(pattern: unknown): string | undefined;
|
|
4996
|
+
|
|
4997
|
+
/**
|
|
4998
|
+
* Read a regular expression's source text through the captured accessor.
|
|
4999
|
+
*
|
|
5000
|
+
* @remarks
|
|
5001
|
+
* `RegExp.prototype.source` is an ACCESSOR on a shared prototype, so replacing
|
|
5002
|
+
* its getter changes what every pattern in the realm reports — not only the
|
|
5003
|
+
* caller's own. A getter answering `'.*'` made `compileSchema` publish
|
|
5004
|
+
* `pattern: ".*"` inside a frozen schema and made `isRegExp('x')` answer `true`.
|
|
5005
|
+
* Reading the descriptor per call, as an earlier round did, captures nothing:
|
|
5006
|
+
* capture is decided by WHEN the reference is taken.
|
|
5007
|
+
*
|
|
5008
|
+
* @param pattern - The candidate regular expression to read
|
|
5009
|
+
* @returns The pattern's source text, or `undefined` when it cannot be read as a string
|
|
5010
|
+
* @throws The exact value the captured accessor throws for a receiver that is not a pattern
|
|
5011
|
+
*
|
|
5012
|
+
* @example
|
|
5013
|
+
* ```ts
|
|
5014
|
+
* readPatternSource(/^a+$/) // '^a+$'
|
|
5015
|
+
* ```
|
|
5016
|
+
*/
|
|
5017
|
+
export declare function readPatternSource(pattern: unknown): string | undefined;
|
|
5018
|
+
|
|
5019
|
+
/**
|
|
5020
|
+
* Check that a value really is a {@link SampleMemo} before a walk stores a
|
|
5021
|
+
* published schema in it.
|
|
5022
|
+
*
|
|
5023
|
+
* @remarks
|
|
5024
|
+
* The memo is the one argument position on `inferSamples` /
|
|
5025
|
+
* `inferRecordSamples` that reaches a `WeakMap` and a `Map` the caller
|
|
5026
|
+
* supplied, and both doors are reachable untyped from JavaScript. Without this
|
|
5027
|
+
* check a wrong value there failed inside the traversal and was published as
|
|
5028
|
+
* `samples could not be read` — a true refusal naming the wrong argument, the
|
|
5029
|
+
* defect class those doors were already corrected for twice. It refuses under
|
|
5030
|
+
* the memo's own name and its own path instead.
|
|
5031
|
+
*
|
|
5032
|
+
* @param memo - The candidate memo
|
|
5033
|
+
* @param reader - The door name the refusal is published under
|
|
5034
|
+
* @returns The same memo when it carries a real `rows` `WeakMap` and `schemas` `Map`
|
|
5035
|
+
* @throws {ContractError} When the memo is not a `SampleMemo`
|
|
5036
|
+
*
|
|
5037
|
+
* @example
|
|
5038
|
+
* ```ts
|
|
5039
|
+
* readSampleMemo(buildSampleMemo(), 'inferSamples') // the same memo
|
|
5040
|
+
* ```
|
|
5041
|
+
*/
|
|
5042
|
+
export declare function readSampleMemo(memo: SampleMemo, reader: string): SampleMemo;
|
|
5043
|
+
|
|
5044
|
+
/**
|
|
5045
|
+
* Snapshot the genuine contents of a caller's `Set` without running an iterator.
|
|
5046
|
+
*
|
|
5047
|
+
* @remarks
|
|
5048
|
+
* `Set.prototype[Symbol.iterator]` is a caller-writable member, and every other
|
|
5049
|
+
* view of the same collection disagrees with a replaced one: an iterator that
|
|
5050
|
+
* silently skips the non-string in `new Set(['a', 42])` made `setOf(isString)`
|
|
5051
|
+
* answer `true` while `forEach` and `size` still reported the real contents. The
|
|
5052
|
+
* sibling `arrayOf` already read its caller's collection through captured
|
|
5053
|
+
* reflection, so the exclusion was not even self-consistent within one file.
|
|
5054
|
+
* `forEach` is the only complete view of `[[SetData]]` that runs no iterator, so
|
|
5055
|
+
* it is dispatched here from the captured table.
|
|
5056
|
+
*
|
|
5057
|
+
* @param value - The set whose genuine entries to snapshot
|
|
5058
|
+
* @returns A frozen entry snapshot, or a failure carrying the exact thrown value
|
|
5059
|
+
*
|
|
5060
|
+
* @example
|
|
5061
|
+
* ```ts
|
|
5062
|
+
* readSetEntries(new Set(['a', 42])) // { success: true, value: ['a', 42] }
|
|
5063
|
+
* ```
|
|
5064
|
+
*/
|
|
5065
|
+
export declare function readSetEntries(value: ReadonlySet<unknown>): Result<readonly unknown[]>;
|
|
5066
|
+
|
|
5067
|
+
/**
|
|
5068
|
+
* Read a value through the shared containment boundary or refuse it with the
|
|
5069
|
+
* contract module's uniform read diagnostic.
|
|
5070
|
+
*
|
|
5071
|
+
* @remarks
|
|
5072
|
+
* Unlike {@link attempt}, this is not an optional-result boundary: a caller
|
|
5073
|
+
* has committed to reading the supplied value, so a failed read cannot be
|
|
5074
|
+
* represented as absence or another permissive answer. Every reader using
|
|
5075
|
+
* this helper throws with the same `<reader>: <subject> could not be read`
|
|
5076
|
+
* message shape and retains the exact thrown value as its cause. Required
|
|
5077
|
+
* structural readers use the defaults; pattern readers supply `pattern` for
|
|
5078
|
+
* both the subject and code.
|
|
5079
|
+
*
|
|
5080
|
+
* @param callback - The read operation to perform
|
|
5081
|
+
* @param reader - The public reader name used in the diagnostic
|
|
5082
|
+
* @param options - Optional subject, code, and structured context
|
|
5083
|
+
* @returns The successfully read value
|
|
5084
|
+
* @throws {ContractError} When the read operation fails
|
|
5085
|
+
*
|
|
5086
|
+
* @example
|
|
5087
|
+
* ```ts
|
|
5088
|
+
* readValue(() => source.value, 'parseRecord')
|
|
5089
|
+
* ```
|
|
5090
|
+
*/
|
|
5091
|
+
export declare function readValue<T>(callback: () => T, reader: string, options?: ReadValueOptions): T;
|
|
5092
|
+
|
|
5093
|
+
/** Optional diagnostic metadata for a required read. */
|
|
5094
|
+
export declare interface ReadValueOptions {
|
|
5095
|
+
/** Argument/domain noun used in the refusal message. */
|
|
5096
|
+
readonly subject?: string;
|
|
5097
|
+
/** Machine-readable refusal category. */
|
|
5098
|
+
readonly code?: ContractCode;
|
|
5099
|
+
/** Structured location and domain details retained by the refusal. */
|
|
5100
|
+
readonly context?: ContractErrorContext;
|
|
5101
|
+
}
|
|
5102
|
+
|
|
3304
5103
|
export declare function recordOf<S extends GuardsShape>(shape: S): Guard<FromGuards<S>>;
|
|
3305
5104
|
|
|
3306
5105
|
export declare function recordOf<S extends GuardsShape, K extends ReadonlyArray<keyof S & string>>(shape: S, optional: K): Guard<OptionalFromGuards<S, K>>;
|
|
@@ -3319,6 +5118,7 @@ export declare function recordOf<S extends GuardsShape>(shape: S, optional: true
|
|
|
3319
5118
|
* @param values - The shape every value must match
|
|
3320
5119
|
* @param options - Optional `description`
|
|
3321
5120
|
* @returns An open object shape
|
|
5121
|
+
* @throws {ContractError} When `values` is absent at runtime
|
|
3322
5122
|
*
|
|
3323
5123
|
* @example
|
|
3324
5124
|
* ```ts
|
|
@@ -3332,16 +5132,55 @@ export declare interface RecordShapeOptions {
|
|
|
3332
5132
|
readonly description?: string;
|
|
3333
5133
|
}
|
|
3334
5134
|
|
|
5135
|
+
/**
|
|
5136
|
+
* Refuse a validated declaration whose compiled expansion exceeds
|
|
5137
|
+
* {@link COMPILE_NODE_LIMIT}.
|
|
5138
|
+
*
|
|
5139
|
+
* @remarks
|
|
5140
|
+
* The compilers' emitted-node bound, written once because two boundaries apply
|
|
5141
|
+
* it over a {@link ShapeValidatorInterface.expansion} count: the eager
|
|
5142
|
+
* {@link validateShapeDepth} function and the lazy {@link ContractCompiler}
|
|
5143
|
+
* preparation. The refusal keeps `validateShapeDepth`'s name because that gate
|
|
5144
|
+
* OWNS the rule and its exact diagnostic is public API — the same reason
|
|
5145
|
+
* `ShapeCloner` publishes the gate's depth wording rather than inventing a
|
|
5146
|
+
* second vocabulary for one rule. Two constructions of one refusal are two
|
|
5147
|
+
* messages waiting to drift apart.
|
|
5148
|
+
*
|
|
5149
|
+
* @param expansion - The node count one successful validation measured
|
|
5150
|
+
* @returns Nothing when the count is within the limit
|
|
5151
|
+
* @throws {ContractError} When the count exceeds {@link COMPILE_NODE_LIMIT}
|
|
5152
|
+
*
|
|
5153
|
+
* @example
|
|
5154
|
+
* ```ts
|
|
5155
|
+
* const validator = new ShapeValidator(shape)
|
|
5156
|
+
* validator.validate()
|
|
5157
|
+
* refuseExpansion(validator.expansion)
|
|
5158
|
+
* ```
|
|
5159
|
+
*/
|
|
5160
|
+
export declare function refuseExpansion(expansion: number): void;
|
|
5161
|
+
|
|
5162
|
+
/**
|
|
5163
|
+
* A compiled coercive-domain diagnostic — the shape of `compileReporter` bound
|
|
5164
|
+
* to one shape.
|
|
5165
|
+
*
|
|
5166
|
+
* @remarks
|
|
5167
|
+
* The counterpart of {@link AuditorFunction} for the wider preimage `parse`
|
|
5168
|
+
* maps into the domain, with the same optional root-path prefix and the same
|
|
5169
|
+
* assignability to {@link ContractInterface.explain}.
|
|
5170
|
+
*/
|
|
5171
|
+
export declare type ReporterFunction = (value: unknown, path?: readonly string[]) => readonly Fault[];
|
|
5172
|
+
|
|
3335
5173
|
/**
|
|
3336
5174
|
* Resolve a (possibly nested) field value from a record by a key or key path.
|
|
3337
5175
|
*
|
|
3338
5176
|
* @remarks
|
|
3339
5177
|
* A single `string` is ONE key (never split on `.`, so dotted keys are safe); a
|
|
3340
|
-
* string array descends left-to-right through nested objects.
|
|
3341
|
-
*
|
|
3342
|
-
*
|
|
3343
|
-
*
|
|
3344
|
-
*
|
|
5178
|
+
* string array descends left-to-right through own properties of nested objects.
|
|
5179
|
+
* The root must satisfy {@link isRecord}; inherited properties are never fields.
|
|
5180
|
+
* Intermediates may be objects or arrays indexed by string. Returns `undefined`
|
|
5181
|
+
* the moment a segment is missing or lands on a non-object, so the lookup is
|
|
5182
|
+
* total — even against a hostile getter or Proxy trap that throws on read,
|
|
5183
|
+
* contained via {@link attempt} so the throw never escapes.
|
|
3345
5184
|
*
|
|
3346
5185
|
* @param record - The source record
|
|
3347
5186
|
* @param path - A property key, or a key path descending into nested objects
|
|
@@ -3356,8 +5195,70 @@ export declare interface RecordShapeOptions {
|
|
|
3356
5195
|
*/
|
|
3357
5196
|
export declare function resolveField(record: Readonly<Record<string, unknown>>, path: FieldPath): unknown;
|
|
3358
5197
|
|
|
3359
|
-
/**
|
|
3360
|
-
|
|
5198
|
+
/**
|
|
5199
|
+
* Discriminated union for operations that can succeed or fail without throwing.
|
|
5200
|
+
*
|
|
5201
|
+
* @remarks
|
|
5202
|
+
* The failure channel defaults to `unknown`. Operations with a guaranteed
|
|
5203
|
+
* domain error name that error type explicitly.
|
|
5204
|
+
*/
|
|
5205
|
+
export declare type Result<T, E = unknown> = Success<T> | Failure<E>;
|
|
5206
|
+
|
|
5207
|
+
/**
|
|
5208
|
+
* Record one node's answer at one remaining-depth allowance in a shared memo.
|
|
5209
|
+
*
|
|
5210
|
+
* @remarks
|
|
5211
|
+
* A depth-bounded walk answers the same node differently depending on how much
|
|
5212
|
+
* allowance was left when it arrived, so each node keeps a `Map` of answers
|
|
5213
|
+
* inside one `WeakMap`. Written inline, the get-or-create ferried that map out
|
|
5214
|
+
* of the branch that built it through a `let`, three times over in
|
|
5215
|
+
* `inferArray`, `inferObject` and `schemaNodeToShape`. Written once it is one
|
|
5216
|
+
* statement per caller, and the captured `WeakMap`/`Map` members stay the only
|
|
5217
|
+
* ones all three dispatch through — a substituted `WeakMap.prototype.get` that
|
|
5218
|
+
* answered a decoy map would otherwise decide what a later call replays.
|
|
5219
|
+
*
|
|
5220
|
+
* @param memo - The per-node depth memo this walk owns
|
|
5221
|
+
* @param node - The source node the answer was computed for
|
|
5222
|
+
* @param depth - The remaining-depth allowance the answer was computed under
|
|
5223
|
+
* @param answer - The answer to record for that node at that allowance
|
|
5224
|
+
*
|
|
5225
|
+
* @example
|
|
5226
|
+
* ```ts
|
|
5227
|
+
* const memo = new WeakMap<object, Map<number, string>>()
|
|
5228
|
+
* retainDepth(memo, node, 8, 'answer')
|
|
5229
|
+
* memo.get(node)?.get(8) // 'answer'
|
|
5230
|
+
* ```
|
|
5231
|
+
*/
|
|
5232
|
+
export declare function retainDepth<T>(memo: WeakMap<object, Map<number, T>>, node: object, depth: number, answer: T): void;
|
|
5233
|
+
|
|
5234
|
+
/**
|
|
5235
|
+
* The per-walk memo {@link inferSamples} and {@link inferRecordSamples} share,
|
|
5236
|
+
* keyed by the ORDERED identities of the rows a slot collected.
|
|
5237
|
+
*
|
|
5238
|
+
* @remarks
|
|
5239
|
+
* `rows` is one step of a prefix chain: following the slot's rows in order
|
|
5240
|
+
* lands on the node that owns that exact row list, so two slots collecting the
|
|
5241
|
+
* same rows in the same order share one entry and two slots collecting
|
|
5242
|
+
* different rows never do. `schemas` holds that row list's already-inferred
|
|
5243
|
+
* results, keyed by EVERY budget and flag the emitted schema depends on
|
|
5244
|
+
* (remaining depth, breadth, `closed`, `format`, `enum`) — so the memo can
|
|
5245
|
+
* only ever return the schema a fresh call would have produced, and two
|
|
5246
|
+
* ordinary calls that differ in one flag cannot be served each other's answer.
|
|
5247
|
+
*
|
|
5248
|
+
* Build one with {@link buildSampleMemo} and give it to ONE walk. It is
|
|
5249
|
+
* traversal state, not a cache to keep: nothing is invalidated when a sample
|
|
5250
|
+
* row is later mutated.
|
|
5251
|
+
*
|
|
5252
|
+
* @example
|
|
5253
|
+
* ```ts
|
|
5254
|
+
* const memo = buildSampleMemo()
|
|
5255
|
+
* inferSamples([{ id: 1 }], 32, 256, true, false, false, memo)
|
|
5256
|
+
* ```
|
|
5257
|
+
*/
|
|
5258
|
+
export declare interface SampleMemo {
|
|
5259
|
+
readonly rows: WeakMap<object, SampleMemo>;
|
|
5260
|
+
readonly schemas: Map<string, JSONSchema>;
|
|
5261
|
+
}
|
|
3361
5262
|
|
|
3362
5263
|
/**
|
|
3363
5264
|
* Classify a list of sample values against the {@link SchemaFormat}
|
|
@@ -3406,6 +5307,7 @@ export declare function samplesToFormat(values: readonly unknown[]): SchemaForma
|
|
|
3406
5307
|
* @param samples - The example values to infer a schema from
|
|
3407
5308
|
* @param options - Optional `maxDepth` / `maxProperties` / `closed` / `format` / `enum` bounds
|
|
3408
5309
|
* @returns The inferred `JSONSchema`
|
|
5310
|
+
* @throws {ContractError} When the samples or options cannot be read
|
|
3409
5311
|
*
|
|
3410
5312
|
* @example
|
|
3411
5313
|
* ```ts
|
|
@@ -3419,7 +5321,7 @@ export declare function samplesToSchema(samples: readonly unknown[], options?: V
|
|
|
3419
5321
|
|
|
3420
5322
|
/**
|
|
3421
5323
|
* Sanitize a user-supplied inference budget (`maxDepth` / `maxProperties`) to
|
|
3422
|
-
* a finite non-negative integer,
|
|
5324
|
+
* a finite non-negative integer, selecting a valid fallback for anything else.
|
|
3423
5325
|
*
|
|
3424
5326
|
* @remarks
|
|
3425
5327
|
* Guards {@link valueToSchema} / {@link samplesToSchema} against a hostile or
|
|
@@ -3429,13 +5331,17 @@ export declare function samplesToSchema(samples: readonly unknown[], options?: V
|
|
|
3429
5331
|
* instead of capping the list (a fractional value has a similarly undefined
|
|
3430
5332
|
* `slice` bound). `Infinity` is rejected too — `Number.isInteger(Infinity)`
|
|
3431
5333
|
* is `false` — since an unbounded budget is exactly the adversarial case the
|
|
3432
|
-
* caps exist to prevent. A valid
|
|
3433
|
-
*
|
|
5334
|
+
* caps exist to prevent. A valid candidate passes through unchanged without
|
|
5335
|
+
* inspecting the fallback. When the candidate is invalid, the fallback must
|
|
5336
|
+
* satisfy the same finite non-negative-integer contract or this boundary
|
|
5337
|
+
* refuses it with a coded error instead of returning an invalid budget.
|
|
3434
5338
|
*
|
|
3435
5339
|
* @param value - The candidate budget value
|
|
3436
|
-
* @param fallback - The default to
|
|
3437
|
-
*
|
|
5340
|
+
* @param fallback - The default to select when `value` is not a finite
|
|
5341
|
+
* non-negative integer
|
|
3438
5342
|
* @returns A finite non-negative integer budget
|
|
5343
|
+
* @throws {ContractError} When the selected fallback is not a finite
|
|
5344
|
+
* non-negative integer
|
|
3439
5345
|
*
|
|
3440
5346
|
* @example
|
|
3441
5347
|
* ```ts
|
|
@@ -3446,6 +5352,61 @@ export declare function samplesToSchema(samples: readonly unknown[], options?: V
|
|
|
3446
5352
|
*/
|
|
3447
5353
|
export declare function sanitizeBudget(value: number | undefined, fallback: number): number;
|
|
3448
5354
|
|
|
5355
|
+
/**
|
|
5356
|
+
* Stateful owner of one JSON Schema snapshot operation.
|
|
5357
|
+
*
|
|
5358
|
+
* @remarks
|
|
5359
|
+
* Construction retains the source without observing it. The first
|
|
5360
|
+
* {@link clone} call performs one iterative identity-memoized walk and settles
|
|
5361
|
+
* permanently. Success replays the exact frozen root; failure rethrows the
|
|
5362
|
+
* exact class-owned error. Nonredirectable terminal settlement releases
|
|
5363
|
+
* populated traversal state before publishing that result, while retaining
|
|
5364
|
+
* the source and exact result afterward. Reentry poisons the active operation
|
|
5365
|
+
* and every later call with one shared cause-free error.
|
|
5366
|
+
*
|
|
5367
|
+
* @param schema - The JSON Schema graph to retain for cloning
|
|
5368
|
+
*
|
|
5369
|
+
* @example
|
|
5370
|
+
* ```ts
|
|
5371
|
+
* const child = { type: 'string' }
|
|
5372
|
+
* const cloner = new SchemaCloner({ anyOf: [child, child] })
|
|
5373
|
+
* const clone = cloner.clone()
|
|
5374
|
+
* clone.anyOf?.[0] === clone.anyOf?.[1] // true
|
|
5375
|
+
* cloner.clone() === clone // true
|
|
5376
|
+
* ```
|
|
5377
|
+
*/
|
|
5378
|
+
export declare class SchemaCloner implements SchemaClonerInterface {
|
|
5379
|
+
#private;
|
|
5380
|
+
constructor(schema: JSONSchema);
|
|
5381
|
+
/**
|
|
5382
|
+
* Clone the retained schema into an identity-preserving frozen graph.
|
|
5383
|
+
*
|
|
5384
|
+
* @returns The settled JSON Schema snapshot
|
|
5385
|
+
* @throws {ContractError} When traversal is unreadable or cloning is reentered
|
|
5386
|
+
*/
|
|
5387
|
+
clone(): JSONSchema;
|
|
5388
|
+
}
|
|
5389
|
+
|
|
5390
|
+
/**
|
|
5391
|
+
* Stateful owner of one JSON Schema snapshot operation.
|
|
5392
|
+
*
|
|
5393
|
+
* @remarks
|
|
5394
|
+
* Construction retains the schema without observing it. The first
|
|
5395
|
+
* {@link clone} call settles once; later calls replay the exact same frozen
|
|
5396
|
+
* schema or exact same owned {@link ContractError}. Nonredirectable terminal
|
|
5397
|
+
* settlement releases populated traversal state before publishing that exact
|
|
5398
|
+
* result, while retaining the source and result afterward.
|
|
5399
|
+
*/
|
|
5400
|
+
export declare interface SchemaClonerInterface {
|
|
5401
|
+
/**
|
|
5402
|
+
* Clone the retained schema into a deeply frozen identity-preserving graph.
|
|
5403
|
+
*
|
|
5404
|
+
* @returns The settled JSON Schema snapshot
|
|
5405
|
+
* @throws {ContractError} When traversal is unreadable or cloning is reentered
|
|
5406
|
+
*/
|
|
5407
|
+
clone(): JSONSchema;
|
|
5408
|
+
}
|
|
5409
|
+
|
|
3449
5410
|
/**
|
|
3450
5411
|
* The closed set of string formats {@link stringToFormat} recognizes.
|
|
3451
5412
|
*
|
|
@@ -3465,15 +5426,15 @@ export declare type SchemaFormat = 'date-time' | 'date' | 'time' | 'uuid' | 'ema
|
|
|
3465
5426
|
* `oneOf` / `anyOf` variants).
|
|
3466
5427
|
*
|
|
3467
5428
|
* @remarks
|
|
3468
|
-
*
|
|
3469
|
-
*
|
|
3470
|
-
*
|
|
5429
|
+
* Guards depth exhaustion, a readable non-record node (the node's static
|
|
5430
|
+
* `JSONSchema` type is not trusted at runtime), and a cyclic re-encounter of
|
|
5431
|
+
* `schema` — all three widen to {@link rawShape}. The
|
|
3471
5432
|
* ancestor set is added to and removed from around the WHOLE subtree
|
|
3472
5433
|
* conversion (not permanently), so a DAG-shaped schema reached twice via two
|
|
3473
5434
|
* different, non-cyclic paths does not false-positive as a cycle. The
|
|
3474
|
-
* subtree conversion itself runs
|
|
3475
|
-
* throwing getter/Proxy anywhere in `schema`
|
|
3476
|
-
*
|
|
5435
|
+
* subtree conversion itself runs through {@link readValue}, so a hostile
|
|
5436
|
+
* throwing getter/Proxy anywhere in `schema` raises a coded read refusal
|
|
5437
|
+
* instead of degrading to an accept-anything shape. A same-node re-conversion at
|
|
3477
5438
|
* the same remaining `depth` is served from `memo` (guards a
|
|
3478
5439
|
* shared-reference schema DAG against exponential blowup), mirroring
|
|
3479
5440
|
* {@link inferObject} / {@link inferArray} (inferers.ts).
|
|
@@ -3488,7 +5449,8 @@ export declare type SchemaFormat = 'date-time' | 'date' | 'time' | 'uuid' | 'ema
|
|
|
3488
5449
|
* recursion state owned by the {@link schemaToShape} entry point; passing
|
|
3489
5450
|
* a shared or pre-populated `WeakMap` changes caching behavior and is not
|
|
3490
5451
|
* supported usage
|
|
3491
|
-
* @returns The built shape for `schema`, or {@link rawShape}
|
|
5452
|
+
* @returns The built shape for `schema`, or {@link rawShape} for readable widening cases
|
|
5453
|
+
* @throws {ContractError} When schema traversal fails
|
|
3492
5454
|
*
|
|
3493
5455
|
* @example
|
|
3494
5456
|
* ```ts
|
|
@@ -3504,7 +5466,7 @@ export declare function schemaNodeToShape(schema: JSONSchema, depth: number, vis
|
|
|
3504
5466
|
* as an MCP-compatible `inputSchema`.
|
|
3505
5467
|
*
|
|
3506
5468
|
* @remarks
|
|
3507
|
-
*
|
|
5469
|
+
* Deterministic for readable input. `schema.type === 'object'` passes through
|
|
3508
5470
|
* unchanged; every other root (a primitive/array `type`, an `anyOf`/`enum`-only
|
|
3509
5471
|
* schema with no `type`, or the empty `{}`) is wrapped as a single required
|
|
3510
5472
|
* `value` property: `{ type: 'object', properties: { value: schema },
|
|
@@ -3513,6 +5475,7 @@ export declare function schemaNodeToShape(schema: JSONSchema, depth: number, vis
|
|
|
3513
5475
|
*
|
|
3514
5476
|
* @param schema - The schema to wrap
|
|
3515
5477
|
* @returns `schema` unchanged when object-rooted, otherwise the wrapped object schema
|
|
5478
|
+
* @throws {ContractError} When the schema root cannot be read
|
|
3516
5479
|
*
|
|
3517
5480
|
* @example
|
|
3518
5481
|
* ```ts
|
|
@@ -3539,10 +5502,11 @@ export declare function schemaToObject(schema: JSONSchema): JSONSchema;
|
|
|
3539
5502
|
*
|
|
3540
5503
|
* @param schema - The compiled JSON Schema (a contract's `schema`)
|
|
3541
5504
|
* @returns The schema as the open tool-parameters record, or `undefined` when it is not a record
|
|
5505
|
+
* @throws {ContractError} When the schema cannot be read
|
|
3542
5506
|
*
|
|
3543
5507
|
* @example
|
|
3544
5508
|
* ```ts
|
|
3545
|
-
* import { createContract, schemaToParameters } from '@
|
|
5509
|
+
* import { createContract, schemaToParameters } from '@orkestrel/contract'
|
|
3546
5510
|
*
|
|
3547
5511
|
* const contract = createContract(shape)
|
|
3548
5512
|
* const parameters = schemaToParameters(contract.schema) // the open record a tool advertises
|
|
@@ -3552,14 +5516,15 @@ export declare function schemaToParameters(schema: JSONSchema): Readonly<Record<
|
|
|
3552
5516
|
|
|
3553
5517
|
/**
|
|
3554
5518
|
* Convert a runtime `JSONSchema` value into a validating {@link ContractShape}
|
|
3555
|
-
* — the inverse of {@link compileSchema}
|
|
3556
|
-
*
|
|
5519
|
+
* — the inverse of {@link compileSchema}. Unlike direct {@link rawShape}
|
|
5520
|
+
* construction, which rejects malformed supported-vocabulary keywords, this
|
|
5521
|
+
* conversion is total and widens an inexpressible input to a valid raw `{}`.
|
|
3557
5522
|
*
|
|
3558
5523
|
* @remarks
|
|
3559
|
-
*
|
|
3560
|
-
*
|
|
3561
|
-
*
|
|
3562
|
-
*
|
|
5524
|
+
* Readable malformed, cyclic, or deeply nested schema nodes widen to
|
|
5525
|
+
* {@link rawShape}; a failed traversal raises the shared coded refusal because
|
|
5526
|
+
* an unreadable value is not a schema. `createContract(schemaToShape(x))`
|
|
5527
|
+
* therefore remains safe for every readable `x`. See
|
|
3563
5528
|
* {@link buildShapeFromNode} for the exact per-keyword precedence.
|
|
3564
5529
|
*
|
|
3565
5530
|
* `format` and `pattern` are NEVER asserted by the compiled shape — `format`
|
|
@@ -3571,12 +5536,16 @@ export declare function schemaToParameters(schema: JSONSchema): Readonly<Record<
|
|
|
3571
5536
|
* cyclic re-encounter — widens to {@link rawShape} (accept any defined
|
|
3572
5537
|
* value), never narrows.
|
|
3573
5538
|
*
|
|
3574
|
-
* ROUND TRIP:
|
|
3575
|
-
*
|
|
3576
|
-
*
|
|
3577
|
-
*
|
|
3578
|
-
*
|
|
3579
|
-
*
|
|
5539
|
+
* ROUND TRIP: for every readable `v`,
|
|
5540
|
+
* `compileGuard(schemaToShape(valueToSchema(v)))(v)` is `true` — including
|
|
5541
|
+
* values no JSON Schema keyword describes (`NaN`, `±Infinity`, a `Map`, a
|
|
5542
|
+
* `Set`, a class instance, a function, a symbol, a bigint, and readable cyclic
|
|
5543
|
+
* hosts), which infer `{}` and widen back to an accept-anything
|
|
5544
|
+
* {@link rawShape}. An unreadable host is refused by {@link valueToSchema}
|
|
5545
|
+
* before this law produces a schema; direct hostile input to
|
|
5546
|
+
* {@link schemaToShape} receives the same coded refusal.
|
|
5547
|
+
* Widening is the only source of looseness. The law has three explicit host
|
|
5548
|
+
* limits:
|
|
3580
5549
|
*
|
|
3581
5550
|
* - **Absence.** `undefined` is not a value: no compiled guard accepts it
|
|
3582
5551
|
* (`rawShape` reserves it as the parser failure sentinel). So `undefined`
|
|
@@ -3601,6 +5570,7 @@ export declare function schemaToParameters(schema: JSONSchema): Readonly<Record<
|
|
|
3601
5570
|
*
|
|
3602
5571
|
* @param schema - The JSON Schema value to convert
|
|
3603
5572
|
* @returns The built {@link ContractShape}
|
|
5573
|
+
* @throws {ContractError} When schema traversal fails
|
|
3604
5574
|
*
|
|
3605
5575
|
* @example
|
|
3606
5576
|
* ```ts
|
|
@@ -3632,6 +5602,55 @@ export declare function schemaToShape(schema: JSONSchema): ContractShape;
|
|
|
3632
5602
|
*/
|
|
3633
5603
|
export declare function seededRandom(seed: number): RandomFunction;
|
|
3634
5604
|
|
|
5605
|
+
/**
|
|
5606
|
+
* A compiled seed-data source — the shape of `compileGenerator` bound to one
|
|
5607
|
+
* shape.
|
|
5608
|
+
*
|
|
5609
|
+
* @remarks
|
|
5610
|
+
* An absent `random` selects the invocation's own wall-clock-seeded source, so
|
|
5611
|
+
* a generator retains no randomness between calls.
|
|
5612
|
+
*
|
|
5613
|
+
* @remarks
|
|
5614
|
+
* Named for the SEED DATA it produces rather than for the `generator` getter it
|
|
5615
|
+
* serves, which is the one place this package's three compiled-diagnostic types
|
|
5616
|
+
* break their own symmetry with {@link AuditorFunction} and
|
|
5617
|
+
* {@link ReporterFunction}. `GeneratorFunction` is already taken twice over: it
|
|
5618
|
+
* is a realm global (the constructor every `function*` reports), and it is the
|
|
5619
|
+
* exact string {@link isGeneratorFunction} compares against. Publishing a type
|
|
5620
|
+
* of that name beside that guard would put two contradictory meanings of one
|
|
5621
|
+
* word in one barrel, where `isGeneratorFunction(contract.generate)` answers
|
|
5622
|
+
* `false` for a value the types call a `GeneratorFunction`.
|
|
5623
|
+
*/
|
|
5624
|
+
export declare type SeederFunction<T> = (random?: RandomFunction) => T;
|
|
5625
|
+
|
|
5626
|
+
/**
|
|
5627
|
+
* Select the report of the variant that came closest to matching.
|
|
5628
|
+
*
|
|
5629
|
+
* @remarks
|
|
5630
|
+
* The union summary both diagnostic doors append their closest variant's faults
|
|
5631
|
+
* to. "Closest" is the SHORTEST report, and an earlier variant wins a tie, so a
|
|
5632
|
+
* union's diagnostic follows declaration order rather than whichever variant a
|
|
5633
|
+
* later comparison happened to visit. The winning report is returned BY
|
|
5634
|
+
* IDENTITY, never copied, so the summary carries the exact fault objects the
|
|
5635
|
+
* variant produced. No report at all — a union whose every variant slot was
|
|
5636
|
+
* unreadable — yields a frozen empty collection rather than `undefined`, so the
|
|
5637
|
+
* caller appends nothing instead of branching.
|
|
5638
|
+
*
|
|
5639
|
+
* The scan is an indexed read of arrays this package built, so neither the
|
|
5640
|
+
* choice of variant nor the length comparison dispatches through a member a
|
|
5641
|
+
* caller can replace.
|
|
5642
|
+
*
|
|
5643
|
+
* @param reports - One report per variant, in declaration order
|
|
5644
|
+
* @returns The first shortest report, or a frozen empty collection when there are none
|
|
5645
|
+
*
|
|
5646
|
+
* @example
|
|
5647
|
+
* ```ts
|
|
5648
|
+
* selectClosestFaults([[fault, fault], [fault]]) // the second report
|
|
5649
|
+
* selectClosestFaults([]) // []
|
|
5650
|
+
* ```
|
|
5651
|
+
*/
|
|
5652
|
+
export declare function selectClosestFaults<T extends AuditFault>(reports: readonly (readonly T[])[]): readonly T[];
|
|
5653
|
+
|
|
3635
5654
|
/**
|
|
3636
5655
|
* Build a guard that accepts `Set` instances whose every element satisfies
|
|
3637
5656
|
* `elementGuard`.
|
|
@@ -3647,18 +5666,99 @@ export declare function setOf<T>(elementGuard: Guard<T>): Guard<ReadonlySet<T>>;
|
|
|
3647
5666
|
|
|
3648
5667
|
export declare function setOf(elementGuard: (value: unknown) => boolean): Guard<ReadonlySet<unknown>>;
|
|
3649
5668
|
|
|
5669
|
+
/**
|
|
5670
|
+
* Stateful owner of one contract-shape snapshot operation.
|
|
5671
|
+
*
|
|
5672
|
+
* @remarks
|
|
5673
|
+
* Construction retains the source without observing it. The first
|
|
5674
|
+
* {@link clone} call captures and validates one owned graph and settles
|
|
5675
|
+
* permanently. Success replays the exact frozen root; failure rethrows the
|
|
5676
|
+
* exact class-owned or directly adopted error. Nonredirectable settlement
|
|
5677
|
+
* releases populated traversal state before publishing that result. Reentry
|
|
5678
|
+
* poisons the active operation and every later call with one shared cause-free
|
|
5679
|
+
* error.
|
|
5680
|
+
*
|
|
5681
|
+
* @param shape - The contract-shape graph to retain for cloning
|
|
5682
|
+
*
|
|
5683
|
+
* @example
|
|
5684
|
+
* ```ts
|
|
5685
|
+
* const cloner = new ShapeCloner({ type: 'string', min: 1 })
|
|
5686
|
+
* const clone = cloner.clone()
|
|
5687
|
+
* cloner.clone() === clone // true
|
|
5688
|
+
* ```
|
|
5689
|
+
*/
|
|
5690
|
+
export declare class ShapeCloner implements ShapeClonerInterface {
|
|
5691
|
+
#private;
|
|
5692
|
+
constructor(shape: ContractShape);
|
|
5693
|
+
/**
|
|
5694
|
+
* Clone the retained declaration into an owned, validated frozen graph.
|
|
5695
|
+
*
|
|
5696
|
+
* @returns The settled contract-shape snapshot
|
|
5697
|
+
* @throws {ContractError} When the declaration is malformed, unreadable, cyclic, too deep, or cloning is reentered
|
|
5698
|
+
*/
|
|
5699
|
+
clone(): ContractShape;
|
|
5700
|
+
}
|
|
5701
|
+
|
|
5702
|
+
export declare interface ShapeClonerInterface {
|
|
5703
|
+
/**
|
|
5704
|
+
* Clone the retained shape into a deeply frozen identity-preserving graph.
|
|
5705
|
+
*
|
|
5706
|
+
* @returns The settled contract-shape snapshot
|
|
5707
|
+
* @throws {ContractError} When the declaration is malformed, unreadable, cyclic, too deep, or cloning is reentered
|
|
5708
|
+
*/
|
|
5709
|
+
clone(): ContractShape;
|
|
5710
|
+
}
|
|
5711
|
+
|
|
5712
|
+
/**
|
|
5713
|
+
* Stateful owner of one contract-shape snapshot operation.
|
|
5714
|
+
*
|
|
5715
|
+
* @remarks
|
|
5716
|
+
* Construction retains the shape without observing it. The first
|
|
5717
|
+
* {@link clone} call settles once; later calls replay the exact same frozen
|
|
5718
|
+
* shape or exact same owned {@link ContractError}. Nonredirectable terminal
|
|
5719
|
+
* settlement releases populated traversal state before publishing that exact
|
|
5720
|
+
* result, while retaining the source and result afterward.
|
|
5721
|
+
* Reentry permanently poisons the active operation with one shared error.
|
|
5722
|
+
*/
|
|
5723
|
+
/**
|
|
5724
|
+
* One captured property of an object shape, held as an ordered entry rather
|
|
5725
|
+
* than as a `Map` pair.
|
|
5726
|
+
*
|
|
5727
|
+
* @remarks
|
|
5728
|
+
* The published property population of a cloned object shape is decided by
|
|
5729
|
+
* walking these entries. A `Map` would be the natural carrier and the wrong
|
|
5730
|
+
* one: iterating it dispatches through `Map.prototype[Symbol.iterator]` and
|
|
5731
|
+
* destructuring each pair through `Array.prototype[Symbol.iterator]`, both
|
|
5732
|
+
* caller-writable, and an arity-preserving substitution there renames a
|
|
5733
|
+
* property inside a snapshot the package publishes as exact.
|
|
5734
|
+
*/
|
|
5735
|
+
export declare interface ShapeProperty {
|
|
5736
|
+
/** The own enumerable key the property was captured under. */
|
|
5737
|
+
readonly key: string;
|
|
5738
|
+
/** The captured child shape, absent when the declaration held no shape. */
|
|
5739
|
+
readonly child: ContractShape | undefined;
|
|
5740
|
+
}
|
|
5741
|
+
|
|
3650
5742
|
/**
|
|
3651
5743
|
* Project a {@link ContractShape} to the {@link FaultKind} it describes.
|
|
3652
5744
|
*
|
|
3653
5745
|
* @remarks
|
|
3654
5746
|
* A structural mapping used by {@link compileReporter} to fill a `Fault`'s
|
|
3655
|
-
* `expected` field
|
|
5747
|
+
* `expected` field and by {@link compileAuditor} to fill an `AuditFault`'s:
|
|
5748
|
+
* most shapes map to their own `type` (`numberShape` maps to
|
|
3656
5749
|
* `'integer'` when `integer: true`, else `'number'`); `optionalShape` /
|
|
3657
5750
|
* `nullableShape` project through to their inner shape's kind, and `rawShape`
|
|
3658
5751
|
* (an arbitrary embedded schema with no fixed kind) projects to `'json'`.
|
|
3659
5752
|
*
|
|
5753
|
+
* A hand-authored node carrying an unrecognized discriminant is REFUSED rather
|
|
5754
|
+
* than answered out of type. The switch used to fall off its end and return
|
|
5755
|
+
* `undefined` for such a node, which made the declared non-optional
|
|
5756
|
+
* {@link FaultKind} return type a lie at a public export; every other door in
|
|
5757
|
+
* this module refuses out-of-domain input, so this one does too.
|
|
5758
|
+
*
|
|
3660
5759
|
* @param shape - The shape to project
|
|
3661
5760
|
* @returns The shape's {@link FaultKind}
|
|
5761
|
+
* @throws {ContractError} When the node carries no recognized shape discriminant
|
|
3662
5762
|
*
|
|
3663
5763
|
* @example
|
|
3664
5764
|
* ```ts
|
|
@@ -3669,6 +5769,106 @@ export declare function setOf(elementGuard: (value: unknown) => boolean): Guard<
|
|
|
3669
5769
|
*/
|
|
3670
5770
|
export declare function shapeToKind(shape: ContractShape): FaultKind;
|
|
3671
5771
|
|
|
5772
|
+
/**
|
|
5773
|
+
* A reusable live validator for one retained contract-shape source.
|
|
5774
|
+
*
|
|
5775
|
+
* @remarks
|
|
5776
|
+
* Construction performs no source observation. Every non-overlapping
|
|
5777
|
+
* {@link validate} call starts a fresh pass over the source's current state.
|
|
5778
|
+
* Reentry poisons the active pass with one shared cause-free structure error;
|
|
5779
|
+
* cleanup always restores an idle validator for the next independent call.
|
|
5780
|
+
*
|
|
5781
|
+
* @example
|
|
5782
|
+
* ```ts
|
|
5783
|
+
* const validator = new ShapeValidator({ type: 'string', min: 1 })
|
|
5784
|
+
* validator.validate()
|
|
5785
|
+
* ```
|
|
5786
|
+
*/
|
|
5787
|
+
export declare class ShapeValidator implements ShapeValidatorInterface {
|
|
5788
|
+
#private;
|
|
5789
|
+
/**
|
|
5790
|
+
* Retain a shape source without observing it.
|
|
5791
|
+
*
|
|
5792
|
+
* @param shape - The live shape source validated by each call
|
|
5793
|
+
*/
|
|
5794
|
+
constructor(shape: ContractShape);
|
|
5795
|
+
/**
|
|
5796
|
+
* The number of nodes the last successful {@link validate} found the source
|
|
5797
|
+
* expands into.
|
|
5798
|
+
*
|
|
5799
|
+
* @remarks
|
|
5800
|
+
* One per node per INCOMING EDGE, summed bottom-up — the size of the tree a
|
|
5801
|
+
* compiler would build from this DAG, which a node count of the declaration
|
|
5802
|
+
* itself does not describe. `0` before the first successful pass and after a
|
|
5803
|
+
* failed one, because a failed pass has no expansion to report.
|
|
5804
|
+
*/
|
|
5805
|
+
get expansion(): number;
|
|
5806
|
+
/**
|
|
5807
|
+
* Validate the retained source's current declaration.
|
|
5808
|
+
*
|
|
5809
|
+
* @remarks
|
|
5810
|
+
* The whole traversal is contained, and a failure this class did not author
|
|
5811
|
+
* is translated into `validateShapeDepth: shape reflection failed` carrying
|
|
5812
|
+
* the exact thrown value. Containment is what makes that claim hold for
|
|
5813
|
+
* dispatch nobody enumerated — including the recognition of an authored error,
|
|
5814
|
+
* which is why that recognition is no longer answerable by any member a caller
|
|
5815
|
+
* can write. The sentence this replaces asserted the same conclusion from the
|
|
5816
|
+
* absence of reachable dispatch, and the absence was not established.
|
|
5817
|
+
*
|
|
5818
|
+
* @returns Nothing when the declaration is valid
|
|
5819
|
+
* @throws {ContractError} When the declaration is malformed, cyclic, too deep, unreadable, or reentered
|
|
5820
|
+
*/
|
|
5821
|
+
validate(): void;
|
|
5822
|
+
}
|
|
5823
|
+
|
|
5824
|
+
/**
|
|
5825
|
+
* Validates one retained contract-shape source on demand.
|
|
5826
|
+
*
|
|
5827
|
+
* @remarks
|
|
5828
|
+
* Construction does not observe the source. Every {@link validate} call is an
|
|
5829
|
+
* independent live validation pass over its current state.
|
|
5830
|
+
*/
|
|
5831
|
+
export declare interface ShapeValidatorInterface {
|
|
5832
|
+
/**
|
|
5833
|
+
* The number of nodes the last successful {@link validate} found the retained
|
|
5834
|
+
* declaration expands into, counting one per node per incoming edge — the
|
|
5835
|
+
* size of the TREE every compiled artifact would build from this DAG. `0`
|
|
5836
|
+
* before the first successful pass and after a failed one.
|
|
5837
|
+
*/
|
|
5838
|
+
readonly expansion: number;
|
|
5839
|
+
/**
|
|
5840
|
+
* Validate the retained shape declaration.
|
|
5841
|
+
*
|
|
5842
|
+
* @returns Nothing when the declaration is valid
|
|
5843
|
+
* @throws {ContractError} When the declaration is malformed, cyclic, or too deep
|
|
5844
|
+
*/
|
|
5845
|
+
validate(): void;
|
|
5846
|
+
}
|
|
5847
|
+
|
|
5848
|
+
/**
|
|
5849
|
+
* Order primitive keys or indices deterministically, on an owned copy, through
|
|
5850
|
+
* the captured sort.
|
|
5851
|
+
*
|
|
5852
|
+
* @remarks
|
|
5853
|
+
* Every schema this package emits is ordered so the same input produces the
|
|
5854
|
+
* same bytes, and `Array.prototype.sort` is a caller-writable member on every
|
|
5855
|
+
* one of those paths: a substitute that empties its receiver made
|
|
5856
|
+
* `valueToSchema({ b: 1, a: 2 })` publish `{"type":"object","additionalProperties":false}`
|
|
5857
|
+
* — a successful answer with the caller's properties silently gone. The copy is
|
|
5858
|
+
* taken first so a caller-owned array is never reordered in place, and the order
|
|
5859
|
+
* is decided by {@link compareValues}, whose `<` / `>` comparison dispatches
|
|
5860
|
+
* through nothing.
|
|
5861
|
+
*
|
|
5862
|
+
* @param values - The keys or indices to order
|
|
5863
|
+
* @returns A fresh array in ascending order
|
|
5864
|
+
*
|
|
5865
|
+
* @example
|
|
5866
|
+
* ```ts
|
|
5867
|
+
* sortValues(['b', 'a']) // ['a', 'b']
|
|
5868
|
+
* ```
|
|
5869
|
+
*/
|
|
5870
|
+
export declare function sortValues<T extends string | number>(values: readonly T[]): readonly T[];
|
|
5871
|
+
|
|
3672
5872
|
/**
|
|
3673
5873
|
* Build a guard that accepts strings satisfying optional length and pattern
|
|
3674
5874
|
* refinements — `min` / `max` length and a `pattern`.
|
|
@@ -3683,6 +5883,7 @@ export declare function shapeToKind(shape: ContractShape): FaultKind;
|
|
|
3683
5883
|
*
|
|
3684
5884
|
* @param options - Optional length bounds and regular expression refinement
|
|
3685
5885
|
* @returns A string guard enforcing the requested refinements
|
|
5886
|
+
* @throws {ContractError} When the options cannot be read
|
|
3686
5887
|
*
|
|
3687
5888
|
* @example
|
|
3688
5889
|
* ```ts
|
|
@@ -3705,7 +5906,10 @@ export declare interface StringShape {
|
|
|
3705
5906
|
readonly type: 'string';
|
|
3706
5907
|
readonly min?: number;
|
|
3707
5908
|
readonly max?: number;
|
|
3708
|
-
/**
|
|
5909
|
+
/**
|
|
5910
|
+
* An unflagged pattern constraint; use inline pattern constructs for flag-like behavior.
|
|
5911
|
+
* Builders and cloners expose an owned fresh frozen zero-state copy per read.
|
|
5912
|
+
*/
|
|
3709
5913
|
readonly pattern?: RegExp;
|
|
3710
5914
|
readonly description?: string;
|
|
3711
5915
|
}
|
|
@@ -3720,7 +5924,7 @@ export declare interface StringShape {
|
|
|
3720
5924
|
*
|
|
3721
5925
|
* @param options - Optional length (`min` / `max`), `pattern`, and `description`
|
|
3722
5926
|
* @returns A string shape
|
|
3723
|
-
* @throws {ContractError} When a present bound is invalid or `pattern` has flags
|
|
5927
|
+
* @throws {ContractError} When a present bound is invalid, `pattern` is not a `RegExp`, or `pattern` has flags
|
|
3724
5928
|
*
|
|
3725
5929
|
* @example
|
|
3726
5930
|
* ```ts
|
|
@@ -3807,14 +6011,15 @@ export declare type TupleFromGuards<Ts extends ReadonlyArray<Guard<unknown>>> =
|
|
|
3807
6011
|
}>;
|
|
3808
6012
|
|
|
3809
6013
|
/**
|
|
3810
|
-
* Build a guard that accepts fixed-arity tuples, testing each index with
|
|
3811
|
-
* corresponding guard.
|
|
6014
|
+
* Build a guard that accepts fixed-arity DENSE tuples, testing each index with
|
|
6015
|
+
* the corresponding guard.
|
|
3812
6016
|
*
|
|
3813
6017
|
* @example
|
|
3814
6018
|
* ```ts
|
|
3815
6019
|
* const isPair = tupleOf(isString, isNumber)
|
|
3816
6020
|
* isPair(['hello', 42]) // true
|
|
3817
6021
|
* isPair(['hello']) // false — wrong arity
|
|
6022
|
+
* isPair([, 42]) // false — sparse; see arrayOf for the dense lens
|
|
3818
6023
|
* ```
|
|
3819
6024
|
*/
|
|
3820
6025
|
export declare function tupleOf<const Gs extends ReadonlyArray<Guard<unknown>>>(...guards: Gs): Guard<TupleFromGuards<Gs>>;
|
|
@@ -3833,13 +6038,18 @@ export declare function tupleOf(...predicates: ReadonlyArray<(value: unknown) =>
|
|
|
3833
6038
|
* `{ anyOf: [...] }`, sorted by their canonical key for deterministic output.
|
|
3834
6039
|
* An empty input list returns the empty accept-anything schema `{}`.
|
|
3835
6040
|
*
|
|
3836
|
-
*
|
|
6041
|
+
* Every captured member must first be a non-null object record. This runtime
|
|
6042
|
+
* requirement is realm-agnostic and admits ordinary and null-prototype schema
|
|
6043
|
+
* records while refusing primitives and callables before canonicalization.
|
|
6044
|
+
*
|
|
6045
|
+
* A readable member {@link canonicalStringify} cannot key — a cyclic or
|
|
3837
6046
|
* otherwise JSON-inexpressible fragment, which only a direct caller can supply
|
|
3838
6047
|
* since the inferers always build plain encodable fragments — has NO
|
|
3839
6048
|
* de-duplication key, so it can participate in neither de-duplication nor the
|
|
3840
6049
|
* canonical-key ordering. It is KEPT (dropping a variant would narrow the
|
|
3841
6050
|
* union, and unification only ever widens), appended in input order after the
|
|
3842
|
-
* sorted keyed members
|
|
6051
|
+
* sorted keyed members. A failed member read propagates the canonicalizer's
|
|
6052
|
+
* coded refusal instead of participating in the union.
|
|
3843
6053
|
*
|
|
3844
6054
|
* @param schemas - The schemas to unify
|
|
3845
6055
|
* @returns The unified schema
|
|
@@ -3899,64 +6109,53 @@ export declare function unionShape<V extends readonly ContractShape[]>(...varian
|
|
|
3899
6109
|
export declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
3900
6110
|
|
|
3901
6111
|
/**
|
|
3902
|
-
*
|
|
3903
|
-
* compilation.
|
|
6112
|
+
* Gate recursive compiler work on shape structure, depth, and cycles.
|
|
3904
6113
|
*
|
|
3905
6114
|
* @remarks
|
|
3906
|
-
*
|
|
3907
|
-
*
|
|
3908
|
-
*
|
|
3909
|
-
*
|
|
3910
|
-
*
|
|
3911
|
-
*
|
|
3912
|
-
*
|
|
3913
|
-
*
|
|
3914
|
-
*
|
|
3915
|
-
*
|
|
3916
|
-
*
|
|
3917
|
-
*
|
|
3918
|
-
*
|
|
3919
|
-
*
|
|
3920
|
-
*
|
|
3921
|
-
*
|
|
3922
|
-
*
|
|
3923
|
-
* -
|
|
3924
|
-
*
|
|
3925
|
-
*
|
|
3926
|
-
*
|
|
3927
|
-
*
|
|
3928
|
-
*
|
|
3929
|
-
*
|
|
3930
|
-
*
|
|
3931
|
-
*
|
|
3932
|
-
*
|
|
6115
|
+
* Constructs a fresh {@link ShapeValidator} and eagerly validates the graph.
|
|
6116
|
+
* The validator walks iteratively with explicit stack space and observes each
|
|
6117
|
+
* unique node exactly ONCE per call, so a shared-child DAG costs its AUTHORED
|
|
6118
|
+
* nodes and edges rather than its paths. Every incoming edge is still inspected:
|
|
6119
|
+
* where an `optional` node is legal depends on the slot it arrived through, and
|
|
6120
|
+
* depth is measured over the captured graph afterwards rather than by walking
|
|
6121
|
+
* each path.
|
|
6122
|
+
*
|
|
6123
|
+
* This is also where the compilers' expansion bound lives. Every compiled
|
|
6124
|
+
* artifact is a tree, so a DAG's cost is the size of the tree it expands into —
|
|
6125
|
+
* `objectShape({ left: node, right: node })` nested thirty times is thirty-one
|
|
6126
|
+
* authored nodes and more than a billion emitted ones. The validator counts
|
|
6127
|
+
* that expansion and {@link refuseExpansion} refuses past
|
|
6128
|
+
* {@link COMPILE_NODE_LIMIT} with an `expansion`-coded {@link ContractError}, so
|
|
6129
|
+
* every standalone compiler and {@link createContract} answer a shared-child
|
|
6130
|
+
* declaration in bounded time instead of not answering. {@link cloneShape} /
|
|
6131
|
+
* {@link ownShape} are deliberately NOT bounded by it: they preserve
|
|
6132
|
+
* shared-child identity, so ownership of the same declaration stays
|
|
6133
|
+
* proportional to its authored nodes.
|
|
6134
|
+
* Every structural child slot must contain a shape node before it can enter the
|
|
6135
|
+
* walk; every scalar field must hold its declared runtime domain before a
|
|
6136
|
+
* compiler can use it; and a missing child, corrupt container, inherited
|
|
6137
|
+
* discriminant, or unrecognized node reports `structure`. This is the SOLE
|
|
6138
|
+
* eager well-formedness pass — there is no second prepass and no alias for it:
|
|
6139
|
+
* it enforces every bound domain and range used by the artifacts, including
|
|
6140
|
+
* non-empty literal/union vocabularies, finite literal numbers, integer-range
|
|
6141
|
+
* satisfiability, unflagged string patterns, optional-shape placement, and the
|
|
6142
|
+
* recursively supported raw-schema vocabulary where every present property or
|
|
6143
|
+
* dense union member is a record.
|
|
6144
|
+
* Active ancestors are tracked so shared children remain legal. Every
|
|
6145
|
+
* standalone compiler reaches this same validation once, over its owned
|
|
6146
|
+
* snapshot, through {@link ContractCompiler} preparation.
|
|
6147
|
+
* Failures have deterministic precedence independent of traversal order: depth,
|
|
6148
|
+
* then structure, then cycle, then field and vocabulary policy.
|
|
3933
6149
|
*
|
|
3934
|
-
* @param shape - The shape to
|
|
3935
|
-
* @
|
|
6150
|
+
* @param shape - The shape graph to gate
|
|
6151
|
+
* @returns Nothing; successful return means recursive compilation is structurally safe, depth-safe, and bounded in emitted nodes
|
|
6152
|
+
* @throws {ContractError} When a node or structural slot is corrupt, a bound or vocabulary is outside its declared domain, the graph is cyclic, it exceeds the compilation depth limit, or it expands past the compilation node limit
|
|
3936
6153
|
*
|
|
3937
6154
|
* @example
|
|
3938
6155
|
* ```ts
|
|
3939
|
-
*
|
|
3940
|
-
* validateShape(stringShape({ min: 10, max: 1 })) // throws
|
|
6156
|
+
* validateShapeDepth(stringShape({ min: 1 })) // returns; the declaration is compilable
|
|
3941
6157
|
* ```
|
|
3942
6158
|
*/
|
|
3943
|
-
export declare function validateShape(shape: ContractShape): void;
|
|
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
6159
|
export declare function validateShapeDepth(shape: ContractShape): void;
|
|
3961
6160
|
|
|
3962
6161
|
/**
|
|
@@ -3964,8 +6163,9 @@ export declare function validateShapeDepth(shape: ContractShape): void;
|
|
|
3964
6163
|
* {@link compileSchema}.
|
|
3965
6164
|
*
|
|
3966
6165
|
* @remarks
|
|
3967
|
-
*
|
|
3968
|
-
* {@link
|
|
6166
|
+
* Cycle/depth/breadth-bounded (see {@link inferValue} / {@link inferArray} /
|
|
6167
|
+
* {@link inferObject}). A failed traversal throws a `structure`
|
|
6168
|
+
* {@link ContractError}; it never becomes `{}` or another permissive schema. Nested
|
|
3969
6169
|
* objects close to unknown keys (`additionalProperties: false`) by default;
|
|
3970
6170
|
* pass `closed: false` to open them. `format` (default `false`) opts a
|
|
3971
6171
|
* string/`Date` leaf into the `format` keyword. Structurally-equal inputs
|
|
@@ -3985,6 +6185,7 @@ export declare function validateShapeDepth(shape: ContractShape): void;
|
|
|
3985
6185
|
* @param value - The value to infer a schema from
|
|
3986
6186
|
* @param options - Optional `maxDepth` / `maxProperties` / `closed` / `format` bounds
|
|
3987
6187
|
* @returns The inferred `JSONSchema`
|
|
6188
|
+
* @throws {ContractError} When the value or options cannot be read
|
|
3988
6189
|
*
|
|
3989
6190
|
* @example
|
|
3990
6191
|
* ```ts
|