@orkestrel/contract 0.0.20 → 0.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/core/index.cjs +2459 -568
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +938 -53
- package/dist/src/core/index.d.ts +938 -53
- package/dist/src/core/index.js +2420 -568
- package/dist/src/core/index.js.map +1 -1
- package/package.json +10 -10
|
@@ -17,6 +17,26 @@
|
|
|
17
17
|
*/
|
|
18
18
|
export declare function admitMember(members: Set<unknown>, value: unknown): void;
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Admits a stable readable value with only lossless repairs and returns the parser's value.
|
|
22
|
+
*
|
|
23
|
+
* @remarks
|
|
24
|
+
* Reads must remain stable across artifacts. Guard-valid input skips both reports and parses
|
|
25
|
+
* once. Repairs use audit, parse once, and explain only on parse failure. Unresolved unions,
|
|
26
|
+
* extras, lossy repairs, and a capped audit refuse. An own optional undefined field omitted or
|
|
27
|
+
* filled by its declared default is not a repair. The result preserves parser ownership, including union input identity.
|
|
28
|
+
* A read failure returns its cause rather than throwing.
|
|
29
|
+
* Snapshot a foreign object before admission, as Tool.execute does. A getter changing between
|
|
30
|
+
* is and parse can publish a value that no recorded repair describes. A shallow snapshot only
|
|
31
|
+
* stabilizes its own level; nested foreign values must also provide stable reads.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* admitValue(createContract(numberShape()), '3') // success carrying 3 and its type repair
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare function admitValue<T>(contract: ContractInterface<T>, value: unknown): Result<ContractAdmission<T>, ContractRefusal>;
|
|
39
|
+
|
|
20
40
|
/**
|
|
21
41
|
* Records an object as entered on a traversal's active path.
|
|
22
42
|
*
|
|
@@ -199,7 +219,7 @@ export declare interface ArrayShapeOptions {
|
|
|
199
219
|
* @example
|
|
200
220
|
* ```ts
|
|
201
221
|
* const outcome = attempt(() => predicate(value))
|
|
202
|
-
* return outcome
|
|
222
|
+
* return isSuccess(outcome) && outcome.value
|
|
203
223
|
* ```
|
|
204
224
|
*/
|
|
205
225
|
export declare function attempt<T>(callback: () => T): Result<T>;
|
|
@@ -207,6 +227,23 @@ export declare function attempt<T>(callback: () => T): Result<T>;
|
|
|
207
227
|
/** Covers every fault an audit reports — the parse faults plus undeclared keys. */
|
|
208
228
|
export declare type AuditFault = Fault | ExtraFault;
|
|
209
229
|
|
|
230
|
+
/** Describes the closed shape built for audit faults, including paired discriminator hints. */
|
|
231
|
+
export declare type AuditFaultShape = ReturnType<FaultShaper['build']>;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Returns the shared closed audit-fault shape, building it on the first call.
|
|
235
|
+
*
|
|
236
|
+
* @remarks
|
|
237
|
+
* Numeric bounds, counts, and discriminator literals must be finite. Discriminator hints
|
|
238
|
+
* carry both key and expected together. The inferred union is assignable to AuditFault.
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```ts
|
|
242
|
+
* createContract(auditFaultShape()).is({ reason: 'extra', path: ['typo'] }) // true
|
|
243
|
+
* ```
|
|
244
|
+
*/
|
|
245
|
+
export declare function auditFaultShape(): AuditFaultShape;
|
|
246
|
+
|
|
210
247
|
/**
|
|
211
248
|
* Represents a compiled strict-domain diagnostic — the shape of `compileAuditor`
|
|
212
249
|
* bound to one shape.
|
|
@@ -476,6 +513,20 @@ export declare function buildStringFaults(shape: StringShape, value: string, pat
|
|
|
476
513
|
*/
|
|
477
514
|
export declare function canonicalStringify(value: unknown): string | undefined;
|
|
478
515
|
|
|
516
|
+
/**
|
|
517
|
+
* Caps at `1024` the number of choices one schema field may offer, frozen.
|
|
518
|
+
*
|
|
519
|
+
* @remarks
|
|
520
|
+
* A retention limit that a declaring package, such as a form or table schema
|
|
521
|
+
* package, enforces; this package enforces none of the retention limits itself.
|
|
522
|
+
*
|
|
523
|
+
* @example
|
|
524
|
+
* ```ts
|
|
525
|
+
* CHOICE_LIMIT // 1024
|
|
526
|
+
* ```
|
|
527
|
+
*/
|
|
528
|
+
export declare const CHOICE_LIMIT = 1024;
|
|
529
|
+
|
|
479
530
|
/**
|
|
480
531
|
* Classifies an already-bounded string against the pattern-only and
|
|
481
532
|
* calendar-checked {@link SchemaFormat} vocabulary.
|
|
@@ -646,7 +697,7 @@ export declare function cloneShape(shape: ContractShape): ContractShape;
|
|
|
646
697
|
*
|
|
647
698
|
* @remarks
|
|
648
699
|
* Extracted rather than written inline for the same reason
|
|
649
|
-
* {@link
|
|
700
|
+
* {@link comparePrimitives} is: `Reflect.apply` takes its arguments as a LIST, so an
|
|
650
701
|
* inline collector is a function expression inside an array literal rather than
|
|
651
702
|
* one passed directly as an argument — a hidden function assignment this
|
|
652
703
|
* repository forbids wherever it appears. Returned directly from a factory it is
|
|
@@ -718,10 +769,10 @@ export declare function collectMembers(values: readonly unknown[]): Set<unknown>
|
|
|
718
769
|
*
|
|
719
770
|
* @example
|
|
720
771
|
* ```ts
|
|
721
|
-
*
|
|
772
|
+
* comparePrimitives('a', 'b') // -1
|
|
722
773
|
* ```
|
|
723
774
|
*/
|
|
724
|
-
export declare function
|
|
775
|
+
export declare function comparePrimitives<T extends string | number>(left: T, right: T): number;
|
|
725
776
|
|
|
726
777
|
/**
|
|
727
778
|
* Caps at `512` the supported nesting depth of a compiled contract shape, frozen.
|
|
@@ -861,7 +912,8 @@ export declare function compileGenerator(shape: ContractShape, random?: RandomFu
|
|
|
861
912
|
* § Validation and contracts requires. It requests
|
|
862
913
|
* exactly the `guard` root of a fresh {@link ContractCompiler}, which owns the
|
|
863
914
|
* declaration once through {@link ownShape} and validates that snapshot once,
|
|
864
|
-
* so an unfrozen caller-owned graph compiles
|
|
915
|
+
* unless the builders already did, so an unfrozen caller-owned graph compiles
|
|
916
|
+
* from a snapshot and excessive
|
|
865
917
|
* nesting, cycles, and excessive expansion are rejected before any guard is
|
|
866
918
|
* built.
|
|
867
919
|
*
|
|
@@ -1014,6 +1066,27 @@ export declare function compileSchema(shape: ContractShape): JSONSchema;
|
|
|
1014
1066
|
*/
|
|
1015
1067
|
export declare function complementOf<TBase, TExcluded extends TBase>(base: Guard<TBase>, excluded: Guard<TExcluded> | ((value: TBase) => value is TExcluded)): Guard<Exclude<TBase, TExcluded>>;
|
|
1016
1068
|
|
|
1069
|
+
/**
|
|
1070
|
+
* Counts the surrogate pairs in a string, stopping at a limit.
|
|
1071
|
+
*
|
|
1072
|
+
* @remarks
|
|
1073
|
+
* A regular-expression literal builds a fresh `RegExp` per evaluation, so this call
|
|
1074
|
+
* owns the `lastIndex` that is its cursor, and the captured `exec` reads the internal
|
|
1075
|
+
* slots and no member a caller can replace. Text with no surrogate costs one native
|
|
1076
|
+
* scan, and a string of pairs costs one `exec` per pair up to the limit.
|
|
1077
|
+
*
|
|
1078
|
+
* @param value - The string to scan
|
|
1079
|
+
* @param limit - The count to stop at
|
|
1080
|
+
* @returns The number of surrogate pairs, or `limit` when the string holds at least that many
|
|
1081
|
+
*
|
|
1082
|
+
* @example
|
|
1083
|
+
* ```ts
|
|
1084
|
+
* computeSurrogatePairs('😀a😀', 8) // 2
|
|
1085
|
+
* computeSurrogatePairs('😀😀😀', 2) // 2
|
|
1086
|
+
* ```
|
|
1087
|
+
*/
|
|
1088
|
+
export declare function computeSurrogatePairs(value: string, limit: number): number;
|
|
1089
|
+
|
|
1017
1090
|
/**
|
|
1018
1091
|
* Runs a public door's whole body and publishes only this package's error class.
|
|
1019
1092
|
*
|
|
@@ -1108,6 +1181,22 @@ export declare const CONTRACT_CODES: readonly ContractCode[];
|
|
|
1108
1181
|
*/
|
|
1109
1182
|
export declare const CONTRACT_ERROR_BRAND: unique symbol;
|
|
1110
1183
|
|
|
1184
|
+
/**
|
|
1185
|
+
* Carries an admitted value and the lossless repairs made while parsing it.
|
|
1186
|
+
*
|
|
1187
|
+
* @remarks
|
|
1188
|
+
* The value keeps the parser's ownership: a union may return the caller's input by identity.
|
|
1189
|
+
*
|
|
1190
|
+
* @example
|
|
1191
|
+
* ```ts
|
|
1192
|
+
* const admitted: ContractAdmission<number> = { value: 3, repairs: [] }
|
|
1193
|
+
* ```
|
|
1194
|
+
*/
|
|
1195
|
+
export declare interface ContractAdmission<T> {
|
|
1196
|
+
readonly value: T;
|
|
1197
|
+
readonly repairs: readonly AuditFault[];
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1111
1200
|
/**
|
|
1112
1201
|
* Names the machine-readable category carried by a {@link ContractError}.
|
|
1113
1202
|
*
|
|
@@ -1159,7 +1248,9 @@ export declare type ContractCode =
|
|
|
1159
1248
|
* chain pays a hundred clones of shrinking graphs and a hundred validations —
|
|
1160
1249
|
* quadratic work for a linear declaration. Here ownership runs once, validation runs once over
|
|
1161
1250
|
* that owned result, and each unique node and structural edge is indexed once
|
|
1162
|
-
* into children-before-parent order.
|
|
1251
|
+
* into children-before-parent order. A declaration a builder sealed is frozen and
|
|
1252
|
+
* validated all the way down already, so it takes neither and is indexed as it
|
|
1253
|
+
* stands. Every artifact family is then a single
|
|
1163
1254
|
* postorder pass whose entries are keyed by node identity, so a shared child is
|
|
1164
1255
|
* compiled once however many parents point at it. The same asymmetry exists on
|
|
1165
1256
|
* the VALUE side and is answered the same way: `guard`, `auditor` and `reporter`
|
|
@@ -1171,7 +1262,9 @@ export declare type ContractCode =
|
|
|
1171
1262
|
* getters after it replay. A getter builds its own family and no other, except
|
|
1172
1263
|
* that `parser`, `reporter` and `generator` build the guard plan when the graph
|
|
1173
1264
|
* holds a union, because a union's membership question IS a guard question in
|
|
1174
|
-
* all three
|
|
1265
|
+
* all three, and every getter builds it when the graph holds a default, because
|
|
1266
|
+
* a default that its inner shape refuses refuses at whichever artifact is read
|
|
1267
|
+
* first.
|
|
1175
1268
|
*
|
|
1176
1269
|
* One terminal lifecycle covers preparation and every family. A failure settles
|
|
1177
1270
|
* the compiler: later getters rethrow that exact error, while an artifact
|
|
@@ -1450,6 +1543,19 @@ export declare interface ContractInterface<T> {
|
|
|
1450
1543
|
generate(random?: RandomFunction): T;
|
|
1451
1544
|
}
|
|
1452
1545
|
|
|
1546
|
+
/**
|
|
1547
|
+
* Carries bounded admission faults and the cause of an unreadable value when present.
|
|
1548
|
+
*
|
|
1549
|
+
* @example
|
|
1550
|
+
* ```ts
|
|
1551
|
+
* const refused: ContractRefusal = { faults: [{ reason: 'extra', path: ['typo'] }] }
|
|
1552
|
+
* ```
|
|
1553
|
+
*/
|
|
1554
|
+
export declare interface ContractRefusal {
|
|
1555
|
+
readonly faults: readonly AuditFault[];
|
|
1556
|
+
readonly cause?: unknown;
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1453
1559
|
/**
|
|
1454
1560
|
* Describes a value declaratively — a declaration the shape builders build and
|
|
1455
1561
|
* the compilers turn into a guard, a parser, a JSON Schema, and a generator.
|
|
@@ -1459,7 +1565,7 @@ export declare interface ContractInterface<T> {
|
|
|
1459
1565
|
* element shape, an `ObjectShape` a map of them). {@link validateShape}
|
|
1460
1566
|
* enforces an acyclic graph within {@link COMPILE_DEPTH_LIMIT}.
|
|
1461
1567
|
*/
|
|
1462
|
-
export declare type ContractShape = StringShape | NumberShape | BooleanShape | NullShape | LiteralShape | ArrayShape | ObjectShape | UnionShape | OptionalShape | NullableShape | JSONShape | RawShape;
|
|
1568
|
+
export declare type ContractShape = StringShape | NumberShape | BooleanShape | NullShape | LiteralShape | ArrayShape | ObjectShape | UnionShape | OptionalShape | NullableShape | JSONShape | ShapeShape | RawShape;
|
|
1463
1569
|
|
|
1464
1570
|
/**
|
|
1465
1571
|
* Compiles a {@link ContractShape} into a {@link ContractInterface} — the
|
|
@@ -1475,24 +1581,30 @@ export declare type ContractShape = StringShape | NumberShape | BooleanShape | N
|
|
|
1475
1581
|
* snapshot — ownership already refuses a malformed structural slot, scalar
|
|
1476
1582
|
* field, or bound rather than normalizing it, so a second walk of the caller's
|
|
1477
1583
|
* live source only added a population that could disagree with the one the
|
|
1478
|
-
* artifacts actually use.
|
|
1479
|
-
*
|
|
1480
|
-
*
|
|
1481
|
-
*
|
|
1482
|
-
*
|
|
1483
|
-
*
|
|
1484
|
-
*
|
|
1485
|
-
*
|
|
1486
|
-
*
|
|
1487
|
-
*
|
|
1488
|
-
*
|
|
1489
|
-
*
|
|
1490
|
-
*
|
|
1491
|
-
*
|
|
1492
|
-
*
|
|
1493
|
-
*
|
|
1494
|
-
*
|
|
1495
|
-
*
|
|
1584
|
+
* artifacts actually use. A declaration made entirely by this package's builders
|
|
1585
|
+
* is frozen and validated all the way down already, so it is that owned graph.
|
|
1586
|
+
* Every artifact compiles once, so `audit`, `explain` and `generate` re-walk and
|
|
1587
|
+
* re-gate nothing on a later call; `contract.audit` and `compileAuditor` run the
|
|
1588
|
+
* same compiled plan reached by different names.
|
|
1589
|
+
* A declaration made entirely by builders compiles its schema at this call and
|
|
1590
|
+
* its function members together on the first call to any of them, so a module
|
|
1591
|
+
* can declare contracts as data and pay only for the ones it uses. Any other
|
|
1592
|
+
* declaration, and a bare leaf, compiles every member at this call. Either way
|
|
1593
|
+
* the members are plain data properties, so `const { is, parse } =
|
|
1594
|
+
* createContract(shape)` destructures those exact values, a spread of the result
|
|
1595
|
+
* copies them, and neither compiles anything. A malformed declaration refuses at
|
|
1596
|
+
* this call rather than at the first read of whichever member a caller happens
|
|
1597
|
+
* to touch, so the failure arrives at the call the caller made — carrying the
|
|
1598
|
+
* authoring door's own diagnosis, which this door adopts rather than rewraps.
|
|
1599
|
+
* Every compiled member is self-contained, and the compiler this call builds
|
|
1600
|
+
* releases its working set once every member has compiled. The contract keeps
|
|
1601
|
+
* part of the declaration even so: the auditor and reporter plans close over the
|
|
1602
|
+
* leaf and array nodes they read their bounds from, and a contract with deferred
|
|
1603
|
+
* members retains its declaration. Nothing the caller keeps reaches back into the
|
|
1604
|
+
* compiler. A deferred member compiled on a host whose members were replaced after
|
|
1605
|
+
* this call would outlive the replacement, so it refuses to compile until they are
|
|
1606
|
+
* restored: until then its guard answers `false`, its parser `undefined`, and its
|
|
1607
|
+
* diagnostics and generator refuse.
|
|
1496
1608
|
*
|
|
1497
1609
|
* @param shape - The shape to compile
|
|
1498
1610
|
* @returns A contract bundling `schema` / `is` / `parse` / `audit` / `explain` / `generate`
|
|
@@ -1568,6 +1680,49 @@ export declare function deriveLengthBounds(min: unknown, max: unknown): BoundsRe
|
|
|
1568
1680
|
*/
|
|
1569
1681
|
export declare function deriveRangeBounds(min: unknown, max: unknown): BoundsRead;
|
|
1570
1682
|
|
|
1683
|
+
/**
|
|
1684
|
+
* Describes one fault as the line a caller corrects from, naming what to send where the schema
|
|
1685
|
+
* says.
|
|
1686
|
+
*
|
|
1687
|
+
* @remarks
|
|
1688
|
+
* Each line opens with the fault's path (or `value` at the root) and reason. An `extra` fault lists
|
|
1689
|
+
* its parent's keys only when the declaring arms agree. A literal names candidates only where
|
|
1690
|
+
* the declarations agree; lists are deduplicated and bounded. A received preview stays as written
|
|
1691
|
+
* within PREVIEW_LIMIT and without control or line-separator characters; otherwise it is encoded
|
|
1692
|
+
* through preview. Received, constraint, and limit details read only own fields. A `variant` or
|
|
1693
|
+
* `oneOf` fault says that the value matches none of the declared forms, or several where one must
|
|
1694
|
+
* match, and summarizes each form through `renderForm`. It names no fault of any one form, because
|
|
1695
|
+
* none of them holds for the others. Those summaries share half of `FAULT_TEXT_LIMIT`.
|
|
1696
|
+
*
|
|
1697
|
+
* @param fault - The fault to describe
|
|
1698
|
+
* @param schema - The compiled schema the fault's path reads
|
|
1699
|
+
* @returns The line, with no line break
|
|
1700
|
+
*
|
|
1701
|
+
* @example
|
|
1702
|
+
* ```ts
|
|
1703
|
+
* import { describeFaultLine } from '@orkestrel/contract'
|
|
1704
|
+
*
|
|
1705
|
+
* describeFaultLine({ reason: 'missing', path: ['left'], expected: 'number' }, {}) // 'left: missing; expected number'
|
|
1706
|
+
* ```
|
|
1707
|
+
*/
|
|
1708
|
+
export declare function describeFaultLine(fault: AuditFault, schema: JSONSchema): string;
|
|
1709
|
+
|
|
1710
|
+
/**
|
|
1711
|
+
* Describes bounded faults with encoded values and a schema-derived correction summary.
|
|
1712
|
+
*
|
|
1713
|
+
* @remarks
|
|
1714
|
+
* Default: 2048 UTF-16 code units. A smaller non-negative integer limit reserves a caller's
|
|
1715
|
+
* heading budget. The report reserves room for omitted-fault and cap notices, and ends with a
|
|
1716
|
+
* send summary when a schema is supplied. Untrusted names, descriptions, and strings use preview.
|
|
1717
|
+
*
|
|
1718
|
+
* @example
|
|
1719
|
+
* ```ts
|
|
1720
|
+
* describeFaults([{ reason: 'missing', path: ['age'], expected: 'number' }])
|
|
1721
|
+
* // 'age: missing; expected number'
|
|
1722
|
+
* ```
|
|
1723
|
+
*/
|
|
1724
|
+
export declare function describeFaults(faults: readonly AuditFault[], schema?: JSONSchema, options?: FaultTextOptions): string;
|
|
1725
|
+
|
|
1571
1726
|
/**
|
|
1572
1727
|
* Draws and validates one generator random sample.
|
|
1573
1728
|
*
|
|
@@ -1709,9 +1864,110 @@ export declare function enumerableSymbolCount(value: object): number;
|
|
|
1709
1864
|
*/
|
|
1710
1865
|
export declare function enumOf<const E extends Record<string, string | number>>(enumeration: E): Guard<E[keyof E]>;
|
|
1711
1866
|
|
|
1867
|
+
/**
|
|
1868
|
+
* Renders a thrown value as a non-empty message.
|
|
1869
|
+
*
|
|
1870
|
+
* @remarks
|
|
1871
|
+
* Reads an `Error`'s `message` and converts any other value with the `String`
|
|
1872
|
+
* function {@link INTRINSICS} captured at load, so a symbol renders as
|
|
1873
|
+
* `Symbol(description)` and a caller that replaces the global `String` does not
|
|
1874
|
+
* change the result. Returns `'unknown failure'` when
|
|
1875
|
+
* that read throws, as a throwing `message` getter or `toString` method does,
|
|
1876
|
+
* when an `Error`'s `message` is not a string, and when the rendered text is
|
|
1877
|
+
* empty. Never throws.
|
|
1878
|
+
*
|
|
1879
|
+
* @param error - The caught value, of any shape
|
|
1880
|
+
* @returns The rendered message, never empty
|
|
1881
|
+
*
|
|
1882
|
+
* @example
|
|
1883
|
+
* ```ts
|
|
1884
|
+
* errorToMessage(new Error('boom')) // 'boom'
|
|
1885
|
+
* errorToMessage('') // 'unknown failure'
|
|
1886
|
+
* ```
|
|
1887
|
+
*/
|
|
1888
|
+
export declare function errorToMessage(error: unknown): string;
|
|
1889
|
+
|
|
1890
|
+
/**
|
|
1891
|
+
* Escapes every regular-expression metacharacter in a text, so the text
|
|
1892
|
+
* matches literally inside a larger `RegExp` source.
|
|
1893
|
+
*
|
|
1894
|
+
* @remarks
|
|
1895
|
+
* Prefixes each of `\ ^ $ . * + ? ( ) [ ] { } |` with a backslash and leaves
|
|
1896
|
+
* every other character, `-` and `/` included, unchanged; the empty string
|
|
1897
|
+
* returns unchanged. The escape is an INDEXED character map rather than
|
|
1898
|
+
* `String.prototype.replace`, which dispatches through the caller-writable
|
|
1899
|
+
* `RegExp.prototype[Symbol.replace]` and `exec` members.
|
|
1900
|
+
*
|
|
1901
|
+
* @param value - The literal text to escape
|
|
1902
|
+
* @returns The text with every metacharacter backslash-escaped
|
|
1903
|
+
*
|
|
1904
|
+
* @example
|
|
1905
|
+
* ```ts
|
|
1906
|
+
* escapeRegExp('a.b*c') // 'a\\.b\\*c'
|
|
1907
|
+
* new RegExp(`^${escapeRegExp('a.b')}$`).test('axb') // false
|
|
1908
|
+
* ```
|
|
1909
|
+
*/
|
|
1910
|
+
export declare function escapeRegExp(value: string): string;
|
|
1911
|
+
|
|
1712
1912
|
/** Evaluates a compiled node at one diagnostic path. */
|
|
1713
1913
|
export declare type EvaluationFunction<T> = (value: unknown, path: readonly string[]) => T;
|
|
1714
1914
|
|
|
1915
|
+
/**
|
|
1916
|
+
* Extracts a schema's forms: the schema itself, then its union arms, so a read looks
|
|
1917
|
+
* through a nullable the same way everywhere.
|
|
1918
|
+
*
|
|
1919
|
+
* @param schema - The schema to read, or `undefined` where none was found
|
|
1920
|
+
* @returns The forms, empty when there is no schema
|
|
1921
|
+
*
|
|
1922
|
+
* @example
|
|
1923
|
+
* ```ts
|
|
1924
|
+
* import { extractForms } from '@orkestrel/contract'
|
|
1925
|
+
*
|
|
1926
|
+
* const object = { type: 'object' } as const
|
|
1927
|
+
* extractForms({ anyOf: [object] }) // [{ anyOf: [object] }, object]
|
|
1928
|
+
* ```
|
|
1929
|
+
*/
|
|
1930
|
+
export declare function extractForms(schema: JSONSchema | undefined): readonly JSONSchema[];
|
|
1931
|
+
|
|
1932
|
+
/**
|
|
1933
|
+
* Extracts a fault's path as its segments, reading a string path as one segment.
|
|
1934
|
+
*
|
|
1935
|
+
* @param fault - The fault to read
|
|
1936
|
+
* @returns The path's segments, empty at the root
|
|
1937
|
+
*
|
|
1938
|
+
* @example
|
|
1939
|
+
* ```ts
|
|
1940
|
+
* import { extractPath } from '@orkestrel/contract'
|
|
1941
|
+
*
|
|
1942
|
+
* extractPath({ reason: 'extra', path: ['page', 'typo'] }) // ['page', 'typo']
|
|
1943
|
+
* ```
|
|
1944
|
+
*/
|
|
1945
|
+
export declare function extractPath(fault: AuditFault): readonly string[];
|
|
1946
|
+
|
|
1947
|
+
/**
|
|
1948
|
+
* Extracts the part of a schema a path reaches, through properties, array items, record values,
|
|
1949
|
+
* and the arms of a union.
|
|
1950
|
+
*
|
|
1951
|
+
* @remarks
|
|
1952
|
+
* Each path segment takes one step, so a union that nests itself cannot hold the walk. Only a
|
|
1953
|
+
* schema's own `properties` keys match, so `constructor` reaches nothing. The walk keeps every
|
|
1954
|
+
* matching union declaration until the end; differing declarations yield no schema. Independently
|
|
1955
|
+
* built declarations agree when their canonical JSON encodings agree.
|
|
1956
|
+
*
|
|
1957
|
+
* @param schema - The schema to walk
|
|
1958
|
+
* @param path - The segments to follow
|
|
1959
|
+
* @returns The schema at that path, or `undefined` where no declaration or no agreement exists
|
|
1960
|
+
*
|
|
1961
|
+
* @example
|
|
1962
|
+
* ```ts
|
|
1963
|
+
* import { extractSchema } from '@orkestrel/contract'
|
|
1964
|
+
*
|
|
1965
|
+
* const schema = { type: 'object', properties: { limit: { type: 'number' } } } as const
|
|
1966
|
+
* extractSchema(schema, ['limit']) // { type: 'number' }
|
|
1967
|
+
* ```
|
|
1968
|
+
*/
|
|
1969
|
+
export declare function extractSchema(schema: JSONSchema, path: readonly string[]): JSONSchema | undefined;
|
|
1970
|
+
|
|
1715
1971
|
/**
|
|
1716
1972
|
* Represents a key present on a value that its closed object shape does not declare.
|
|
1717
1973
|
*
|
|
@@ -1735,6 +1991,19 @@ export declare interface Failure<E> {
|
|
|
1735
1991
|
readonly error: E;
|
|
1736
1992
|
}
|
|
1737
1993
|
|
|
1994
|
+
/**
|
|
1995
|
+
* Boxes an error as the {@link Failure} branch of a {@link Result}.
|
|
1996
|
+
*
|
|
1997
|
+
* @param error - The error to box, retained exactly
|
|
1998
|
+
* @returns A fresh `{ success: false, error }` record
|
|
1999
|
+
*
|
|
2000
|
+
* @example
|
|
2001
|
+
* ```ts
|
|
2002
|
+
* failure('offline') // { success: false, error: 'offline' }
|
|
2003
|
+
* ```
|
|
2004
|
+
*/
|
|
2005
|
+
export declare function failure<E>(error: E): Failure<E>;
|
|
2006
|
+
|
|
1738
2007
|
/**
|
|
1739
2008
|
* Represents a single structured parse-failure diagnostic — one entry of an
|
|
1740
2009
|
* {@link ContractInterface.explain} report.
|
|
@@ -1750,6 +2019,10 @@ export declare interface Failure<E> {
|
|
|
1750
2019
|
* variant count, followed (in the report) by the closest variant's own faults.
|
|
1751
2020
|
* - `'oneOf'` — a `oneOf`-mode union matched zero or two-or-more variants;
|
|
1752
2021
|
* `matched` is the raw match count.
|
|
2022
|
+
*
|
|
2023
|
+
* A union whose arms a required literal property can tell apart reports the named
|
|
2024
|
+
* arm's own faults in place of either, and when the value names no one arm, `key`
|
|
2025
|
+
* and `expected` on these two say which property would and what it may hold.
|
|
1753
2026
|
*/
|
|
1754
2027
|
export declare type Fault = {
|
|
1755
2028
|
readonly reason: 'type';
|
|
@@ -1771,12 +2044,42 @@ export declare type Fault = {
|
|
|
1771
2044
|
readonly reason: 'variant';
|
|
1772
2045
|
readonly path: FieldPath;
|
|
1773
2046
|
readonly variants: number;
|
|
2047
|
+
readonly key?: string;
|
|
2048
|
+
readonly expected?: readonly LiteralValue[];
|
|
1774
2049
|
} | {
|
|
1775
2050
|
readonly reason: 'oneOf';
|
|
1776
2051
|
readonly path: FieldPath;
|
|
1777
2052
|
readonly matched: number;
|
|
2053
|
+
readonly key?: string;
|
|
2054
|
+
readonly expected?: readonly LiteralValue[];
|
|
1778
2055
|
};
|
|
1779
2056
|
|
|
2057
|
+
/**
|
|
2058
|
+
* Lists every {@link FaultConstraint}, frozen.
|
|
2059
|
+
*
|
|
2060
|
+
* @remarks
|
|
2061
|
+
* The runtime source of truth for the vocabulary, which {@link isFault} reads.
|
|
2062
|
+
*
|
|
2063
|
+
* @example
|
|
2064
|
+
* ```ts
|
|
2065
|
+
* FAULT_CONSTRAINTS.includes('min') // true
|
|
2066
|
+
* ```
|
|
2067
|
+
*/
|
|
2068
|
+
export declare const FAULT_CONSTRAINTS: readonly FaultConstraint[];
|
|
2069
|
+
|
|
2070
|
+
/**
|
|
2071
|
+
* Lists every {@link FaultKind}, frozen.
|
|
2072
|
+
*
|
|
2073
|
+
* @remarks
|
|
2074
|
+
* The runtime source of truth for the vocabulary, which {@link isFault} reads.
|
|
2075
|
+
*
|
|
2076
|
+
* @example
|
|
2077
|
+
* ```ts
|
|
2078
|
+
* FAULT_KINDS.includes('string') // true
|
|
2079
|
+
* ```
|
|
2080
|
+
*/
|
|
2081
|
+
export declare const FAULT_KINDS: readonly FaultKind[];
|
|
2082
|
+
|
|
1780
2083
|
/**
|
|
1781
2084
|
* Caps at `64` the number of {@link Fault} / {@link AuditFault} entries a single
|
|
1782
2085
|
* `explain` or `audit` report ever returns, frozen.
|
|
@@ -1792,6 +2095,15 @@ export declare type Fault = {
|
|
|
1792
2095
|
*/
|
|
1793
2096
|
export declare const FAULT_LIMIT = 64;
|
|
1794
2097
|
|
|
2098
|
+
/** Caps the keys or literal candidates named in a fault line. */
|
|
2099
|
+
export declare const FAULT_LIST_LIMIT = 12;
|
|
2100
|
+
|
|
2101
|
+
/** Caps a schema summary's UTF-16 code units. */
|
|
2102
|
+
export declare const FAULT_SCHEMA_LIMIT = 160;
|
|
2103
|
+
|
|
2104
|
+
/** Caps a fault message at 2048 UTF-16 code units. */
|
|
2105
|
+
export declare const FAULT_TEXT_LIMIT = 2048;
|
|
2106
|
+
|
|
1795
2107
|
/**
|
|
1796
2108
|
* Names the refinement a {@link Fault} of reason `'constraint'` violates.
|
|
1797
2109
|
*
|
|
@@ -1806,9 +2118,86 @@ export declare type FaultConstraint = 'min' | 'max' | 'pattern' | 'integer';
|
|
|
1806
2118
|
*
|
|
1807
2119
|
* @remarks
|
|
1808
2120
|
* The vocabulary is `'string'`, `'number'`, `'integer'`, `'boolean'`, `'null'`,
|
|
1809
|
-
* `'literal'`, `'array'`, `'object'`, `'union'`, and `'
|
|
2121
|
+
* `'literal'`, `'array'`, `'object'`, `'union'`, `'json'`, and `'shape'`.
|
|
1810
2122
|
*/
|
|
1811
|
-
export declare type FaultKind = 'string' | 'number' | 'integer' | 'boolean' | 'null' | 'literal' | 'array' | 'object' | 'union' | 'json';
|
|
2123
|
+
export declare type FaultKind = 'string' | 'number' | 'integer' | 'boolean' | 'null' | 'literal' | 'array' | 'object' | 'union' | 'json' | 'shape';
|
|
2124
|
+
|
|
2125
|
+
/**
|
|
2126
|
+
* Retains a lazily built fault declaration without building shapes during module evaluation.
|
|
2127
|
+
*
|
|
2128
|
+
* @example
|
|
2129
|
+
* ```ts
|
|
2130
|
+
* FaultShaper.shape === FaultShaper.shape // true
|
|
2131
|
+
* ```
|
|
2132
|
+
*/
|
|
2133
|
+
declare class FaultShaper {
|
|
2134
|
+
#private;
|
|
2135
|
+
/** Returns the shared fault declaration, building it on the first read. */
|
|
2136
|
+
static get shape(): AuditFaultShape;
|
|
2137
|
+
/**
|
|
2138
|
+
* Builds a closed fault union whose inferred values satisfy the audit fault contract.
|
|
2139
|
+
*
|
|
2140
|
+
* @example
|
|
2141
|
+
* ```ts
|
|
2142
|
+
* createContract(new FaultShaper().build()).is({ reason: 'extra', path: [] }) // true
|
|
2143
|
+
* ```
|
|
2144
|
+
*/
|
|
2145
|
+
build(): UnionShape<readonly [ObjectShape< {
|
|
2146
|
+
reason: LiteralShape<readonly ["type"]>;
|
|
2147
|
+
path: UnionShape<readonly [StringShape, ArrayShape<StringShape>]>;
|
|
2148
|
+
expected: LiteralShape<readonly FaultKind[]>;
|
|
2149
|
+
received: StringShape;
|
|
2150
|
+
}, false>, ObjectShape< {
|
|
2151
|
+
reason: LiteralShape<readonly ["missing"]>;
|
|
2152
|
+
path: UnionShape<readonly [StringShape, ArrayShape<StringShape>]>;
|
|
2153
|
+
expected: LiteralShape<readonly FaultKind[]>;
|
|
2154
|
+
}, false>, ObjectShape< {
|
|
2155
|
+
reason: LiteralShape<readonly ["constraint"]>;
|
|
2156
|
+
path: UnionShape<readonly [StringShape, ArrayShape<StringShape>]>;
|
|
2157
|
+
expected: LiteralShape<readonly FaultKind[]>;
|
|
2158
|
+
constraint: LiteralShape<readonly FaultConstraint[]>;
|
|
2159
|
+
limit: OptionalShape<UnionShape<readonly [NumberShape, StringShape]>>;
|
|
2160
|
+
received: StringShape;
|
|
2161
|
+
}, false>, ObjectShape< {
|
|
2162
|
+
reason: LiteralShape<readonly ["variant"]>;
|
|
2163
|
+
path: UnionShape<readonly [StringShape, ArrayShape<StringShape>]>;
|
|
2164
|
+
variants: NumberShape;
|
|
2165
|
+
}, false>, ObjectShape< {
|
|
2166
|
+
reason: LiteralShape<readonly ["variant"]>;
|
|
2167
|
+
path: UnionShape<readonly [StringShape, ArrayShape<StringShape>]>;
|
|
2168
|
+
variants: NumberShape;
|
|
2169
|
+
key: StringShape;
|
|
2170
|
+
expected: ArrayShape<UnionShape<readonly [StringShape, NumberShape, BooleanShape]>>;
|
|
2171
|
+
}, false>, ObjectShape< {
|
|
2172
|
+
reason: LiteralShape<readonly ["oneOf"]>;
|
|
2173
|
+
path: UnionShape<readonly [StringShape, ArrayShape<StringShape>]>;
|
|
2174
|
+
matched: NumberShape;
|
|
2175
|
+
}, false>, ObjectShape< {
|
|
2176
|
+
reason: LiteralShape<readonly ["oneOf"]>;
|
|
2177
|
+
path: UnionShape<readonly [StringShape, ArrayShape<StringShape>]>;
|
|
2178
|
+
matched: NumberShape;
|
|
2179
|
+
key: StringShape;
|
|
2180
|
+
expected: ArrayShape<UnionShape<readonly [StringShape, NumberShape, BooleanShape]>>;
|
|
2181
|
+
}, false>, ObjectShape< {
|
|
2182
|
+
reason: LiteralShape<readonly ["extra"]>;
|
|
2183
|
+
path: UnionShape<readonly [StringShape, ArrayShape<StringShape>]>;
|
|
2184
|
+
}, false>]>;
|
|
2185
|
+
}
|
|
2186
|
+
|
|
2187
|
+
/**
|
|
2188
|
+
* Reserves a caller's text budget for a bounded fault report.
|
|
2189
|
+
*
|
|
2190
|
+
* @remarks
|
|
2191
|
+
* The limit is a non-negative integer of UTF-16 code units, at most 2048. Default: 2048.
|
|
2192
|
+
*
|
|
2193
|
+
* @example
|
|
2194
|
+
* ```ts
|
|
2195
|
+
* const options: FaultTextOptions = { limit: 1024 }
|
|
2196
|
+
* ```
|
|
2197
|
+
*/
|
|
2198
|
+
export declare interface FaultTextOptions {
|
|
2199
|
+
readonly limit?: number;
|
|
2200
|
+
}
|
|
1812
2201
|
|
|
1813
2202
|
/**
|
|
1814
2203
|
* Addresses one field in a record: a single key, or an ordered list of keys to
|
|
@@ -1961,7 +2350,7 @@ export declare function holds(callback: () => boolean): boolean;
|
|
|
1961
2350
|
* nesting. {@link InferIndex} and {@link InferOpenIndex} apply the identical
|
|
1962
2351
|
* tuple guard to their own `A` parameter for the same reason.
|
|
1963
2352
|
*/
|
|
1964
|
-
export declare type Infer<S extends ContractShape> = [ContractShape] extends [S] ? unknown : S extends StringShape ? string : S extends NumberShape ? number : S extends BooleanShape ? boolean : S extends NullShape ? null : S extends {
|
|
2353
|
+
export declare type Infer<S extends ContractShape> = [ContractShape] extends [S] ? unknown : S extends ShapeShape ? ContractShape : S extends StringShape ? string : S extends NumberShape ? number : S extends BooleanShape ? boolean : S extends NullShape ? null : S extends {
|
|
1965
2354
|
readonly category: 'literal';
|
|
1966
2355
|
readonly values: infer V;
|
|
1967
2356
|
} ? V extends ReadonlyArray<infer L> ? L : never : S extends {
|
|
@@ -2592,6 +2981,23 @@ export declare function isAsyncGeneratorFunction(value: unknown): value is (...a
|
|
|
2592
2981
|
*/
|
|
2593
2982
|
export declare function isAsyncIterable<T = unknown>(value: unknown): value is AsyncIterable<T>;
|
|
2594
2983
|
|
|
2984
|
+
/**
|
|
2985
|
+
* Determines whether a value is exactly one {@link AuditFault}.
|
|
2986
|
+
*
|
|
2987
|
+
* @remarks
|
|
2988
|
+
* Every {@link Fault}, and the undeclared-key fault an audit adds.
|
|
2989
|
+
*
|
|
2990
|
+
* @param value - The value to inspect
|
|
2991
|
+
* @returns True if the value is a well-formed audit fault; false otherwise
|
|
2992
|
+
*
|
|
2993
|
+
* @example
|
|
2994
|
+
* ```ts
|
|
2995
|
+
* isAuditFault({ reason: 'extra', path: ['a'] }) // true
|
|
2996
|
+
* isAuditFault({}) // false
|
|
2997
|
+
* ```
|
|
2998
|
+
*/
|
|
2999
|
+
export declare function isAuditFault(value: unknown): value is AuditFault;
|
|
3000
|
+
|
|
2595
3001
|
/** Determines whether a value is a bigint.
|
|
2596
3002
|
*
|
|
2597
3003
|
* @param value - The value to inspect
|
|
@@ -2866,6 +3272,19 @@ export declare function isEmptyString(value: unknown): value is '';
|
|
|
2866
3272
|
*/
|
|
2867
3273
|
export declare function isError(value: unknown): value is Error;
|
|
2868
3274
|
|
|
3275
|
+
/**
|
|
3276
|
+
* Narrows a {@link Result} to its {@link Failure} branch.
|
|
3277
|
+
*
|
|
3278
|
+
* @param result - The result to narrow
|
|
3279
|
+
* @returns True if the result failed; false otherwise
|
|
3280
|
+
*
|
|
3281
|
+
* @example
|
|
3282
|
+
* ```ts
|
|
3283
|
+
* isFailure(failure('offline')) // true
|
|
3284
|
+
* ```
|
|
3285
|
+
*/
|
|
3286
|
+
export declare function isFailure<T, E>(result: Result<T, E>): result is Failure<E>;
|
|
3287
|
+
|
|
2869
3288
|
/** Determines whether a value is exactly `false`.
|
|
2870
3289
|
*
|
|
2871
3290
|
* @param value - The value to inspect
|
|
@@ -2879,6 +3298,39 @@ export declare function isError(value: unknown): value is Error;
|
|
|
2879
3298
|
*/
|
|
2880
3299
|
export declare function isFalse(value: unknown): value is false;
|
|
2881
3300
|
|
|
3301
|
+
/**
|
|
3302
|
+
* Determines whether a value is exactly one {@link Fault}.
|
|
3303
|
+
*
|
|
3304
|
+
* @remarks
|
|
3305
|
+
* A closed reading of the union: each reason's required fields, each with its own
|
|
3306
|
+
* vocabulary, and no other key, so `{}` and a fault carrying a stray field both answer
|
|
3307
|
+
* `false`. An undeclared-key fault is an {@link AuditFault} and answers `false` here.
|
|
3308
|
+
*
|
|
3309
|
+
* @param value - The value to inspect
|
|
3310
|
+
* @returns True if the value is a well-formed fault; false otherwise
|
|
3311
|
+
*
|
|
3312
|
+
* @example
|
|
3313
|
+
* ```ts
|
|
3314
|
+
* isFault({ reason: 'missing', path: ['a'], expected: 'string' }) // true
|
|
3315
|
+
* isFault({}) // false
|
|
3316
|
+
* ```
|
|
3317
|
+
*/
|
|
3318
|
+
export declare function isFault(value: unknown): value is Fault;
|
|
3319
|
+
|
|
3320
|
+
/**
|
|
3321
|
+
* Determines whether a value is the {@link FieldPath} a fault carries: a key, or a dense list of keys.
|
|
3322
|
+
*
|
|
3323
|
+
* @param value - The value to inspect
|
|
3324
|
+
* @returns True if the value is a string or a dense array of strings; false otherwise
|
|
3325
|
+
*
|
|
3326
|
+
* @example
|
|
3327
|
+
* ```ts
|
|
3328
|
+
* isFaultPath(['user', 'name']) // true
|
|
3329
|
+
* isFaultPath([1]) // false
|
|
3330
|
+
* ```
|
|
3331
|
+
*/
|
|
3332
|
+
export declare function isFaultPath(value: unknown): value is FieldPath;
|
|
3333
|
+
|
|
2882
3334
|
/** Determines whether a value is a finite number (excludes `NaN` and `±Infinity`).
|
|
2883
3335
|
*
|
|
2884
3336
|
* @param value - The value to inspect
|
|
@@ -3465,6 +3917,19 @@ export declare function isSharedArrayBuffer(value: unknown): value is SharedArra
|
|
|
3465
3917
|
*/
|
|
3466
3918
|
export declare function isString(value: unknown): value is string;
|
|
3467
3919
|
|
|
3920
|
+
/**
|
|
3921
|
+
* Narrows a {@link Result} to its {@link Success} branch.
|
|
3922
|
+
*
|
|
3923
|
+
* @param result - The result to narrow
|
|
3924
|
+
* @returns True if the result succeeded; false otherwise
|
|
3925
|
+
*
|
|
3926
|
+
* @example
|
|
3927
|
+
* ```ts
|
|
3928
|
+
* isSuccess(success(1)) // true
|
|
3929
|
+
* ```
|
|
3930
|
+
*/
|
|
3931
|
+
export declare function isSuccess<T, E>(result: Result<T, E>): result is Success<T>;
|
|
3932
|
+
|
|
3468
3933
|
/** Determines whether a value is a symbol.
|
|
3469
3934
|
*
|
|
3470
3935
|
* @param value - The value to inspect
|
|
@@ -3700,10 +4165,13 @@ export declare class JSONCloner implements JSONClonerInterface {
|
|
|
3700
4165
|
/**
|
|
3701
4166
|
* Clones the retained source into exact, deeply frozen JSON data.
|
|
3702
4167
|
*
|
|
4168
|
+
* @param budget - Remaining nodes shared with an enclosing ownership operation
|
|
3703
4169
|
* @returns The settled JSON snapshot
|
|
3704
4170
|
* @throws {ContractError} When the source is inexact, cyclic, unreadable, or cloning is reentered
|
|
3705
4171
|
*/
|
|
3706
|
-
clone(
|
|
4172
|
+
clone(budget?: {
|
|
4173
|
+
left: number;
|
|
4174
|
+
}): JSONValue;
|
|
3707
4175
|
}
|
|
3708
4176
|
|
|
3709
4177
|
/**
|
|
@@ -3729,10 +4197,13 @@ export declare interface JSONClonerInterface {
|
|
|
3729
4197
|
* alike. Active reentry permanently settles the instance with one exact cause-free
|
|
3730
4198
|
* `clone` poison shared by the nested call, the outer call, and every later call.
|
|
3731
4199
|
*
|
|
4200
|
+
* @param budget - Remaining nodes shared with an enclosing ownership operation; omitted for an independent snapshot
|
|
3732
4201
|
* @returns The settled JSON snapshot
|
|
3733
4202
|
* @throws {ContractError} When the source is inexact, cyclic, unreadable, or cloning is reentered
|
|
3734
4203
|
*/
|
|
3735
|
-
clone(
|
|
4204
|
+
clone(budget?: {
|
|
4205
|
+
left: number;
|
|
4206
|
+
}): JSONValue;
|
|
3736
4207
|
}
|
|
3737
4208
|
|
|
3738
4209
|
/**
|
|
@@ -3781,6 +4252,7 @@ export declare type JSONRecord = {
|
|
|
3781
4252
|
export declare interface JSONSchema {
|
|
3782
4253
|
readonly type?: JSONSchemaType;
|
|
3783
4254
|
readonly description?: string;
|
|
4255
|
+
readonly default?: JSONValue;
|
|
3784
4256
|
readonly enum?: readonly LiteralValue[];
|
|
3785
4257
|
readonly minLength?: number;
|
|
3786
4258
|
readonly maxLength?: number;
|
|
@@ -4023,6 +4495,27 @@ export declare function mapOf<K, V>(keyGuard: Guard<K>, valueGuard: Guard<V>): G
|
|
|
4023
4495
|
|
|
4024
4496
|
export declare function mapOf(keyPredicate: (value: unknown) => boolean, valuePredicate: (value: unknown) => boolean): Guard<ReadonlyMap<unknown, unknown>>;
|
|
4025
4497
|
|
|
4498
|
+
/**
|
|
4499
|
+
* Determines whether an object holds exactly the named own keys.
|
|
4500
|
+
*
|
|
4501
|
+
* @remarks
|
|
4502
|
+
* Every required key must be own, and every own key, symbols and non-enumerable ones
|
|
4503
|
+
* included, must be named, so an inherited field and a stray one both answer `false`.
|
|
4504
|
+
*
|
|
4505
|
+
* @param value - The object to inspect
|
|
4506
|
+
* @param required - The keys it must own
|
|
4507
|
+
* @param optional - The keys it may own
|
|
4508
|
+
* @returns True if its own keys are exactly those; false otherwise
|
|
4509
|
+
*
|
|
4510
|
+
* @example
|
|
4511
|
+
* ```ts
|
|
4512
|
+
* matchesExactKeys({ a: 1 }, ['a']) // true
|
|
4513
|
+
* matchesExactKeys({ a: 1, b: 2 }, ['a']) // false
|
|
4514
|
+
* matchesExactKeys({ a: 1, b: 2 }, ['a'], ['b']) // true
|
|
4515
|
+
* ```
|
|
4516
|
+
*/
|
|
4517
|
+
export declare function matchesExactKeys(value: object, required: readonly string[], optional?: readonly string[]): boolean;
|
|
4518
|
+
|
|
4026
4519
|
/**
|
|
4027
4520
|
* Checks whether a supported ISO-8601 date or date-time names a real instant.
|
|
4028
4521
|
*
|
|
@@ -4096,13 +4589,16 @@ export declare function matchesJSONDepth(value: unknown): boolean;
|
|
|
4096
4589
|
*
|
|
4097
4590
|
* Beside the ancestor set the walk keeps a walk-local PROVED set, so a node
|
|
4098
4591
|
* whose whole subtree already matched is not re-walked when a second path
|
|
4099
|
-
* reaches it.
|
|
4592
|
+
* reaches it. A supplied capture budget instead charges each occurrence that
|
|
4593
|
+
* ownership would copy; exhaustion throws `structure` with a `clone` cause.
|
|
4594
|
+
* Removing the recursion alone left the work exponential in shared
|
|
4100
4595
|
* aliases: thirty aliases — thirty-one ordinary records — cost `2^30` visits
|
|
4101
4596
|
* through `isJSONValue`, `parseJSONValue`, `canonicalStringify` and every
|
|
4102
4597
|
* `jsonShape` contract.
|
|
4103
4598
|
*
|
|
4104
4599
|
* @param entry - The value to inspect
|
|
4105
4600
|
* @param ancestors - Objects on the active traversal path
|
|
4601
|
+
* @param budget - Remaining capture nodes; aliases are charged for each copied occurrence when supplied
|
|
4106
4602
|
* @returns True if the value is a cycle-free JSON value; false otherwise
|
|
4107
4603
|
*
|
|
4108
4604
|
* @example
|
|
@@ -4111,7 +4607,37 @@ export declare function matchesJSONDepth(value: unknown): boolean;
|
|
|
4111
4607
|
* matchesJSONValue(Number.NaN, new WeakSet()) // false
|
|
4112
4608
|
* ```
|
|
4113
4609
|
*/
|
|
4114
|
-
export declare function matchesJSONValue(entry: unknown, ancestors: WeakSet<object
|
|
4610
|
+
export declare function matchesJSONValue(entry: unknown, ancestors: WeakSet<object>, budget?: {
|
|
4611
|
+
left: number;
|
|
4612
|
+
}): entry is JSONValue;
|
|
4613
|
+
|
|
4614
|
+
/**
|
|
4615
|
+
* Determines whether a repair `parse` made kept everything the value said.
|
|
4616
|
+
*
|
|
4617
|
+
* @remarks
|
|
4618
|
+
* `parse`'s coercions are published and form input relies on them, but some change what
|
|
4619
|
+
* a value means: `1` to `true`, `'9007199254740993'` to `...992`, `'1e-400'` to `0`,
|
|
4620
|
+
* `'0x10'` to `16`. A door that must not guess asks this about each fault `audit` reported,
|
|
4621
|
+
* since `audit` already names every nested repair with its path and the kind it wanted.
|
|
4622
|
+
*
|
|
4623
|
+
* Only a `type` fault is a repair. A number needs a string whose trim is the number's own
|
|
4624
|
+
* text, so `'30'` and `' 30 '` pass while `'30.0'`, `'007'`, `'0x10'`, `'1e3'`, `'+5'`,
|
|
4625
|
+
* `'.5'`, `'-0'`, and `'9007199254740993'` do not. A boolean needs `'true'` or `'false'`,
|
|
4626
|
+
* a literal its trimmed text, and a string a finite number. Anything else refuses.
|
|
4627
|
+
*
|
|
4628
|
+
* @param fault - A fault `audit` reported
|
|
4629
|
+
* @param input - The value as it arrived
|
|
4630
|
+
* @param parsed - What `parse` made of it
|
|
4631
|
+
* @returns True if the repair loses nothing; false otherwise
|
|
4632
|
+
*
|
|
4633
|
+
* @example
|
|
4634
|
+
* ```ts
|
|
4635
|
+
* const fault = { reason: 'type', path: ['a'], expected: 'number', received: '' } as const
|
|
4636
|
+
* matchesLosslessRepair(fault, { a: ' 30 ' }, { a: 30 }) // true
|
|
4637
|
+
* matchesLosslessRepair(fault, { a: '30.0' }, { a: 30 }) // false
|
|
4638
|
+
* ```
|
|
4639
|
+
*/
|
|
4640
|
+
export declare function matchesLosslessRepair(fault: AuditFault, input: unknown, parsed: unknown): boolean;
|
|
4115
4641
|
|
|
4116
4642
|
/**
|
|
4117
4643
|
* Determines whether a value is a member of a collected vocabulary, by
|
|
@@ -4226,6 +4752,44 @@ export declare function matchesPattern(pattern: RegExp, value: string): boolean;
|
|
|
4226
4752
|
*/
|
|
4227
4753
|
export declare function matchesRecordBrand(value: unknown): boolean;
|
|
4228
4754
|
|
|
4755
|
+
/**
|
|
4756
|
+
* Checks whether a string's length in code points falls within optional bounds.
|
|
4757
|
+
*
|
|
4758
|
+
* @remarks
|
|
4759
|
+
* Counts what JSON Schema's `minLength` and `maxLength` count: a surrogate pair is
|
|
4760
|
+
* one, and a lone surrogate is one. A string holds at most `.length` code points and
|
|
4761
|
+
* at least half that, so `.length` decides every bound it can with no count. The
|
|
4762
|
+
* count runs only where a bound sits between those two figures, which limits its
|
|
4763
|
+
* work to about twice the bound rather than to the size of the string.
|
|
4764
|
+
*
|
|
4765
|
+
* @param value - The string to measure
|
|
4766
|
+
* @param min - The inclusive lower bound, when there is one
|
|
4767
|
+
* @param max - The inclusive upper bound, when there is one
|
|
4768
|
+
* @returns True if the code-point count is within every present bound; false otherwise
|
|
4769
|
+
*
|
|
4770
|
+
* @example
|
|
4771
|
+
* ```ts
|
|
4772
|
+
* matchesStringLength('😀😀', undefined, 2) // true
|
|
4773
|
+
* matchesStringLength('😀', 2, undefined) // false
|
|
4774
|
+
* ```
|
|
4775
|
+
*/
|
|
4776
|
+
export declare function matchesStringLength(value: string, min: number | undefined, max: number | undefined): boolean;
|
|
4777
|
+
|
|
4778
|
+
/**
|
|
4779
|
+
* Checks whether a type fault describes an own undefined field omitted or defaulted by a successful parse.
|
|
4780
|
+
*
|
|
4781
|
+
* @remarks
|
|
4782
|
+
* Reads own members directly so absence and an unreadable member cannot stand for undefined.
|
|
4783
|
+
* A compiled parser fills an undefined field only through its declared default; it never coerces it.
|
|
4784
|
+
* Required reads propagate their cause to the admission boundary.
|
|
4785
|
+
*
|
|
4786
|
+
* @example
|
|
4787
|
+
* ```ts
|
|
4788
|
+
* matchesUndefinedField({ reason: 'type', path: ['note'], expected: 'string', received: 'undefined' }, { note: undefined }, {}) // true
|
|
4789
|
+
* ```
|
|
4790
|
+
*/
|
|
4791
|
+
export declare function matchesUndefinedField(fault: AuditFault, value: unknown, parsed: unknown): boolean;
|
|
4792
|
+
|
|
4229
4793
|
/**
|
|
4230
4794
|
* Determines whether an object is already on a traversal's active path.
|
|
4231
4795
|
*
|
|
@@ -4270,6 +4834,34 @@ export declare function matchesVisited(visited: WeakSet<object>, value: object):
|
|
|
4270
4834
|
*/
|
|
4271
4835
|
export declare function matchOf(pattern: RegExp): Guard<string>;
|
|
4272
4836
|
|
|
4837
|
+
/**
|
|
4838
|
+
* Caps at `128` the length, in UTF-16 code units, of one name a schema declares, frozen:
|
|
4839
|
+
* a schema, group, or field name, or a column key.
|
|
4840
|
+
*
|
|
4841
|
+
* @remarks
|
|
4842
|
+
* A retention limit that a declaring package enforces, as {@link CHOICE_LIMIT} states.
|
|
4843
|
+
*
|
|
4844
|
+
* @example
|
|
4845
|
+
* ```ts
|
|
4846
|
+
* NAME_LIMIT // 128
|
|
4847
|
+
* ```
|
|
4848
|
+
*/
|
|
4849
|
+
export declare const NAME_LIMIT = 128;
|
|
4850
|
+
|
|
4851
|
+
/**
|
|
4852
|
+
* Caps at `16384` the total number of records, arrays, and leaves one schema retains,
|
|
4853
|
+
* frozen.
|
|
4854
|
+
*
|
|
4855
|
+
* @remarks
|
|
4856
|
+
* A retention limit that a declaring package enforces, as {@link CHOICE_LIMIT} states.
|
|
4857
|
+
*
|
|
4858
|
+
* @example
|
|
4859
|
+
* ```ts
|
|
4860
|
+
* NODE_LIMIT // 16384
|
|
4861
|
+
* ```
|
|
4862
|
+
*/
|
|
4863
|
+
export declare const NODE_LIMIT = 16384;
|
|
4864
|
+
|
|
4273
4865
|
/**
|
|
4274
4866
|
* Negates a guard or predicate — passes when `guard` returns `false`.
|
|
4275
4867
|
*
|
|
@@ -4532,9 +5124,11 @@ export declare function omitVisited(visited: WeakSet<object>, value: object): vo
|
|
|
4532
5124
|
* {@link unionShape}'s guard.
|
|
4533
5125
|
* - **Parser**: judged on the RAW input's guard matches only, with NO
|
|
4534
5126
|
* coercion fallback for an ambiguous input. When exactly one variant's
|
|
4535
|
-
* guard accepts the raw value,
|
|
5127
|
+
* guard accepts the raw value, the input itself is returned. Zero matches or
|
|
4536
5128
|
* two-or-more matches both parse to `undefined` — a value ambiguous
|
|
4537
|
-
* between variants has no well-defined coercion target.
|
|
5129
|
+
* between variants has no well-defined coercion target. A value that names one
|
|
5130
|
+
* arm through a required literal is not ambiguous, so as in {@link unionShape}
|
|
5131
|
+
* that arm answers alone, and its parser may coerce.
|
|
4538
5132
|
*
|
|
4539
5133
|
* Prefer {@link unionShape} when a value may legitimately satisfy more than
|
|
4540
5134
|
* one variant (for example overlapping shapes) and any match is acceptable. Prefer
|
|
@@ -4588,10 +5182,19 @@ export declare type OptionalFromGuards<S extends GuardsShape, K extends Readonly
|
|
|
4588
5182
|
*/
|
|
4589
5183
|
export declare function optionalOf<T>(guard: Guard<T>): Guard<T | undefined>;
|
|
4590
5184
|
|
|
4591
|
-
/**
|
|
5185
|
+
/**
|
|
5186
|
+
* Wraps an inner shape that may be absent (`undefined`).
|
|
5187
|
+
*
|
|
5188
|
+
* @remarks
|
|
5189
|
+
* `default` holds the JSON value the compiled parser fills when the property is
|
|
5190
|
+
* absent or `undefined`. It leaves `is`, `audit`, `explain`, and `generate`
|
|
5191
|
+
* unchanged, because an absent optional stays canonical. It must satisfy `inner`,
|
|
5192
|
+
* and it may sit only on a property of a closed object with no union ancestor.
|
|
5193
|
+
*/
|
|
4592
5194
|
export declare interface OptionalShape<S extends ContractShape = ContractShape> {
|
|
4593
5195
|
readonly category: 'optional';
|
|
4594
5196
|
readonly inner: S;
|
|
5197
|
+
readonly default?: JSONValue;
|
|
4595
5198
|
}
|
|
4596
5199
|
|
|
4597
5200
|
/**
|
|
@@ -4599,12 +5202,28 @@ export declare interface OptionalShape<S extends ContractShape = ContractShape>
|
|
|
4599
5202
|
*
|
|
4600
5203
|
* @remarks
|
|
4601
5204
|
* As an {@link objectShape} property, the field becomes a true optional property
|
|
4602
|
-
* in the inferred type.
|
|
5205
|
+
* in the inferred type. A `default` is captured as an owned frozen copy, and the
|
|
5206
|
+
* compiled parser fills it where the property is absent or `undefined`; the guard,
|
|
5207
|
+
* the audit, the report, and the generator keep treating the property as optional.
|
|
5208
|
+
* The compilers refuse a default `inner` refuses, a default under a union, and a
|
|
5209
|
+
* default on a property of an open object.
|
|
4603
5210
|
*
|
|
4604
5211
|
* @param inner - The wrapped shape
|
|
5212
|
+
* @param options - Optional `default`, a JSON value
|
|
4605
5213
|
* @returns An optional shape
|
|
5214
|
+
* @throws {ContractError} When a present `default` is not a JSON value, `undefined` included
|
|
5215
|
+
*
|
|
5216
|
+
* @example
|
|
5217
|
+
* ```ts
|
|
5218
|
+
* const retries = optionalShape(integerShape({ min: 0 }), { default: 3 })
|
|
5219
|
+
* ```
|
|
4606
5220
|
*/
|
|
4607
|
-
export declare function optionalShape<S extends ContractShape>(inner: S): OptionalShape<S>;
|
|
5221
|
+
export declare function optionalShape<S extends ContractShape>(inner: S, options?: OptionalShapeOptions): OptionalShape<S>;
|
|
5222
|
+
|
|
5223
|
+
/** Groups the options for {@link OptionalShape} (through `optionalShape`). */
|
|
5224
|
+
export declare interface OptionalShapeOptions {
|
|
5225
|
+
readonly default?: JSONValue;
|
|
5226
|
+
}
|
|
4608
5227
|
|
|
4609
5228
|
/**
|
|
4610
5229
|
* Combines `left` and `right` with logical OR — passes when at least one passes.
|
|
@@ -5279,6 +5898,8 @@ export declare interface RawShape {
|
|
|
5279
5898
|
* For values the shape DSL can't express. The fragment is recursively checked
|
|
5280
5899
|
* against the lean {@link JSONSchema} vocabulary before ownership is taken;
|
|
5281
5900
|
* malformed or unsupported keywords throw a coded {@link ContractError}. The
|
|
5901
|
+
* refusal path starts at `schema` and names the nested keyword or member.
|
|
5902
|
+
* A non-record root is refused at `schema` itself. The
|
|
5282
5903
|
* compiled guard accepts every
|
|
5283
5904
|
* DEFINED value — `undefined` alone fails, because it is the parser's failure
|
|
5284
5905
|
* sentinel; wrap the shape in {@link optionalShape} to admit absence. The
|
|
@@ -5672,6 +6293,87 @@ export declare interface RecordShapeOptions {
|
|
|
5672
6293
|
*/
|
|
5673
6294
|
export declare function refuseExpansion(expansion: number | undefined): void;
|
|
5674
6295
|
|
|
6296
|
+
/**
|
|
6297
|
+
* Refuses an options record that holds a key its builder does not read.
|
|
6298
|
+
*
|
|
6299
|
+
* @remarks
|
|
6300
|
+
* The door for an option that decides what a contract accepts, where a misspelled key
|
|
6301
|
+
* would otherwise read as an absent option and fail open. It looks at own enumerable
|
|
6302
|
+
* string keys, so an inherited or symbol-keyed member is not a misspelling.
|
|
6303
|
+
*
|
|
6304
|
+
* @param source - The builder options, when there are any
|
|
6305
|
+
* @param keys - The keys that builder reads
|
|
6306
|
+
* @param builder - The builder name used in the refusal
|
|
6307
|
+
* @param shape - The shape category used in structured error context
|
|
6308
|
+
* @throws {ContractError} When the record holds any other key
|
|
6309
|
+
*
|
|
6310
|
+
* @example
|
|
6311
|
+
* ```ts
|
|
6312
|
+
* refuseOptionKeys({ strict: true }, ['strict'], 'schemaToShape', 'schema') // returns
|
|
6313
|
+
* refuseOptionKeys({ strct: true }, ['strict'], 'schemaToShape', 'schema') // throws
|
|
6314
|
+
* ```
|
|
6315
|
+
*/
|
|
6316
|
+
export declare function refuseOptionKeys(source: object | undefined, keys: readonly string[], builder: string, shape: string): void;
|
|
6317
|
+
|
|
6318
|
+
/**
|
|
6319
|
+
* Renders a schema as a short summary of what to send, such as `{"city": string, "note"?: string}`.
|
|
6320
|
+
*
|
|
6321
|
+
* @remarks
|
|
6322
|
+
* An object lists its required keys first and marks the rest with `?`, at most `FAULT_LIST_LIMIT`
|
|
6323
|
+
* of them. A leaf or array keeps its numeric bounds and pattern, a union joins its forms with ` | `,
|
|
6324
|
+
* and a schema that says nothing reads `any`. Each nested form takes half the room of its parent, and
|
|
6325
|
+
* below 16 characters a form stops at its type, so a schema that holds itself still ends. Text past
|
|
6326
|
+
* `room` is cut and closes with `…`. A type word outside JSON Schema's vocabulary is encoded.
|
|
6327
|
+
*
|
|
6328
|
+
* @param schema - The schema to summarize
|
|
6329
|
+
* @param room - The characters the summary can take. Default: `FAULT_SCHEMA_LIMIT`
|
|
6330
|
+
* @returns The summary
|
|
6331
|
+
*
|
|
6332
|
+
* @example
|
|
6333
|
+
* ```ts
|
|
6334
|
+
* import { renderForm } from '@orkestrel/contract'
|
|
6335
|
+
*
|
|
6336
|
+
* renderForm({ type: 'object', properties: { zip: { type: 'number' } }, required: ['zip'] }) // '{"zip": number}'
|
|
6337
|
+
* ```
|
|
6338
|
+
*/
|
|
6339
|
+
export declare function renderForm(schema: JSONSchema, room?: number): string;
|
|
6340
|
+
|
|
6341
|
+
/**
|
|
6342
|
+
* Renders a list as text, naming at most `FAULT_LIST_LIMIT` of its entries and counting the rest.
|
|
6343
|
+
*
|
|
6344
|
+
* @param entries - The rendered entries
|
|
6345
|
+
* @returns The entries joined with `, `
|
|
6346
|
+
*
|
|
6347
|
+
* @example
|
|
6348
|
+
* ```ts
|
|
6349
|
+
* import { renderList } from '@orkestrel/contract'
|
|
6350
|
+
*
|
|
6351
|
+
* renderList(['city', 'units']) // 'city, units'
|
|
6352
|
+
* ```
|
|
6353
|
+
*/
|
|
6354
|
+
export declare function renderList(entries: readonly string[]): string;
|
|
6355
|
+
|
|
6356
|
+
/**
|
|
6357
|
+
* Renders a path as text, encoding what a caller could have sent and eliding the middle of a long
|
|
6358
|
+
* one.
|
|
6359
|
+
*
|
|
6360
|
+
* @remarks
|
|
6361
|
+
* A segment that is not a plain identifier is JSON-encoded and length-capped, because a key the
|
|
6362
|
+
* caller sent is untrusted text. A path of more than four segments past 120 characters keeps its
|
|
6363
|
+
* first segment and its last three.
|
|
6364
|
+
*
|
|
6365
|
+
* @param path - The segments to render
|
|
6366
|
+
* @returns The segments joined with `.`, empty at the root
|
|
6367
|
+
*
|
|
6368
|
+
* @example
|
|
6369
|
+
* ```ts
|
|
6370
|
+
* import { renderPath } from '@orkestrel/contract'
|
|
6371
|
+
*
|
|
6372
|
+
* renderPath(['rows', '0', 'a b']) // 'rows.0."a b"'
|
|
6373
|
+
* ```
|
|
6374
|
+
*/
|
|
6375
|
+
export declare function renderPath(path: readonly string[]): string;
|
|
6376
|
+
|
|
5675
6377
|
/**
|
|
5676
6378
|
* Represents a compiled coercive-domain diagnostic — the shape of
|
|
5677
6379
|
* `compileReporter` bound to one shape.
|
|
@@ -6033,8 +6735,9 @@ export declare function schemaToParameters(schema: JSONSchema): Readonly<Record<
|
|
|
6033
6735
|
/**
|
|
6034
6736
|
* Converts a runtime `JSONSchema` value into a validating {@link ContractShape}
|
|
6035
6737
|
* — the inverse of {@link compileSchema}. Unlike direct {@link rawShape}
|
|
6036
|
-
* construction, which rejects malformed supported-vocabulary keywords,
|
|
6037
|
-
* conversion is total and widens an inexpressible input to a valid raw
|
|
6738
|
+
* construction, which rejects malformed supported-vocabulary keywords, the
|
|
6739
|
+
* lenient conversion is total and widens an inexpressible input to a valid raw
|
|
6740
|
+
* `{}`, and a strict conversion refuses that input.
|
|
6038
6741
|
*
|
|
6039
6742
|
* @remarks
|
|
6040
6743
|
* Readable malformed, cyclic, or deeply nested schema nodes widen to
|
|
@@ -6085,9 +6788,36 @@ export declare function schemaToParameters(schema: JSONSchema): Readonly<Record<
|
|
|
6085
6788
|
* when the conversion widened anywhere — `schema` / `is` / `parse` / `explain`
|
|
6086
6789
|
* stay total.
|
|
6087
6790
|
*
|
|
6791
|
+
* LENIENT LAW: a value a JSON Schema accepts is never refused by the shape a
|
|
6792
|
+
* lenient conversion builds from it, because the walk widens what it cannot read.
|
|
6793
|
+
* So `oneOf` builds an inclusive union, a `true` arm is a raw variant and a `false`
|
|
6794
|
+
* arm is dropped, an `enum` holding `null` admits it, `nullable: true` wraps the
|
|
6795
|
+
* node, a present `patternProperties` opens the object, a present `prefixItems`
|
|
6796
|
+
* widens the elements, and a `$ref` widens the node before anything beside it is read.
|
|
6797
|
+
* A schema past the compile limit keeps only its root's type, an array's bounds, and an
|
|
6798
|
+
* object's required keys and closure, with every value below them raw.
|
|
6799
|
+
*
|
|
6800
|
+
* DEFAULTS: a lenient conversion carries a property's `default` when the key is
|
|
6801
|
+
* not `required`, its object is closed with no union ancestor, and the value is
|
|
6802
|
+
* one the property's primitive or literal guard accepts. It drops every other
|
|
6803
|
+
* `default`, because carrying it would make the shape refuse to compile.
|
|
6804
|
+
*
|
|
6805
|
+
* STRICT: `{ strict: true }` is the door for a foreign schema. It refuses, with
|
|
6806
|
+
* code `structure`, every node the lenient walk would widen, every keyword it
|
|
6807
|
+
* would ignore (`pattern`, `format`, `nullable`, `patternProperties`,
|
|
6808
|
+
* `propertyNames`, `prefixItems`, and anything else outside
|
|
6809
|
+
* {@link STRICT_SCHEMA_KEYWORDS}), a malformed keyword or annotation, a boolean schema, an `enum` entry
|
|
6810
|
+
* that is neither a finite primitive nor `null`, a contradictory bound, a `required` name not in
|
|
6811
|
+
* `properties`, breadth past {@link INFER_BREADTH_LIMIT}, and a `default` with no
|
|
6812
|
+
* optional property to fill. An object with no `additionalProperties` closes
|
|
6813
|
+
* rather than opens, and `oneOf` keeps exactly-one, because a strict walk reads
|
|
6814
|
+
* every keyword that could keep two arms apart. Below a `oneOf` that object refuses
|
|
6815
|
+
* instead, since closing it could change which arms match.
|
|
6816
|
+
*
|
|
6088
6817
|
* @param schema - The JSON Schema value to convert
|
|
6818
|
+
* @param options - Optional `strict`, default `false`
|
|
6089
6819
|
* @returns The built {@link ContractShape}
|
|
6090
|
-
* @throws {ContractError} When schema traversal fails
|
|
6820
|
+
* @throws {ContractError} When schema traversal fails, or when a strict conversion refuses
|
|
6091
6821
|
*
|
|
6092
6822
|
* @example
|
|
6093
6823
|
* ```ts
|
|
@@ -6097,7 +6827,18 @@ export declare function schemaToParameters(schema: JSONSchema): Readonly<Record<
|
|
|
6097
6827
|
* contract.parse({ id: 'nope' }) // undefined
|
|
6098
6828
|
* ```
|
|
6099
6829
|
*/
|
|
6100
|
-
export declare function schemaToShape(schema: JSONSchema): ContractShape;
|
|
6830
|
+
export declare function schemaToShape(schema: JSONSchema, options?: SchemaToShapeOptions): ContractShape;
|
|
6831
|
+
|
|
6832
|
+
/**
|
|
6833
|
+
* Groups the options for converting a JSON Schema (through `schemaToShape`).
|
|
6834
|
+
*
|
|
6835
|
+
* @remarks
|
|
6836
|
+
* `strict` (default `false`) refuses what the lenient conversion widens or
|
|
6837
|
+
* ignores, and closes an object with no `additionalProperties`.
|
|
6838
|
+
*/
|
|
6839
|
+
export declare interface SchemaToShapeOptions {
|
|
6840
|
+
readonly strict?: boolean;
|
|
6841
|
+
}
|
|
6101
6842
|
|
|
6102
6843
|
/**
|
|
6103
6844
|
* Builds a deterministic pseudo-random source seeded from a single number.
|
|
@@ -6186,6 +6927,16 @@ export declare function setOf<T>(elementGuard: Guard<T>): Guard<ReadonlySet<T>>;
|
|
|
6186
6927
|
|
|
6187
6928
|
export declare function setOf(elementGuard: (value: unknown) => boolean): Guard<ReadonlySet<unknown>>;
|
|
6188
6929
|
|
|
6930
|
+
/**
|
|
6931
|
+
* Lists the fields recognized for every contract declaration category, frozen.
|
|
6932
|
+
*
|
|
6933
|
+
* @example
|
|
6934
|
+
* ```ts
|
|
6935
|
+
* SHAPE_FIELDS.shape // ['description']
|
|
6936
|
+
* ```
|
|
6937
|
+
*/
|
|
6938
|
+
export declare const SHAPE_FIELDS: Readonly<Record<string, readonly string[]>>;
|
|
6939
|
+
|
|
6189
6940
|
/**
|
|
6190
6941
|
* Owns the state of one contract-shape snapshot operation.
|
|
6191
6942
|
*
|
|
@@ -6270,6 +7021,56 @@ export declare interface ShapeProperty {
|
|
|
6270
7021
|
readonly child: ContractShape | undefined;
|
|
6271
7022
|
}
|
|
6272
7023
|
|
|
7024
|
+
/**
|
|
7025
|
+
* Describes a value that is a structurally valid contract declaration.
|
|
7026
|
+
*
|
|
7027
|
+
* @remarks
|
|
7028
|
+
* Acceptance is a successful {@link validateShape}, including its expansion bound.
|
|
7029
|
+
* Parsing preserves identity and never compiles the value or fills its defaults.
|
|
7030
|
+
* The shallow schema over-approximates declarations whose JSON projection preserves
|
|
7031
|
+
* their own enumerable data. Values with a different projection or no JSON projection
|
|
7032
|
+
* fall outside this schema, including custom `toJSON`, hidden keys, proxy omissions,
|
|
7033
|
+
* and extra members containing a BigInt or a cycle. Structural acceptance is unchanged.
|
|
7034
|
+
* It describes only the category and cannot
|
|
7035
|
+
* teach a language model to author a complete nested declaration. The guard decides.
|
|
7036
|
+
*
|
|
7037
|
+
* @example
|
|
7038
|
+
* ```ts
|
|
7039
|
+
* const declaration: ShapeShape = { category: 'shape' }
|
|
7040
|
+
* createContract(declaration).is(numberShape()) // true
|
|
7041
|
+
* ```
|
|
7042
|
+
*/
|
|
7043
|
+
export declare interface ShapeShape {
|
|
7044
|
+
readonly category: 'shape';
|
|
7045
|
+
readonly description?: string;
|
|
7046
|
+
}
|
|
7047
|
+
|
|
7048
|
+
/**
|
|
7049
|
+
* Builds a {@link ShapeShape}.
|
|
7050
|
+
*
|
|
7051
|
+
* @param options - Optional description
|
|
7052
|
+
* @returns A shape-valued declaration
|
|
7053
|
+
*
|
|
7054
|
+
* @example
|
|
7055
|
+
* ```ts
|
|
7056
|
+
* const definition = objectShape({ shape: shapeShape() })
|
|
7057
|
+
* ```
|
|
7058
|
+
*/
|
|
7059
|
+
export declare function shapeShape(options?: ShapeShapeOptions): ShapeShape;
|
|
7060
|
+
|
|
7061
|
+
/**
|
|
7062
|
+
* Configures the description of a shape-valued declaration.
|
|
7063
|
+
*
|
|
7064
|
+
* @example
|
|
7065
|
+
* ```ts
|
|
7066
|
+
* const options: ShapeShapeOptions = { description: 'A question declaration' }
|
|
7067
|
+
* shapeShape(options).description // 'A question declaration'
|
|
7068
|
+
* ```
|
|
7069
|
+
*/
|
|
7070
|
+
export declare interface ShapeShapeOptions {
|
|
7071
|
+
readonly description?: string;
|
|
7072
|
+
}
|
|
7073
|
+
|
|
6273
7074
|
/**
|
|
6274
7075
|
* Projects a {@link ContractShape} to the {@link FaultKind} it describes.
|
|
6275
7076
|
*
|
|
@@ -6321,7 +7122,7 @@ export declare class ShapeValidator implements ShapeValidatorInterface {
|
|
|
6321
7122
|
*
|
|
6322
7123
|
* @param shape - The live shape source validated by each call
|
|
6323
7124
|
*/
|
|
6324
|
-
constructor(shape:
|
|
7125
|
+
constructor(shape: unknown);
|
|
6325
7126
|
/**
|
|
6326
7127
|
* Reports the number of nodes the last successful {@link validate} found the
|
|
6327
7128
|
* source expands into.
|
|
@@ -6399,7 +7200,7 @@ export declare interface ShapeValidatorInterface {
|
|
|
6399
7200
|
* `valueToSchema({ b: 1, a: 2 })` publish `{"type":"object","additionalProperties":false}`
|
|
6400
7201
|
* — a successful answer with the caller's properties silently gone. The copy is
|
|
6401
7202
|
* taken first so a caller-owned array is never reordered in place, and the order
|
|
6402
|
-
* is decided by {@link
|
|
7203
|
+
* is decided by {@link comparePrimitives}, whose `<` / `>` comparison dispatches
|
|
6403
7204
|
* through nothing.
|
|
6404
7205
|
*
|
|
6405
7206
|
* @param values - The keys or indices to order
|
|
@@ -6412,6 +7213,38 @@ export declare interface ShapeValidatorInterface {
|
|
|
6412
7213
|
*/
|
|
6413
7214
|
export declare function sortValues<T extends string | number>(values: readonly T[]): readonly T[];
|
|
6414
7215
|
|
|
7216
|
+
/**
|
|
7217
|
+
* Lists the JSON Schema keywords a strict `schemaToShape` conversion reads, frozen.
|
|
7218
|
+
*
|
|
7219
|
+
* @remarks
|
|
7220
|
+
* The keywords a shape can honour, plus the annotations that assert nothing and so
|
|
7221
|
+
* lose nothing when a shape drops them: `title`, `$schema`, `$id`, `$comment`,
|
|
7222
|
+
* `examples`, `deprecated`, `readOnly`, and `writeOnly`. Every other keyword refuses.
|
|
7223
|
+
* `pattern` and `format` are absent because no compiled shape asserts them, so a
|
|
7224
|
+
* schema holding one would convert to a shape wider than the schema it came from.
|
|
7225
|
+
*
|
|
7226
|
+
* @example
|
|
7227
|
+
* ```ts
|
|
7228
|
+
* STRICT_SCHEMA_KEYWORDS.includes('title') // true
|
|
7229
|
+
* STRICT_SCHEMA_KEYWORDS.includes('pattern') // false
|
|
7230
|
+
* ```
|
|
7231
|
+
*/
|
|
7232
|
+
export declare const STRICT_SCHEMA_KEYWORDS: readonly string[];
|
|
7233
|
+
|
|
7234
|
+
/**
|
|
7235
|
+
* Caps at `65536` the length, in UTF-16 code units, of any single string one schema
|
|
7236
|
+
* retains, frozen.
|
|
7237
|
+
*
|
|
7238
|
+
* @remarks
|
|
7239
|
+
* A retention limit that a declaring package enforces, as {@link CHOICE_LIMIT} states.
|
|
7240
|
+
*
|
|
7241
|
+
* @example
|
|
7242
|
+
* ```ts
|
|
7243
|
+
* STRING_LIMIT // 65536
|
|
7244
|
+
* ```
|
|
7245
|
+
*/
|
|
7246
|
+
export declare const STRING_LIMIT = 65536;
|
|
7247
|
+
|
|
6415
7248
|
/**
|
|
6416
7249
|
* Groups the options for the `stringOf` guard builder.
|
|
6417
7250
|
*
|
|
@@ -6432,8 +7265,9 @@ export declare interface StringGuardOptions {
|
|
|
6432
7265
|
* refinements — `min` / `max` length and a `pattern`.
|
|
6433
7266
|
*
|
|
6434
7267
|
* @remarks
|
|
6435
|
-
* Composes {@link isString} with {@link
|
|
6436
|
-
*
|
|
7268
|
+
* Composes {@link isString} with {@link matchesStringLength}, which counts code
|
|
7269
|
+
* points as the `minLength` and `maxLength` a string shape emits do, and an owned
|
|
7270
|
+
* stateless pattern (the same refinement {@link matchOf} performs).
|
|
6437
7271
|
* When `min`, `max`, and `pattern` are all absent it returns the bare {@link isString} guard
|
|
6438
7272
|
* (the unconstrained fast path), so an unrefined string leaf pays no wrapping
|
|
6439
7273
|
* cost. The single source of the string refinement shared by the compiled guard
|
|
@@ -6455,7 +7289,13 @@ export declare interface StringGuardOptions {
|
|
|
6455
7289
|
*/
|
|
6456
7290
|
export declare function stringOf(options?: StringGuardOptions): Guard<string>;
|
|
6457
7291
|
|
|
6458
|
-
/**
|
|
7292
|
+
/**
|
|
7293
|
+
* Describes a string with optional length and pattern constraints.
|
|
7294
|
+
*
|
|
7295
|
+
* @remarks
|
|
7296
|
+
* `min` and `max` count code points, as the `minLength` and `maxLength` the shape
|
|
7297
|
+
* emits do, so one astral character has a length of one.
|
|
7298
|
+
*/
|
|
6459
7299
|
export declare interface StringShape {
|
|
6460
7300
|
readonly category: 'string';
|
|
6461
7301
|
readonly min?: number;
|
|
@@ -6532,6 +7372,33 @@ export declare interface Success<T> {
|
|
|
6532
7372
|
readonly value: T;
|
|
6533
7373
|
}
|
|
6534
7374
|
|
|
7375
|
+
/**
|
|
7376
|
+
* Boxes a value as the {@link Success} branch of a {@link Result}.
|
|
7377
|
+
*
|
|
7378
|
+
* @param value - The value to box, retained exactly
|
|
7379
|
+
* @returns A fresh `{ success: true, value }` record
|
|
7380
|
+
*
|
|
7381
|
+
* @example
|
|
7382
|
+
* ```ts
|
|
7383
|
+
* success(42) // { success: true, value: 42 }
|
|
7384
|
+
* ```
|
|
7385
|
+
*/
|
|
7386
|
+
export declare function success<T>(value: T): Success<T>;
|
|
7387
|
+
|
|
7388
|
+
/**
|
|
7389
|
+
* Caps at `1048576` the total length, in UTF-16 code units, of every string one schema
|
|
7390
|
+
* retains, frozen.
|
|
7391
|
+
*
|
|
7392
|
+
* @remarks
|
|
7393
|
+
* A retention limit that a declaring package enforces, as {@link CHOICE_LIMIT} states.
|
|
7394
|
+
*
|
|
7395
|
+
* @example
|
|
7396
|
+
* ```ts
|
|
7397
|
+
* TEXT_LIMIT // 1048576
|
|
7398
|
+
* ```
|
|
7399
|
+
*/
|
|
7400
|
+
export declare const TEXT_LIMIT = 1048576;
|
|
7401
|
+
|
|
6535
7402
|
/**
|
|
6536
7403
|
* Builds a guard that passes when the base passes AND the projection of the value
|
|
6537
7404
|
* satisfies the target guard. Still narrows to `T` (the base type) — the target
|
|
@@ -6565,6 +7432,16 @@ export declare function transformOf<T, U>(base: Guard<T>, project: (value: T) =>
|
|
|
6565
7432
|
|
|
6566
7433
|
export declare function transformOf<T>(base: Guard<T>, project: (value: T) => unknown, target: (value: unknown) => boolean): Guard<T>;
|
|
6567
7434
|
|
|
7435
|
+
/**
|
|
7436
|
+
* Truncates text to a UTF-16 budget without splitting a surrogate pair.
|
|
7437
|
+
*
|
|
7438
|
+
* @example
|
|
7439
|
+
* ```ts
|
|
7440
|
+
* truncateFaultText('abcdef', 4) // 'abc…'
|
|
7441
|
+
* ```
|
|
7442
|
+
*/
|
|
7443
|
+
export declare function truncateFaultText(text: string, limit: number): string;
|
|
7444
|
+
|
|
6568
7445
|
/** Maps a tuple of element guards to a readonly tuple of their guarded types. */
|
|
6569
7446
|
export declare type TupleFromGuards<Ts extends ReadonlyArray<Guard<unknown>>> = Readonly<{
|
|
6570
7447
|
[K in keyof Ts]: GuardType<Ts[K]>;
|
|
@@ -6665,6 +7542,13 @@ export declare interface UnionShape<V extends readonly ContractShape[] = readonl
|
|
|
6665
7542
|
/**
|
|
6666
7543
|
* Builds a {@link UnionShape} from a list of variant shapes (`anyOf` in JSON Schema).
|
|
6667
7544
|
*
|
|
7545
|
+
* @remarks
|
|
7546
|
+
* When every arm is an object declaring one required literal property, and a value's
|
|
7547
|
+
* own property exactly equals a literal only one arm holds, that arm answers: `explain`
|
|
7548
|
+
* and `audit` report its faults at their real paths, and `parse` repairs through it
|
|
7549
|
+
* alone. Any other value reports `variant`, with `key` and `expected` saying what would
|
|
7550
|
+
* have named an arm. `is` does not change.
|
|
7551
|
+
*
|
|
6668
7552
|
* @param variants - The candidate shapes; the first match wins at runtime
|
|
6669
7553
|
* @returns A union shape whose `Infer` is the union of the variants
|
|
6670
7554
|
*
|
|
@@ -6709,14 +7593,15 @@ export declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void
|
|
|
6709
7593
|
* eager well-formedness pass — there is no second prepass and no alias for it:
|
|
6710
7594
|
* it enforces every bound domain and range used by the artifacts, including
|
|
6711
7595
|
* non-empty literal/union vocabularies, finite literal numbers, integer-range
|
|
6712
|
-
* satisfiability, unflagged string patterns, optional-shape
|
|
6713
|
-
* recursively supported raw-schema vocabulary where every
|
|
6714
|
-
* dense union member is a record.
|
|
7596
|
+
* satisfiability, unflagged string patterns, optional-shape and default
|
|
7597
|
+
* placement, and the recursively supported raw-schema vocabulary where every
|
|
7598
|
+
* present property or dense union member is a record. It judges a default's
|
|
7599
|
+
* structure only; each compiler refuses a default its inner guard refuses.
|
|
6715
7600
|
* Active ancestors are tracked so shared children remain legal. Every
|
|
6716
7601
|
* standalone compiler reaches this same validation once, over its owned
|
|
6717
7602
|
* snapshot, through {@link ContractCompiler} preparation.
|
|
6718
|
-
*
|
|
6719
|
-
*
|
|
7603
|
+
* Cyclic graphs refuse before depth measurement, with structure before cycle.
|
|
7604
|
+
* Acyclic graphs report depth before structure, then field and vocabulary policy.
|
|
6720
7605
|
*
|
|
6721
7606
|
* @param shape - The shape graph to gate
|
|
6722
7607
|
* @returns Nothing; successful return means recursive compilation is structurally safe, depth-safe, and bounded in emitted nodes
|
|
@@ -6727,7 +7612,7 @@ export declare type UnionToIntersection<U> = (U extends unknown ? (k: U) => void
|
|
|
6727
7612
|
* validateShape(stringShape({ min: 1 })) // returns; the declaration is compilable
|
|
6728
7613
|
* ```
|
|
6729
7614
|
*/
|
|
6730
|
-
export declare function validateShape(shape:
|
|
7615
|
+
export declare function validateShape(shape: unknown): void;
|
|
6731
7616
|
|
|
6732
7617
|
/**
|
|
6733
7618
|
* Infers a `JSONSchema` for one unknown value — the reverse direction of
|