@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
package/dist/src/core/index.cjs
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
// src/core/constants.ts
|
|
3
3
|
/**
|
|
4
|
+
* Lists the fields recognized for every contract declaration category, frozen.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* SHAPE_FIELDS.shape // ['description']
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
var SHAPE_FIELDS = Object.freeze({
|
|
12
|
+
string: Object.freeze([
|
|
13
|
+
"min",
|
|
14
|
+
"max",
|
|
15
|
+
"pattern",
|
|
16
|
+
"description"
|
|
17
|
+
]),
|
|
18
|
+
number: Object.freeze([
|
|
19
|
+
"integer",
|
|
20
|
+
"min",
|
|
21
|
+
"max",
|
|
22
|
+
"description"
|
|
23
|
+
]),
|
|
24
|
+
boolean: Object.freeze(["description"]),
|
|
25
|
+
null: Object.freeze(["description"]),
|
|
26
|
+
literal: Object.freeze(["values", "description"]),
|
|
27
|
+
array: Object.freeze([
|
|
28
|
+
"items",
|
|
29
|
+
"min",
|
|
30
|
+
"max",
|
|
31
|
+
"description"
|
|
32
|
+
]),
|
|
33
|
+
object: Object.freeze([
|
|
34
|
+
"properties",
|
|
35
|
+
"additionalProperties",
|
|
36
|
+
"description"
|
|
37
|
+
]),
|
|
38
|
+
union: Object.freeze([
|
|
39
|
+
"variants",
|
|
40
|
+
"mode",
|
|
41
|
+
"description"
|
|
42
|
+
]),
|
|
43
|
+
optional: Object.freeze(["inner", "default"]),
|
|
44
|
+
nullable: Object.freeze(["inner"]),
|
|
45
|
+
json: Object.freeze(["description"]),
|
|
46
|
+
raw: Object.freeze(["schema"]),
|
|
47
|
+
shape: Object.freeze(["description"])
|
|
48
|
+
});
|
|
49
|
+
/**
|
|
4
50
|
* Holds the registry-global key used to recognize {@link ContractError} values
|
|
5
51
|
* across package copies.
|
|
6
52
|
*
|
|
@@ -10,7 +56,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
10
56
|
* own. The registry makes the key discoverable; it is a recognition mechanism,
|
|
11
57
|
* not an unforgeable provenance marker.
|
|
12
58
|
*/
|
|
13
|
-
var CONTRACT_ERROR_BRAND = Symbol.for("@orkestrel/contract.
|
|
59
|
+
var CONTRACT_ERROR_BRAND = Symbol.for("@orkestrel/contract.ContractError");
|
|
14
60
|
/**
|
|
15
61
|
* Captures every host operation this package dispatches through, while this
|
|
16
62
|
* module evaluates.
|
|
@@ -296,6 +342,48 @@ var JSON_SCHEMA_TYPES = Object.freeze([
|
|
|
296
342
|
"string"
|
|
297
343
|
]);
|
|
298
344
|
/**
|
|
345
|
+
* Lists the JSON Schema keywords a strict `schemaToShape` conversion reads, frozen.
|
|
346
|
+
*
|
|
347
|
+
* @remarks
|
|
348
|
+
* The keywords a shape can honour, plus the annotations that assert nothing and so
|
|
349
|
+
* lose nothing when a shape drops them: `title`, `$schema`, `$id`, `$comment`,
|
|
350
|
+
* `examples`, `deprecated`, `readOnly`, and `writeOnly`. Every other keyword refuses.
|
|
351
|
+
* `pattern` and `format` are absent because no compiled shape asserts them, so a
|
|
352
|
+
* schema holding one would convert to a shape wider than the schema it came from.
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* ```ts
|
|
356
|
+
* STRICT_SCHEMA_KEYWORDS.includes('title') // true
|
|
357
|
+
* STRICT_SCHEMA_KEYWORDS.includes('pattern') // false
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
var STRICT_SCHEMA_KEYWORDS = Object.freeze([
|
|
361
|
+
"type",
|
|
362
|
+
"title",
|
|
363
|
+
"description",
|
|
364
|
+
"$schema",
|
|
365
|
+
"$id",
|
|
366
|
+
"$comment",
|
|
367
|
+
"examples",
|
|
368
|
+
"deprecated",
|
|
369
|
+
"readOnly",
|
|
370
|
+
"writeOnly",
|
|
371
|
+
"default",
|
|
372
|
+
"enum",
|
|
373
|
+
"minLength",
|
|
374
|
+
"maxLength",
|
|
375
|
+
"minimum",
|
|
376
|
+
"maximum",
|
|
377
|
+
"minItems",
|
|
378
|
+
"maxItems",
|
|
379
|
+
"items",
|
|
380
|
+
"properties",
|
|
381
|
+
"required",
|
|
382
|
+
"additionalProperties",
|
|
383
|
+
"anyOf",
|
|
384
|
+
"oneOf"
|
|
385
|
+
]);
|
|
386
|
+
/**
|
|
299
387
|
* Lists every declared {@link ContractCode} refusal category, frozen.
|
|
300
388
|
*
|
|
301
389
|
* @remarks
|
|
@@ -329,6 +417,47 @@ var CONTRACT_CODES = Object.freeze([
|
|
|
329
417
|
"expansion"
|
|
330
418
|
]);
|
|
331
419
|
/**
|
|
420
|
+
* Lists every {@link FaultKind}, frozen.
|
|
421
|
+
*
|
|
422
|
+
* @remarks
|
|
423
|
+
* The runtime source of truth for the vocabulary, which {@link isFault} reads.
|
|
424
|
+
*
|
|
425
|
+
* @example
|
|
426
|
+
* ```ts
|
|
427
|
+
* FAULT_KINDS.includes('string') // true
|
|
428
|
+
* ```
|
|
429
|
+
*/
|
|
430
|
+
var FAULT_KINDS = Object.freeze([
|
|
431
|
+
"string",
|
|
432
|
+
"number",
|
|
433
|
+
"integer",
|
|
434
|
+
"boolean",
|
|
435
|
+
"null",
|
|
436
|
+
"literal",
|
|
437
|
+
"array",
|
|
438
|
+
"object",
|
|
439
|
+
"union",
|
|
440
|
+
"json",
|
|
441
|
+
"shape"
|
|
442
|
+
]);
|
|
443
|
+
/**
|
|
444
|
+
* Lists every {@link FaultConstraint}, frozen.
|
|
445
|
+
*
|
|
446
|
+
* @remarks
|
|
447
|
+
* The runtime source of truth for the vocabulary, which {@link isFault} reads.
|
|
448
|
+
*
|
|
449
|
+
* @example
|
|
450
|
+
* ```ts
|
|
451
|
+
* FAULT_CONSTRAINTS.includes('min') // true
|
|
452
|
+
* ```
|
|
453
|
+
*/
|
|
454
|
+
var FAULT_CONSTRAINTS = Object.freeze([
|
|
455
|
+
"min",
|
|
456
|
+
"max",
|
|
457
|
+
"pattern",
|
|
458
|
+
"integer"
|
|
459
|
+
]);
|
|
460
|
+
/**
|
|
332
461
|
* Caps at `64` the number of {@link Fault} / {@link AuditFault} entries a single
|
|
333
462
|
* `explain` or `audit` report ever returns, frozen.
|
|
334
463
|
*
|
|
@@ -548,6 +677,77 @@ var FORMAT_PATTERNS = Object.freeze({
|
|
|
548
677
|
email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
549
678
|
uri: /^[a-z][a-z0-9+.-]*:\/\//i
|
|
550
679
|
});
|
|
680
|
+
/**
|
|
681
|
+
* Caps at `1024` the number of choices one schema field may offer, frozen.
|
|
682
|
+
*
|
|
683
|
+
* @remarks
|
|
684
|
+
* A retention limit that a declaring package, such as a form or table schema
|
|
685
|
+
* package, enforces; this package enforces none of the retention limits itself.
|
|
686
|
+
*
|
|
687
|
+
* @example
|
|
688
|
+
* ```ts
|
|
689
|
+
* CHOICE_LIMIT // 1024
|
|
690
|
+
* ```
|
|
691
|
+
*/
|
|
692
|
+
var CHOICE_LIMIT = 1024;
|
|
693
|
+
/**
|
|
694
|
+
* Caps at `128` the length, in UTF-16 code units, of one name a schema declares, frozen:
|
|
695
|
+
* a schema, group, or field name, or a column key.
|
|
696
|
+
*
|
|
697
|
+
* @remarks
|
|
698
|
+
* A retention limit that a declaring package enforces, as {@link CHOICE_LIMIT} states.
|
|
699
|
+
*
|
|
700
|
+
* @example
|
|
701
|
+
* ```ts
|
|
702
|
+
* NAME_LIMIT // 128
|
|
703
|
+
* ```
|
|
704
|
+
*/
|
|
705
|
+
var NAME_LIMIT = 128;
|
|
706
|
+
/**
|
|
707
|
+
* Caps at `65536` the length, in UTF-16 code units, of any single string one schema
|
|
708
|
+
* retains, frozen.
|
|
709
|
+
*
|
|
710
|
+
* @remarks
|
|
711
|
+
* A retention limit that a declaring package enforces, as {@link CHOICE_LIMIT} states.
|
|
712
|
+
*
|
|
713
|
+
* @example
|
|
714
|
+
* ```ts
|
|
715
|
+
* STRING_LIMIT // 65536
|
|
716
|
+
* ```
|
|
717
|
+
*/
|
|
718
|
+
var STRING_LIMIT = 65536;
|
|
719
|
+
/**
|
|
720
|
+
* Caps at `1048576` the total length, in UTF-16 code units, of every string one schema
|
|
721
|
+
* retains, frozen.
|
|
722
|
+
*
|
|
723
|
+
* @remarks
|
|
724
|
+
* A retention limit that a declaring package enforces, as {@link CHOICE_LIMIT} states.
|
|
725
|
+
*
|
|
726
|
+
* @example
|
|
727
|
+
* ```ts
|
|
728
|
+
* TEXT_LIMIT // 1048576
|
|
729
|
+
* ```
|
|
730
|
+
*/
|
|
731
|
+
var TEXT_LIMIT = 1048576;
|
|
732
|
+
/**
|
|
733
|
+
* Caps at `16384` the total number of records, arrays, and leaves one schema retains,
|
|
734
|
+
* frozen.
|
|
735
|
+
*
|
|
736
|
+
* @remarks
|
|
737
|
+
* A retention limit that a declaring package enforces, as {@link CHOICE_LIMIT} states.
|
|
738
|
+
*
|
|
739
|
+
* @example
|
|
740
|
+
* ```ts
|
|
741
|
+
* NODE_LIMIT // 16384
|
|
742
|
+
* ```
|
|
743
|
+
*/
|
|
744
|
+
var NODE_LIMIT = 16384;
|
|
745
|
+
/** Caps a fault message at 2048 UTF-16 code units. */
|
|
746
|
+
var FAULT_TEXT_LIMIT = 2048;
|
|
747
|
+
/** Caps the keys or literal candidates named in a fault line. */
|
|
748
|
+
var FAULT_LIST_LIMIT = 12;
|
|
749
|
+
/** Caps a schema summary's UTF-16 code units. */
|
|
750
|
+
var FAULT_SCHEMA_LIMIT = 160;
|
|
551
751
|
// src/core/errors.ts
|
|
552
752
|
/**
|
|
553
753
|
* Carries a machine-readable contract category, optional context, and an exact optional
|
|
@@ -1198,6 +1398,103 @@ function isObject(value) {
|
|
|
1198
1398
|
function isRecord(value) {
|
|
1199
1399
|
return holds(() => matchesRecordBrand(value));
|
|
1200
1400
|
}
|
|
1401
|
+
/**
|
|
1402
|
+
* Determines whether a value is the {@link FieldPath} a fault carries: a key, or a dense list of keys.
|
|
1403
|
+
*
|
|
1404
|
+
* @param value - The value to inspect
|
|
1405
|
+
* @returns True if the value is a string or a dense array of strings; false otherwise
|
|
1406
|
+
*
|
|
1407
|
+
* @example
|
|
1408
|
+
* ```ts
|
|
1409
|
+
* isFaultPath(['user', 'name']) // true
|
|
1410
|
+
* isFaultPath([1]) // false
|
|
1411
|
+
* ```
|
|
1412
|
+
*/
|
|
1413
|
+
function isFaultPath(value) {
|
|
1414
|
+
if (isString(value)) return true;
|
|
1415
|
+
return holds(() => {
|
|
1416
|
+
if (!isArray(value)) return false;
|
|
1417
|
+
const snapshot = readArrayEntries(value);
|
|
1418
|
+
if (isFailure(snapshot) || !snapshot.value.dense) return false;
|
|
1419
|
+
return !INTRINSICS.reflect.apply(INTRINSICS.some, snapshot.value.entries, [(entry) => !isString(entry)]);
|
|
1420
|
+
});
|
|
1421
|
+
}
|
|
1422
|
+
/**
|
|
1423
|
+
* Determines whether a value is exactly one {@link Fault}.
|
|
1424
|
+
*
|
|
1425
|
+
* @remarks
|
|
1426
|
+
* A closed reading of the union: each reason's required fields, each with its own
|
|
1427
|
+
* vocabulary, and no other key, so `{}` and a fault carrying a stray field both answer
|
|
1428
|
+
* `false`. An undeclared-key fault is an {@link AuditFault} and answers `false` here.
|
|
1429
|
+
*
|
|
1430
|
+
* @param value - The value to inspect
|
|
1431
|
+
* @returns True if the value is a well-formed fault; false otherwise
|
|
1432
|
+
*
|
|
1433
|
+
* @example
|
|
1434
|
+
* ```ts
|
|
1435
|
+
* isFault({ reason: 'missing', path: ['a'], expected: 'string' }) // true
|
|
1436
|
+
* isFault({}) // false
|
|
1437
|
+
* ```
|
|
1438
|
+
*/
|
|
1439
|
+
function isFault(value) {
|
|
1440
|
+
return holds(() => {
|
|
1441
|
+
if (!isRecord(value) || !isFaultPath(value.path)) return false;
|
|
1442
|
+
const kind = INTRINSICS.reflect.apply(INTRINSICS.some, FAULT_KINDS, [(entry) => entry === value.expected]);
|
|
1443
|
+
const named = INTRINSICS.own(value, "key") || INTRINSICS.own(value, "expected");
|
|
1444
|
+
const list = readArrayEntries(isArray(value.expected) ? value.expected : [void 0]);
|
|
1445
|
+
const hinted = !named || isString(value.key) && !isFailure(list) && list.value.dense && !INTRINSICS.reflect.apply(INTRINSICS.some, list.value.entries, [(entry) => !isLiteralValue(entry) || isNumber(entry) && !isFiniteNumber(entry)]);
|
|
1446
|
+
switch (value.reason) {
|
|
1447
|
+
case "type": return kind && isString(value.received) && matchesExactKeys(value, [
|
|
1448
|
+
"reason",
|
|
1449
|
+
"path",
|
|
1450
|
+
"expected",
|
|
1451
|
+
"received"
|
|
1452
|
+
]);
|
|
1453
|
+
case "missing": return kind && matchesExactKeys(value, [
|
|
1454
|
+
"reason",
|
|
1455
|
+
"path",
|
|
1456
|
+
"expected"
|
|
1457
|
+
]);
|
|
1458
|
+
case "constraint": return kind && isString(value.received) && INTRINSICS.reflect.apply(INTRINSICS.some, FAULT_CONSTRAINTS, [(entry) => entry === value.constraint]) && (!INTRINSICS.own(value, "limit") || isFiniteNumber(value.limit) || isString(value.limit)) && matchesExactKeys(value, [
|
|
1459
|
+
"reason",
|
|
1460
|
+
"path",
|
|
1461
|
+
"expected",
|
|
1462
|
+
"constraint",
|
|
1463
|
+
"received"
|
|
1464
|
+
], ["limit"]);
|
|
1465
|
+
case "variant": return isNonNegativeInteger(value.variants) && hinted && matchesExactKeys(value, [
|
|
1466
|
+
"reason",
|
|
1467
|
+
"path",
|
|
1468
|
+
"variants"
|
|
1469
|
+
], named ? ["key", "expected"] : []);
|
|
1470
|
+
case "oneOf": return isNonNegativeInteger(value.matched) && hinted && matchesExactKeys(value, [
|
|
1471
|
+
"reason",
|
|
1472
|
+
"path",
|
|
1473
|
+
"matched"
|
|
1474
|
+
], named ? ["key", "expected"] : []);
|
|
1475
|
+
default: return false;
|
|
1476
|
+
}
|
|
1477
|
+
});
|
|
1478
|
+
}
|
|
1479
|
+
/**
|
|
1480
|
+
* Determines whether a value is exactly one {@link AuditFault}.
|
|
1481
|
+
*
|
|
1482
|
+
* @remarks
|
|
1483
|
+
* Every {@link Fault}, and the undeclared-key fault an audit adds.
|
|
1484
|
+
*
|
|
1485
|
+
* @param value - The value to inspect
|
|
1486
|
+
* @returns True if the value is a well-formed audit fault; false otherwise
|
|
1487
|
+
*
|
|
1488
|
+
* @example
|
|
1489
|
+
* ```ts
|
|
1490
|
+
* isAuditFault({ reason: 'extra', path: ['a'] }) // true
|
|
1491
|
+
* isAuditFault({}) // false
|
|
1492
|
+
* ```
|
|
1493
|
+
*/
|
|
1494
|
+
function isAuditFault(value) {
|
|
1495
|
+
if (isFault(value)) return true;
|
|
1496
|
+
return holds(() => isRecord(value) && value.reason === "extra" && isFaultPath(value.path) && matchesExactKeys(value, ["reason", "path"]));
|
|
1497
|
+
}
|
|
1201
1498
|
/** Determines whether a value is a `Map`.
|
|
1202
1499
|
*
|
|
1203
1500
|
* @param value - The value to inspect
|
|
@@ -1841,6 +2138,126 @@ function isJSONPrimitive(value) {
|
|
|
1841
2138
|
}
|
|
1842
2139
|
// src/core/helpers.ts
|
|
1843
2140
|
/**
|
|
2141
|
+
* Admits a stable readable value with only lossless repairs and returns the parser's value.
|
|
2142
|
+
*
|
|
2143
|
+
* @remarks
|
|
2144
|
+
* Reads must remain stable across artifacts. Guard-valid input skips both reports and parses
|
|
2145
|
+
* once. Repairs use audit, parse once, and explain only on parse failure. Unresolved unions,
|
|
2146
|
+
* extras, lossy repairs, and a capped audit refuse. An own optional undefined field omitted or
|
|
2147
|
+
* filled by its declared default is not a repair. The result preserves parser ownership, including union input identity.
|
|
2148
|
+
* A read failure returns its cause rather than throwing.
|
|
2149
|
+
* Snapshot a foreign object before admission, as Tool.execute does. A getter changing between
|
|
2150
|
+
* is and parse can publish a value that no recorded repair describes. A shallow snapshot only
|
|
2151
|
+
* stabilizes its own level; nested foreign values must also provide stable reads.
|
|
2152
|
+
*
|
|
2153
|
+
* @example
|
|
2154
|
+
* ```ts
|
|
2155
|
+
* admitValue(createContract(numberShape()), '3') // success carrying 3 and its type repair
|
|
2156
|
+
* ```
|
|
2157
|
+
*/
|
|
2158
|
+
function admitValue(contract, value) {
|
|
2159
|
+
try {
|
|
2160
|
+
if (contract.is(value)) {
|
|
2161
|
+
const parsed = contract.parse(value);
|
|
2162
|
+
if (parsed === void 0 && value !== void 0) return failure({ faults: [] });
|
|
2163
|
+
return success({
|
|
2164
|
+
value: parsed === void 0 ? value : parsed,
|
|
2165
|
+
repairs: []
|
|
2166
|
+
});
|
|
2167
|
+
}
|
|
2168
|
+
const audited = contract.audit(value);
|
|
2169
|
+
const parsed = contract.parse(value);
|
|
2170
|
+
const explained = parsed === void 0 ? contract.explain(value) : [];
|
|
2171
|
+
const repairs = audited.filter((fault) => matchesLosslessRepair(fault, value, parsed));
|
|
2172
|
+
const refused = audited.filter((fault) => !repairs.includes(fault) && !matchesUndefinedField(fault, value, parsed));
|
|
2173
|
+
const merged = [...explained, ...refused];
|
|
2174
|
+
const faults = [...new Map(merged.map((fault) => [JSON.stringify([fault.reason, extractPath(fault)]), fault])).values()];
|
|
2175
|
+
const covers = faults.filter((fault) => fault.reason === "variant" || fault.reason === "oneOf").map((fault) => extractPath(fault));
|
|
2176
|
+
const uncovered = faults.filter((fault) => {
|
|
2177
|
+
const path = extractPath(fault);
|
|
2178
|
+
const room = fault.reason === "variant" || fault.reason === "oneOf" ? 1 : 0;
|
|
2179
|
+
return !covers.some((cover) => cover.length + room <= path.length && cover.every((segment, index) => segment === path[index]));
|
|
2180
|
+
});
|
|
2181
|
+
if (uncovered.length > 0) return failure({ faults: limitEntries(uncovered, 64) });
|
|
2182
|
+
if (audited.length >= 64) return failure({ faults: limitEntries(audited, 64) });
|
|
2183
|
+
if (parsed === void 0) return failure({ faults: limitEntries(audited, 64) });
|
|
2184
|
+
return success({
|
|
2185
|
+
value: parsed,
|
|
2186
|
+
repairs
|
|
2187
|
+
});
|
|
2188
|
+
} catch (cause) {
|
|
2189
|
+
return failure({
|
|
2190
|
+
faults: [],
|
|
2191
|
+
cause
|
|
2192
|
+
});
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
/**
|
|
2196
|
+
* Checks whether a type fault describes an own undefined field omitted or defaulted by a successful parse.
|
|
2197
|
+
*
|
|
2198
|
+
* @remarks
|
|
2199
|
+
* Reads own members directly so absence and an unreadable member cannot stand for undefined.
|
|
2200
|
+
* A compiled parser fills an undefined field only through its declared default; it never coerces it.
|
|
2201
|
+
* Required reads propagate their cause to the admission boundary.
|
|
2202
|
+
*
|
|
2203
|
+
* @example
|
|
2204
|
+
* ```ts
|
|
2205
|
+
* matchesUndefinedField({ reason: 'type', path: ['note'], expected: 'string', received: 'undefined' }, { note: undefined }, {}) // true
|
|
2206
|
+
* ```
|
|
2207
|
+
*/
|
|
2208
|
+
function matchesUndefinedField(fault, value, parsed) {
|
|
2209
|
+
if (fault.reason !== "type" || parsed === void 0) return false;
|
|
2210
|
+
const path = extractPath(fault);
|
|
2211
|
+
const key = path.at(-1);
|
|
2212
|
+
if (key === void 0) return false;
|
|
2213
|
+
const [raw, result] = path.slice(0, -1).reduce(([raw, result], segment) => [isObject(raw) && INTRINSICS.own(raw, segment) ? INTRINSICS.reflect.read(raw, segment) : void 0, isObject(result) && INTRINSICS.own(result, segment) ? INTRINSICS.reflect.read(result, segment) : void 0], [value, parsed]);
|
|
2214
|
+
return isObject(raw) && INTRINSICS.own(raw, key) && INTRINSICS.reflect.read(raw, key) === void 0 && isObject(result);
|
|
2215
|
+
}
|
|
2216
|
+
/**
|
|
2217
|
+
* Truncates text to a UTF-16 budget without splitting a surrogate pair.
|
|
2218
|
+
*
|
|
2219
|
+
* @example
|
|
2220
|
+
* ```ts
|
|
2221
|
+
* truncateFaultText('abcdef', 4) // 'abc…'
|
|
2222
|
+
* ```
|
|
2223
|
+
*/
|
|
2224
|
+
function truncateFaultText(text, limit) {
|
|
2225
|
+
if (limit <= 0) return "";
|
|
2226
|
+
if (text.length <= limit) return text;
|
|
2227
|
+
return `${text.slice(0, limit - 1).replace(/[\uD800-\uDBFF]$/u, "")}…`;
|
|
2228
|
+
}
|
|
2229
|
+
/**
|
|
2230
|
+
* Describes bounded faults with encoded values and a schema-derived correction summary.
|
|
2231
|
+
*
|
|
2232
|
+
* @remarks
|
|
2233
|
+
* Default: 2048 UTF-16 code units. A smaller non-negative integer limit reserves a caller's
|
|
2234
|
+
* heading budget. The report reserves room for omitted-fault and cap notices, and ends with a
|
|
2235
|
+
* send summary when a schema is supplied. Untrusted names, descriptions, and strings use preview.
|
|
2236
|
+
*
|
|
2237
|
+
* @example
|
|
2238
|
+
* ```ts
|
|
2239
|
+
* describeFaults([{ reason: 'missing', path: ['age'], expected: 'number' }])
|
|
2240
|
+
* // 'age: missing; expected number'
|
|
2241
|
+
* ```
|
|
2242
|
+
*/
|
|
2243
|
+
function describeFaults(faults, schema, options) {
|
|
2244
|
+
const requested = options?.limit ?? 2048;
|
|
2245
|
+
if (!isInteger(requested) || requested < 0) throw new ContractError("Fault text limit must be a non-negative integer", { code: "range" });
|
|
2246
|
+
const limit = Math.min(requested, FAULT_TEXT_LIMIT);
|
|
2247
|
+
const send = schema === void 0 ? "" : `send ${renderForm(schema, Math.min(160, Math.floor(limit / 3)))}`;
|
|
2248
|
+
const lines = limitEntries(faults, 64).map((fault) => describeFaultLine(fault, schema ?? {}));
|
|
2249
|
+
const room = Math.max(0, limit - send.length - 82);
|
|
2250
|
+
const kept = lines.filter((_, index) => lines.slice(0, index + 1).join("\n").length <= room);
|
|
2251
|
+
const hidden = faults.length - kept.length;
|
|
2252
|
+
const note = [...hidden > 0 ? [`and ${String(hidden)} more faults`] : [], ...faults.length >= 64 ? [`the report stops at ${String(64)}`] : []].join(", ");
|
|
2253
|
+
return truncateFaultText([
|
|
2254
|
+
...kept,
|
|
2255
|
+
...kept.length === 0 && lines.length > 0 && room > 0 ? [truncateFaultText(lines[0] ?? "", room)] : [],
|
|
2256
|
+
...note.length === 0 ? [] : [note],
|
|
2257
|
+
...send.length === 0 ? [] : [send]
|
|
2258
|
+
].join("\n"), limit);
|
|
2259
|
+
}
|
|
2260
|
+
/**
|
|
1844
2261
|
* Iterates indices while rereading the upper bound before every step.
|
|
1845
2262
|
*
|
|
1846
2263
|
* @remarks
|
|
@@ -1998,10 +2415,10 @@ function limitEntries(entries, limit) {
|
|
|
1998
2415
|
*
|
|
1999
2416
|
* @example
|
|
2000
2417
|
* ```ts
|
|
2001
|
-
*
|
|
2418
|
+
* comparePrimitives('a', 'b') // -1
|
|
2002
2419
|
* ```
|
|
2003
2420
|
*/
|
|
2004
|
-
function
|
|
2421
|
+
function comparePrimitives(left, right) {
|
|
2005
2422
|
return left < right ? -1 : left > right ? 1 : 0;
|
|
2006
2423
|
}
|
|
2007
2424
|
/**
|
|
@@ -2015,7 +2432,7 @@ function compareValues(left, right) {
|
|
|
2015
2432
|
* `valueToSchema({ b: 1, a: 2 })` publish `{"type":"object","additionalProperties":false}`
|
|
2016
2433
|
* — a successful answer with the caller's properties silently gone. The copy is
|
|
2017
2434
|
* taken first so a caller-owned array is never reordered in place, and the order
|
|
2018
|
-
* is decided by {@link
|
|
2435
|
+
* is decided by {@link comparePrimitives}, whose `<` / `>` comparison dispatches
|
|
2019
2436
|
* through nothing.
|
|
2020
2437
|
*
|
|
2021
2438
|
* @param values - The keys or indices to order
|
|
@@ -2029,7 +2446,7 @@ function compareValues(left, right) {
|
|
|
2029
2446
|
function sortValues(values) {
|
|
2030
2447
|
const observed = INTRINSICS.from(iterateIndices(() => values.length), (index) => values[index]);
|
|
2031
2448
|
const owned = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(observed), [(value) => value !== void 0]);
|
|
2032
|
-
INTRINSICS.reflect.apply(INTRINSICS.order, owned, [
|
|
2449
|
+
INTRINSICS.reflect.apply(INTRINSICS.order, owned, [comparePrimitives]);
|
|
2033
2450
|
return owned;
|
|
2034
2451
|
}
|
|
2035
2452
|
/**
|
|
@@ -2273,7 +2690,7 @@ function readSampleMemo(memo, reader) {
|
|
|
2273
2690
|
*
|
|
2274
2691
|
* @remarks
|
|
2275
2692
|
* Extracted rather than written inline for the same reason
|
|
2276
|
-
* {@link
|
|
2693
|
+
* {@link comparePrimitives} is: `Reflect.apply` takes its arguments as a LIST, so an
|
|
2277
2694
|
* inline collector is a function expression inside an array literal rather than
|
|
2278
2695
|
* one passed directly as an argument — a hidden function assignment this
|
|
2279
2696
|
* repository forbids wherever it appears. Returned directly from a factory it is
|
|
@@ -2423,6 +2840,133 @@ function matchesPattern(pattern, value) {
|
|
|
2423
2840
|
return INTRINSICS.reflect.apply(INTRINSICS.captures, pattern, [value]) !== null;
|
|
2424
2841
|
}
|
|
2425
2842
|
/**
|
|
2843
|
+
* Checks whether a string's length in code points falls within optional bounds.
|
|
2844
|
+
*
|
|
2845
|
+
* @remarks
|
|
2846
|
+
* Counts what JSON Schema's `minLength` and `maxLength` count: a surrogate pair is
|
|
2847
|
+
* one, and a lone surrogate is one. A string holds at most `.length` code points and
|
|
2848
|
+
* at least half that, so `.length` decides every bound it can with no count. The
|
|
2849
|
+
* count runs only where a bound sits between those two figures, which limits its
|
|
2850
|
+
* work to about twice the bound rather than to the size of the string.
|
|
2851
|
+
*
|
|
2852
|
+
* @param value - The string to measure
|
|
2853
|
+
* @param min - The inclusive lower bound, when there is one
|
|
2854
|
+
* @param max - The inclusive upper bound, when there is one
|
|
2855
|
+
* @returns True if the code-point count is within every present bound; false otherwise
|
|
2856
|
+
*
|
|
2857
|
+
* @example
|
|
2858
|
+
* ```ts
|
|
2859
|
+
* matchesStringLength('😀😀', undefined, 2) // true
|
|
2860
|
+
* matchesStringLength('😀', 2, undefined) // false
|
|
2861
|
+
* ```
|
|
2862
|
+
*/
|
|
2863
|
+
function matchesStringLength(value, min, max) {
|
|
2864
|
+
const length = value.length;
|
|
2865
|
+
if (min !== void 0 && length < min) return false;
|
|
2866
|
+
if (max !== void 0 && length > 2 * max) return false;
|
|
2867
|
+
if ((min === void 0 || INTRINSICS.ceil(length / 2) >= min) && (max === void 0 || length <= max)) return true;
|
|
2868
|
+
const points = length - computeSurrogatePairs(value, length - (min === void 0 ? max ?? 0 : min - 1));
|
|
2869
|
+
return (min === void 0 || points >= min) && (max === void 0 || points <= max);
|
|
2870
|
+
}
|
|
2871
|
+
/**
|
|
2872
|
+
* Counts the surrogate pairs in a string, stopping at a limit.
|
|
2873
|
+
*
|
|
2874
|
+
* @remarks
|
|
2875
|
+
* A regular-expression literal builds a fresh `RegExp` per evaluation, so this call
|
|
2876
|
+
* owns the `lastIndex` that is its cursor, and the captured `exec` reads the internal
|
|
2877
|
+
* slots and no member a caller can replace. Text with no surrogate costs one native
|
|
2878
|
+
* scan, and a string of pairs costs one `exec` per pair up to the limit.
|
|
2879
|
+
*
|
|
2880
|
+
* @param value - The string to scan
|
|
2881
|
+
* @param limit - The count to stop at
|
|
2882
|
+
* @returns The number of surrogate pairs, or `limit` when the string holds at least that many
|
|
2883
|
+
*
|
|
2884
|
+
* @example
|
|
2885
|
+
* ```ts
|
|
2886
|
+
* computeSurrogatePairs('😀a😀', 8) // 2
|
|
2887
|
+
* computeSurrogatePairs('😀😀😀', 2) // 2
|
|
2888
|
+
* ```
|
|
2889
|
+
*/
|
|
2890
|
+
function computeSurrogatePairs(value, limit) {
|
|
2891
|
+
const pair = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
|
|
2892
|
+
const steps = limit > 0 ? INTRINSICS.ceil(limit) : 0;
|
|
2893
|
+
for (const step of iterateIndices(() => steps)) if (INTRINSICS.reflect.apply(INTRINSICS.captures, pair, [value]) === null) return step;
|
|
2894
|
+
return steps;
|
|
2895
|
+
}
|
|
2896
|
+
/**
|
|
2897
|
+
* Refuses an options record that holds a key its builder does not read.
|
|
2898
|
+
*
|
|
2899
|
+
* @remarks
|
|
2900
|
+
* The door for an option that decides what a contract accepts, where a misspelled key
|
|
2901
|
+
* would otherwise read as an absent option and fail open. It looks at own enumerable
|
|
2902
|
+
* string keys, so an inherited or symbol-keyed member is not a misspelling.
|
|
2903
|
+
*
|
|
2904
|
+
* @param source - The builder options, when there are any
|
|
2905
|
+
* @param keys - The keys that builder reads
|
|
2906
|
+
* @param builder - The builder name used in the refusal
|
|
2907
|
+
* @param shape - The shape category used in structured error context
|
|
2908
|
+
* @throws {ContractError} When the record holds any other key
|
|
2909
|
+
*
|
|
2910
|
+
* @example
|
|
2911
|
+
* ```ts
|
|
2912
|
+
* refuseOptionKeys({ strict: true }, ['strict'], 'schemaToShape', 'schema') // returns
|
|
2913
|
+
* refuseOptionKeys({ strct: true }, ['strict'], 'schemaToShape', 'schema') // throws
|
|
2914
|
+
* ```
|
|
2915
|
+
*/
|
|
2916
|
+
function refuseOptionKeys(source, keys, builder, shape) {
|
|
2917
|
+
if (source === void 0) return;
|
|
2918
|
+
const first = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(INTRINSICS.keys(source)), [(key) => !INTRINSICS.reflect.apply(INTRINSICS.some, keys, [(name) => name === key])])[0];
|
|
2919
|
+
if (first === void 0) return;
|
|
2920
|
+
const known = INTRINSICS.reflect.apply(INTRINSICS.join, keys, [", "]);
|
|
2921
|
+
throw new ContractError(`${builder}: unknown option ${preview(first)}; the options are ${known}`, {
|
|
2922
|
+
code: "structure",
|
|
2923
|
+
context: {
|
|
2924
|
+
path: [first],
|
|
2925
|
+
shape
|
|
2926
|
+
}
|
|
2927
|
+
});
|
|
2928
|
+
}
|
|
2929
|
+
/**
|
|
2930
|
+
* Escapes every regular-expression metacharacter in a text, so the text
|
|
2931
|
+
* matches literally inside a larger `RegExp` source.
|
|
2932
|
+
*
|
|
2933
|
+
* @remarks
|
|
2934
|
+
* Prefixes each of `\ ^ $ . * + ? ( ) [ ] { } |` with a backslash and leaves
|
|
2935
|
+
* every other character, `-` and `/` included, unchanged; the empty string
|
|
2936
|
+
* returns unchanged. The escape is an INDEXED character map rather than
|
|
2937
|
+
* `String.prototype.replace`, which dispatches through the caller-writable
|
|
2938
|
+
* `RegExp.prototype[Symbol.replace]` and `exec` members.
|
|
2939
|
+
*
|
|
2940
|
+
* @param value - The literal text to escape
|
|
2941
|
+
* @returns The text with every metacharacter backslash-escaped
|
|
2942
|
+
*
|
|
2943
|
+
* @example
|
|
2944
|
+
* ```ts
|
|
2945
|
+
* escapeRegExp('a.b*c') // 'a\\.b\\*c'
|
|
2946
|
+
* new RegExp(`^${escapeRegExp('a.b')}$`).test('axb') // false
|
|
2947
|
+
* ```
|
|
2948
|
+
*/
|
|
2949
|
+
function escapeRegExp(value) {
|
|
2950
|
+
const metacharacters = collectMembers([
|
|
2951
|
+
"\\",
|
|
2952
|
+
"^",
|
|
2953
|
+
"$",
|
|
2954
|
+
".",
|
|
2955
|
+
"*",
|
|
2956
|
+
"+",
|
|
2957
|
+
"?",
|
|
2958
|
+
"(",
|
|
2959
|
+
")",
|
|
2960
|
+
"[",
|
|
2961
|
+
"]",
|
|
2962
|
+
"{",
|
|
2963
|
+
"}",
|
|
2964
|
+
"|"
|
|
2965
|
+
]);
|
|
2966
|
+
const characters = INTRINSICS.from(iterateIndices(() => value.length), (index) => matchesMember(metacharacters, value[index]) ? `\\${value[index]}` : value[index]);
|
|
2967
|
+
return INTRINSICS.reflect.apply(INTRINSICS.join, characters, [""]);
|
|
2968
|
+
}
|
|
2969
|
+
/**
|
|
2426
2970
|
* Rebuilds a caller's regular expression as a stateless pattern this package
|
|
2427
2971
|
* owns.
|
|
2428
2972
|
*
|
|
@@ -2548,6 +3092,68 @@ function pinMembers(prototype, owner) {
|
|
|
2548
3092
|
}
|
|
2549
3093
|
}
|
|
2550
3094
|
/**
|
|
3095
|
+
* Boxes a value as the {@link Success} branch of a {@link Result}.
|
|
3096
|
+
*
|
|
3097
|
+
* @param value - The value to box, retained exactly
|
|
3098
|
+
* @returns A fresh `{ success: true, value }` record
|
|
3099
|
+
*
|
|
3100
|
+
* @example
|
|
3101
|
+
* ```ts
|
|
3102
|
+
* success(42) // { success: true, value: 42 }
|
|
3103
|
+
* ```
|
|
3104
|
+
*/
|
|
3105
|
+
function success(value) {
|
|
3106
|
+
return {
|
|
3107
|
+
success: true,
|
|
3108
|
+
value
|
|
3109
|
+
};
|
|
3110
|
+
}
|
|
3111
|
+
/**
|
|
3112
|
+
* Boxes an error as the {@link Failure} branch of a {@link Result}.
|
|
3113
|
+
*
|
|
3114
|
+
* @param error - The error to box, retained exactly
|
|
3115
|
+
* @returns A fresh `{ success: false, error }` record
|
|
3116
|
+
*
|
|
3117
|
+
* @example
|
|
3118
|
+
* ```ts
|
|
3119
|
+
* failure('offline') // { success: false, error: 'offline' }
|
|
3120
|
+
* ```
|
|
3121
|
+
*/
|
|
3122
|
+
function failure(error) {
|
|
3123
|
+
return {
|
|
3124
|
+
success: false,
|
|
3125
|
+
error
|
|
3126
|
+
};
|
|
3127
|
+
}
|
|
3128
|
+
/**
|
|
3129
|
+
* Narrows a {@link Result} to its {@link Success} branch.
|
|
3130
|
+
*
|
|
3131
|
+
* @param result - The result to narrow
|
|
3132
|
+
* @returns True if the result succeeded; false otherwise
|
|
3133
|
+
*
|
|
3134
|
+
* @example
|
|
3135
|
+
* ```ts
|
|
3136
|
+
* isSuccess(success(1)) // true
|
|
3137
|
+
* ```
|
|
3138
|
+
*/
|
|
3139
|
+
function isSuccess(result) {
|
|
3140
|
+
return result.success;
|
|
3141
|
+
}
|
|
3142
|
+
/**
|
|
3143
|
+
* Narrows a {@link Result} to its {@link Failure} branch.
|
|
3144
|
+
*
|
|
3145
|
+
* @param result - The result to narrow
|
|
3146
|
+
* @returns True if the result failed; false otherwise
|
|
3147
|
+
*
|
|
3148
|
+
* @example
|
|
3149
|
+
* ```ts
|
|
3150
|
+
* isFailure(failure('offline')) // true
|
|
3151
|
+
* ```
|
|
3152
|
+
*/
|
|
3153
|
+
function isFailure(result) {
|
|
3154
|
+
return !result.success;
|
|
3155
|
+
}
|
|
3156
|
+
/**
|
|
2551
3157
|
* Invokes a callback once and synchronously captures its exact outcome as a
|
|
2552
3158
|
* {@link Result}.
|
|
2553
3159
|
*
|
|
@@ -2575,23 +3181,42 @@ function pinMembers(prototype, owner) {
|
|
|
2575
3181
|
* @example
|
|
2576
3182
|
* ```ts
|
|
2577
3183
|
* const outcome = attempt(() => predicate(value))
|
|
2578
|
-
* return outcome
|
|
3184
|
+
* return isSuccess(outcome) && outcome.value
|
|
2579
3185
|
* ```
|
|
2580
3186
|
*/
|
|
2581
3187
|
function attempt(callback) {
|
|
2582
3188
|
try {
|
|
2583
|
-
return
|
|
2584
|
-
success: true,
|
|
2585
|
-
value: callback()
|
|
2586
|
-
};
|
|
3189
|
+
return success(callback());
|
|
2587
3190
|
} catch (error) {
|
|
2588
|
-
return
|
|
2589
|
-
success: false,
|
|
2590
|
-
error
|
|
2591
|
-
};
|
|
3191
|
+
return failure(error);
|
|
2592
3192
|
}
|
|
2593
3193
|
}
|
|
2594
3194
|
/**
|
|
3195
|
+
* Renders a thrown value as a non-empty message.
|
|
3196
|
+
*
|
|
3197
|
+
* @remarks
|
|
3198
|
+
* Reads an `Error`'s `message` and converts any other value with the `String`
|
|
3199
|
+
* function {@link INTRINSICS} captured at load, so a symbol renders as
|
|
3200
|
+
* `Symbol(description)` and a caller that replaces the global `String` does not
|
|
3201
|
+
* change the result. Returns `'unknown failure'` when
|
|
3202
|
+
* that read throws, as a throwing `message` getter or `toString` method does,
|
|
3203
|
+
* when an `Error`'s `message` is not a string, and when the rendered text is
|
|
3204
|
+
* empty. Never throws.
|
|
3205
|
+
*
|
|
3206
|
+
* @param error - The caught value, of any shape
|
|
3207
|
+
* @returns The rendered message, never empty
|
|
3208
|
+
*
|
|
3209
|
+
* @example
|
|
3210
|
+
* ```ts
|
|
3211
|
+
* errorToMessage(new Error('boom')) // 'boom'
|
|
3212
|
+
* errorToMessage('') // 'unknown failure'
|
|
3213
|
+
* ```
|
|
3214
|
+
*/
|
|
3215
|
+
function errorToMessage(error) {
|
|
3216
|
+
const read = attempt(() => isError(error) ? error.message : INTRINSICS.text(error));
|
|
3217
|
+
return isSuccess(read) && isString(read.value) && read.value.length > 0 ? read.value : "unknown failure";
|
|
3218
|
+
}
|
|
3219
|
+
/**
|
|
2595
3220
|
* Reads a value through the shared containment boundary or refuses it with the
|
|
2596
3221
|
* contract module's uniform read diagnostic.
|
|
2597
3222
|
*
|
|
@@ -2627,7 +3252,7 @@ function readValue(callback, reader, options) {
|
|
|
2627
3252
|
};
|
|
2628
3253
|
const requested = options?.code;
|
|
2629
3254
|
const subject = options?.subject;
|
|
2630
|
-
const code = requested !== void 0 &&
|
|
3255
|
+
const code = requested !== void 0 && INTRINSICS.reflect.apply(INTRINSICS.some, CONTRACT_CODES, [(known) => known === requested]) ? requested : "structure";
|
|
2631
3256
|
return {
|
|
2632
3257
|
reader: isString(reader) ? reader : "readValue",
|
|
2633
3258
|
subject: isString(subject) ? subject : "value",
|
|
@@ -2635,12 +3260,12 @@ function readValue(callback, reader, options) {
|
|
|
2635
3260
|
owned
|
|
2636
3261
|
};
|
|
2637
3262
|
});
|
|
2638
|
-
if (
|
|
3263
|
+
if (isFailure(diagnostics)) throw new ContractError("readValue: options could not be read", {
|
|
2639
3264
|
code: "structure",
|
|
2640
3265
|
cause: diagnostics.error
|
|
2641
3266
|
});
|
|
2642
3267
|
const outcome = attempt(callback);
|
|
2643
|
-
if (
|
|
3268
|
+
if (isFailure(outcome)) {
|
|
2644
3269
|
const owned = diagnostics.value.owned;
|
|
2645
3270
|
const context = owned === void 0 ? void 0 : {
|
|
2646
3271
|
...owned.path === void 0 ? {} : { path: owned.path },
|
|
@@ -2702,7 +3327,7 @@ function readValue(callback, reader, options) {
|
|
|
2702
3327
|
*/
|
|
2703
3328
|
function contain(callback, door, options) {
|
|
2704
3329
|
const outcome = attempt(callback);
|
|
2705
|
-
if (outcome
|
|
3330
|
+
if (isSuccess(outcome)) return outcome.value;
|
|
2706
3331
|
if (isContractError(outcome.error)) throw outcome.error;
|
|
2707
3332
|
throw new ContractError(`${door}: a host operation this door depends on failed`, {
|
|
2708
3333
|
code: options?.code ?? "structure",
|
|
@@ -2728,7 +3353,7 @@ function contain(callback, door, options) {
|
|
|
2728
3353
|
*/
|
|
2729
3354
|
function holds(callback) {
|
|
2730
3355
|
const outcome = attempt(callback);
|
|
2731
|
-
return outcome
|
|
3356
|
+
return isSuccess(outcome) && outcome.value === true;
|
|
2732
3357
|
}
|
|
2733
3358
|
/**
|
|
2734
3359
|
* Determines whether a value carries the plain-record brand, raising a hostile
|
|
@@ -2934,7 +3559,7 @@ function readGuardShape(shape, optional, reader) {
|
|
|
2934
3559
|
if (optional === true) return collectMembers(declared.names);
|
|
2935
3560
|
if (!INTRINSICS.array(optional)) return collectMembers([]);
|
|
2936
3561
|
const entries = readArrayEntries(optional);
|
|
2937
|
-
if (
|
|
3562
|
+
if (isFailure(entries)) throw entries.error;
|
|
2938
3563
|
if (!entries.value.dense) throw new INTRINSICS.error("Optional key list must be dense");
|
|
2939
3564
|
return collectMembers(INTRINSICS.from(iterateIndices(() => entries.value.entries.length), (index) => INTRINSICS.text(entries.value.entries[index])));
|
|
2940
3565
|
}, reader, { subject: "optional" });
|
|
@@ -2965,7 +3590,7 @@ function readGuardShape(shape, optional, reader) {
|
|
|
2965
3590
|
*/
|
|
2966
3591
|
function enumerableKeys(value) {
|
|
2967
3592
|
const outcome = attempt(() => INTRINSICS.freeze(INTRINSICS.keys(value)));
|
|
2968
|
-
return outcome
|
|
3593
|
+
return isSuccess(outcome) ? outcome.value : void 0;
|
|
2969
3594
|
}
|
|
2970
3595
|
/**
|
|
2971
3596
|
* Validates and snapshots a shape-builder options record through every reflective
|
|
@@ -3062,7 +3687,7 @@ function readOptions(source, keys, builder, shape) {
|
|
|
3062
3687
|
function drawRandom(random, shape) {
|
|
3063
3688
|
return contain(() => {
|
|
3064
3689
|
const outcome = attempt(random);
|
|
3065
|
-
if (
|
|
3690
|
+
if (isFailure(outcome)) throw new ContractError("drawRandom: the random source threw", {
|
|
3066
3691
|
code: "random",
|
|
3067
3692
|
context: {
|
|
3068
3693
|
shape,
|
|
@@ -3120,23 +3745,87 @@ function resolveField(record, path) {
|
|
|
3120
3745
|
return INTRINSICS.reflect.read(current, key);
|
|
3121
3746
|
}, record]);
|
|
3122
3747
|
});
|
|
3123
|
-
return outcome
|
|
3748
|
+
return isSuccess(outcome) ? outcome.value : void 0;
|
|
3124
3749
|
}
|
|
3125
3750
|
/**
|
|
3126
|
-
* Determines whether
|
|
3751
|
+
* Determines whether an object holds exactly the named own keys.
|
|
3127
3752
|
*
|
|
3128
3753
|
* @remarks
|
|
3129
|
-
*
|
|
3130
|
-
*
|
|
3131
|
-
* level, and shared aliases are answered from the shallowest depth at which
|
|
3132
|
-
* the alias already fit. Arrays are traversed through their reflected
|
|
3133
|
-
* own-index population, so sparse work is proportional to populated entries
|
|
3134
|
-
* rather than advertised length. Every observable operation is contained;
|
|
3135
|
-
* hostile or contradictory reads return `false`.
|
|
3754
|
+
* Every required key must be own, and every own key, symbols and non-enumerable ones
|
|
3755
|
+
* included, must be named, so an inherited field and a stray one both answer `false`.
|
|
3136
3756
|
*
|
|
3137
|
-
*
|
|
3138
|
-
*
|
|
3139
|
-
*
|
|
3757
|
+
* @param value - The object to inspect
|
|
3758
|
+
* @param required - The keys it must own
|
|
3759
|
+
* @param optional - The keys it may own
|
|
3760
|
+
* @returns True if its own keys are exactly those; false otherwise
|
|
3761
|
+
*
|
|
3762
|
+
* @example
|
|
3763
|
+
* ```ts
|
|
3764
|
+
* matchesExactKeys({ a: 1 }, ['a']) // true
|
|
3765
|
+
* matchesExactKeys({ a: 1, b: 2 }, ['a']) // false
|
|
3766
|
+
* matchesExactKeys({ a: 1, b: 2 }, ['a'], ['b']) // true
|
|
3767
|
+
* ```
|
|
3768
|
+
*/
|
|
3769
|
+
function matchesExactKeys(value, required, optional = []) {
|
|
3770
|
+
if (INTRINSICS.reflect.apply(INTRINSICS.some, required, [(key) => !INTRINSICS.own(value, key)])) return false;
|
|
3771
|
+
return !INTRINSICS.reflect.apply(INTRINSICS.some, INTRINSICS.reflect.members(value), [(key) => !INTRINSICS.reflect.apply(INTRINSICS.some, required, [(name) => name === key]) && !INTRINSICS.reflect.apply(INTRINSICS.some, optional, [(name) => name === key])]);
|
|
3772
|
+
}
|
|
3773
|
+
/**
|
|
3774
|
+
* Determines whether a repair `parse` made kept everything the value said.
|
|
3775
|
+
*
|
|
3776
|
+
* @remarks
|
|
3777
|
+
* `parse`'s coercions are published and form input relies on them, but some change what
|
|
3778
|
+
* a value means: `1` to `true`, `'9007199254740993'` to `...992`, `'1e-400'` to `0`,
|
|
3779
|
+
* `'0x10'` to `16`. A door that must not guess asks this about each fault `audit` reported,
|
|
3780
|
+
* since `audit` already names every nested repair with its path and the kind it wanted.
|
|
3781
|
+
*
|
|
3782
|
+
* Only a `type` fault is a repair. A number needs a string whose trim is the number's own
|
|
3783
|
+
* text, so `'30'` and `' 30 '` pass while `'30.0'`, `'007'`, `'0x10'`, `'1e3'`, `'+5'`,
|
|
3784
|
+
* `'.5'`, `'-0'`, and `'9007199254740993'` do not. A boolean needs `'true'` or `'false'`,
|
|
3785
|
+
* a literal its trimmed text, and a string a finite number. Anything else refuses.
|
|
3786
|
+
*
|
|
3787
|
+
* @param fault - A fault `audit` reported
|
|
3788
|
+
* @param input - The value as it arrived
|
|
3789
|
+
* @param parsed - What `parse` made of it
|
|
3790
|
+
* @returns True if the repair loses nothing; false otherwise
|
|
3791
|
+
*
|
|
3792
|
+
* @example
|
|
3793
|
+
* ```ts
|
|
3794
|
+
* const fault = { reason: 'type', path: ['a'], expected: 'number', received: '' } as const
|
|
3795
|
+
* matchesLosslessRepair(fault, { a: ' 30 ' }, { a: 30 }) // true
|
|
3796
|
+
* matchesLosslessRepair(fault, { a: '30.0' }, { a: 30 }) // false
|
|
3797
|
+
* ```
|
|
3798
|
+
*/
|
|
3799
|
+
function matchesLosslessRepair(fault, input, parsed) {
|
|
3800
|
+
if (fault.reason !== "type") return false;
|
|
3801
|
+
const keys = isString(fault.path) ? [fault.path] : fault.path;
|
|
3802
|
+
const route = INTRINSICS.from(iterateIndices(() => keys.length + 1), (index) => index === 0 ? "root" : keys[index - 1] ?? "");
|
|
3803
|
+
const raw = resolveField({ root: input }, route);
|
|
3804
|
+
const value = resolveField({ root: parsed }, route);
|
|
3805
|
+
switch (fault.expected) {
|
|
3806
|
+
case "number":
|
|
3807
|
+
case "integer": return isString(raw) && isNumber(value) && raw.trim() === INTRINSICS.text(value);
|
|
3808
|
+
case "literal": return isString(raw) && (isString(value) || isNumber(value) || isBoolean(value)) && raw.trim() === INTRINSICS.text(value);
|
|
3809
|
+
case "boolean": return raw === "true" || raw === "false";
|
|
3810
|
+
case "string": return isFiniteNumber(raw);
|
|
3811
|
+
default: return false;
|
|
3812
|
+
}
|
|
3813
|
+
}
|
|
3814
|
+
/**
|
|
3815
|
+
* Determines whether a readable value stays within the fixed JSON container-depth limit.
|
|
3816
|
+
*
|
|
3817
|
+
* @remarks
|
|
3818
|
+
* Counts array and plain-record containers on each active root-to-value path.
|
|
3819
|
+
* Primitive and readable non-record objects are leaves, active cycles add no
|
|
3820
|
+
* level, and shared aliases are answered from the shallowest depth at which
|
|
3821
|
+
* the alias already fit. Arrays are traversed through their reflected
|
|
3822
|
+
* own-index population, so sparse work is proportional to populated entries
|
|
3823
|
+
* rather than advertised length. Every observable operation is contained;
|
|
3824
|
+
* hostile or contradictory reads return `false`.
|
|
3825
|
+
*
|
|
3826
|
+
* The walk carries a settled-depth memo beside its active-path set: a
|
|
3827
|
+
* container that fit at depth `d` also fits at any depth `<= d`, because every
|
|
3828
|
+
* path below it is then shallower than the one already measured. Without it a
|
|
3140
3829
|
* node reachable by `k` distinct paths was re-walked `k` times, so an ORDINARY
|
|
3141
3830
|
* record graph with thirty shared aliases — thirty-one nodes — cost `2^30`
|
|
3142
3831
|
* visits through a public guard.
|
|
@@ -3183,7 +3872,7 @@ function matchesJSONDepth(value) {
|
|
|
3183
3872
|
};
|
|
3184
3873
|
if (array) {
|
|
3185
3874
|
const snapshot = readArrayEntries(entry);
|
|
3186
|
-
if (
|
|
3875
|
+
if (isFailure(snapshot)) return false;
|
|
3187
3876
|
const children = INTRINSICS.values(snapshot.value.entries);
|
|
3188
3877
|
for (const index of iterateIndices(() => children.length)) stack[stack.length] = {
|
|
3189
3878
|
operation: "enter",
|
|
@@ -3230,13 +3919,16 @@ function matchesJSONDepth(value) {
|
|
|
3230
3919
|
*
|
|
3231
3920
|
* Beside the ancestor set the walk keeps a walk-local PROVED set, so a node
|
|
3232
3921
|
* whose whole subtree already matched is not re-walked when a second path
|
|
3233
|
-
* reaches it.
|
|
3922
|
+
* reaches it. A supplied capture budget instead charges each occurrence that
|
|
3923
|
+
* ownership would copy; exhaustion throws `structure` with a `clone` cause.
|
|
3924
|
+
* Removing the recursion alone left the work exponential in shared
|
|
3234
3925
|
* aliases: thirty aliases — thirty-one ordinary records — cost `2^30` visits
|
|
3235
3926
|
* through `isJSONValue`, `parseJSONValue`, `canonicalStringify` and every
|
|
3236
3927
|
* `jsonShape` contract.
|
|
3237
3928
|
*
|
|
3238
3929
|
* @param entry - The value to inspect
|
|
3239
3930
|
* @param ancestors - Objects on the active traversal path
|
|
3931
|
+
* @param budget - Remaining capture nodes; aliases are charged for each copied occurrence when supplied
|
|
3240
3932
|
* @returns True if the value is a cycle-free JSON value; false otherwise
|
|
3241
3933
|
*
|
|
3242
3934
|
* @example
|
|
@@ -3245,8 +3937,12 @@ function matchesJSONDepth(value) {
|
|
|
3245
3937
|
* matchesJSONValue(Number.NaN, new WeakSet()) // false
|
|
3246
3938
|
* ```
|
|
3247
3939
|
*/
|
|
3248
|
-
function matchesJSONValue(entry, ancestors) {
|
|
3940
|
+
function matchesJSONValue(entry, ancestors, budget) {
|
|
3249
3941
|
return readValue(() => {
|
|
3942
|
+
if (budget !== void 0) {
|
|
3943
|
+
if (!INTRINSICS.safe(budget.left)) throw new ContractError("matchesJSONValue: invalid node budget", { code: "clone" });
|
|
3944
|
+
budget.left = INTRINSICS.min(budget.left, CLONE_NODE_LIMIT);
|
|
3945
|
+
}
|
|
3250
3946
|
const stack = [{
|
|
3251
3947
|
operation: "enter",
|
|
3252
3948
|
value: entry
|
|
@@ -3260,13 +3956,20 @@ function matchesJSONValue(entry, ancestors) {
|
|
|
3260
3956
|
admitVisited(proved, frame.value);
|
|
3261
3957
|
continue;
|
|
3262
3958
|
}
|
|
3959
|
+
if (budget !== void 0) {
|
|
3960
|
+
budget.left -= 1;
|
|
3961
|
+
if (budget.left < 0) throw new ContractError(`validateShape: default exceeds the capture node limit ${CLONE_NODE_LIMIT}`, {
|
|
3962
|
+
code: "clone",
|
|
3963
|
+
context: { limit: CLONE_NODE_LIMIT }
|
|
3964
|
+
});
|
|
3965
|
+
}
|
|
3263
3966
|
const node = frame.value;
|
|
3264
3967
|
if (node === null || isString(node) || isBoolean(node) || isFiniteNumber(node)) continue;
|
|
3265
3968
|
if (INTRINSICS.array(node)) {
|
|
3266
3969
|
if (matchesVisited(ancestors, node)) return false;
|
|
3267
|
-
if (matchesVisited(proved, node)) continue;
|
|
3970
|
+
if (budget === void 0 && matchesVisited(proved, node)) continue;
|
|
3268
3971
|
const snapshot = readArrayEntries(node);
|
|
3269
|
-
if (
|
|
3972
|
+
if (isFailure(snapshot)) throw snapshot.error;
|
|
3270
3973
|
if (!snapshot.value.dense) return false;
|
|
3271
3974
|
admitVisited(ancestors, node);
|
|
3272
3975
|
stack[stack.length] = {
|
|
@@ -3285,7 +3988,7 @@ function matchesJSONValue(entry, ancestors) {
|
|
|
3285
3988
|
}
|
|
3286
3989
|
if (!isRecord(node)) return false;
|
|
3287
3990
|
if (matchesVisited(ancestors, node)) return false;
|
|
3288
|
-
if (matchesVisited(proved, node)) continue;
|
|
3991
|
+
if (budget === void 0 && matchesVisited(proved, node)) continue;
|
|
3289
3992
|
admitVisited(ancestors, node);
|
|
3290
3993
|
stack[stack.length] = {
|
|
3291
3994
|
operation: "exit",
|
|
@@ -3395,7 +4098,7 @@ function schemaToObject(schema) {
|
|
|
3395
4098
|
return contain(() => {
|
|
3396
4099
|
return readValue(() => {
|
|
3397
4100
|
INTRINSICS.values(schema);
|
|
3398
|
-
if (schema.type === "object") return schema;
|
|
4101
|
+
if (INTRINSICS.own(schema, "type") && schema.type === "object") return schema;
|
|
3399
4102
|
return {
|
|
3400
4103
|
type: "object",
|
|
3401
4104
|
properties: { value: schema },
|
|
@@ -3501,7 +4204,7 @@ function canonicalStringify(value) {
|
|
|
3501
4204
|
if (matchesVisited(ancestors, container)) return void 0;
|
|
3502
4205
|
if (INTRINSICS.reflect.apply(INTRINSICS.recall, encodings, [container]) !== void 0) continue;
|
|
3503
4206
|
const snapshot = isArray(container) ? readArrayEntries(container) : void 0;
|
|
3504
|
-
if (snapshot !== void 0 &&
|
|
4207
|
+
if (snapshot !== void 0 && isFailure(snapshot)) throw snapshot.error;
|
|
3505
4208
|
if (snapshot !== void 0 && !snapshot.value.dense) return void 0;
|
|
3506
4209
|
const keys = snapshot === void 0 ? sortValues(INTRINSICS.keys(container)) : void 0;
|
|
3507
4210
|
const members = snapshot === void 0 ? INTRINSICS.from(iterateIndices(() => keys?.length ?? 0), (index) => {
|
|
@@ -3574,7 +4277,7 @@ function matchesISOInstant(value) {
|
|
|
3574
4277
|
const date = new INTRINSICS.date(value);
|
|
3575
4278
|
return !INTRINSICS.nan(INTRINSICS.reflect.apply(INTRINSICS.instant, date, []));
|
|
3576
4279
|
});
|
|
3577
|
-
return outcome
|
|
4280
|
+
return isSuccess(outcome) && outcome.value;
|
|
3578
4281
|
}
|
|
3579
4282
|
/**
|
|
3580
4283
|
* Classifies an already-bounded string against the pattern-only and
|
|
@@ -3864,7 +4567,7 @@ function preview(value) {
|
|
|
3864
4567
|
function buildStringFaults(shape, value, path, pattern) {
|
|
3865
4568
|
return readValue(() => {
|
|
3866
4569
|
const faults = [];
|
|
3867
|
-
if (shape.min !== void 0 && value
|
|
4570
|
+
if (INTRINSICS.own(shape, "min") && shape.min !== void 0 && !matchesStringLength(value, shape.min, void 0)) faults[faults.length] = {
|
|
3868
4571
|
reason: "constraint",
|
|
3869
4572
|
path,
|
|
3870
4573
|
expected: "string",
|
|
@@ -3872,7 +4575,7 @@ function buildStringFaults(shape, value, path, pattern) {
|
|
|
3872
4575
|
limit: shape.min,
|
|
3873
4576
|
received: preview(value)
|
|
3874
4577
|
};
|
|
3875
|
-
if (shape.max !== void 0 && value
|
|
4578
|
+
if (INTRINSICS.own(shape, "max") && shape.max !== void 0 && !matchesStringLength(value, void 0, shape.max)) faults[faults.length] = {
|
|
3876
4579
|
reason: "constraint",
|
|
3877
4580
|
path,
|
|
3878
4581
|
expected: "string",
|
|
@@ -3880,7 +4583,7 @@ function buildStringFaults(shape, value, path, pattern) {
|
|
|
3880
4583
|
limit: shape.max,
|
|
3881
4584
|
received: preview(value)
|
|
3882
4585
|
};
|
|
3883
|
-
const stateless = pattern ?? (shape.pattern === void 0 ? void 0 : readPattern(shape.pattern));
|
|
4586
|
+
const stateless = pattern ?? (!INTRINSICS.own(shape, "pattern") || shape.pattern === void 0 ? void 0 : readPattern(shape.pattern));
|
|
3884
4587
|
if (stateless !== void 0 && !matchesPattern(stateless, value)) {
|
|
3885
4588
|
const limit = readPatternSource(stateless);
|
|
3886
4589
|
faults[faults.length] = {
|
|
@@ -3920,16 +4623,16 @@ function buildStringFaults(shape, value, path, pattern) {
|
|
|
3920
4623
|
*/
|
|
3921
4624
|
function buildNumberFaults(shape, value, path) {
|
|
3922
4625
|
return readValue(() => {
|
|
3923
|
-
const expected = shape.integer === true ? "integer" : "number";
|
|
4626
|
+
const expected = INTRINSICS.own(shape, "integer") && shape.integer === true ? "integer" : "number";
|
|
3924
4627
|
const faults = [];
|
|
3925
|
-
if (shape.integer === true && !INTRINSICS.integer(value)) faults[faults.length] = {
|
|
4628
|
+
if (INTRINSICS.own(shape, "integer") && shape.integer === true && !INTRINSICS.integer(value)) faults[faults.length] = {
|
|
3926
4629
|
reason: "constraint",
|
|
3927
4630
|
path,
|
|
3928
4631
|
expected,
|
|
3929
4632
|
constraint: "integer",
|
|
3930
4633
|
received: preview(value)
|
|
3931
4634
|
};
|
|
3932
|
-
if (shape.min !== void 0 && value < shape.min) faults[faults.length] = {
|
|
4635
|
+
if (INTRINSICS.own(shape, "min") && shape.min !== void 0 && value < shape.min) faults[faults.length] = {
|
|
3933
4636
|
reason: "constraint",
|
|
3934
4637
|
path,
|
|
3935
4638
|
expected,
|
|
@@ -3937,7 +4640,7 @@ function buildNumberFaults(shape, value, path) {
|
|
|
3937
4640
|
limit: shape.min,
|
|
3938
4641
|
received: preview(value)
|
|
3939
4642
|
};
|
|
3940
|
-
if (shape.max !== void 0 && value > shape.max) faults[faults.length] = {
|
|
4643
|
+
if (INTRINSICS.own(shape, "max") && shape.max !== void 0 && value > shape.max) faults[faults.length] = {
|
|
3941
4644
|
reason: "constraint",
|
|
3942
4645
|
path,
|
|
3943
4646
|
expected,
|
|
@@ -3975,7 +4678,7 @@ function buildNumberFaults(shape, value, path) {
|
|
|
3975
4678
|
function buildArrayFaults(shape, length, path) {
|
|
3976
4679
|
return readValue(() => {
|
|
3977
4680
|
const faults = [];
|
|
3978
|
-
if (shape.min !== void 0 && length < shape.min) faults[faults.length] = {
|
|
4681
|
+
if (INTRINSICS.own(shape, "min") && shape.min !== void 0 && length < shape.min) faults[faults.length] = {
|
|
3979
4682
|
reason: "constraint",
|
|
3980
4683
|
path,
|
|
3981
4684
|
expected: "array",
|
|
@@ -3983,7 +4686,7 @@ function buildArrayFaults(shape, length, path) {
|
|
|
3983
4686
|
limit: shape.min,
|
|
3984
4687
|
received: INTRINSICS.text(length)
|
|
3985
4688
|
};
|
|
3986
|
-
if (shape.max !== void 0 && length > shape.max) faults[faults.length] = {
|
|
4689
|
+
if (INTRINSICS.own(shape, "max") && shape.max !== void 0 && length > shape.max) faults[faults.length] = {
|
|
3987
4690
|
reason: "constraint",
|
|
3988
4691
|
path,
|
|
3989
4692
|
expected: "array",
|
|
@@ -4057,7 +4760,7 @@ function shapeToKind(shape) {
|
|
|
4057
4760
|
return readValue(() => {
|
|
4058
4761
|
switch (shape.category) {
|
|
4059
4762
|
case "string": return "string";
|
|
4060
|
-
case "number": return shape.integer === true ? "integer" : "number";
|
|
4763
|
+
case "number": return INTRINSICS.own(shape, "integer") && shape.integer === true ? "integer" : "number";
|
|
4061
4764
|
case "boolean": return "boolean";
|
|
4062
4765
|
case "null": return "null";
|
|
4063
4766
|
case "literal": return "literal";
|
|
@@ -4065,6 +4768,7 @@ function shapeToKind(shape) {
|
|
|
4065
4768
|
case "object": return "object";
|
|
4066
4769
|
case "union": return "union";
|
|
4067
4770
|
case "json": return "json";
|
|
4771
|
+
case "shape": return "shape";
|
|
4068
4772
|
case "optional": return shapeToKind(shape.inner);
|
|
4069
4773
|
case "nullable": return shapeToKind(shape.inner);
|
|
4070
4774
|
case "raw": return "json";
|
|
@@ -4209,6 +4913,233 @@ for (const generator of [iterateIndices, encodeParts]) {
|
|
|
4209
4913
|
INTRINSICS.freeze(generator.prototype);
|
|
4210
4914
|
INTRINSICS.freeze(generator);
|
|
4211
4915
|
}
|
|
4916
|
+
/**
|
|
4917
|
+
* Extracts a fault's path as its segments, reading a string path as one segment.
|
|
4918
|
+
*
|
|
4919
|
+
* @param fault - The fault to read
|
|
4920
|
+
* @returns The path's segments, empty at the root
|
|
4921
|
+
*
|
|
4922
|
+
* @example
|
|
4923
|
+
* ```ts
|
|
4924
|
+
* import { extractPath } from '@orkestrel/contract'
|
|
4925
|
+
*
|
|
4926
|
+
* extractPath({ reason: 'extra', path: ['page', 'typo'] }) // ['page', 'typo']
|
|
4927
|
+
* ```
|
|
4928
|
+
*/
|
|
4929
|
+
function extractPath(fault) {
|
|
4930
|
+
return isString(fault.path) ? [fault.path] : fault.path;
|
|
4931
|
+
}
|
|
4932
|
+
/**
|
|
4933
|
+
* Extracts a schema's forms: the schema itself, then its union arms, so a read looks
|
|
4934
|
+
* through a nullable the same way everywhere.
|
|
4935
|
+
*
|
|
4936
|
+
* @param schema - The schema to read, or `undefined` where none was found
|
|
4937
|
+
* @returns The forms, empty when there is no schema
|
|
4938
|
+
*
|
|
4939
|
+
* @example
|
|
4940
|
+
* ```ts
|
|
4941
|
+
* import { extractForms } from '@orkestrel/contract'
|
|
4942
|
+
*
|
|
4943
|
+
* const object = { type: 'object' } as const
|
|
4944
|
+
* extractForms({ anyOf: [object] }) // [{ anyOf: [object] }, object]
|
|
4945
|
+
* ```
|
|
4946
|
+
*/
|
|
4947
|
+
function extractForms(schema) {
|
|
4948
|
+
if (schema === void 0) return [];
|
|
4949
|
+
const anyOf = INTRINSICS.own(schema, "anyOf") ? schema.anyOf : void 0;
|
|
4950
|
+
const oneOf = INTRINSICS.own(schema, "oneOf") ? schema.oneOf : void 0;
|
|
4951
|
+
return [schema, ...anyOf ?? oneOf ?? []];
|
|
4952
|
+
}
|
|
4953
|
+
/**
|
|
4954
|
+
* Extracts the part of a schema a path reaches, through properties, array items, record values,
|
|
4955
|
+
* and the arms of a union.
|
|
4956
|
+
*
|
|
4957
|
+
* @remarks
|
|
4958
|
+
* Each path segment takes one step, so a union that nests itself cannot hold the walk. Only a
|
|
4959
|
+
* schema's own `properties` keys match, so `constructor` reaches nothing. The walk keeps every
|
|
4960
|
+
* matching union declaration until the end; differing declarations yield no schema. Independently
|
|
4961
|
+
* built declarations agree when their canonical JSON encodings agree.
|
|
4962
|
+
*
|
|
4963
|
+
* @param schema - The schema to walk
|
|
4964
|
+
* @param path - The segments to follow
|
|
4965
|
+
* @returns The schema at that path, or `undefined` where no declaration or no agreement exists
|
|
4966
|
+
*
|
|
4967
|
+
* @example
|
|
4968
|
+
* ```ts
|
|
4969
|
+
* import { extractSchema } from '@orkestrel/contract'
|
|
4970
|
+
*
|
|
4971
|
+
* const schema = { type: 'object', properties: { limit: { type: 'number' } } } as const
|
|
4972
|
+
* extractSchema(schema, ['limit']) // { type: 'number' }
|
|
4973
|
+
* ```
|
|
4974
|
+
*/
|
|
4975
|
+
function extractSchema(schema, path) {
|
|
4976
|
+
const nodes = path.reduce((parents, key) => Array.from(new Set(parents.flatMap((node) => extractForms(node)).map((form) => {
|
|
4977
|
+
const properties = INTRINSICS.own(form, "properties") ? form.properties : void 0;
|
|
4978
|
+
const additional = INTRINSICS.own(form, "additionalProperties") ? form.additionalProperties : void 0;
|
|
4979
|
+
return properties !== void 0 && INTRINSICS.own(properties, key) ? properties[key] : isRecord(additional) ? additional : INTRINSICS.own(form, "items") ? form.items : void 0;
|
|
4980
|
+
}).filter((child) => child !== void 0))), [schema]);
|
|
4981
|
+
const first = nodes[0];
|
|
4982
|
+
if (nodes.length < 2) return first;
|
|
4983
|
+
const encoded = canonicalStringify(first);
|
|
4984
|
+
return encoded !== void 0 && nodes.every((node) => canonicalStringify(node) === encoded) ? first : void 0;
|
|
4985
|
+
}
|
|
4986
|
+
/**
|
|
4987
|
+
* Renders a path as text, encoding what a caller could have sent and eliding the middle of a long
|
|
4988
|
+
* one.
|
|
4989
|
+
*
|
|
4990
|
+
* @remarks
|
|
4991
|
+
* A segment that is not a plain identifier is JSON-encoded and length-capped, because a key the
|
|
4992
|
+
* caller sent is untrusted text. A path of more than four segments past 120 characters keeps its
|
|
4993
|
+
* first segment and its last three.
|
|
4994
|
+
*
|
|
4995
|
+
* @param path - The segments to render
|
|
4996
|
+
* @returns The segments joined with `.`, empty at the root
|
|
4997
|
+
*
|
|
4998
|
+
* @example
|
|
4999
|
+
* ```ts
|
|
5000
|
+
* import { renderPath } from '@orkestrel/contract'
|
|
5001
|
+
*
|
|
5002
|
+
* renderPath(['rows', '0', 'a b']) // 'rows.0."a b"'
|
|
5003
|
+
* ```
|
|
5004
|
+
*/
|
|
5005
|
+
function renderPath(path) {
|
|
5006
|
+
const segments = path.map((segment) => /^[\w$-]{1,64}$/u.test(segment) ? segment : preview(segment));
|
|
5007
|
+
const text = segments.join(".");
|
|
5008
|
+
return segments.length <= 4 || text.length <= 120 ? text : [
|
|
5009
|
+
...segments.slice(0, 1),
|
|
5010
|
+
"…",
|
|
5011
|
+
...segments.slice(-3)
|
|
5012
|
+
].join(".");
|
|
5013
|
+
}
|
|
5014
|
+
/**
|
|
5015
|
+
* Renders a list as text, naming at most `FAULT_LIST_LIMIT` of its entries and counting the rest.
|
|
5016
|
+
*
|
|
5017
|
+
* @param entries - The rendered entries
|
|
5018
|
+
* @returns The entries joined with `, `
|
|
5019
|
+
*
|
|
5020
|
+
* @example
|
|
5021
|
+
* ```ts
|
|
5022
|
+
* import { renderList } from '@orkestrel/contract'
|
|
5023
|
+
*
|
|
5024
|
+
* renderList(['city', 'units']) // 'city, units'
|
|
5025
|
+
* ```
|
|
5026
|
+
*/
|
|
5027
|
+
function renderList(entries) {
|
|
5028
|
+
const listed = entries.slice(0, 12).join(", ");
|
|
5029
|
+
return entries.length > 12 ? `${listed} and ${String(entries.length - 12)} more` : listed;
|
|
5030
|
+
}
|
|
5031
|
+
/**
|
|
5032
|
+
* Renders a schema as a short summary of what to send, such as `{"city": string, "note"?: string}`.
|
|
5033
|
+
*
|
|
5034
|
+
* @remarks
|
|
5035
|
+
* An object lists its required keys first and marks the rest with `?`, at most `FAULT_LIST_LIMIT`
|
|
5036
|
+
* of them. A leaf or array keeps its numeric bounds and pattern, a union joins its forms with ` | `,
|
|
5037
|
+
* and a schema that says nothing reads `any`. Each nested form takes half the room of its parent, and
|
|
5038
|
+
* below 16 characters a form stops at its type, so a schema that holds itself still ends. Text past
|
|
5039
|
+
* `room` is cut and closes with `…`. A type word outside JSON Schema's vocabulary is encoded.
|
|
5040
|
+
*
|
|
5041
|
+
* @param schema - The schema to summarize
|
|
5042
|
+
* @param room - The characters the summary can take. Default: `FAULT_SCHEMA_LIMIT`
|
|
5043
|
+
* @returns The summary
|
|
5044
|
+
*
|
|
5045
|
+
* @example
|
|
5046
|
+
* ```ts
|
|
5047
|
+
* import { renderForm } from '@orkestrel/contract'
|
|
5048
|
+
*
|
|
5049
|
+
* renderForm({ type: 'object', properties: { zip: { type: 'number' } }, required: ['zip'] }) // '{"zip": number}'
|
|
5050
|
+
* ```
|
|
5051
|
+
*/
|
|
5052
|
+
function renderForm(schema, room = 160) {
|
|
5053
|
+
const deep = room >= 16;
|
|
5054
|
+
const inner = Math.floor(room / 2);
|
|
5055
|
+
const anyOf = INTRINSICS.own(schema, "anyOf") ? schema.anyOf : void 0;
|
|
5056
|
+
const oneOf = INTRINSICS.own(schema, "oneOf") ? schema.oneOf : void 0;
|
|
5057
|
+
const forms = anyOf ?? oneOf;
|
|
5058
|
+
const required = (INTRINSICS.own(schema, "required") ? schema.required : void 0) ?? [];
|
|
5059
|
+
const entries = Object.entries((INTRINSICS.own(schema, "properties") ? schema.properties : void 0) ?? {});
|
|
5060
|
+
const type = INTRINSICS.own(schema, "type") ? schema.type : void 0;
|
|
5061
|
+
const enumeration = INTRINSICS.own(schema, "enum") ? schema.enum : void 0;
|
|
5062
|
+
const additional = INTRINSICS.own(schema, "additionalProperties") ? schema.additionalProperties : void 0;
|
|
5063
|
+
const listed = (deep ? entries : []).toSorted(([left], [right]) => Number(required.includes(right)) - Number(required.includes(left))).slice(0, 12).map(([key, child]) => `${preview(key)}${required.includes(key) ? "" : "?"}: ${renderForm(child, inner)}`);
|
|
5064
|
+
const left = entries.length - listed.length;
|
|
5065
|
+
const child = type === "array" ? INTRINSICS.own(schema, "items") ? schema.items : void 0 : isRecord(additional) ? additional : void 0;
|
|
5066
|
+
const bounds = Object.entries(schema).filter(([key]) => [
|
|
5067
|
+
"minLength",
|
|
5068
|
+
"maxLength",
|
|
5069
|
+
"minimum",
|
|
5070
|
+
"maximum",
|
|
5071
|
+
"minItems",
|
|
5072
|
+
"maxItems",
|
|
5073
|
+
"pattern"
|
|
5074
|
+
].includes(key)).map(([key, value]) => `${key} ${preview(value)}`);
|
|
5075
|
+
return truncateFaultText(forms !== void 0 ? deep ? forms.map((form) => renderForm(form, inner)).join(" | ") : "any" : enumeration !== void 0 ? enumeration.map((value) => preview(value)).join(" | ") : listed.length > 0 ? `{${[...listed, ...left > 0 ? [`and ${String(left)} more`] : []].join(", ")}}` : [
|
|
5076
|
+
type === void 0 ? entries.length > 0 ? "object" : "any" : JSON_SCHEMA_TYPES.some((name) => name === type) ? type : preview(type),
|
|
5077
|
+
...deep && child !== void 0 ? [` of ${renderForm(child, inner)}`] : [],
|
|
5078
|
+
...bounds.length > 0 ? [` (${bounds.join(", ")})`] : []
|
|
5079
|
+
].join(""), room);
|
|
5080
|
+
}
|
|
5081
|
+
/**
|
|
5082
|
+
* Describes one fault as the line a caller corrects from, naming what to send where the schema
|
|
5083
|
+
* says.
|
|
5084
|
+
*
|
|
5085
|
+
* @remarks
|
|
5086
|
+
* Each line opens with the fault's path (or `value` at the root) and reason. An `extra` fault lists
|
|
5087
|
+
* its parent's keys only when the declaring arms agree. A literal names candidates only where
|
|
5088
|
+
* the declarations agree; lists are deduplicated and bounded. A received preview stays as written
|
|
5089
|
+
* within PREVIEW_LIMIT and without control or line-separator characters; otherwise it is encoded
|
|
5090
|
+
* through preview. Received, constraint, and limit details read only own fields. A `variant` or
|
|
5091
|
+
* `oneOf` fault says that the value matches none of the declared forms, or several where one must
|
|
5092
|
+
* match, and summarizes each form through `renderForm`. It names no fault of any one form, because
|
|
5093
|
+
* none of them holds for the others. Those summaries share half of `FAULT_TEXT_LIMIT`.
|
|
5094
|
+
*
|
|
5095
|
+
* @param fault - The fault to describe
|
|
5096
|
+
* @param schema - The compiled schema the fault's path reads
|
|
5097
|
+
* @returns The line, with no line break
|
|
5098
|
+
*
|
|
5099
|
+
* @example
|
|
5100
|
+
* ```ts
|
|
5101
|
+
* import { describeFaultLine } from '@orkestrel/contract'
|
|
5102
|
+
*
|
|
5103
|
+
* describeFaultLine({ reason: 'missing', path: ['left'], expected: 'number' }, {}) // 'left: missing; expected number'
|
|
5104
|
+
* ```
|
|
5105
|
+
*/
|
|
5106
|
+
function describeFaultLine(fault, schema) {
|
|
5107
|
+
const path = extractPath(fault);
|
|
5108
|
+
const head = `${renderPath(path) || "value"}: ${fault.reason}`;
|
|
5109
|
+
if (fault.reason === "extra") {
|
|
5110
|
+
const declarations = extractForms(extractSchema(schema, path.slice(0, -1))).filter((form) => INTRINSICS.own(form, "properties") && form.properties !== void 0).map((form) => Object.keys(form.properties ?? {}));
|
|
5111
|
+
const first = declarations[0] ?? [];
|
|
5112
|
+
const keys = declarations.every((declared) => declared.length === first.length && declared.every((key) => first.includes(key))) ? first : [];
|
|
5113
|
+
return keys.length === 0 ? `${head}; this key is not declared, remove it` : `${head}; this key is not declared, remove it or use a declared key: ${renderList(keys.map((key) => renderPath([key])))}`;
|
|
5114
|
+
}
|
|
5115
|
+
const node = extractSchema(schema, path);
|
|
5116
|
+
const anyOf = node !== void 0 && INTRINSICS.own(node, "anyOf") ? node.anyOf : void 0;
|
|
5117
|
+
const oneOf = node !== void 0 && INTRINSICS.own(node, "oneOf") ? node.oneOf : void 0;
|
|
5118
|
+
if (fault.reason === "variant" || fault.reason === "oneOf") {
|
|
5119
|
+
const count = fault.reason === "variant" ? fault.variants : oneOf?.length;
|
|
5120
|
+
const forms = count === void 0 ? "the declared forms" : `the ${String(count)} declared forms`;
|
|
5121
|
+
const claim = fault.reason === "oneOf" && fault.matched > 0 ? `the value matches ${String(fault.matched)} of ${forms} and must match exactly one` : `the value matches none of ${forms}`;
|
|
5122
|
+
const declared = anyOf ?? oneOf ?? [];
|
|
5123
|
+
const shown = declared.slice(0, 12);
|
|
5124
|
+
const room = Math.min(160, Math.floor(FAULT_TEXT_LIMIT / 2 / Math.max(shown.length, 1)));
|
|
5125
|
+
const more = declared.length - shown.length;
|
|
5126
|
+
const summary = shown.length === 0 ? "" : `, one of: ${shown.map((form) => renderForm(form, room)).join(" or ")}${more > 0 ? ` and ${String(more)} more` : ""}`;
|
|
5127
|
+
return `${head}; ${claim}${fault.key === void 0 ? "" : `; discriminator ${preview(fault.key)} expects ${renderList((fault.expected ?? []).map((value) => preview(value)))}`}; send it exactly as the schema declares${summary}`;
|
|
5128
|
+
}
|
|
5129
|
+
const candidates = fault.expected === "literal" ? extractForms(node).flatMap((form) => [...(INTRINSICS.own(form, "enum") ? form.enum : void 0) ?? [], ...INTRINSICS.own(form, "type") && form.type === "null" ? [null] : []]) : [];
|
|
5130
|
+
const description = node !== void 0 && INTRINSICS.own(node, "description") ? node.description : void 0;
|
|
5131
|
+
const received = "received" in fault && INTRINSICS.own(fault, "received") ? fault.received : void 0;
|
|
5132
|
+
const shown = received !== void 0 && received.length <= 64 && !/[\p{Cc}\p{Zl}\p{Zp}]/u.test(received) ? received : preview(received);
|
|
5133
|
+
return [
|
|
5134
|
+
head,
|
|
5135
|
+
`expected ${fault.expected}`,
|
|
5136
|
+
...received !== void 0 ? [`received ${shown}`] : [],
|
|
5137
|
+
..."constraint" in fault && INTRINSICS.own(fault, "constraint") ? [`constraint ${fault.constraint}`] : [],
|
|
5138
|
+
..."limit" in fault && INTRINSICS.own(fault, "limit") && fault.limit !== void 0 ? [`limit ${preview(fault.limit)}`] : [],
|
|
5139
|
+
...description === void 0 ? [] : [`description ${preview(description)}`],
|
|
5140
|
+
...candidates.length > 0 ? [`send one of ${renderList(Array.from(new Set(candidates), (value) => preview(value)))}`] : []
|
|
5141
|
+
].join("; ");
|
|
5142
|
+
}
|
|
4212
5143
|
// src/core/JSONCloner.ts
|
|
4213
5144
|
/**
|
|
4214
5145
|
* Owns the state of one exact JSON snapshot operation.
|
|
@@ -4240,7 +5171,6 @@ var JSONCloner = class JSONCloner {
|
|
|
4240
5171
|
#owned;
|
|
4241
5172
|
#active;
|
|
4242
5173
|
#pending;
|
|
4243
|
-
#produced;
|
|
4244
5174
|
#poison;
|
|
4245
5175
|
#terminal;
|
|
4246
5176
|
constructor(value) {
|
|
@@ -4248,38 +5178,35 @@ var JSONCloner = class JSONCloner {
|
|
|
4248
5178
|
this.#owned = new JSONCloner.#weakSet();
|
|
4249
5179
|
this.#active = new JSONCloner.#weakSet();
|
|
4250
5180
|
this.#pending = [];
|
|
4251
|
-
this.#produced = 0;
|
|
4252
5181
|
this.#poison = void 0;
|
|
4253
5182
|
this.#terminal = void 0;
|
|
4254
5183
|
}
|
|
4255
5184
|
/**
|
|
4256
5185
|
* Clones the retained source into exact, deeply frozen JSON data.
|
|
4257
5186
|
*
|
|
5187
|
+
* @param budget - Remaining nodes shared with an enclosing ownership operation
|
|
4258
5188
|
* @returns The settled JSON snapshot
|
|
4259
5189
|
* @throws {ContractError} When the source is inexact, cyclic, unreadable, or cloning is reentered
|
|
4260
5190
|
*/
|
|
4261
|
-
clone() {
|
|
5191
|
+
clone(budget = { left: CLONE_NODE_LIMIT }) {
|
|
4262
5192
|
const terminal = this.#terminal;
|
|
4263
5193
|
if (terminal !== void 0) {
|
|
4264
|
-
if (terminal
|
|
5194
|
+
if (isSuccess(terminal)) return terminal.value;
|
|
4265
5195
|
throw terminal.error;
|
|
4266
5196
|
}
|
|
4267
5197
|
const poison = this.#poison;
|
|
4268
5198
|
if (poison !== void 0) {
|
|
4269
|
-
this.#terminal =
|
|
4270
|
-
success: false,
|
|
4271
|
-
error: poison
|
|
4272
|
-
};
|
|
5199
|
+
this.#terminal = failure(poison);
|
|
4273
5200
|
throw poison;
|
|
4274
5201
|
}
|
|
4275
5202
|
this.#poison = this.#create("JSONCloner.clone: JSON cloning may not be reentered");
|
|
4276
|
-
const outcome = attempt(() => this.#execute());
|
|
5203
|
+
const outcome = attempt(() => this.#execute(budget));
|
|
4277
5204
|
const interruption = this.#terminal;
|
|
4278
5205
|
if (interruption !== void 0) {
|
|
4279
|
-
if (interruption
|
|
5206
|
+
if (isSuccess(interruption)) return interruption.value;
|
|
4280
5207
|
this.#fail(interruption.error);
|
|
4281
5208
|
}
|
|
4282
|
-
if (outcome
|
|
5209
|
+
if (isSuccess(outcome)) {
|
|
4283
5210
|
this.#terminal = outcome;
|
|
4284
5211
|
return outcome.value;
|
|
4285
5212
|
}
|
|
@@ -4287,19 +5214,18 @@ var JSONCloner = class JSONCloner {
|
|
|
4287
5214
|
this.#fail(error);
|
|
4288
5215
|
}
|
|
4289
5216
|
#fail(error) {
|
|
4290
|
-
this.#terminal =
|
|
4291
|
-
success: false,
|
|
4292
|
-
error
|
|
4293
|
-
};
|
|
5217
|
+
this.#terminal = failure(error);
|
|
4294
5218
|
this.#pending = JSONCloner.#emptyPending;
|
|
4295
5219
|
throw error;
|
|
4296
5220
|
}
|
|
4297
|
-
#execute() {
|
|
4298
|
-
|
|
4299
|
-
|
|
5221
|
+
#execute(budget) {
|
|
5222
|
+
if (!INTRINSICS.safe(budget.left)) this.#refuse("cloneJSONValue: invalid node budget");
|
|
5223
|
+
budget.left = INTRINSICS.min(budget.left, CLONE_NODE_LIMIT);
|
|
5224
|
+
const root = this.#capture(this.#source, budget);
|
|
5225
|
+
this.#drain(budget);
|
|
4300
5226
|
return root;
|
|
4301
5227
|
}
|
|
4302
|
-
#drain() {
|
|
5228
|
+
#drain(budget) {
|
|
4303
5229
|
while (this.#pending.length > 0) {
|
|
4304
5230
|
const frame = this.#pending[this.#pending.length - 1];
|
|
4305
5231
|
if (frame === void 0) continue;
|
|
@@ -4312,7 +5238,7 @@ var JSONCloner = class JSONCloner {
|
|
|
4312
5238
|
continue;
|
|
4313
5239
|
}
|
|
4314
5240
|
frame.index += 1;
|
|
4315
|
-
const clone = this.#capture(entry[1]);
|
|
5241
|
+
const clone = this.#capture(entry[1], budget);
|
|
4316
5242
|
INTRINSICS.define(frame.clone, entry[0], {
|
|
4317
5243
|
value: clone,
|
|
4318
5244
|
enumerable: true,
|
|
@@ -4321,9 +5247,9 @@ var JSONCloner = class JSONCloner {
|
|
|
4321
5247
|
});
|
|
4322
5248
|
}
|
|
4323
5249
|
}
|
|
4324
|
-
#capture(value) {
|
|
4325
|
-
|
|
4326
|
-
if (
|
|
5250
|
+
#capture(value, budget) {
|
|
5251
|
+
budget.left -= 1;
|
|
5252
|
+
if (budget.left < 0) this.#refuse(`cloneJSONValue: snapshot exceeds the node limit ${CLONE_NODE_LIMIT}`, CLONE_NODE_LIMIT);
|
|
4327
5253
|
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
|
|
4328
5254
|
if (typeof value === "number") {
|
|
4329
5255
|
if (INTRINSICS.finite(value)) return value;
|
|
@@ -4335,7 +5261,7 @@ var JSONCloner = class JSONCloner {
|
|
|
4335
5261
|
#captureObject(source) {
|
|
4336
5262
|
if (matchesVisited(this.#active, source)) this.#refuse("cloneJSONValue: cycle detected");
|
|
4337
5263
|
const arrayOutcome = attempt(() => INTRINSICS.array(source));
|
|
4338
|
-
if (
|
|
5264
|
+
if (isFailure(arrayOutcome)) this.#refuse("cloneJSONValue: value brand could not be inspected");
|
|
4339
5265
|
if (arrayOutcome.value) return this.#schedule(source, true);
|
|
4340
5266
|
if (isRecord(source)) return this.#schedule(source, false);
|
|
4341
5267
|
this.#refuse("cloneJSONValue: object is not a plain record");
|
|
@@ -4354,9 +5280,9 @@ var JSONCloner = class JSONCloner {
|
|
|
4354
5280
|
}
|
|
4355
5281
|
#captureArray(source) {
|
|
4356
5282
|
const keysOutcome = attempt(() => INTRINSICS.reflect.members(source));
|
|
4357
|
-
if (
|
|
5283
|
+
if (isFailure(keysOutcome)) this.#refuse("cloneJSONValue: own keys could not be inspected");
|
|
4358
5284
|
const lengthOutcome = attempt(() => INTRINSICS.reflect.describe(source, "length"));
|
|
4359
|
-
if (
|
|
5285
|
+
if (isFailure(lengthOutcome)) this.#refuse("cloneJSONValue: array length could not be inspected");
|
|
4360
5286
|
const lengthDescriptor = lengthOutcome.value;
|
|
4361
5287
|
if (lengthDescriptor === void 0 || !("value" in lengthDescriptor) || typeof lengthDescriptor.value !== "number" || !INTRINSICS.integer(lengthDescriptor.value) || lengthDescriptor.value < 0 || lengthDescriptor.value > 4294967295 || lengthDescriptor.enumerable !== false || lengthDescriptor.configurable !== false) this.#refuse("cloneJSONValue: array is not intrinsic and dense");
|
|
4362
5288
|
const owned = collectMembers(keysOutcome.value);
|
|
@@ -4366,7 +5292,7 @@ var JSONCloner = class JSONCloner {
|
|
|
4366
5292
|
const key = INTRINSICS.text(index);
|
|
4367
5293
|
if (!matchesMember(owned, key)) this.#refuse("cloneJSONValue: array own keys are not exact");
|
|
4368
5294
|
const descriptorOutcome = attempt(() => INTRINSICS.reflect.describe(source, key));
|
|
4369
|
-
if (
|
|
5295
|
+
if (isFailure(descriptorOutcome)) this.#refuse("cloneJSONValue: array index could not be inspected");
|
|
4370
5296
|
const descriptor = descriptorOutcome.value;
|
|
4371
5297
|
if (descriptor === void 0 || !("value" in descriptor) || descriptor.enumerable !== true) this.#refuse("cloneJSONValue: array index is not enumerable data");
|
|
4372
5298
|
return [key, descriptor.value];
|
|
@@ -4374,22 +5300,25 @@ var JSONCloner = class JSONCloner {
|
|
|
4374
5300
|
}
|
|
4375
5301
|
#captureRecord(source) {
|
|
4376
5302
|
const keysOutcome = attempt(() => INTRINSICS.reflect.members(source));
|
|
4377
|
-
if (
|
|
5303
|
+
if (isFailure(keysOutcome)) this.#refuse("cloneJSONValue: own keys could not be inspected");
|
|
4378
5304
|
const names = keysOutcome.value;
|
|
4379
5305
|
return INTRINSICS.from(iterateIndices(() => names.length), (index) => {
|
|
4380
5306
|
const key = names[index];
|
|
4381
5307
|
if (typeof key !== "string") this.#refuse("cloneJSONValue: record has a symbol property");
|
|
4382
5308
|
const descriptorOutcome = attempt(() => INTRINSICS.reflect.describe(source, key));
|
|
4383
|
-
if (
|
|
5309
|
+
if (isFailure(descriptorOutcome)) this.#refuse("cloneJSONValue: record property could not be inspected");
|
|
4384
5310
|
const descriptor = descriptorOutcome.value;
|
|
4385
5311
|
if (descriptor === void 0 || !("value" in descriptor) || descriptor.enumerable !== true) this.#refuse("cloneJSONValue: record property is not enumerable data");
|
|
4386
5312
|
return [key, descriptor.value];
|
|
4387
5313
|
});
|
|
4388
5314
|
}
|
|
4389
|
-
#create(message) {
|
|
5315
|
+
#create(message, limit) {
|
|
4390
5316
|
const error = new ContractError(message, {
|
|
4391
5317
|
code: "clone",
|
|
4392
|
-
context: {
|
|
5318
|
+
context: {
|
|
5319
|
+
shape: "json",
|
|
5320
|
+
...limit === void 0 ? {} : { limit }
|
|
5321
|
+
}
|
|
4393
5322
|
});
|
|
4394
5323
|
admitVisited(this.#owned, error);
|
|
4395
5324
|
return error;
|
|
@@ -4397,10 +5326,10 @@ var JSONCloner = class JSONCloner {
|
|
|
4397
5326
|
#owns(error) {
|
|
4398
5327
|
return isObject(error) && matchesVisited(this.#owned, error);
|
|
4399
5328
|
}
|
|
4400
|
-
#refuse(message) {
|
|
5329
|
+
#refuse(message, limit) {
|
|
4401
5330
|
const terminal = this.#terminal;
|
|
4402
|
-
if (terminal !== void 0 &&
|
|
4403
|
-
throw this.#create(message);
|
|
5331
|
+
if (terminal !== void 0 && isFailure(terminal)) throw terminal.error;
|
|
5332
|
+
throw this.#create(message, limit);
|
|
4404
5333
|
}
|
|
4405
5334
|
static {
|
|
4406
5335
|
INTRINSICS.freeze(JSONCloner.#emptyPending);
|
|
@@ -4458,7 +5387,7 @@ var SchemaCloner = class SchemaCloner {
|
|
|
4458
5387
|
const state = this.#state;
|
|
4459
5388
|
switch (state.phase) {
|
|
4460
5389
|
case "settled":
|
|
4461
|
-
if (state.result
|
|
5390
|
+
if (isSuccess(state.result)) return state.result.value;
|
|
4462
5391
|
throw state.result.error;
|
|
4463
5392
|
case "running":
|
|
4464
5393
|
case "interrupted":
|
|
@@ -4477,16 +5406,10 @@ var SchemaCloner = class SchemaCloner {
|
|
|
4477
5406
|
}
|
|
4478
5407
|
#complete(outcome) {
|
|
4479
5408
|
const state = this.#state;
|
|
4480
|
-
if (state.phase === "interrupted") return this.#settle(
|
|
4481
|
-
|
|
4482
|
-
error: state.poison
|
|
4483
|
-
});
|
|
4484
|
-
if (outcome.success) return this.#settle(outcome);
|
|
5409
|
+
if (state.phase === "interrupted") return this.#settle(failure(state.poison));
|
|
5410
|
+
if (isSuccess(outcome)) return this.#settle(outcome);
|
|
4485
5411
|
const error = this.#owns(outcome.error) ? outcome.error : this.#create("cloneSchema: failed to create an owned schema snapshot", { cause: outcome.error });
|
|
4486
|
-
return this.#settle(
|
|
4487
|
-
success: false,
|
|
4488
|
-
error
|
|
4489
|
-
});
|
|
5412
|
+
return this.#settle(failure(error));
|
|
4490
5413
|
}
|
|
4491
5414
|
#execute() {
|
|
4492
5415
|
if (INTRINSICS.array(this.#source)) this.#refuse("cloneSchema: a schema root must be a record, not an array", "structure", []);
|
|
@@ -4548,7 +5471,7 @@ var SchemaCloner = class SchemaCloner {
|
|
|
4548
5471
|
}
|
|
4549
5472
|
#read(source, key, path) {
|
|
4550
5473
|
const outcome = attempt(() => INTRINSICS.reflect.read(source, key));
|
|
4551
|
-
if (outcome
|
|
5474
|
+
if (isSuccess(outcome)) return outcome.value;
|
|
4552
5475
|
throw this.#create("cloneSchema: property access failed", {
|
|
4553
5476
|
path,
|
|
4554
5477
|
cause: outcome.error
|
|
@@ -4599,7 +5522,7 @@ var SchemaCloner = class SchemaCloner {
|
|
|
4599
5522
|
phase: "settled",
|
|
4600
5523
|
result
|
|
4601
5524
|
};
|
|
4602
|
-
if (result
|
|
5525
|
+
if (isSuccess(result)) return result.value;
|
|
4603
5526
|
throw result.error;
|
|
4604
5527
|
}
|
|
4605
5528
|
static {
|
|
@@ -4612,7 +5535,23 @@ var SchemaCloner = class SchemaCloner {
|
|
|
4612
5535
|
var ShapePass = class ShapePass {
|
|
4613
5536
|
static #weakSet = WeakSet;
|
|
4614
5537
|
static #weakMap = WeakMap;
|
|
5538
|
+
static #arrays = Array.prototype;
|
|
5539
|
+
static #pop = Array.prototype.pop;
|
|
5540
|
+
static #iterators = INTRINSICS.prototype(INTRINSICS.reflect.apply(INTRINSICS.indices, [], []));
|
|
5541
|
+
static #blank = INTRINSICS.freeze(new INTRINSICS.pattern(""));
|
|
5542
|
+
static #fields = [
|
|
5543
|
+
"min",
|
|
5544
|
+
"max",
|
|
5545
|
+
"pattern",
|
|
5546
|
+
"description",
|
|
5547
|
+
"integer",
|
|
5548
|
+
"additionalProperties",
|
|
5549
|
+
"mode",
|
|
5550
|
+
"default"
|
|
5551
|
+
];
|
|
5552
|
+
static #seals = new ShapePass.#weakMap();
|
|
4615
5553
|
#source;
|
|
5554
|
+
#budget;
|
|
4616
5555
|
#stack = [];
|
|
4617
5556
|
#path = [];
|
|
4618
5557
|
#active = new ShapePass.#weakSet();
|
|
@@ -4622,20 +5561,91 @@ var ShapePass = class ShapePass {
|
|
|
4622
5561
|
#height = [];
|
|
4623
5562
|
#reach = [];
|
|
4624
5563
|
#via = [];
|
|
5564
|
+
#within = [];
|
|
4625
5565
|
#counts = [];
|
|
4626
5566
|
#schemas = new ShapePass.#weakMap();
|
|
4627
5567
|
#children = [];
|
|
4628
5568
|
#category;
|
|
4629
5569
|
#raw = 0;
|
|
5570
|
+
#defaulted = false;
|
|
5571
|
+
#open = false;
|
|
4630
5572
|
#structure;
|
|
4631
5573
|
#cycle;
|
|
4632
5574
|
#domain;
|
|
4633
|
-
#clock = 0;
|
|
4634
|
-
#oldest = 0;
|
|
4635
|
-
#trail = [];
|
|
4636
5575
|
#poison;
|
|
4637
|
-
constructor(shape) {
|
|
5576
|
+
constructor(shape, budget) {
|
|
4638
5577
|
this.#source = shape;
|
|
5578
|
+
this.#budget = budget;
|
|
5579
|
+
}
|
|
5580
|
+
/**
|
|
5581
|
+
* Validates a node fresh from a builder, freezes it, and records what the walk found.
|
|
5582
|
+
*
|
|
5583
|
+
* @remarks
|
|
5584
|
+
* The node's own fields are always observed, and each child is answered from its
|
|
5585
|
+
* seal rather than walked. Anything short of a clean answer takes the whole walk
|
|
5586
|
+
* instead, which is what authors every refusal, so a refusal reads the same and
|
|
5587
|
+
* fires at the same call as it would with no seals at all. That second look is safe
|
|
5588
|
+
* because the first read nothing a caller wrote: the node is this package's own
|
|
5589
|
+
* literal, and a child with no seal stops the survey before it is read.
|
|
5590
|
+
*
|
|
5591
|
+
* @param shape - The builder's fresh node, which no caller has seen
|
|
5592
|
+
* @param check - What the whole walk validates, when the node is only legal in a slot
|
|
5593
|
+
* @returns The frozen node
|
|
5594
|
+
* @throws {ContractError} When the node is refused
|
|
5595
|
+
*/
|
|
5596
|
+
static seal(shape, check = shape) {
|
|
5597
|
+
const frozen = INTRINSICS.freeze(shape);
|
|
5598
|
+
if (!new ShapePass(shape).#survey(check === shape ? 0 : 1)) new ShapePass(check, { left: CLONE_NODE_LIMIT }).validate();
|
|
5599
|
+
return frozen;
|
|
5600
|
+
}
|
|
5601
|
+
/**
|
|
5602
|
+
* Reports the expansion of a sealed node a compiler can take as it stands.
|
|
5603
|
+
*
|
|
5604
|
+
* @remarks
|
|
5605
|
+
* `undefined` sends the caller to its own ownership and validation: the node was
|
|
5606
|
+
* never sealed, an inherited field would answer a walk of it, or it is an `optional`
|
|
5607
|
+
* node, which a walk refuses as a root.
|
|
5608
|
+
*
|
|
5609
|
+
* @param shape - The declaration to look up
|
|
5610
|
+
* @returns The expansion a walk would report, or `undefined`
|
|
5611
|
+
*/
|
|
5612
|
+
static expansionOf(shape) {
|
|
5613
|
+
if (ShapePass.polluted()) return void 0;
|
|
5614
|
+
const seal = INTRINSICS.reflect.apply(INTRINSICS.recall, ShapePass.#seals, [shape]);
|
|
5615
|
+
return seal === void 0 || seal.category === "optional" ? void 0 : seal.count;
|
|
5616
|
+
}
|
|
5617
|
+
/**
|
|
5618
|
+
* Reports whether a walk could answer differently from a seal.
|
|
5619
|
+
*
|
|
5620
|
+
* @remarks
|
|
5621
|
+
* A field a node may leave out reads through to `Object.prototype`, which is a
|
|
5622
|
+
* verdict about the declaration, and a {@link tampered} host breaks a walk into a
|
|
5623
|
+
* refusal a seal would otherwise skip.
|
|
5624
|
+
*
|
|
5625
|
+
* @returns True when a walk has to answer rather than a seal
|
|
5626
|
+
*/
|
|
5627
|
+
static polluted() {
|
|
5628
|
+
if (ShapePass.tampered()) return true;
|
|
5629
|
+
return INTRINSICS.reflect.apply(INTRINSICS.some, ShapePass.#fields, [(field) => INTRINSICS.own(INTRINSICS.base, field)]);
|
|
5630
|
+
}
|
|
5631
|
+
/**
|
|
5632
|
+
* Reports whether a member a walk or a compile runs on has been replaced.
|
|
5633
|
+
*
|
|
5634
|
+
* @remarks
|
|
5635
|
+
* An index on either prototype, reached by array writes, a replaced `pop` or array
|
|
5636
|
+
* iterator `next`, and a flag getter a pattern's `flags` reads. An array's `length`
|
|
5637
|
+
* always passes its highest index, and own keys list an index first, so one read
|
|
5638
|
+
* answers for every index, and a name that merely opens with a digit only costs a walk.
|
|
5639
|
+
*
|
|
5640
|
+
* @returns True when the host is not the one this package loaded on
|
|
5641
|
+
*/
|
|
5642
|
+
static tampered() {
|
|
5643
|
+
if (ShapePass.#arrays.length !== 0) return true;
|
|
5644
|
+
const first = INTRINSICS.reflect.members(INTRINSICS.base)[0];
|
|
5645
|
+
if (typeof first === "string" && first >= "0" && first < ":") return true;
|
|
5646
|
+
if (INTRINSICS.describe(ShapePass.#arrays, "pop")?.value !== ShapePass.#pop) return true;
|
|
5647
|
+
if (INTRINSICS.describe(ShapePass.#iterators, "next")?.value !== INTRINSICS.advance) return true;
|
|
5648
|
+
return !holds(() => readPatternFlags(ShapePass.#blank) === "");
|
|
4639
5649
|
}
|
|
4640
5650
|
/** Poisons this pass and throws the same refusal on every reentry. */
|
|
4641
5651
|
interrupt() {
|
|
@@ -4649,7 +5659,7 @@ var ShapePass = class ShapePass {
|
|
|
4649
5659
|
validate() {
|
|
4650
5660
|
const outcome = attempt(() => this.#execute());
|
|
4651
5661
|
if (this.#poison !== void 0) throw this.#poison;
|
|
4652
|
-
if (outcome
|
|
5662
|
+
if (isSuccess(outcome)) return this.#counts[0] ?? 1;
|
|
4653
5663
|
if (isContractError(outcome.error)) throw outcome.error;
|
|
4654
5664
|
throw new ContractError("validateShape: shape reflection failed", {
|
|
4655
5665
|
code: "structure",
|
|
@@ -4657,6 +5667,34 @@ var ShapePass = class ShapePass {
|
|
|
4657
5667
|
cause: outcome.error
|
|
4658
5668
|
});
|
|
4659
5669
|
}
|
|
5670
|
+
#survey(lift) {
|
|
5671
|
+
if (ShapePass.polluted()) return false;
|
|
5672
|
+
const shape = this.#source;
|
|
5673
|
+
if (!isRecord(shape)) return false;
|
|
5674
|
+
const outcome = attempt(() => this.#observe(shape, 0));
|
|
5675
|
+
if (isFailure(outcome) || !outcome.value || this.#domain !== void 0) return false;
|
|
5676
|
+
const seal = {
|
|
5677
|
+
category: shape.category,
|
|
5678
|
+
reach: this.#raw,
|
|
5679
|
+
count: 1,
|
|
5680
|
+
defaulted: this.#defaulted,
|
|
5681
|
+
held: this.#defaulted
|
|
5682
|
+
};
|
|
5683
|
+
for (const index of iterateIndices(() => this.#children.length)) {
|
|
5684
|
+
const child = this.#children[index];
|
|
5685
|
+
const known = INTRINSICS.reflect.apply(INTRINSICS.recall, ShapePass.#seals, [child?.shape]);
|
|
5686
|
+
if (child === void 0 || known === void 0) return false;
|
|
5687
|
+
if (known.category === "optional" && !child.optional) return false;
|
|
5688
|
+
if (known.held && shape.category === "union") return false;
|
|
5689
|
+
if (known.defaulted && this.#open) return false;
|
|
5690
|
+
seal.reach = INTRINSICS.max(seal.reach, known.reach + 1);
|
|
5691
|
+
seal.count += known.count;
|
|
5692
|
+
seal.held ||= known.held;
|
|
5693
|
+
}
|
|
5694
|
+
if (seal.reach + lift > 512) return false;
|
|
5695
|
+
INTRINSICS.reflect.apply(INTRINSICS.retain, ShapePass.#seals, [shape, seal]);
|
|
5696
|
+
return true;
|
|
5697
|
+
}
|
|
4660
5698
|
#execute() {
|
|
4661
5699
|
this.#stack[this.#stack.length] = {
|
|
4662
5700
|
operation: "enter",
|
|
@@ -4678,9 +5716,11 @@ var ShapePass = class ShapePass {
|
|
|
4678
5716
|
this.#path.length -= frame.segments;
|
|
4679
5717
|
return;
|
|
4680
5718
|
}
|
|
4681
|
-
const
|
|
4682
|
-
|
|
4683
|
-
|
|
5719
|
+
const first = INTRINSICS.own(frame, "first") ? frame.first : void 0;
|
|
5720
|
+
const second = INTRINSICS.own(frame, "second") ? frame.second : void 0;
|
|
5721
|
+
const segments = (first === void 0 ? 0 : 1) + (second === void 0 ? 0 : 1);
|
|
5722
|
+
if (first !== void 0) this.#path[this.#path.length] = first;
|
|
5723
|
+
if (second !== void 0) this.#path[this.#path.length] = second;
|
|
4684
5724
|
const current = frame.shape;
|
|
4685
5725
|
if (typeof current !== "object" || current === null || !isRecord(current)) {
|
|
4686
5726
|
this.#structure ??= new ContractError("validateShape: every structural child must be a shape", {
|
|
@@ -4707,8 +5747,11 @@ var ShapePass = class ShapePass {
|
|
|
4707
5747
|
this.#children = [];
|
|
4708
5748
|
this.#category = void 0;
|
|
4709
5749
|
this.#raw = 0;
|
|
5750
|
+
this.#defaulted = false;
|
|
5751
|
+
this.#open = false;
|
|
4710
5752
|
const outcome = attempt(() => this.#observe(current, frame.depth));
|
|
4711
|
-
|
|
5753
|
+
this.#spend(0);
|
|
5754
|
+
if (isFailure(outcome) || !outcome.value) {
|
|
4712
5755
|
this.#structure ??= new ContractError("validateShape: every node must be a recognized shape", {
|
|
4713
5756
|
code: "structure",
|
|
4714
5757
|
context: { path: pathOf(this.#path) }
|
|
@@ -4724,7 +5767,7 @@ var ShapePass = class ShapePass {
|
|
|
4724
5767
|
if (descriptor === void 0 || !INTRINSICS.own(descriptor, "value")) return this.#refuse("validateShape: every node must be a recognized shape");
|
|
4725
5768
|
const category = current.category;
|
|
4726
5769
|
if (current.category !== category || descriptor.value !== category) return this.#refuse("validateShape: every node must be a recognized shape");
|
|
4727
|
-
const fields =
|
|
5770
|
+
const fields = typeof category === "string" && INTRINSICS.own(SHAPE_FIELDS, category) ? SHAPE_FIELDS[category] : void 0;
|
|
4728
5771
|
if (fields === void 0) return this.#refuse("validateShape: every node must be a recognized shape");
|
|
4729
5772
|
this.#category = category;
|
|
4730
5773
|
if (!this.#scan(current, fields)) return false;
|
|
@@ -4732,66 +5775,27 @@ var ShapePass = class ShapePass {
|
|
|
4732
5775
|
if (category === "raw") {
|
|
4733
5776
|
const schema = current.schema;
|
|
4734
5777
|
const outcome = attempt(() => this.#inspect(schema));
|
|
4735
|
-
if (
|
|
5778
|
+
if (isFailure(outcome)) return this.#refuse("validateShape: every node must be a recognized shape", "schema");
|
|
4736
5779
|
if (!outcome.value) return false;
|
|
4737
5780
|
if (depth + this.#raw > 512) return this.#refuse("validateShape: raw schema exceeds the compilation depth limit", "schema");
|
|
4738
5781
|
}
|
|
4739
5782
|
if (category === "object") {
|
|
4740
5783
|
const outcome = attempt(() => this.#populate(current));
|
|
4741
|
-
if (
|
|
5784
|
+
if (isFailure(outcome)) return this.#refuse("validateShape: every node must be a recognized shape", "properties");
|
|
4742
5785
|
return outcome.value;
|
|
4743
5786
|
}
|
|
4744
5787
|
if (category === "union") {
|
|
4745
5788
|
const outcome = attempt(() => this.#populate(current));
|
|
4746
|
-
if (
|
|
5789
|
+
if (isFailure(outcome)) return this.#refuse("validateShape: every node must be a recognized shape", "variants");
|
|
4747
5790
|
return outcome.value;
|
|
4748
5791
|
}
|
|
4749
5792
|
if (category === "literal") {
|
|
4750
5793
|
const outcome = attempt(() => this.#populate(current));
|
|
4751
|
-
if (
|
|
5794
|
+
if (isFailure(outcome)) return this.#refuse("validateShape: every node must be a recognized shape", "values");
|
|
4752
5795
|
return outcome.value;
|
|
4753
5796
|
}
|
|
4754
5797
|
return this.#populate(current);
|
|
4755
5798
|
}
|
|
4756
|
-
#recognize(category) {
|
|
4757
|
-
switch (category) {
|
|
4758
|
-
case "string": return [
|
|
4759
|
-
"min",
|
|
4760
|
-
"max",
|
|
4761
|
-
"pattern",
|
|
4762
|
-
"description"
|
|
4763
|
-
];
|
|
4764
|
-
case "number": return [
|
|
4765
|
-
"integer",
|
|
4766
|
-
"min",
|
|
4767
|
-
"max",
|
|
4768
|
-
"description"
|
|
4769
|
-
];
|
|
4770
|
-
case "boolean":
|
|
4771
|
-
case "null":
|
|
4772
|
-
case "json": return ["description"];
|
|
4773
|
-
case "literal": return ["values", "description"];
|
|
4774
|
-
case "array": return [
|
|
4775
|
-
"items",
|
|
4776
|
-
"min",
|
|
4777
|
-
"max",
|
|
4778
|
-
"description"
|
|
4779
|
-
];
|
|
4780
|
-
case "object": return [
|
|
4781
|
-
"properties",
|
|
4782
|
-
"additionalProperties",
|
|
4783
|
-
"description"
|
|
4784
|
-
];
|
|
4785
|
-
case "union": return [
|
|
4786
|
-
"variants",
|
|
4787
|
-
"mode",
|
|
4788
|
-
"description"
|
|
4789
|
-
];
|
|
4790
|
-
case "optional":
|
|
4791
|
-
case "nullable": return ["inner"];
|
|
4792
|
-
case "raw": return ["schema"];
|
|
4793
|
-
}
|
|
4794
|
-
}
|
|
4795
5799
|
#scan(current, fields) {
|
|
4796
5800
|
for (const fieldIndex of iterateIndices(() => fields.length)) {
|
|
4797
5801
|
const field = fields[fieldIndex];
|
|
@@ -4808,12 +5812,16 @@ var ShapePass = class ShapePass {
|
|
|
4808
5812
|
}
|
|
4809
5813
|
return field === "pattern" && isRegExp(first) && isRegExp(second) && readPatternSource(first) === readPatternSource(second) && readPatternFlags(first) === readPatternFlags(second) && INTRINSICS.frozen(first) && INTRINSICS.frozen(second);
|
|
4810
5814
|
});
|
|
4811
|
-
if (
|
|
5815
|
+
if (isFailure(outcome) || !outcome.value) return this.#refuse("validateShape: every node must be a recognized shape", field);
|
|
4812
5816
|
}
|
|
4813
5817
|
return true;
|
|
4814
5818
|
}
|
|
4815
5819
|
#constrain(current) {
|
|
4816
5820
|
if (current.category !== "optional" && current.category !== "nullable" && current.category !== "raw" && current.description !== void 0 && typeof current.description !== "string") return this.#refuse("validateShape: description must be a string", "description");
|
|
5821
|
+
if (current.category === "optional" && INTRINSICS.own(current, "default")) {
|
|
5822
|
+
if (!holds(() => matchesJSONValue(current.default, new ShapePass.#weakSet(), this.#budget))) return this.#refuse("validateShape: an optional default must be a JSON value", "default");
|
|
5823
|
+
this.#defaulted = true;
|
|
5824
|
+
}
|
|
4817
5825
|
if (current.category === "string") {
|
|
4818
5826
|
if (current.min !== void 0 && typeof current.min !== "number") return this.#refuse("validateShape: string min must be a number", "min");
|
|
4819
5827
|
if (current.max !== void 0 && typeof current.max !== "number") return this.#refuse("validateShape: string max must be a number", "max");
|
|
@@ -4849,7 +5857,7 @@ var ShapePass = class ShapePass {
|
|
|
4849
5857
|
received: INTRINSICS.text(current.max)
|
|
4850
5858
|
}
|
|
4851
5859
|
});
|
|
4852
|
-
if (this.#domain === void 0 && current.min !== void 0 && current.max !== void 0 && current.min > current.max) this.#domain = new ContractError("validateShape: a number shape has min greater than max", {
|
|
5860
|
+
if (this.#domain === void 0 && current.min !== void 0 && current.max !== void 0 && typeof current.min === "number" && typeof current.max === "number" && current.min > current.max) this.#domain = new ContractError("validateShape: a number shape has min greater than max", {
|
|
4853
5861
|
code: "range",
|
|
4854
5862
|
context: {
|
|
4855
5863
|
path: pathOf(this.#path),
|
|
@@ -4868,7 +5876,7 @@ var ShapePass = class ShapePass {
|
|
|
4868
5876
|
}
|
|
4869
5877
|
if (current.category === "array" && current.min !== void 0 && typeof current.min !== "number") return this.#refuse("validateShape: array min must be a number", "min");
|
|
4870
5878
|
if (current.category === "array" && current.max !== void 0 && typeof current.max !== "number") return this.#refuse("validateShape: array max must be a number", "max");
|
|
4871
|
-
if (current.category === "array" && this.#domain === void 0 && current.min !== void 0 && (!INTRINSICS.safe(current.min) || current.min < 0)) this.#domain = new ContractError("validateShape: an array shape min must be a non-negative safe integer", {
|
|
5879
|
+
if (current.category === "array" && this.#domain === void 0 && current.min !== void 0 && (typeof current.min !== "number" || !INTRINSICS.safe(current.min) || current.min < 0)) this.#domain = new ContractError("validateShape: an array shape min must be a non-negative safe integer", {
|
|
4872
5880
|
code: "bound",
|
|
4873
5881
|
context: {
|
|
4874
5882
|
path: pathOf(this.#path),
|
|
@@ -4877,7 +5885,7 @@ var ShapePass = class ShapePass {
|
|
|
4877
5885
|
received: INTRINSICS.text(current.min)
|
|
4878
5886
|
}
|
|
4879
5887
|
});
|
|
4880
|
-
if (current.category === "array" && this.#domain === void 0 && current.max !== void 0 && (!INTRINSICS.safe(current.max) || current.max < 0)) this.#domain = new ContractError("validateShape: an array shape max must be a non-negative safe integer", {
|
|
5888
|
+
if (current.category === "array" && this.#domain === void 0 && current.max !== void 0 && (typeof current.max !== "number" || !INTRINSICS.safe(current.max) || current.max < 0)) this.#domain = new ContractError("validateShape: an array shape max must be a non-negative safe integer", {
|
|
4881
5889
|
code: "bound",
|
|
4882
5890
|
context: {
|
|
4883
5891
|
path: pathOf(this.#path),
|
|
@@ -4886,7 +5894,7 @@ var ShapePass = class ShapePass {
|
|
|
4886
5894
|
received: INTRINSICS.text(current.max)
|
|
4887
5895
|
}
|
|
4888
5896
|
});
|
|
4889
|
-
if (current.category === "array" && this.#domain === void 0 && current.min !== void 0 && current.max !== void 0 && current.min > current.max) this.#domain = new ContractError("validateShape: an array shape has min greater than max", {
|
|
5897
|
+
if (current.category === "array" && this.#domain === void 0 && current.min !== void 0 && current.max !== void 0 && typeof current.min === "number" && typeof current.max === "number" && current.min > current.max) this.#domain = new ContractError("validateShape: an array shape has min greater than max", {
|
|
4890
5898
|
code: "range",
|
|
4891
5899
|
context: {
|
|
4892
5900
|
path: pathOf(this.#path),
|
|
@@ -4908,7 +5916,7 @@ var ShapePass = class ShapePass {
|
|
|
4908
5916
|
});
|
|
4909
5917
|
}
|
|
4910
5918
|
#restrict(current, source = "", flags = "") {
|
|
4911
|
-
if (this.#domain === void 0 && current.min !== void 0 && (!INTRINSICS.safe(current.min) || current.min < 0)) this.#domain = new ContractError("validateShape: a string shape min must be a non-negative safe integer", {
|
|
5919
|
+
if (this.#domain === void 0 && current.min !== void 0 && (typeof current.min !== "number" || !INTRINSICS.safe(current.min) || current.min < 0)) this.#domain = new ContractError("validateShape: a string shape min must be a non-negative safe integer", {
|
|
4912
5920
|
code: "bound",
|
|
4913
5921
|
context: {
|
|
4914
5922
|
path: pathOf(this.#path),
|
|
@@ -4917,7 +5925,7 @@ var ShapePass = class ShapePass {
|
|
|
4917
5925
|
received: INTRINSICS.text(current.min)
|
|
4918
5926
|
}
|
|
4919
5927
|
});
|
|
4920
|
-
if (this.#domain === void 0 && current.max !== void 0 && (!INTRINSICS.safe(current.max) || current.max < 0)) this.#domain = new ContractError("validateShape: a string shape max must be a non-negative safe integer", {
|
|
5928
|
+
if (this.#domain === void 0 && current.max !== void 0 && (typeof current.max !== "number" || !INTRINSICS.safe(current.max) || current.max < 0)) this.#domain = new ContractError("validateShape: a string shape max must be a non-negative safe integer", {
|
|
4921
5929
|
code: "bound",
|
|
4922
5930
|
context: {
|
|
4923
5931
|
path: pathOf(this.#path),
|
|
@@ -4934,7 +5942,7 @@ var ShapePass = class ShapePass {
|
|
|
4934
5942
|
received: `/${source}/${flags}`
|
|
4935
5943
|
}
|
|
4936
5944
|
});
|
|
4937
|
-
if (this.#domain === void 0 && current.min !== void 0 && current.max !== void 0 && current.min > current.max) this.#domain = new ContractError("validateShape: a string shape has min greater than max", {
|
|
5945
|
+
if (this.#domain === void 0 && current.min !== void 0 && current.max !== void 0 && typeof current.min === "number" && typeof current.max === "number" && current.min > current.max) this.#domain = new ContractError("validateShape: a string shape has min greater than max", {
|
|
4938
5946
|
code: "range",
|
|
4939
5947
|
context: {
|
|
4940
5948
|
path: pathOf(this.#path),
|
|
@@ -4943,6 +5951,7 @@ var ShapePass = class ShapePass {
|
|
|
4943
5951
|
});
|
|
4944
5952
|
}
|
|
4945
5953
|
#inspect(source) {
|
|
5954
|
+
if (this.#structure !== void 0) return true;
|
|
4946
5955
|
if (!isRecord(source)) return this.#refuse("validateShape: raw schema must be a plain record", "schema");
|
|
4947
5956
|
const active = new ShapePass.#weakSet();
|
|
4948
5957
|
const stack = [{
|
|
@@ -4957,24 +5966,27 @@ var ShapePass = class ShapePass {
|
|
|
4957
5966
|
this.#settleSchema(frame.schema, frame.nested);
|
|
4958
5967
|
continue;
|
|
4959
5968
|
}
|
|
5969
|
+
const first = INTRINSICS.own(frame, "first") ? frame.first : void 0;
|
|
5970
|
+
const second = INTRINSICS.own(frame, "second") ? frame.second : void 0;
|
|
4960
5971
|
const schema = frame.schema;
|
|
4961
|
-
if (!isRecord(schema)) return this.#
|
|
4962
|
-
if (matchesVisited(active, schema)) return this.#
|
|
5972
|
+
if (!isRecord(schema)) return this.#refuseSchema("validateShape: every raw schema child must be a plain record", stack, first, second, void 0);
|
|
5973
|
+
if (matchesVisited(active, schema)) return this.#refuseSchema("validateShape: a raw schema may not contain a cycle", stack, first, second, void 0);
|
|
4963
5974
|
if (INTRINSICS.reflect.apply(INTRINSICS.recall, this.#schemas, [schema]) !== void 0) continue;
|
|
4964
5975
|
admitVisited(active, schema);
|
|
4965
5976
|
const keyList = INTRINSICS.keys(schema);
|
|
4966
5977
|
for (const keyIndex of iterateIndices(() => keyList.length)) {
|
|
4967
5978
|
const key = keyList[keyIndex];
|
|
4968
5979
|
if (key === void 0) continue;
|
|
4969
|
-
if (key !== "type" && key !== "description" && key !== "enum" && key !== "minLength" && key !== "maxLength" && key !== "pattern" && key !== "format" && key !== "minimum" && key !== "maximum" && key !== "minItems" && key !== "maxItems" && key !== "items" && key !== "properties" && key !== "required" && key !== "additionalProperties" && key !== "anyOf" && key !== "oneOf") return this.#
|
|
5980
|
+
if (key !== "type" && key !== "description" && key !== "default" && key !== "enum" && key !== "minLength" && key !== "maxLength" && key !== "pattern" && key !== "format" && key !== "minimum" && key !== "maximum" && key !== "minItems" && key !== "maxItems" && key !== "items" && key !== "properties" && key !== "required" && key !== "additionalProperties" && key !== "anyOf" && key !== "oneOf") return this.#refuseSchema("validateShape: raw schema contains an unsupported keyword", stack, first, second, key);
|
|
4970
5981
|
}
|
|
4971
|
-
const category = schema.type;
|
|
4972
|
-
if (category !== void 0 && category !== "null" && category !== "boolean" && category !== "object" && category !== "array" && category !== "number" && category !== "integer" && category !== "string") return this.#
|
|
4973
|
-
if (schema.description !== void 0 && typeof schema.description !== "string") return this.#
|
|
4974
|
-
if (schema
|
|
4975
|
-
if (schema.
|
|
4976
|
-
|
|
4977
|
-
if (
|
|
5982
|
+
const category = INTRINSICS.own(schema, "type") ? schema.type : void 0;
|
|
5983
|
+
if (category !== void 0 && category !== "null" && category !== "boolean" && category !== "object" && category !== "array" && category !== "number" && category !== "integer" && category !== "string") return this.#refuseSchema("validateShape: raw schema type is outside the supported vocabulary", stack, first, second, "type");
|
|
5984
|
+
if (INTRINSICS.own(schema, "description") && schema.description !== void 0 && typeof schema.description !== "string") return this.#refuseSchema("validateShape: raw schema description must be a string", stack, first, second, "description");
|
|
5985
|
+
if (INTRINSICS.own(schema, "default") && !isJSONValue(schema.default)) return this.#refuseSchema("validateShape: raw schema default must be a JSON value", stack, first, second, "default");
|
|
5986
|
+
if (INTRINSICS.own(schema, "format") && schema.format !== void 0 && typeof schema.format !== "string") return this.#refuseSchema("validateShape: raw schema format must be a string", stack, first, second, "format");
|
|
5987
|
+
if (INTRINSICS.own(schema, "pattern") && schema.pattern !== void 0) {
|
|
5988
|
+
if (typeof schema.pattern !== "string") return this.#refuseSchema("validateShape: raw schema pattern must be a string", stack, first, second, "pattern");
|
|
5989
|
+
if (isFailure(attempt(() => INTRINSICS.reflect.apply(INTRINSICS.pattern, void 0, [schema.pattern])))) return this.#refuseSchema("validateShape: raw schema pattern must be valid", stack, first, second, "pattern");
|
|
4978
5990
|
}
|
|
4979
5991
|
const lengthList = [
|
|
4980
5992
|
"minLength",
|
|
@@ -4985,77 +5997,95 @@ var ShapePass = class ShapePass {
|
|
|
4985
5997
|
for (const lengthIndex of iterateIndices(() => lengthList.length)) {
|
|
4986
5998
|
const key = lengthList[lengthIndex];
|
|
4987
5999
|
if (key === void 0) continue;
|
|
4988
|
-
const value = schema[key];
|
|
4989
|
-
if (value !== void 0 && (!INTRINSICS.safe(value) || INTRINSICS.numeric(value) < 0)) return this.#
|
|
6000
|
+
const value = INTRINSICS.own(schema, key) ? schema[key] : void 0;
|
|
6001
|
+
if (value !== void 0 && (!INTRINSICS.safe(value) || INTRINSICS.numeric(value) < 0)) return this.#refuseSchema("validateShape: raw schema length bounds must be non-negative safe integers", stack, first, second, key);
|
|
4990
6002
|
}
|
|
4991
6003
|
const rangeList = ["minimum", "maximum"];
|
|
4992
6004
|
for (const rangeIndex of iterateIndices(() => rangeList.length)) {
|
|
4993
6005
|
const key = rangeList[rangeIndex];
|
|
4994
6006
|
if (key === void 0) continue;
|
|
4995
|
-
const value = schema[key];
|
|
4996
|
-
if (value !== void 0 && (typeof value !== "number" || !INTRINSICS.finite(value))) return this.#
|
|
6007
|
+
const value = INTRINSICS.own(schema, key) ? schema[key] : void 0;
|
|
6008
|
+
if (value !== void 0 && (typeof value !== "number" || !INTRINSICS.finite(value))) return this.#refuseSchema("validateShape: raw schema numeric bounds must be finite numbers", stack, first, second, key);
|
|
4997
6009
|
}
|
|
4998
|
-
const population = schema.enum;
|
|
6010
|
+
const population = INTRINSICS.own(schema, "enum") ? schema.enum : void 0;
|
|
4999
6011
|
if (population !== void 0) {
|
|
5000
|
-
if (!INTRINSICS.array(population)) return this.#
|
|
6012
|
+
if (!INTRINSICS.array(population)) return this.#refuseSchema("validateShape: raw schema enum must be a non-empty array", stack, first, second, "enum");
|
|
5001
6013
|
const snapshot = readArrayEntries(population);
|
|
5002
|
-
if (
|
|
5003
|
-
if (!snapshot.value.dense) return this.#
|
|
6014
|
+
if (isFailure(snapshot) || snapshot.value.entries.length === 0) return this.#refuseSchema("validateShape: raw schema enum must be a non-empty array", stack, first, second, "enum");
|
|
6015
|
+
if (!snapshot.value.dense) return this.#refuseSchema("validateShape: raw schema enum must be dense", stack, first, second, "enum");
|
|
5004
6016
|
const values = collectMembers([]);
|
|
5005
6017
|
for (const valueIndex of iterateIndices(() => snapshot.value.entries.length)) {
|
|
5006
6018
|
const value = snapshot.value.entries[valueIndex];
|
|
5007
|
-
if (!isLiteralValue(value) || typeof value === "number" && !INTRINSICS.finite(value) || matchesMember(values, value)) return this.#
|
|
6019
|
+
if (!isLiteralValue(value) || typeof value === "number" && !INTRINSICS.finite(value) || matchesMember(values, value)) return this.#refuseSchema("validateShape: raw schema enum values must be finite unique primitives", stack, first, second, "enum", INTRINSICS.text(valueIndex));
|
|
5008
6020
|
admitMember(values, value);
|
|
5009
6021
|
}
|
|
5010
6022
|
}
|
|
5011
|
-
const names = schema.required;
|
|
6023
|
+
const names = INTRINSICS.own(schema, "required") ? schema.required : void 0;
|
|
5012
6024
|
if (names !== void 0) {
|
|
5013
|
-
if (!INTRINSICS.array(names)) return this.#
|
|
6025
|
+
if (!INTRINSICS.array(names)) return this.#refuseSchema("validateShape: raw schema required must be an array", stack, first, second, "required");
|
|
5014
6026
|
const snapshot = readArrayEntries(names);
|
|
5015
|
-
if (
|
|
5016
|
-
if (!snapshot.value.dense) return this.#
|
|
6027
|
+
if (isFailure(snapshot)) return this.#refuseSchema("validateShape: raw schema required must be an array", stack, first, second, "required");
|
|
6028
|
+
if (!snapshot.value.dense) return this.#refuseSchema("validateShape: raw schema required must be dense", stack, first, second, "required");
|
|
5017
6029
|
const required = collectMembers([]);
|
|
5018
6030
|
for (const valueIndex of iterateIndices(() => snapshot.value.entries.length)) {
|
|
5019
6031
|
const value = snapshot.value.entries[valueIndex];
|
|
5020
6032
|
if (value === void 0) continue;
|
|
5021
|
-
if (typeof value !== "string" || matchesMember(required, value)) return this.#
|
|
6033
|
+
if (typeof value !== "string" || matchesMember(required, value)) return this.#refuseSchema("validateShape: raw schema required values must be unique strings", stack, first, second, "required", INTRINSICS.text(valueIndex));
|
|
5022
6034
|
admitMember(required, value);
|
|
5023
6035
|
}
|
|
5024
6036
|
}
|
|
5025
6037
|
const nested = [];
|
|
5026
|
-
|
|
5027
|
-
if (schema.
|
|
5028
|
-
|
|
6038
|
+
const edges = [];
|
|
6039
|
+
if (INTRINSICS.own(schema, "items") && schema.items !== void 0) {
|
|
6040
|
+
edges[nested.length] = ["items"];
|
|
6041
|
+
nested[nested.length] = schema.items;
|
|
6042
|
+
}
|
|
6043
|
+
if (INTRINSICS.own(schema, "properties") && schema.properties !== void 0) {
|
|
6044
|
+
if (!isRecord(schema.properties)) return this.#refuseSchema("validateShape: raw schema properties must be a plain record", stack, first, second, "properties");
|
|
5029
6045
|
const propertyList = INTRINSICS.keys(schema.properties);
|
|
5030
6046
|
for (const propertyIndex of iterateIndices(() => propertyList.length)) {
|
|
5031
6047
|
const key = propertyList[propertyIndex];
|
|
5032
6048
|
if (key === void 0) continue;
|
|
5033
|
-
|
|
6049
|
+
edges[nested.length] = ["properties", key];
|
|
6050
|
+
nested[nested.length] = INTRINSICS.own(schema.properties, key) ? schema.properties[key] : void 0;
|
|
5034
6051
|
}
|
|
5035
6052
|
}
|
|
5036
|
-
if (schema.additionalProperties !== void 0 && schema.additionalProperties !== true && schema.additionalProperties !== false)
|
|
6053
|
+
if (INTRINSICS.own(schema, "additionalProperties") && schema.additionalProperties !== void 0 && schema.additionalProperties !== true && schema.additionalProperties !== false) {
|
|
6054
|
+
edges[nested.length] = ["additionalProperties"];
|
|
6055
|
+
nested[nested.length] = schema.additionalProperties;
|
|
6056
|
+
}
|
|
5037
6057
|
const unionList = ["anyOf", "oneOf"];
|
|
5038
6058
|
for (const unionIndex of iterateIndices(() => unionList.length)) {
|
|
5039
6059
|
const key = unionList[unionIndex];
|
|
5040
6060
|
if (key === void 0) continue;
|
|
5041
|
-
const variants = schema[key];
|
|
6061
|
+
const variants = INTRINSICS.own(schema, key) ? schema[key] : void 0;
|
|
5042
6062
|
if (variants === void 0) continue;
|
|
5043
|
-
if (!INTRINSICS.array(variants)) return this.#
|
|
6063
|
+
if (!INTRINSICS.array(variants)) return this.#refuseSchema("validateShape: raw schema unions must be non-empty arrays", stack, first, second, key);
|
|
5044
6064
|
const snapshot = readArrayEntries(variants);
|
|
5045
|
-
if (
|
|
5046
|
-
if (!snapshot.value.dense) return this.#
|
|
5047
|
-
for (const variantIndex of iterateIndices(() => snapshot.value.entries.length))
|
|
6065
|
+
if (isFailure(snapshot) || snapshot.value.entries.length === 0) return this.#refuseSchema("validateShape: raw schema unions must be non-empty arrays", stack, first, second, key);
|
|
6066
|
+
if (!snapshot.value.dense) return this.#refuseSchema("validateShape: raw schema unions must be dense arrays", stack, first, second, key);
|
|
6067
|
+
for (const variantIndex of iterateIndices(() => snapshot.value.entries.length)) {
|
|
6068
|
+
edges[nested.length] = [key, INTRINSICS.text(variantIndex)];
|
|
6069
|
+
nested[nested.length] = snapshot.value.entries[variantIndex];
|
|
6070
|
+
}
|
|
5048
6071
|
}
|
|
5049
6072
|
stack[stack.length] = {
|
|
5050
6073
|
operation: "exit",
|
|
5051
6074
|
schema,
|
|
5052
|
-
nested
|
|
6075
|
+
nested,
|
|
6076
|
+
...first === void 0 ? {} : { first },
|
|
6077
|
+
...second === void 0 ? {} : { second }
|
|
5053
6078
|
};
|
|
5054
6079
|
for (const offset of iterateIndices(() => nested.length)) {
|
|
5055
6080
|
const index = nested.length - 1 - offset;
|
|
6081
|
+
const childFirst = edges[index]?.[0];
|
|
6082
|
+
const edge = edges[index];
|
|
6083
|
+
const childSecond = edge !== void 0 && INTRINSICS.own(edge, 1) ? edge[1] : void 0;
|
|
5056
6084
|
stack[stack.length] = {
|
|
5057
6085
|
operation: "enter",
|
|
5058
|
-
schema: nested[index]
|
|
6086
|
+
schema: nested[index],
|
|
6087
|
+
...childFirst === void 0 ? {} : { first: childFirst },
|
|
6088
|
+
...childSecond === void 0 ? {} : { second: childSecond }
|
|
5059
6089
|
};
|
|
5060
6090
|
}
|
|
5061
6091
|
}
|
|
@@ -5072,6 +6102,7 @@ var ShapePass = class ShapePass {
|
|
|
5072
6102
|
#populate(current) {
|
|
5073
6103
|
switch (current.category) {
|
|
5074
6104
|
case "array":
|
|
6105
|
+
this.#spend(1);
|
|
5075
6106
|
this.#children[this.#children.length] = {
|
|
5076
6107
|
shape: current.items,
|
|
5077
6108
|
first: "items",
|
|
@@ -5085,8 +6116,9 @@ var ShapePass = class ShapePass {
|
|
|
5085
6116
|
for (const keyIndex of iterateIndices(() => keys.length)) {
|
|
5086
6117
|
const key = keys[keyIndex];
|
|
5087
6118
|
if (key === void 0) continue;
|
|
6119
|
+
this.#spend(1);
|
|
5088
6120
|
const descriptorOutcome = attempt(() => INTRINSICS.describe(properties, key));
|
|
5089
|
-
if (
|
|
6121
|
+
if (isFailure(descriptorOutcome)) return this.#refuse("validateShape: every node must be a recognized shape", "properties", key);
|
|
5090
6122
|
const descriptor = descriptorOutcome.value;
|
|
5091
6123
|
if (descriptor === void 0 || !INTRINSICS.own(descriptor, "value")) {
|
|
5092
6124
|
this.#children[this.#children.length] = {
|
|
@@ -5097,11 +6129,17 @@ var ShapePass = class ShapePass {
|
|
|
5097
6129
|
};
|
|
5098
6130
|
continue;
|
|
5099
6131
|
}
|
|
5100
|
-
const childOutcome = attempt(() =>
|
|
5101
|
-
|
|
6132
|
+
const childOutcome = attempt(() => {
|
|
6133
|
+
const population = current.properties;
|
|
6134
|
+
return isRecord(population) ? population[key] : void 0;
|
|
6135
|
+
});
|
|
6136
|
+
if (isFailure(childOutcome)) return this.#refuse("validateShape: every node must be a recognized shape", "properties", key);
|
|
5102
6137
|
const child = childOutcome.value;
|
|
5103
|
-
const stable = attempt(() =>
|
|
5104
|
-
|
|
6138
|
+
const stable = attempt(() => {
|
|
6139
|
+
const population = current.properties;
|
|
6140
|
+
return isRecord(population) && INTRINSICS.same(population[key], child) && INTRINSICS.same(descriptor.value, child);
|
|
6141
|
+
});
|
|
6142
|
+
if (isFailure(stable)) return this.#refuse("validateShape: every node must be a recognized shape", "properties", key);
|
|
5105
6143
|
if (!stable.value) {
|
|
5106
6144
|
this.#children[this.#children.length] = {
|
|
5107
6145
|
shape: void 0,
|
|
@@ -5119,17 +6157,21 @@ var ShapePass = class ShapePass {
|
|
|
5119
6157
|
};
|
|
5120
6158
|
}
|
|
5121
6159
|
const extra = current.additionalProperties;
|
|
5122
|
-
|
|
5123
|
-
|
|
5124
|
-
|
|
5125
|
-
|
|
5126
|
-
|
|
6160
|
+
this.#open = extra !== void 0 && extra !== false;
|
|
6161
|
+
if (extra !== void 0 && extra !== true && extra !== false) {
|
|
6162
|
+
this.#spend(1);
|
|
6163
|
+
this.#children[this.#children.length] = {
|
|
6164
|
+
shape: extra,
|
|
6165
|
+
first: "additionalProperties",
|
|
6166
|
+
optional: false
|
|
6167
|
+
};
|
|
6168
|
+
}
|
|
5127
6169
|
break;
|
|
5128
6170
|
}
|
|
5129
6171
|
case "union": {
|
|
5130
6172
|
if (!INTRINSICS.array(current.variants)) return this.#refuse("validateShape: variants must be a finite array", "variants");
|
|
5131
6173
|
const snapshot = readArrayEntries(current.variants);
|
|
5132
|
-
if (
|
|
6174
|
+
if (isFailure(snapshot)) return this.#refuse("validateShape: variants must be a finite array", "variants");
|
|
5133
6175
|
if (!snapshot.value.dense) return this.#refuse("validateShape: variants must be a dense data array", "variants");
|
|
5134
6176
|
const variants = snapshot.value.entries;
|
|
5135
6177
|
const length = variants.length;
|
|
@@ -5142,8 +6184,9 @@ var ShapePass = class ShapePass {
|
|
|
5142
6184
|
});
|
|
5143
6185
|
for (const index of iterateIndices(() => length)) {
|
|
5144
6186
|
const key = INTRINSICS.text(index);
|
|
6187
|
+
this.#spend(1);
|
|
5145
6188
|
const descriptorOutcome = attempt(() => INTRINSICS.describe(current.variants, key));
|
|
5146
|
-
if (
|
|
6189
|
+
if (isFailure(descriptorOutcome)) return this.#refuse("validateShape: every node must be a recognized shape", "variants", key);
|
|
5147
6190
|
const descriptor = descriptorOutcome.value;
|
|
5148
6191
|
if (descriptor === void 0 || !INTRINSICS.own(descriptor, "value")) {
|
|
5149
6192
|
this.#children[this.#children.length] = {
|
|
@@ -5155,8 +6198,11 @@ var ShapePass = class ShapePass {
|
|
|
5155
6198
|
continue;
|
|
5156
6199
|
}
|
|
5157
6200
|
const variant = variants[index];
|
|
5158
|
-
const stable = attempt(() =>
|
|
5159
|
-
|
|
6201
|
+
const stable = attempt(() => {
|
|
6202
|
+
const population = current.variants;
|
|
6203
|
+
return INTRINSICS.array(population) && INTRINSICS.same(population[index], variant) && INTRINSICS.same(descriptor.value, variant);
|
|
6204
|
+
});
|
|
6205
|
+
if (isFailure(stable)) return this.#refuse("validateShape: every node must be a recognized shape", "variants", key);
|
|
5160
6206
|
if (!stable.value) {
|
|
5161
6207
|
this.#children[this.#children.length] = {
|
|
5162
6208
|
shape: void 0,
|
|
@@ -5178,7 +6224,7 @@ var ShapePass = class ShapePass {
|
|
|
5178
6224
|
case "literal": {
|
|
5179
6225
|
if (!INTRINSICS.array(current.values)) return this.#refuse("validateShape: values must be a finite literal array", "values");
|
|
5180
6226
|
const snapshot = readArrayEntries(current.values);
|
|
5181
|
-
if (
|
|
6227
|
+
if (isFailure(snapshot)) return this.#refuse("validateShape: values must be a finite literal array", "values");
|
|
5182
6228
|
if (!snapshot.value.dense) return this.#refuse("validateShape: values must be a dense data array", "values");
|
|
5183
6229
|
const literals = snapshot.value.entries;
|
|
5184
6230
|
const length = literals.length;
|
|
@@ -5193,12 +6239,15 @@ var ShapePass = class ShapePass {
|
|
|
5193
6239
|
for (const index of iterateIndices(() => length)) {
|
|
5194
6240
|
const key = INTRINSICS.text(index);
|
|
5195
6241
|
const descriptorOutcome = attempt(() => INTRINSICS.describe(current.values, key));
|
|
5196
|
-
if (
|
|
6242
|
+
if (isFailure(descriptorOutcome)) return this.#refuse("validateShape: every node must be a recognized shape", "values", key);
|
|
5197
6243
|
const descriptor = descriptorOutcome.value;
|
|
5198
6244
|
if (descriptor === void 0 || !INTRINSICS.own(descriptor, "value")) return this.#refuse("validateShape: values must be a dense data array", "values", key);
|
|
5199
6245
|
const value = literals[index];
|
|
5200
|
-
const stable = attempt(() =>
|
|
5201
|
-
|
|
6246
|
+
const stable = attempt(() => {
|
|
6247
|
+
const population = current.values;
|
|
6248
|
+
return INTRINSICS.array(population) && INTRINSICS.same(population[index], value) && INTRINSICS.same(descriptor.value, value);
|
|
6249
|
+
});
|
|
6250
|
+
if (isFailure(stable)) return this.#refuse("validateShape: every node must be a recognized shape", "values", key);
|
|
5202
6251
|
if (!stable.value) return this.#refuse("validateShape: values must be a stable data array", "values", key);
|
|
5203
6252
|
if (!isLiteralValue(value)) return this.#refuse("validateShape: every literal value must be a string, number, or boolean", "values", key);
|
|
5204
6253
|
if (matchesMember(values, value)) return this.#refuse("validateShape: literal values must be unique", "values", key);
|
|
@@ -5215,14 +6264,28 @@ var ShapePass = class ShapePass {
|
|
|
5215
6264
|
break;
|
|
5216
6265
|
}
|
|
5217
6266
|
case "optional":
|
|
5218
|
-
case "nullable":
|
|
5219
|
-
|
|
5220
|
-
|
|
5221
|
-
|
|
5222
|
-
|
|
6267
|
+
case "nullable":
|
|
6268
|
+
this.#spend(1);
|
|
6269
|
+
this.#children[this.#children.length] = {
|
|
6270
|
+
shape: current.inner,
|
|
6271
|
+
first: "inner",
|
|
6272
|
+
optional: false
|
|
6273
|
+
};
|
|
5223
6274
|
}
|
|
5224
6275
|
return true;
|
|
5225
6276
|
}
|
|
6277
|
+
#spend(count) {
|
|
6278
|
+
const budget = this.#budget;
|
|
6279
|
+
if (budget === void 0) return;
|
|
6280
|
+
budget.left -= count;
|
|
6281
|
+
if (budget.left < 0) throw new ContractError(`validateShape: capture exceeds the node limit ${CLONE_NODE_LIMIT}`, {
|
|
6282
|
+
code: "clone",
|
|
6283
|
+
context: {
|
|
6284
|
+
path: pathOf(this.#path),
|
|
6285
|
+
limit: CLONE_NODE_LIMIT
|
|
6286
|
+
}
|
|
6287
|
+
});
|
|
6288
|
+
}
|
|
5226
6289
|
#schedule(current, depth, segments) {
|
|
5227
6290
|
admitVisited(this.#active, current);
|
|
5228
6291
|
const children = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(this.#children), [(child) => child !== void 0]);
|
|
@@ -5241,7 +6304,7 @@ var ShapePass = class ShapePass {
|
|
|
5241
6304
|
depth: depth + 1,
|
|
5242
6305
|
optional: child.optional,
|
|
5243
6306
|
first: child.first,
|
|
5244
|
-
...child.second
|
|
6307
|
+
...INTRINSICS.own(child, "second") && child.second !== void 0 ? { second: child.second } : {}
|
|
5245
6308
|
};
|
|
5246
6309
|
}
|
|
5247
6310
|
return captured;
|
|
@@ -5252,25 +6315,27 @@ var ShapePass = class ShapePass {
|
|
|
5252
6315
|
shape,
|
|
5253
6316
|
category: this.#category,
|
|
5254
6317
|
children,
|
|
5255
|
-
raw: this.#raw
|
|
6318
|
+
raw: this.#raw,
|
|
6319
|
+
defaulted: this.#defaulted,
|
|
6320
|
+
open: this.#open
|
|
5256
6321
|
};
|
|
5257
6322
|
INTRINSICS.reflect.apply(INTRINSICS.retain, this.#index, [shape, index]);
|
|
5258
6323
|
return index;
|
|
5259
6324
|
}
|
|
5260
6325
|
#locate(shape) {
|
|
5261
|
-
if (shape ===
|
|
6326
|
+
if (typeof shape !== "object" || shape === null) return void 0;
|
|
5262
6327
|
return INTRINSICS.reflect.apply(INTRINSICS.recall, this.#index, [shape]);
|
|
5263
6328
|
}
|
|
5264
6329
|
#measure() {
|
|
6330
|
+
if (this.#cycle !== void 0) {
|
|
6331
|
+
this.#finish();
|
|
6332
|
+
return;
|
|
6333
|
+
}
|
|
5265
6334
|
for (const index of iterateIndices(() => this.#captures.length)) {
|
|
5266
6335
|
this.#height[index] = 0;
|
|
5267
6336
|
this.#reach[index] = 0;
|
|
5268
6337
|
this.#counts[index] = 1;
|
|
5269
6338
|
}
|
|
5270
|
-
if (this.#cycle !== void 0) {
|
|
5271
|
-
this.#trace();
|
|
5272
|
-
return;
|
|
5273
|
-
}
|
|
5274
6339
|
for (const step of iterateIndices(() => this.#post.length)) {
|
|
5275
6340
|
const index = this.#post[step];
|
|
5276
6341
|
if (index === void 0) continue;
|
|
@@ -5308,7 +6373,7 @@ var ShapePass = class ShapePass {
|
|
|
5308
6373
|
this.#via[located] = {
|
|
5309
6374
|
parent: index,
|
|
5310
6375
|
first: child.first,
|
|
5311
|
-
...child.second
|
|
6376
|
+
...INTRINSICS.own(child, "second") && child.second !== void 0 ? { second: child.second } : {}
|
|
5312
6377
|
};
|
|
5313
6378
|
}
|
|
5314
6379
|
}
|
|
@@ -5321,11 +6386,52 @@ var ShapePass = class ShapePass {
|
|
|
5321
6386
|
context: { path: pathOf(this.#ascend(index), "schema") }
|
|
5322
6387
|
});
|
|
5323
6388
|
}
|
|
6389
|
+
this.#placeDefaults();
|
|
6390
|
+
}
|
|
6391
|
+
#placeDefaults() {
|
|
6392
|
+
if (!INTRINSICS.reflect.apply(INTRINSICS.some, this.#captures, [(capture) => capture.defaulted])) return;
|
|
6393
|
+
for (const offset of iterateIndices(() => this.#post.length)) {
|
|
6394
|
+
const index = this.#post[this.#post.length - 1 - offset];
|
|
6395
|
+
if (index === void 0) continue;
|
|
6396
|
+
const capture = this.#captures[index];
|
|
6397
|
+
if (capture === void 0) continue;
|
|
6398
|
+
const enclosed = capture.category === "union" || INTRINSICS.own(this.#within, index) && this.#within[index] !== void 0;
|
|
6399
|
+
for (const edge of iterateIndices(() => capture.children.length)) {
|
|
6400
|
+
const child = capture.children[edge];
|
|
6401
|
+
if (child === void 0) continue;
|
|
6402
|
+
const located = this.#locate(child.shape);
|
|
6403
|
+
if (located === void 0) continue;
|
|
6404
|
+
if (enclosed && (!INTRINSICS.own(this.#within, located) || this.#within[located] === void 0)) this.#within[located] = {
|
|
6405
|
+
parent: index,
|
|
6406
|
+
first: child.first,
|
|
6407
|
+
...INTRINSICS.own(child, "second") && child.second !== void 0 ? { second: child.second } : {}
|
|
6408
|
+
};
|
|
6409
|
+
if (this.#captures[located]?.defaulted !== true) continue;
|
|
6410
|
+
if (enclosed) this.#domain ??= new ContractError("validateShape: a default may not sit under a union, whose parser can return a valid value without filling it; move the default outside the union or remove it", {
|
|
6411
|
+
code: "placement",
|
|
6412
|
+
context: {
|
|
6413
|
+
path: pathOf(this.#route(index), child.first, INTRINSICS.own(child, "second") ? child.second : void 0),
|
|
6414
|
+
shape: "optional"
|
|
6415
|
+
}
|
|
6416
|
+
});
|
|
6417
|
+
if (capture.open) this.#domain ??= new ContractError("validateShape: a default may not sit on a property of an open object, where a misspelled key would fill it unreported; close the object or remove the default", {
|
|
6418
|
+
code: "placement",
|
|
6419
|
+
context: {
|
|
6420
|
+
path: pathOf(this.#ascend(index), child.first, INTRINSICS.own(child, "second") ? child.second : void 0),
|
|
6421
|
+
shape: "optional"
|
|
6422
|
+
}
|
|
6423
|
+
});
|
|
6424
|
+
}
|
|
6425
|
+
}
|
|
6426
|
+
}
|
|
6427
|
+
#route(index) {
|
|
6428
|
+
const edge = INTRINSICS.own(this.#within, index) ? this.#within[index] : void 0;
|
|
6429
|
+
return edge === void 0 ? this.#ascend(index) : pathOf(this.#route(edge.parent), edge.first, INTRINSICS.own(edge, "second") ? edge.second : void 0);
|
|
5324
6430
|
}
|
|
5325
6431
|
#ascend(index) {
|
|
5326
6432
|
if (index === 0) return [];
|
|
5327
|
-
const edge = this.#via[index];
|
|
5328
|
-
return edge === void 0 ? [] : pathOf(this.#ascend(edge.parent), edge.first, edge.second);
|
|
6433
|
+
const edge = INTRINSICS.own(this.#via, index) ? this.#via[index] : void 0;
|
|
6434
|
+
return edge === void 0 ? [] : pathOf(this.#ascend(edge.parent), edge.first, INTRINSICS.own(edge, "second") ? edge.second : void 0);
|
|
5329
6435
|
}
|
|
5330
6436
|
#witness(index, depth, path) {
|
|
5331
6437
|
if (depth <= 512) {
|
|
@@ -5336,7 +6442,7 @@ var ShapePass = class ShapePass {
|
|
|
5336
6442
|
const reached = located === void 0 ? 0 : this.#height[located] ?? 0;
|
|
5337
6443
|
return depth + 1 + reached > 512;
|
|
5338
6444
|
}])];
|
|
5339
|
-
if (chosen !== void 0) return this.#witness(this.#locate(chosen.shape), depth + 1, pathOf(path, chosen.first, chosen.second));
|
|
6445
|
+
if (chosen !== void 0) return this.#witness(this.#locate(chosen.shape), depth + 1, pathOf(path, chosen.first, INTRINSICS.own(chosen, "second") ? chosen.second : void 0));
|
|
5340
6446
|
}
|
|
5341
6447
|
throw new ContractError("validateShape: a shape exceeds the compilation depth limit", {
|
|
5342
6448
|
code: "depth",
|
|
@@ -5346,86 +6452,24 @@ var ShapePass = class ShapePass {
|
|
|
5346
6452
|
}
|
|
5347
6453
|
});
|
|
5348
6454
|
}
|
|
5349
|
-
#trace() {
|
|
5350
|
-
const settled = [];
|
|
5351
|
-
const order = [];
|
|
5352
|
-
const active = new ShapePass.#weakSet();
|
|
5353
|
-
const stack = [{
|
|
5354
|
-
operation: "enter",
|
|
5355
|
-
index: 0,
|
|
5356
|
-
depth: 0
|
|
5357
|
-
}];
|
|
5358
|
-
while (stack.length > 0) {
|
|
5359
|
-
const frame = stack.pop();
|
|
5360
|
-
if (frame === void 0) continue;
|
|
5361
|
-
if (frame.operation === "exit") {
|
|
5362
|
-
const capture = this.#captures[frame.index];
|
|
5363
|
-
if (capture !== void 0) omitVisited(active, capture.shape);
|
|
5364
|
-
if (this.#oldest >= (order[frame.index] ?? 0) && (settled[frame.index] ?? -1) < frame.depth) settled[frame.index] = frame.depth;
|
|
5365
|
-
if (frame.outer < this.#oldest) this.#oldest = frame.outer;
|
|
5366
|
-
this.#trail.length -= frame.segments;
|
|
5367
|
-
continue;
|
|
5368
|
-
}
|
|
5369
|
-
const segments = (frame.first === void 0 ? 0 : 1) + (frame.second === void 0 ? 0 : 1);
|
|
5370
|
-
if (frame.first !== void 0) this.#trail[this.#trail.length] = frame.first;
|
|
5371
|
-
if (frame.second !== void 0) this.#trail[this.#trail.length] = frame.second;
|
|
5372
|
-
if (frame.depth > 512) throw new ContractError("validateShape: a shape exceeds the compilation depth limit", {
|
|
5373
|
-
code: "depth",
|
|
5374
|
-
context: {
|
|
5375
|
-
path: pathOf(this.#trail),
|
|
5376
|
-
limit: 512
|
|
5377
|
-
}
|
|
5378
|
-
});
|
|
5379
|
-
const index = frame.index;
|
|
5380
|
-
const capture = index === void 0 ? void 0 : this.#captures[index];
|
|
5381
|
-
if (index === void 0 || capture === void 0) {
|
|
5382
|
-
this.#trail.length -= segments;
|
|
5383
|
-
continue;
|
|
5384
|
-
}
|
|
5385
|
-
if (matchesVisited(active, capture.shape)) {
|
|
5386
|
-
const blocker = order[index] ?? 0;
|
|
5387
|
-
if (blocker < this.#oldest) this.#oldest = blocker;
|
|
5388
|
-
this.#trail.length -= segments;
|
|
5389
|
-
continue;
|
|
5390
|
-
}
|
|
5391
|
-
const recorded = settled[index];
|
|
5392
|
-
if (recorded !== void 0 && frame.depth <= recorded) {
|
|
5393
|
-
this.#trail.length -= segments;
|
|
5394
|
-
continue;
|
|
5395
|
-
}
|
|
5396
|
-
admitVisited(active, capture.shape);
|
|
5397
|
-
order[index] = this.#clock;
|
|
5398
|
-
this.#clock += 1;
|
|
5399
|
-
stack[stack.length] = {
|
|
5400
|
-
operation: "exit",
|
|
5401
|
-
index,
|
|
5402
|
-
depth: frame.depth,
|
|
5403
|
-
segments,
|
|
5404
|
-
outer: this.#oldest
|
|
5405
|
-
};
|
|
5406
|
-
this.#oldest = this.#clock;
|
|
5407
|
-
for (const offset of iterateIndices(() => capture.children.length)) {
|
|
5408
|
-
const edge = capture.children.length - 1 - offset;
|
|
5409
|
-
const child = capture.children[edge];
|
|
5410
|
-
if (child === void 0) continue;
|
|
5411
|
-
stack[stack.length] = {
|
|
5412
|
-
operation: "enter",
|
|
5413
|
-
index: this.#locate(child.shape),
|
|
5414
|
-
depth: frame.depth + 1,
|
|
5415
|
-
first: child.first,
|
|
5416
|
-
...child.second === void 0 ? {} : { second: child.second }
|
|
5417
|
-
};
|
|
5418
|
-
}
|
|
5419
|
-
}
|
|
5420
|
-
}
|
|
5421
6455
|
#finish() {
|
|
5422
6456
|
if (this.#structure !== void 0) throw this.#structure;
|
|
5423
6457
|
if (this.#cycle !== void 0) throw this.#cycle;
|
|
5424
6458
|
if (this.#domain !== void 0) throw this.#domain;
|
|
5425
6459
|
}
|
|
5426
|
-
#
|
|
6460
|
+
#refuseSchema(message, stack, first, second, keyword, member) {
|
|
6461
|
+
if (this.#structure !== void 0) return false;
|
|
6462
|
+
const ancestors = INTRINSICS.reflect.apply(INTRINSICS.expand, readIndexView(stack), [(frame) => frame.operation === "exit" ? pathOf([], INTRINSICS.own(frame, "first") ? frame.first : void 0, INTRINSICS.own(frame, "second") ? frame.second : void 0) : []]);
|
|
6463
|
+
const prefix = [];
|
|
6464
|
+
appendEntries(prefix, this.#path);
|
|
6465
|
+
prefix[prefix.length] = "schema";
|
|
6466
|
+
appendEntries(prefix, ancestors);
|
|
6467
|
+
const path = pathOf(prefix, first, second);
|
|
6468
|
+
return this.#refuse(message, keyword, member, path);
|
|
6469
|
+
}
|
|
6470
|
+
#refuse(message, first, second, prefix = this.#path) {
|
|
5427
6471
|
if (this.#structure === void 0) {
|
|
5428
|
-
const path = pathOf(
|
|
6472
|
+
const path = pathOf(prefix, first, second);
|
|
5429
6473
|
this.#structure = new ContractError(message, {
|
|
5430
6474
|
code: "structure",
|
|
5431
6475
|
context: { path }
|
|
@@ -5493,7 +6537,7 @@ var ShapeValidator = class ShapeValidator {
|
|
|
5493
6537
|
*/
|
|
5494
6538
|
validate() {
|
|
5495
6539
|
INTRINSICS.reflect.apply(INTRINSICS.sweep, this.#passes, [(pass) => pass.interrupt()]);
|
|
5496
|
-
const pass = new ShapePass(this.#source);
|
|
6540
|
+
const pass = new ShapePass(this.#source, { left: CLONE_NODE_LIMIT });
|
|
5497
6541
|
INTRINSICS.reflect.apply(INTRINSICS.admit, this.#passes, [pass]);
|
|
5498
6542
|
this.#expansion = void 0;
|
|
5499
6543
|
try {
|
|
@@ -5564,7 +6608,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5564
6608
|
clone() {
|
|
5565
6609
|
const state = this.#state;
|
|
5566
6610
|
if (state.phase === "settled") {
|
|
5567
|
-
if (state.result
|
|
6611
|
+
if (isSuccess(state.result)) return state.result.value;
|
|
5568
6612
|
throw state.result.error;
|
|
5569
6613
|
}
|
|
5570
6614
|
if (state.phase === "running" || state.phase === "interrupted") {
|
|
@@ -5586,22 +6630,17 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5586
6630
|
}
|
|
5587
6631
|
#complete(outcome) {
|
|
5588
6632
|
const state = this.#state;
|
|
5589
|
-
if (state.phase === "interrupted") return this.#settle(
|
|
5590
|
-
|
|
5591
|
-
error: state.poison
|
|
5592
|
-
});
|
|
5593
|
-
if (outcome.success) return this.#settle(outcome);
|
|
6633
|
+
if (state.phase === "interrupted") return this.#settle(failure(state.poison));
|
|
6634
|
+
if (isSuccess(outcome)) return this.#settle(outcome);
|
|
5594
6635
|
const error = this.#owns(outcome.error) ? outcome.error : this.#create("cloneShape: failed to create an owned shape snapshot", {
|
|
5595
6636
|
code: "clone",
|
|
5596
6637
|
context: { shape: "shape" },
|
|
5597
6638
|
cause: outcome.error
|
|
5598
6639
|
});
|
|
5599
|
-
return this.#settle(
|
|
5600
|
-
success: false,
|
|
5601
|
-
error
|
|
5602
|
-
});
|
|
6640
|
+
return this.#settle(failure(error));
|
|
5603
6641
|
}
|
|
5604
6642
|
#execute() {
|
|
6643
|
+
const budget = { left: CLONE_NODE_LIMIT };
|
|
5605
6644
|
const paths = this.#paths;
|
|
5606
6645
|
const memo = this.#memo;
|
|
5607
6646
|
if (paths === void 0 || memo === void 0) throw this.#unavailable();
|
|
@@ -5610,7 +6649,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5610
6649
|
shape: this.#source,
|
|
5611
6650
|
depth: 0
|
|
5612
6651
|
};
|
|
5613
|
-
this.#drain();
|
|
6652
|
+
this.#drain(budget);
|
|
5614
6653
|
this.#wireNodes();
|
|
5615
6654
|
const root = INTRINSICS.reflect.apply(INTRINSICS.fetch, memo, [this.#source]);
|
|
5616
6655
|
if (root === void 0) throw this.#create("ShapeCloner.clone: captured root is unavailable", {
|
|
@@ -5622,19 +6661,19 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5622
6661
|
if (this.#fidelity !== void 0) throw this.#fidelity;
|
|
5623
6662
|
return root;
|
|
5624
6663
|
}
|
|
5625
|
-
#drain() {
|
|
6664
|
+
#drain(budget) {
|
|
5626
6665
|
const memo = this.#memo;
|
|
5627
6666
|
if (memo === void 0) throw this.#unavailable();
|
|
5628
6667
|
for (const cursor of iterateIndices(() => this.#pending.length)) {
|
|
5629
6668
|
const entry = this.#pending[cursor];
|
|
5630
6669
|
if (entry === void 0) continue;
|
|
5631
6670
|
if (INTRINSICS.reflect.apply(INTRINSICS.keyed, memo, [entry.shape])) continue;
|
|
5632
|
-
if (entry.depth > 512) this.#refuseDepth();
|
|
5633
|
-
this.#captureNode(entry.shape, entry.depth);
|
|
6671
|
+
if (entry.depth > 512) this.#refuseDepth(budget);
|
|
6672
|
+
this.#captureNode(entry.shape, entry.depth, budget);
|
|
5634
6673
|
}
|
|
5635
6674
|
}
|
|
5636
|
-
#refuseDepth() {
|
|
5637
|
-
this.#validateShape(this.#source);
|
|
6675
|
+
#refuseDepth(budget) {
|
|
6676
|
+
this.#validateShape(this.#source, [], budget);
|
|
5638
6677
|
throw this.#create("validateShape: a shape exceeds the compilation depth limit", {
|
|
5639
6678
|
code: "depth",
|
|
5640
6679
|
context: {
|
|
@@ -5643,7 +6682,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5643
6682
|
}
|
|
5644
6683
|
});
|
|
5645
6684
|
}
|
|
5646
|
-
#captureShell(source, path) {
|
|
6685
|
+
#captureShell(source, path, budget) {
|
|
5647
6686
|
if (!matchesRecordBrand(source)) throw this.#create("validateShape: every structural child must be a shape", {
|
|
5648
6687
|
code: "structure",
|
|
5649
6688
|
context: { path }
|
|
@@ -5665,13 +6704,14 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5665
6704
|
case "boolean": return this.#captureSimple(source, path, "boolean");
|
|
5666
6705
|
case "null": return this.#captureSimple(source, path, "null");
|
|
5667
6706
|
case "literal": return this.#captureLiteral(source, path);
|
|
5668
|
-
case "array": return this.#captureArray(source, path);
|
|
5669
|
-
case "object": return this.#captureObject(source, path);
|
|
5670
|
-
case "union": return this.#captureUnion(source, path);
|
|
5671
|
-
case "optional": return this.#
|
|
5672
|
-
case "nullable": return this.#
|
|
6707
|
+
case "array": return this.#captureArray(source, path, budget);
|
|
6708
|
+
case "object": return this.#captureObject(source, path, budget);
|
|
6709
|
+
case "union": return this.#captureUnion(source, path, budget);
|
|
6710
|
+
case "optional": return this.#captureOptional(source, path, budget);
|
|
6711
|
+
case "nullable": return this.#captureNullable(source, path, budget);
|
|
5673
6712
|
case "json": return this.#captureSimple(source, path, "json");
|
|
5674
|
-
case "
|
|
6713
|
+
case "shape": return this.#captureSimple(source, path, "shape");
|
|
6714
|
+
case "raw": return this.#captureRaw(source, path, budget);
|
|
5675
6715
|
default: throw this.#create("validateShape: every node must be a recognized shape", {
|
|
5676
6716
|
code: "structure",
|
|
5677
6717
|
context: { path }
|
|
@@ -5747,7 +6787,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5747
6787
|
source: readPatternSource(pattern)
|
|
5748
6788
|
};
|
|
5749
6789
|
});
|
|
5750
|
-
if (typeof sourceText !== "string" || typeof flags !== "string" ||
|
|
6790
|
+
if (typeof sourceText !== "string" || typeof flags !== "string" || isFailure(repeated) || typeof repeated.value.source !== "string" || typeof repeated.value.flags !== "string" || repeated.value.source !== sourceText || repeated.value.flags !== flags) {
|
|
5751
6791
|
this.#fidelity ??= this.#create("validateShape: string pattern must be stable", {
|
|
5752
6792
|
code: "structure",
|
|
5753
6793
|
context: { path: pathOf(path, "pattern") }
|
|
@@ -5759,12 +6799,12 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5759
6799
|
flags
|
|
5760
6800
|
};
|
|
5761
6801
|
}
|
|
5762
|
-
#captureNode(source, depth) {
|
|
6802
|
+
#captureNode(source, depth, budget) {
|
|
5763
6803
|
const paths = this.#paths;
|
|
5764
6804
|
const memo = this.#memo;
|
|
5765
6805
|
if (paths === void 0 || memo === void 0) throw this.#unavailable();
|
|
5766
6806
|
const path = INTRINSICS.reflect.apply(INTRINSICS.fetch, paths, [source]) ?? [];
|
|
5767
|
-
const clone = this.#captureShell(source, path);
|
|
6807
|
+
const clone = this.#captureShell(source, path, budget);
|
|
5768
6808
|
INTRINSICS.reflect.apply(INTRINSICS.store, memo, [source, clone]);
|
|
5769
6809
|
this.#sources[this.#sources.length] = source;
|
|
5770
6810
|
this.#scheduleNode(source, clone, path, depth);
|
|
@@ -5813,6 +6853,10 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5813
6853
|
category: "null",
|
|
5814
6854
|
...description === void 0 ? {} : { description }
|
|
5815
6855
|
};
|
|
6856
|
+
if (category === "shape") return {
|
|
6857
|
+
category: "shape",
|
|
6858
|
+
...description === void 0 ? {} : { description }
|
|
6859
|
+
};
|
|
5816
6860
|
return {
|
|
5817
6861
|
category: "json",
|
|
5818
6862
|
...description === void 0 ? {} : { description }
|
|
@@ -5827,7 +6871,8 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5827
6871
|
...description === void 0 ? {} : { description }
|
|
5828
6872
|
};
|
|
5829
6873
|
}
|
|
5830
|
-
#captureArray(source, path) {
|
|
6874
|
+
#captureArray(source, path, budget) {
|
|
6875
|
+
this.#spend(budget, 1, path);
|
|
5831
6876
|
const items = this.#captureField(source, "items", path);
|
|
5832
6877
|
const min = this.#captureField(source, "min", path);
|
|
5833
6878
|
const max = this.#captureField(source, "max", path);
|
|
@@ -5844,7 +6889,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5844
6889
|
...description === void 0 ? {} : { description }
|
|
5845
6890
|
};
|
|
5846
6891
|
}
|
|
5847
|
-
#captureObject(source, path) {
|
|
6892
|
+
#captureObject(source, path, budget) {
|
|
5848
6893
|
const propertySource = this.#captureField(source, "properties", path);
|
|
5849
6894
|
const additional = this.#captureField(source, "additionalProperties", path);
|
|
5850
6895
|
const description = this.#captureField(source, "description", path);
|
|
@@ -5852,7 +6897,8 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5852
6897
|
code: "structure",
|
|
5853
6898
|
context: { path: pathOf(path, "properties") }
|
|
5854
6899
|
});
|
|
5855
|
-
this.#captureProperties(source, propertySource, path);
|
|
6900
|
+
this.#captureProperties(source, propertySource, path, budget);
|
|
6901
|
+
if (additional !== void 0 && additional !== true && additional !== false) this.#spend(budget, 1, path);
|
|
5856
6902
|
return {
|
|
5857
6903
|
category: "object",
|
|
5858
6904
|
properties: INTRINSICS.create(null),
|
|
@@ -5860,11 +6906,11 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5860
6906
|
...description === void 0 ? {} : { description }
|
|
5861
6907
|
};
|
|
5862
6908
|
}
|
|
5863
|
-
#captureUnion(source, path) {
|
|
6909
|
+
#captureUnion(source, path, budget) {
|
|
5864
6910
|
const variantSource = this.#captureField(source, "variants", path);
|
|
5865
6911
|
const mode = this.#captureField(source, "mode", path);
|
|
5866
6912
|
const description = this.#captureField(source, "description", path);
|
|
5867
|
-
this.#captureVariants(source, variantSource, path);
|
|
6913
|
+
this.#captureVariants(source, variantSource, path, budget);
|
|
5868
6914
|
return {
|
|
5869
6915
|
category: "union",
|
|
5870
6916
|
variants: [],
|
|
@@ -5872,21 +6918,41 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5872
6918
|
...description === void 0 ? {} : { description }
|
|
5873
6919
|
};
|
|
5874
6920
|
}
|
|
5875
|
-
#
|
|
5876
|
-
const inner = this.#
|
|
5877
|
-
|
|
6921
|
+
#captureOptional(source, path, budget) {
|
|
6922
|
+
const inner = this.#captureInner(source, path, budget);
|
|
6923
|
+
const value = this.#captureField(source, "default", path);
|
|
6924
|
+
if (!INTRINSICS.own(source, "default")) return {
|
|
6925
|
+
category: "optional",
|
|
6926
|
+
inner
|
|
6927
|
+
};
|
|
6928
|
+
const owned = isJSONValue(value) ? attempt(() => new JSONCloner(value).clone(budget)) : void 0;
|
|
6929
|
+
if (owned !== void 0 && isFailure(owned) && budget.left < 0 && isContractError(owned.error)) throw this.#prefix(owned.error, pathOf(path, "default"));
|
|
6930
|
+
if (owned === void 0 || isFailure(owned)) throw this.#create("validateShape: an optional default must be a JSON value", {
|
|
5878
6931
|
code: "structure",
|
|
5879
|
-
context: { path: pathOf(path, "
|
|
6932
|
+
context: { path: pathOf(path, "default") }
|
|
5880
6933
|
});
|
|
5881
|
-
return
|
|
6934
|
+
return {
|
|
5882
6935
|
category: "optional",
|
|
5883
|
-
inner
|
|
5884
|
-
|
|
6936
|
+
inner,
|
|
6937
|
+
default: owned.value
|
|
6938
|
+
};
|
|
6939
|
+
}
|
|
6940
|
+
#captureNullable(source, path, budget) {
|
|
6941
|
+
return {
|
|
5885
6942
|
category: "nullable",
|
|
5886
|
-
inner
|
|
6943
|
+
inner: this.#captureInner(source, path, budget)
|
|
5887
6944
|
};
|
|
5888
6945
|
}
|
|
5889
|
-
#
|
|
6946
|
+
#captureInner(source, path, budget) {
|
|
6947
|
+
this.#spend(budget, 1, path);
|
|
6948
|
+
const inner = this.#captureField(source, "inner", path);
|
|
6949
|
+
if (inner === void 0) throw this.#create("validateShape: every structural child must be a shape", {
|
|
6950
|
+
code: "structure",
|
|
6951
|
+
context: { path: pathOf(path, "inner") }
|
|
6952
|
+
});
|
|
6953
|
+
return inner;
|
|
6954
|
+
}
|
|
6955
|
+
#captureRaw(source, path, budget) {
|
|
5890
6956
|
const schema = this.#captureField(source, "schema", path);
|
|
5891
6957
|
if (!isRecord(schema)) throw this.#create("validateShape: raw schema must be a plain record", {
|
|
5892
6958
|
code: "structure",
|
|
@@ -5895,10 +6961,10 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5895
6961
|
this.#validateShape({
|
|
5896
6962
|
category: "raw",
|
|
5897
6963
|
schema
|
|
5898
|
-
});
|
|
6964
|
+
}, path, budget);
|
|
5899
6965
|
const outcome = attempt(() => new SchemaCloner(schema).clone());
|
|
5900
|
-
if (
|
|
5901
|
-
if (isContractError(outcome.error))
|
|
6966
|
+
if (isFailure(outcome)) {
|
|
6967
|
+
if (isContractError(outcome.error)) throw this.#prefix(outcome.error, pathOf(path, "schema"));
|
|
5902
6968
|
throw outcome.error;
|
|
5903
6969
|
}
|
|
5904
6970
|
return {
|
|
@@ -5912,7 +6978,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5912
6978
|
context: { path: pathOf(path, "values") }
|
|
5913
6979
|
});
|
|
5914
6980
|
const snapshot = readArrayEntries(values);
|
|
5915
|
-
if (
|
|
6981
|
+
if (isFailure(snapshot)) throw this.#create("validateShape: values must be a finite literal array", {
|
|
5916
6982
|
code: "structure",
|
|
5917
6983
|
context: { path: pathOf(path, "values") }
|
|
5918
6984
|
});
|
|
@@ -5927,11 +6993,11 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5927
6993
|
const key = INTRINSICS.text(index);
|
|
5928
6994
|
const descriptor = attempt(() => INTRINSICS.describe(values, key));
|
|
5929
6995
|
const repeated = attempt(() => values[index]);
|
|
5930
|
-
if (
|
|
6996
|
+
if (isFailure(descriptor) || descriptor.value === void 0 || !INTRINSICS.own(descriptor.value, "value")) this.#fidelity ??= this.#create("validateShape: values must be a dense data array", {
|
|
5931
6997
|
code: "structure",
|
|
5932
6998
|
context: { path: pathOf(path, "values", key) }
|
|
5933
6999
|
});
|
|
5934
|
-
else if (
|
|
7000
|
+
else if (isFailure(repeated) || !INTRINSICS.same(repeated.value, value) || !INTRINSICS.same(descriptor.value.value, value)) this.#fidelity ??= this.#create("validateShape: values must be a stable data array", {
|
|
5935
7001
|
code: "structure",
|
|
5936
7002
|
context: { path: pathOf(path, "values", key) }
|
|
5937
7003
|
});
|
|
@@ -5941,9 +7007,10 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5941
7007
|
});
|
|
5942
7008
|
return value;
|
|
5943
7009
|
}
|
|
5944
|
-
#captureProperties(source, properties, path) {
|
|
7010
|
+
#captureProperties(source, properties, path, budget) {
|
|
5945
7011
|
const keys = INTRINSICS.keys(properties);
|
|
5946
7012
|
const snapshot = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(keys), [(key) => {
|
|
7013
|
+
this.#spend(budget, 1, path);
|
|
5947
7014
|
const descriptor = INTRINSICS.describe(properties, key);
|
|
5948
7015
|
if (descriptor === void 0 || !INTRINSICS.own(descriptor, "value")) throw this.#create("validateShape: every structural child must be a shape", {
|
|
5949
7016
|
code: "structure",
|
|
@@ -5970,13 +7037,13 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5970
7037
|
if (captured === void 0) throw this.#unavailable();
|
|
5971
7038
|
INTRINSICS.reflect.apply(INTRINSICS.store, captured, [source, snapshot]);
|
|
5972
7039
|
}
|
|
5973
|
-
#captureVariants(source, variants, path) {
|
|
7040
|
+
#captureVariants(source, variants, path, budget) {
|
|
5974
7041
|
if (!INTRINSICS.array(variants)) throw this.#create("validateShape: variants must be a finite array", {
|
|
5975
7042
|
code: "structure",
|
|
5976
7043
|
context: { path: pathOf(path, "variants") }
|
|
5977
7044
|
});
|
|
5978
7045
|
const snapshot = readArrayEntries(variants);
|
|
5979
|
-
if (
|
|
7046
|
+
if (isFailure(snapshot)) throw this.#create("validateShape: variants must be a finite array", {
|
|
5980
7047
|
code: "structure",
|
|
5981
7048
|
context: { path: pathOf(path, "variants") }
|
|
5982
7049
|
});
|
|
@@ -5985,6 +7052,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5985
7052
|
context: { path: pathOf(path, "variants") }
|
|
5986
7053
|
});
|
|
5987
7054
|
const entries = snapshot.value.entries;
|
|
7055
|
+
this.#spend(budget, entries.length, path);
|
|
5988
7056
|
const captured = this.#variants;
|
|
5989
7057
|
if (captured === void 0) throw this.#unavailable();
|
|
5990
7058
|
INTRINSICS.reflect.apply(INTRINSICS.store, captured, [source, entries]);
|
|
@@ -5993,7 +7061,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5993
7061
|
const key = INTRINSICS.text(index);
|
|
5994
7062
|
const descriptor = attempt(() => INTRINSICS.describe(variants, key));
|
|
5995
7063
|
const repeated = attempt(() => variants[index]);
|
|
5996
|
-
if (
|
|
7064
|
+
if (isFailure(descriptor) || descriptor.value === void 0 || !INTRINSICS.own(descriptor.value, "value") || isFailure(repeated) || !INTRINSICS.same(repeated.value, variant) || !INTRINSICS.same(descriptor.value.value, variant)) this.#fidelity ??= this.#create("validateShape: every structural child must be a shape", {
|
|
5997
7065
|
code: "structure",
|
|
5998
7066
|
context: { path: pathOf(path, "variants", key) }
|
|
5999
7067
|
});
|
|
@@ -6168,12 +7236,40 @@ var ShapeCloner = class ShapeCloner {
|
|
|
6168
7236
|
if (clone !== void 0) INTRINSICS.freeze(clone);
|
|
6169
7237
|
}
|
|
6170
7238
|
}
|
|
6171
|
-
#validateShape(shape) {
|
|
6172
|
-
const outcome = attempt(() => new
|
|
6173
|
-
if (outcome
|
|
6174
|
-
if (isContractError(outcome.error))
|
|
7239
|
+
#validateShape(shape, path = [], budget) {
|
|
7240
|
+
const outcome = attempt(() => new ShapePass(shape, budget).validate());
|
|
7241
|
+
if (isSuccess(outcome)) return;
|
|
7242
|
+
if (isContractError(outcome.error)) throw this.#prefix(outcome.error, path);
|
|
6175
7243
|
throw outcome.error;
|
|
6176
7244
|
}
|
|
7245
|
+
#spend(budget, count, path) {
|
|
7246
|
+
budget.left -= count;
|
|
7247
|
+
if (budget.left < 0) throw this.#create(`cloneShape: capture exceeds the node limit ${CLONE_NODE_LIMIT}`, {
|
|
7248
|
+
code: "clone",
|
|
7249
|
+
context: {
|
|
7250
|
+
path,
|
|
7251
|
+
limit: CLONE_NODE_LIMIT
|
|
7252
|
+
}
|
|
7253
|
+
});
|
|
7254
|
+
}
|
|
7255
|
+
#prefix(error, path) {
|
|
7256
|
+
if (path.length === 0) {
|
|
7257
|
+
admitVisited(this.#owned, error);
|
|
7258
|
+
return error;
|
|
7259
|
+
}
|
|
7260
|
+
const tail = error.context?.path;
|
|
7261
|
+
const location = [];
|
|
7262
|
+
appendEntries(location, path);
|
|
7263
|
+
appendEntries(location, typeof tail === "string" ? [tail] : tail ?? []);
|
|
7264
|
+
return this.#create(error.message, {
|
|
7265
|
+
code: error.code,
|
|
7266
|
+
context: {
|
|
7267
|
+
...error.context,
|
|
7268
|
+
path: location
|
|
7269
|
+
},
|
|
7270
|
+
...INTRINSICS.own(error, "cause") ? { cause: error.cause } : {}
|
|
7271
|
+
});
|
|
7272
|
+
}
|
|
6177
7273
|
#unavailable() {
|
|
6178
7274
|
return this.#create("ShapeCloner.clone: the capture state is unavailable", {
|
|
6179
7275
|
code: "clone",
|
|
@@ -6200,7 +7296,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
6200
7296
|
phase: "settled",
|
|
6201
7297
|
result
|
|
6202
7298
|
};
|
|
6203
|
-
if (result
|
|
7299
|
+
if (isSuccess(result)) return result.value;
|
|
6204
7300
|
throw result.error;
|
|
6205
7301
|
}
|
|
6206
7302
|
static {
|
|
@@ -6338,11 +7434,11 @@ function ownShape(shape) {
|
|
|
6338
7434
|
return contain(() => {
|
|
6339
7435
|
const frozen = holds(() => INTRINSICS.frozen(shape));
|
|
6340
7436
|
const fidelity = attempt(() => cloneShape(shape));
|
|
6341
|
-
if (fidelity
|
|
6342
|
-
if (isContractError(fidelity.error) && fidelity.error.code !== "clone") throw fidelity.error;
|
|
7437
|
+
if (isSuccess(fidelity)) return fidelity.value;
|
|
7438
|
+
if (isContractError(fidelity.error) && (fidelity.error.code !== "clone" || fidelity.error.context?.limit === 262144)) throw fidelity.error;
|
|
6343
7439
|
if (!frozen) throw fidelity.error;
|
|
6344
7440
|
const validation = attempt(() => new ShapeValidator(shape).validate());
|
|
6345
|
-
if (
|
|
7441
|
+
if (isFailure(validation) && isContractError(validation.error)) throw validation.error;
|
|
6346
7442
|
throw fidelity.error;
|
|
6347
7443
|
}, "ownShape");
|
|
6348
7444
|
}
|
|
@@ -6370,7 +7466,7 @@ function arrayOf(elementGuard) {
|
|
|
6370
7466
|
return (value) => holds(() => {
|
|
6371
7467
|
if (!isArray(value)) return false;
|
|
6372
7468
|
const entries = readArrayEntries(value);
|
|
6373
|
-
if (
|
|
7469
|
+
if (isFailure(entries) || !entries.value.dense) return false;
|
|
6374
7470
|
for (const index of iterateIndices(() => entries.value.entries.length)) if (!elementGuard(entries.value.entries[index])) return false;
|
|
6375
7471
|
return true;
|
|
6376
7472
|
});
|
|
@@ -6379,7 +7475,7 @@ function tupleOf(...guards) {
|
|
|
6379
7475
|
return (value) => holds(() => {
|
|
6380
7476
|
if (!isArray(value)) return false;
|
|
6381
7477
|
const entries = readArrayEntries(value);
|
|
6382
|
-
if (
|
|
7478
|
+
if (isFailure(entries) || !entries.value.dense) return false;
|
|
6383
7479
|
if (entries.value.entries.length !== guards.length) return false;
|
|
6384
7480
|
for (const index of iterateIndices(() => guards.length)) {
|
|
6385
7481
|
const guard = guards[index];
|
|
@@ -6399,7 +7495,7 @@ function literalOf(...literals) {
|
|
|
6399
7495
|
}
|
|
6400
7496
|
}) : false) && isArray(first) ? readValue(() => {
|
|
6401
7497
|
const entries = readArrayEntries(first);
|
|
6402
|
-
if (
|
|
7498
|
+
if (isFailure(entries)) throw entries.error;
|
|
6403
7499
|
if (!entries.value.dense) throw new INTRINSICS.error("Literal vocabulary must be dense");
|
|
6404
7500
|
return entries.value.entries;
|
|
6405
7501
|
}, "literalOf", {
|
|
@@ -6481,7 +7577,7 @@ function setOf(elementGuard) {
|
|
|
6481
7577
|
return (value) => holds(() => {
|
|
6482
7578
|
if (!isSet(value)) return false;
|
|
6483
7579
|
const entries = readSetEntries(value);
|
|
6484
|
-
if (
|
|
7580
|
+
if (isFailure(entries)) return false;
|
|
6485
7581
|
for (const index of iterateIndices(() => entries.value.length)) if (!elementGuard(entries.value[index])) return false;
|
|
6486
7582
|
return true;
|
|
6487
7583
|
});
|
|
@@ -6490,7 +7586,7 @@ function mapOf(keyGuard, valueGuard) {
|
|
|
6490
7586
|
return (value) => holds(() => {
|
|
6491
7587
|
if (!isMap(value)) return false;
|
|
6492
7588
|
const entries = readMapEntries(value);
|
|
6493
|
-
if (
|
|
7589
|
+
if (isFailure(entries)) return false;
|
|
6494
7590
|
for (const index of iterateIndices(() => entries.value.length)) {
|
|
6495
7591
|
const entry = entries.value[index];
|
|
6496
7592
|
if (entry === void 0) return false;
|
|
@@ -6590,7 +7686,7 @@ function pickOf(shape, keys) {
|
|
|
6590
7686
|
return contain(() => {
|
|
6591
7687
|
const selected = readValue(() => {
|
|
6592
7688
|
const entries = readArrayEntries(keys);
|
|
6593
|
-
if (
|
|
7689
|
+
if (isFailure(entries)) throw entries.error;
|
|
6594
7690
|
if (!entries.value.dense) throw new INTRINSICS.error("Picked key list must be dense");
|
|
6595
7691
|
return collectMembers(entries.value.entries);
|
|
6596
7692
|
}, "pickOf", { subject: "keys" });
|
|
@@ -6629,7 +7725,7 @@ function omitOf(shape, keys) {
|
|
|
6629
7725
|
return contain(() => {
|
|
6630
7726
|
const skipped = readValue(() => {
|
|
6631
7727
|
const entries = readArrayEntries(keys);
|
|
6632
|
-
if (
|
|
7728
|
+
if (isFailure(entries)) throw entries.error;
|
|
6633
7729
|
if (!entries.value.dense) throw new INTRINSICS.error("Omitted key list must be dense");
|
|
6634
7730
|
return collectMembers(entries.value.entries);
|
|
6635
7731
|
}, "omitOf", { subject: "keys" });
|
|
@@ -6816,8 +7912,9 @@ function matchOf(pattern) {
|
|
|
6816
7912
|
* refinements — `min` / `max` length and a `pattern`.
|
|
6817
7913
|
*
|
|
6818
7914
|
* @remarks
|
|
6819
|
-
* Composes {@link isString} with {@link
|
|
6820
|
-
*
|
|
7915
|
+
* Composes {@link isString} with {@link matchesStringLength}, which counts code
|
|
7916
|
+
* points as the `minLength` and `maxLength` a string shape emits do, and an owned
|
|
7917
|
+
* stateless pattern (the same refinement {@link matchOf} performs).
|
|
6821
7918
|
* When `min`, `max`, and `pattern` are all absent it returns the bare {@link isString} guard
|
|
6822
7919
|
* (the unconstrained fast path), so an unrefined string leaf pays no wrapping
|
|
6823
7920
|
* cost. The single source of the string refinement shared by the compiled guard
|
|
@@ -6844,14 +7941,13 @@ function stringOf(options) {
|
|
|
6844
7941
|
"max",
|
|
6845
7942
|
"pattern"
|
|
6846
7943
|
], "stringOf", "string");
|
|
6847
|
-
const min = safe
|
|
6848
|
-
const max = safe
|
|
6849
|
-
const source = safe
|
|
7944
|
+
const min = safe !== void 0 && INTRINSICS.own(safe, "min") ? safe.min : void 0;
|
|
7945
|
+
const max = safe !== void 0 && INTRINSICS.own(safe, "max") ? safe.max : void 0;
|
|
7946
|
+
const source = safe !== void 0 && INTRINSICS.own(safe, "pattern") ? safe.pattern : void 0;
|
|
6850
7947
|
if (source !== void 0 && !isRegExp(source)) throw new ContractError("stringOf: pattern must be a RegExp", { code: "pattern" });
|
|
6851
7948
|
const pattern = source === void 0 ? void 0 : ownPattern(source, "stringOf");
|
|
6852
7949
|
if (min === void 0 && max === void 0 && pattern === void 0) return isString;
|
|
6853
|
-
|
|
6854
|
-
return whereOf(isString, (value) => withinLength(value.length) && (pattern === void 0 || matchesPattern(pattern, value)));
|
|
7950
|
+
return whereOf(isString, (value) => matchesStringLength(value, min, max) && (pattern === void 0 || matchesPattern(pattern, value)));
|
|
6855
7951
|
}, "stringOf");
|
|
6856
7952
|
}
|
|
6857
7953
|
/**
|
|
@@ -6937,7 +8033,7 @@ function parseNumber(value) {
|
|
|
6937
8033
|
const parsed = INTRINSICS.numeric(value);
|
|
6938
8034
|
return INTRINSICS.finite(parsed) ? parsed : void 0;
|
|
6939
8035
|
});
|
|
6940
|
-
return outcome
|
|
8036
|
+
return isSuccess(outcome) ? outcome.value : void 0;
|
|
6941
8037
|
}
|
|
6942
8038
|
/**
|
|
6943
8039
|
* Parses an unknown value to a finite integer.
|
|
@@ -7044,7 +8140,7 @@ function parseArray(value, guard) {
|
|
|
7044
8140
|
if (!isArray(value)) return void 0;
|
|
7045
8141
|
if (guard === void 0) return value;
|
|
7046
8142
|
const entries = readArrayEntries(value);
|
|
7047
|
-
if (
|
|
8143
|
+
if (isFailure(entries) || !entries.value.dense) return void 0;
|
|
7048
8144
|
if (!holds(() => INTRINSICS.reflect.apply(INTRINSICS.find, entries.value.entries, [(entry) => !guard(entry)]) === -1)) return;
|
|
7049
8145
|
return value;
|
|
7050
8146
|
}
|
|
@@ -7077,7 +8173,7 @@ function parseJSONValue(value) {
|
|
|
7077
8173
|
}
|
|
7078
8174
|
function parseEnum(value, allowed) {
|
|
7079
8175
|
const outcome = readArrayEntries(allowed);
|
|
7080
|
-
if (
|
|
8176
|
+
if (isFailure(outcome) || !outcome.value.dense) return void 0;
|
|
7081
8177
|
if (!holds(() => matchesMember(collectMembers(outcome.value.entries), value))) return void 0;
|
|
7082
8178
|
return isLiteralValue(value) ? value : void 0;
|
|
7083
8179
|
}
|
|
@@ -7285,7 +8381,7 @@ function parseJSONAs(value, guard) {
|
|
|
7285
8381
|
const parsed = parseJSON(value);
|
|
7286
8382
|
if (parsed === void 0) return void 0;
|
|
7287
8383
|
const checked = attempt(() => guard(parsed) ? parsed : void 0);
|
|
7288
|
-
return checked
|
|
8384
|
+
return isSuccess(checked) ? checked.value : void 0;
|
|
7289
8385
|
}
|
|
7290
8386
|
// src/core/EvaluationScope.ts
|
|
7291
8387
|
/** Owns compiled-node memo entries for one synchronous evaluation. */
|
|
@@ -7297,7 +8393,7 @@ var EvaluationScope = class EvaluationScope {
|
|
|
7297
8393
|
static run(plan, value, path = EvaluationScope.#path, clean = false) {
|
|
7298
8394
|
if (!isObject(value)) return plan(value, path);
|
|
7299
8395
|
const entries = readSetEntries(EvaluationScope.#active);
|
|
7300
|
-
if (
|
|
8396
|
+
if (isFailure(entries)) throw entries.error;
|
|
7301
8397
|
const active = entries.value[0];
|
|
7302
8398
|
if (active instanceof EvaluationScope) return active.#evaluate(plan, value, path, clean);
|
|
7303
8399
|
if (active !== void 0) throw new INTRINSICS.error("Evaluation scope is unavailable");
|
|
@@ -7376,7 +8472,9 @@ function seededRandom(seed) {
|
|
|
7376
8472
|
* chain pays a hundred clones of shrinking graphs and a hundred validations —
|
|
7377
8473
|
* quadratic work for a linear declaration. Here ownership runs once, validation runs once over
|
|
7378
8474
|
* that owned result, and each unique node and structural edge is indexed once
|
|
7379
|
-
* into children-before-parent order.
|
|
8475
|
+
* into children-before-parent order. A declaration a builder sealed is frozen and
|
|
8476
|
+
* validated all the way down already, so it takes neither and is indexed as it
|
|
8477
|
+
* stands. Every artifact family is then a single
|
|
7380
8478
|
* postorder pass whose entries are keyed by node identity, so a shared child is
|
|
7381
8479
|
* compiled once however many parents point at it. The same asymmetry exists on
|
|
7382
8480
|
* the VALUE side and is answered the same way: `guard`, `auditor` and `reporter`
|
|
@@ -7388,7 +8486,9 @@ function seededRandom(seed) {
|
|
|
7388
8486
|
* getters after it replay. A getter builds its own family and no other, except
|
|
7389
8487
|
* that `parser`, `reporter` and `generator` build the guard plan when the graph
|
|
7390
8488
|
* holds a union, because a union's membership question IS a guard question in
|
|
7391
|
-
* all three
|
|
8489
|
+
* all three, and every getter builds it when the graph holds a default, because
|
|
8490
|
+
* a default that its inner shape refuses refuses at whichever artifact is read
|
|
8491
|
+
* first.
|
|
7392
8492
|
*
|
|
7393
8493
|
* One terminal lifecycle covers preparation and every family. A failure settles
|
|
7394
8494
|
* the compiler: later getters rethrow that exact error, while an artifact
|
|
@@ -7559,7 +8659,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7559
8659
|
#leave(outcome) {
|
|
7560
8660
|
const state = this.#state;
|
|
7561
8661
|
if (state.phase === "interrupted") return this.#fail(state.poison);
|
|
7562
|
-
if (
|
|
8662
|
+
if (isFailure(outcome)) return this.#fail(isContractError(outcome.error) ? outcome.error : new ContractError("ContractCompiler: contract compilation failed", {
|
|
7563
8663
|
code: "structure",
|
|
7564
8664
|
context: {
|
|
7565
8665
|
path: [],
|
|
@@ -7608,10 +8708,15 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7608
8708
|
shape: "contract"
|
|
7609
8709
|
}
|
|
7610
8710
|
});
|
|
8711
|
+
const sealed = ShapePass.expansionOf(source);
|
|
8712
|
+
if (sealed !== void 0) {
|
|
8713
|
+
refuseExpansion(sealed);
|
|
8714
|
+
this.#discover(source);
|
|
8715
|
+
this.#source = void 0;
|
|
8716
|
+
return;
|
|
8717
|
+
}
|
|
7611
8718
|
const owned = ownShape(source);
|
|
7612
|
-
|
|
7613
|
-
validator.validate();
|
|
7614
|
-
refuseExpansion(validator.expansion);
|
|
8719
|
+
refuseExpansion(new ShapePass(owned).validate());
|
|
7615
8720
|
this.#discover(owned);
|
|
7616
8721
|
this.#source = void 0;
|
|
7617
8722
|
}
|
|
@@ -7640,7 +8745,9 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7640
8745
|
const shape = frame.shape;
|
|
7641
8746
|
if (INTRINSICS.reflect.apply(INTRINSICS.recall, known, [shape]) !== void 0) continue;
|
|
7642
8747
|
const index = this.#nodes.length;
|
|
7643
|
-
|
|
8748
|
+
const node = INTRINSICS.rebase({ ...shape }, null);
|
|
8749
|
+
this.#nodes[index] = node;
|
|
8750
|
+
INTRINSICS.reflect.apply(INTRINSICS.retain, known, [node, index]);
|
|
7644
8751
|
INTRINSICS.reflect.apply(INTRINSICS.retain, known, [shape, index]);
|
|
7645
8752
|
this.#stack[this.#stack.length] = {
|
|
7646
8753
|
operation: "exit",
|
|
@@ -7724,12 +8831,60 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7724
8831
|
path: [],
|
|
7725
8832
|
shape: "contract"
|
|
7726
8833
|
}
|
|
7727
|
-
});
|
|
7728
|
-
return shape;
|
|
7729
|
-
}
|
|
7730
|
-
#unions() {
|
|
7731
|
-
for (const index of iterateIndices(() => this.#nodes.length)) if (this.#nodes[index]?.category === "union") return true;
|
|
7732
|
-
return false;
|
|
8834
|
+
});
|
|
8835
|
+
return shape;
|
|
8836
|
+
}
|
|
8837
|
+
#unions() {
|
|
8838
|
+
for (const index of iterateIndices(() => this.#nodes.length)) if (this.#nodes[index]?.category === "union") return true;
|
|
8839
|
+
return false;
|
|
8840
|
+
}
|
|
8841
|
+
#defaults() {
|
|
8842
|
+
for (const index of iterateIndices(() => this.#nodes.length)) {
|
|
8843
|
+
const node = this.#nodes[index];
|
|
8844
|
+
if (node?.category === "optional" && INTRINSICS.own(node, "default")) return true;
|
|
8845
|
+
}
|
|
8846
|
+
return false;
|
|
8847
|
+
}
|
|
8848
|
+
#checkDefaults(shape, path) {
|
|
8849
|
+
switch (shape.category) {
|
|
8850
|
+
case "array":
|
|
8851
|
+
this.#checkDefaults(shape.items, pathOf(path, "items"));
|
|
8852
|
+
return;
|
|
8853
|
+
case "nullable":
|
|
8854
|
+
this.#checkDefaults(shape.inner, pathOf(path, "inner"));
|
|
8855
|
+
return;
|
|
8856
|
+
case "optional":
|
|
8857
|
+
this.#checkDefaults(shape.inner, pathOf(path, "inner"));
|
|
8858
|
+
if (!INTRINSICS.own(shape, "default") || this.#guardAt(shape.inner)(shape.default)) return;
|
|
8859
|
+
throw new ContractError("ContractCompiler: a default must satisfy the shape it fills; change the default or the shape", {
|
|
8860
|
+
code: "structure",
|
|
8861
|
+
context: {
|
|
8862
|
+
path: pathOf(path, "default"),
|
|
8863
|
+
shape: "optional",
|
|
8864
|
+
received: preview(shape.default)
|
|
8865
|
+
}
|
|
8866
|
+
});
|
|
8867
|
+
case "union":
|
|
8868
|
+
for (const index of iterateIndices(() => shape.variants.length)) {
|
|
8869
|
+
const variant = shape.variants[index];
|
|
8870
|
+
if (variant === void 0) continue;
|
|
8871
|
+
this.#checkDefaults(variant, pathOf(path, "variants", INTRINSICS.text(index)));
|
|
8872
|
+
}
|
|
8873
|
+
return;
|
|
8874
|
+
case "object": {
|
|
8875
|
+
const keys = INTRINSICS.keys(shape.properties);
|
|
8876
|
+
for (const index of iterateIndices(() => keys.length)) {
|
|
8877
|
+
const key = keys[index];
|
|
8878
|
+
const child = key === void 0 ? void 0 : shape.properties[key];
|
|
8879
|
+
if (child === void 0) continue;
|
|
8880
|
+
this.#checkDefaults(child, pathOf(path, "properties", key));
|
|
8881
|
+
}
|
|
8882
|
+
const extra = shape.additionalProperties;
|
|
8883
|
+
if (extra !== void 0 && extra !== true && extra !== false) this.#checkDefaults(extra, pathOf(path, "additionalProperties"));
|
|
8884
|
+
return;
|
|
8885
|
+
}
|
|
8886
|
+
default: return;
|
|
8887
|
+
}
|
|
7733
8888
|
}
|
|
7734
8889
|
#repeats(index) {
|
|
7735
8890
|
const category = this.#node(index).category;
|
|
@@ -7742,10 +8897,64 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7742
8897
|
#trackFaults(plan) {
|
|
7743
8898
|
return (value, path) => EvaluationScope.run(plan, value, path, true);
|
|
7744
8899
|
}
|
|
8900
|
+
#discriminate(owned) {
|
|
8901
|
+
const arms = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(owned.variants), [(variant) => variant?.category === "object"]);
|
|
8902
|
+
const first = arms[0];
|
|
8903
|
+
if (first === void 0 || arms.length !== owned.variants.length) return void 0;
|
|
8904
|
+
const keys = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(INTRINSICS.keys(first.properties)), [(key) => !INTRINSICS.reflect.apply(INTRINSICS.some, arms, [(arm) => !INTRINSICS.own(arm.properties, key) || arm.properties[key]?.category !== "literal"])]);
|
|
8905
|
+
const tables = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(keys), [(key) => ContractCompiler.#tableOf(key, arms)]);
|
|
8906
|
+
const named = tables[0];
|
|
8907
|
+
if (named === void 0) return void 0;
|
|
8908
|
+
return {
|
|
8909
|
+
hint: INTRINSICS.freeze({
|
|
8910
|
+
key: named.key,
|
|
8911
|
+
expected: named.expected
|
|
8912
|
+
}),
|
|
8913
|
+
select: (value) => {
|
|
8914
|
+
if (!isObject(value)) return void 0;
|
|
8915
|
+
for (const index of iterateIndices(() => tables.length)) {
|
|
8916
|
+
const table = tables[index];
|
|
8917
|
+
if (table === void 0) continue;
|
|
8918
|
+
const held = attempt(() => INTRINSICS.describe(value, table.key));
|
|
8919
|
+
if (isFailure(held) || held.value === void 0) continue;
|
|
8920
|
+
if (!INTRINSICS.own(held.value, "value")) continue;
|
|
8921
|
+
const arm = INTRINSICS.reflect.apply(INTRINSICS.fetch, table.members, [held.value.value]);
|
|
8922
|
+
if (arm !== void 0 && arm >= 0) return arm;
|
|
8923
|
+
}
|
|
8924
|
+
}
|
|
8925
|
+
};
|
|
8926
|
+
}
|
|
8927
|
+
static #tableOf(key, arms) {
|
|
8928
|
+
const members = new INTRINSICS.map();
|
|
8929
|
+
const expected = [];
|
|
8930
|
+
for (const armIndex of iterateIndices(() => arms.length)) {
|
|
8931
|
+
const node = arms[armIndex]?.properties[key];
|
|
8932
|
+
if (node?.category !== "literal") continue;
|
|
8933
|
+
for (const valueIndex of iterateIndices(() => node.values.length)) {
|
|
8934
|
+
const literal = node.values[valueIndex];
|
|
8935
|
+
if (literal === void 0) continue;
|
|
8936
|
+
const known = INTRINSICS.reflect.apply(INTRINSICS.fetch, members, [literal]);
|
|
8937
|
+
if (known === void 0) expected[expected.length] = literal;
|
|
8938
|
+
const arm = known === void 0 || known === armIndex ? armIndex : -1;
|
|
8939
|
+
INTRINSICS.reflect.apply(INTRINSICS.store, members, [literal, arm]);
|
|
8940
|
+
}
|
|
8941
|
+
}
|
|
8942
|
+
return {
|
|
8943
|
+
key,
|
|
8944
|
+
members,
|
|
8945
|
+
expected: INTRINSICS.freeze(expected)
|
|
8946
|
+
};
|
|
8947
|
+
}
|
|
8948
|
+
static #parseArm(arm, value) {
|
|
8949
|
+
if (arm.guard(value)) return value;
|
|
8950
|
+
const parsed = arm.parse(value);
|
|
8951
|
+
return parsed !== void 0 && arm.guard(parsed) ? parsed : void 0;
|
|
8952
|
+
}
|
|
7745
8953
|
#buildSchema() {
|
|
7746
8954
|
const ready = this.#schema;
|
|
7747
8955
|
if (ready !== void 0) return ready;
|
|
7748
8956
|
this.#prepare();
|
|
8957
|
+
if (this.#defaults()) this.#buildGuard();
|
|
7749
8958
|
for (const step of iterateIndices(() => this.#order.length)) {
|
|
7750
8959
|
const index = this.#order[step];
|
|
7751
8960
|
if (index === void 0) continue;
|
|
@@ -7769,6 +8978,13 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7769
8978
|
#schemaOf(index) {
|
|
7770
8979
|
const owned = this.#node(index);
|
|
7771
8980
|
switch (owned.category) {
|
|
8981
|
+
case "shape": return INTRINSICS.freeze({
|
|
8982
|
+
type: "object",
|
|
8983
|
+
description: owned.description ?? "Over-approximates declarations whose JSON projection preserves their own enumerable data; values without that projection are outside this schema. is decides whether the complete shape is valid.",
|
|
8984
|
+
properties: INTRINSICS.freeze({ category: INTRINSICS.freeze({ enum: INTRINSICS.freeze(INTRINSICS.keys(SHAPE_FIELDS)) }) }),
|
|
8985
|
+
required: INTRINSICS.freeze(["category"]),
|
|
8986
|
+
additionalProperties: true
|
|
8987
|
+
});
|
|
7772
8988
|
case "string": {
|
|
7773
8989
|
const text = owned.pattern === void 0 ? void 0 : readPatternSource(owned.pattern);
|
|
7774
8990
|
if (owned.pattern !== void 0 && text === void 0) throw new ContractError("compileSchema: pattern source could not be read", {
|
|
@@ -7818,7 +9034,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7818
9034
|
if (child === void 0) return [];
|
|
7819
9035
|
return [{
|
|
7820
9036
|
key,
|
|
7821
|
-
schema: this.#schemaAt(child
|
|
9037
|
+
schema: this.#schemaAt(child),
|
|
7822
9038
|
required: child.category !== "optional"
|
|
7823
9039
|
}];
|
|
7824
9040
|
}]);
|
|
@@ -7842,7 +9058,14 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7842
9058
|
...owned.description !== void 0 ? { description: owned.description } : {}
|
|
7843
9059
|
});
|
|
7844
9060
|
}
|
|
7845
|
-
case "optional":
|
|
9061
|
+
case "optional": {
|
|
9062
|
+
const emitted = this.#schemaAt(owned.inner);
|
|
9063
|
+
const value = owned.default;
|
|
9064
|
+
return value === void 0 ? emitted : INTRINSICS.freeze({
|
|
9065
|
+
...emitted,
|
|
9066
|
+
default: value
|
|
9067
|
+
});
|
|
9068
|
+
}
|
|
7846
9069
|
case "nullable": return INTRINSICS.freeze({ anyOf: INTRINSICS.freeze([this.#schemaAt(owned.inner), INTRINSICS.freeze({ type: "null" })]) });
|
|
7847
9070
|
case "raw": return cloneSchema(owned.schema);
|
|
7848
9071
|
}
|
|
@@ -7858,6 +9081,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7858
9081
|
this.#guards[index] = this.#repeats(index) ? this.#trackGuard(plan) : plan;
|
|
7859
9082
|
}
|
|
7860
9083
|
const root = this.#guardAt(this.#node(0));
|
|
9084
|
+
if (this.#defaults()) this.#checkDefaults(this.#node(0), []);
|
|
7861
9085
|
this.#guard = root;
|
|
7862
9086
|
return root;
|
|
7863
9087
|
}
|
|
@@ -7872,14 +9096,24 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7872
9096
|
});
|
|
7873
9097
|
return compiled;
|
|
7874
9098
|
}
|
|
9099
|
+
#stringGuard(owned) {
|
|
9100
|
+
return stringOf(INTRINSICS.rebase({
|
|
9101
|
+
...owned.min === void 0 ? {} : { min: owned.min },
|
|
9102
|
+
...owned.max === void 0 ? {} : { max: owned.max },
|
|
9103
|
+
...owned.pattern === void 0 ? {} : { pattern: owned.pattern }
|
|
9104
|
+
}, null));
|
|
9105
|
+
}
|
|
9106
|
+
static #acceptShape(value) {
|
|
9107
|
+
return isSuccess(attempt(() => {
|
|
9108
|
+
const validator = new ShapeValidator(value);
|
|
9109
|
+
validator.validate();
|
|
9110
|
+
refuseExpansion(validator.expansion);
|
|
9111
|
+
}));
|
|
9112
|
+
}
|
|
7875
9113
|
#guardOf(index) {
|
|
7876
9114
|
const owned = this.#node(index);
|
|
7877
9115
|
switch (owned.category) {
|
|
7878
|
-
case "string": return
|
|
7879
|
-
...owned.min === void 0 ? {} : { min: owned.min },
|
|
7880
|
-
...owned.max === void 0 ? {} : { max: owned.max },
|
|
7881
|
-
...owned.pattern === void 0 ? {} : { pattern: owned.pattern }
|
|
7882
|
-
});
|
|
9116
|
+
case "string": return this.#stringGuard(owned);
|
|
7883
9117
|
case "number": {
|
|
7884
9118
|
const base = owned.integer === true ? isInteger : isFiniteNumber;
|
|
7885
9119
|
if (owned.min === void 0 && owned.max === void 0) return base;
|
|
@@ -7888,6 +9122,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7888
9122
|
case "boolean": return isBoolean;
|
|
7889
9123
|
case "null": return isNull;
|
|
7890
9124
|
case "json": return isJSONValue;
|
|
9125
|
+
case "shape": return (value) => ContractCompiler.#acceptShape(value);
|
|
7891
9126
|
case "literal": return literalOf(owned.values);
|
|
7892
9127
|
case "array": {
|
|
7893
9128
|
const base = arrayOf(this.#guardAt(owned.items));
|
|
@@ -7945,7 +9180,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7945
9180
|
}
|
|
7946
9181
|
return true;
|
|
7947
9182
|
});
|
|
7948
|
-
return outcome
|
|
9183
|
+
return isSuccess(outcome) && outcome.value;
|
|
7949
9184
|
};
|
|
7950
9185
|
}
|
|
7951
9186
|
case "union": {
|
|
@@ -7964,7 +9199,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7964
9199
|
const ready = this.#parser;
|
|
7965
9200
|
if (ready !== void 0) return ready;
|
|
7966
9201
|
this.#prepare();
|
|
7967
|
-
if (this.#unions()) this.#buildGuard();
|
|
9202
|
+
if (this.#unions() || this.#defaults()) this.#buildGuard();
|
|
7968
9203
|
for (const step of iterateIndices(() => this.#order.length)) {
|
|
7969
9204
|
const index = this.#order[step];
|
|
7970
9205
|
if (index === void 0) continue;
|
|
@@ -7990,11 +9225,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7990
9225
|
switch (owned.category) {
|
|
7991
9226
|
case "string": {
|
|
7992
9227
|
if (owned.min === void 0 && owned.max === void 0 && owned.pattern === void 0) return parseString;
|
|
7993
|
-
const guard =
|
|
7994
|
-
...owned.min === void 0 ? {} : { min: owned.min },
|
|
7995
|
-
...owned.max === void 0 ? {} : { max: owned.max },
|
|
7996
|
-
...owned.pattern === void 0 ? {} : { pattern: owned.pattern }
|
|
7997
|
-
});
|
|
9228
|
+
const guard = this.#stringGuard(owned);
|
|
7998
9229
|
return (value) => {
|
|
7999
9230
|
const parsed = parseString(value);
|
|
8000
9231
|
return parsed !== void 0 && guard(parsed) ? parsed : void 0;
|
|
@@ -8012,6 +9243,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8012
9243
|
case "boolean": return parseBoolean;
|
|
8013
9244
|
case "null": return (value) => value === null ? null : void 0;
|
|
8014
9245
|
case "json": return parseJSONValue;
|
|
9246
|
+
case "shape": return (value) => ContractCompiler.#acceptShape(value) ? value : void 0;
|
|
8015
9247
|
case "literal": {
|
|
8016
9248
|
const allowed = literalOf(owned.values);
|
|
8017
9249
|
return (value) => {
|
|
@@ -8033,7 +9265,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8033
9265
|
});
|
|
8034
9266
|
if (!isArray(value)) return void 0;
|
|
8035
9267
|
const entries = readArrayEntries(value);
|
|
8036
|
-
if (
|
|
9268
|
+
if (isFailure(entries)) return readValue(() => {
|
|
8037
9269
|
throw entries.error;
|
|
8038
9270
|
}, "compileParser", {
|
|
8039
9271
|
subject: "array",
|
|
@@ -8059,7 +9291,8 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8059
9291
|
return [{
|
|
8060
9292
|
key,
|
|
8061
9293
|
parse: this.#parserAt(inner),
|
|
8062
|
-
optional
|
|
9294
|
+
optional,
|
|
9295
|
+
fill: optional && INTRINSICS.own(child, "default") ? child.default : void 0
|
|
8063
9296
|
}];
|
|
8064
9297
|
}]);
|
|
8065
9298
|
const known = readValue(() => collectMembers(INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(entries), [(entry) => entry.key])), "compileParser", {
|
|
@@ -8083,14 +9316,14 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8083
9316
|
for (const entryIndex of iterateIndices(() => entries.length)) {
|
|
8084
9317
|
const entry = entries[entryIndex];
|
|
8085
9318
|
if (entry === void 0) continue;
|
|
8086
|
-
|
|
8087
|
-
if (entry.optional) continue;
|
|
8088
|
-
return;
|
|
8089
|
-
}
|
|
8090
|
-
const raw = record[entry.key];
|
|
9319
|
+
const raw = (present === void 0 ? (seen & 1 << entryIndex) === 0 : !matchesMember(present, entry.key)) ? void 0 : record[entry.key];
|
|
8091
9320
|
if (raw === void 0) {
|
|
8092
|
-
if (entry.optional)
|
|
8093
|
-
|
|
9321
|
+
if (!entry.optional) return void 0;
|
|
9322
|
+
if (entry.fill !== void 0) {
|
|
9323
|
+
const fill = isObject(entry.fill) ? cloneJSONValue(entry.fill) : entry.fill;
|
|
9324
|
+
selected[selected.length] = [entry.key, entry.parse(fill)];
|
|
9325
|
+
}
|
|
9326
|
+
continue;
|
|
8094
9327
|
}
|
|
8095
9328
|
const parsed = entry.parse(raw);
|
|
8096
9329
|
if (parsed === void 0) return void 0;
|
|
@@ -8109,7 +9342,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8109
9342
|
}
|
|
8110
9343
|
return INTRINSICS.rebase(INTRINSICS.record({ [Symbol.iterator]: INTRINSICS.reflect.apply(INTRINSICS.bind, INTRINSICS.iterator, [selected]) }), null);
|
|
8111
9344
|
});
|
|
8112
|
-
if (outcome
|
|
9345
|
+
if (isSuccess(outcome)) return outcome.value;
|
|
8113
9346
|
if (isContractError(outcome.error)) throw outcome.error;
|
|
8114
9347
|
return readValue(() => {
|
|
8115
9348
|
throw outcome.error;
|
|
@@ -8124,12 +9357,19 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8124
9357
|
parse: this.#parserAt(variant),
|
|
8125
9358
|
guard: this.#guardAt(variant)
|
|
8126
9359
|
}]]);
|
|
9360
|
+
const discriminated = this.#discriminate(owned);
|
|
8127
9361
|
if (owned.mode === "oneOf") return (value) => {
|
|
9362
|
+
const selected = discriminated?.select(value);
|
|
9363
|
+
const arm = selected === void 0 ? void 0 : variants[selected];
|
|
9364
|
+
if (arm !== void 0) return ContractCompiler.#parseArm(arm, value);
|
|
8128
9365
|
const matching = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(variants), [(variant) => variant.guard(value)]);
|
|
8129
9366
|
const only = matching[0];
|
|
8130
|
-
return matching.length === 1 && only !== void 0 ?
|
|
9367
|
+
return matching.length === 1 && only !== void 0 ? value : void 0;
|
|
8131
9368
|
};
|
|
8132
9369
|
return (value) => {
|
|
9370
|
+
const selected = discriminated?.select(value);
|
|
9371
|
+
const arm = selected === void 0 ? void 0 : variants[selected];
|
|
9372
|
+
if (arm !== void 0) return ContractCompiler.#parseArm(arm, value);
|
|
8133
9373
|
for (const variantIndex of iterateIndices(() => variants.length)) {
|
|
8134
9374
|
const variant = variants[variantIndex];
|
|
8135
9375
|
if (variant === void 0) continue;
|
|
@@ -8158,6 +9398,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8158
9398
|
const ready = this.#auditor;
|
|
8159
9399
|
if (ready !== void 0) return ready;
|
|
8160
9400
|
this.#prepare();
|
|
9401
|
+
if (this.#defaults()) this.#buildGuard();
|
|
8161
9402
|
for (const step of iterateIndices(() => this.#order.length)) {
|
|
8162
9403
|
const index = this.#order[step];
|
|
8163
9404
|
if (index === void 0) continue;
|
|
@@ -8232,6 +9473,12 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8232
9473
|
expected: "json",
|
|
8233
9474
|
received: preview(value)
|
|
8234
9475
|
}];
|
|
9476
|
+
case "shape": return (value, path) => ContractCompiler.#acceptShape(value) ? [] : [{
|
|
9477
|
+
reason: "type",
|
|
9478
|
+
path,
|
|
9479
|
+
expected: "shape",
|
|
9480
|
+
received: preview(value)
|
|
9481
|
+
}];
|
|
8235
9482
|
case "literal": {
|
|
8236
9483
|
const allowed = literalOf(owned.values);
|
|
8237
9484
|
return (value, path) => allowed(value) ? [] : [{
|
|
@@ -8259,7 +9506,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8259
9506
|
received: preview(value)
|
|
8260
9507
|
}];
|
|
8261
9508
|
const entries = readArrayEntries(value);
|
|
8262
|
-
if (
|
|
9509
|
+
if (isFailure(entries)) return readValue(() => {
|
|
8263
9510
|
throw entries.error;
|
|
8264
9511
|
}, "compileAuditor", {
|
|
8265
9512
|
subject: "array",
|
|
@@ -8295,7 +9542,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8295
9542
|
const closed = extra === void 0 || extra === false;
|
|
8296
9543
|
const additional = closed || extra === true ? void 0 : this.#auditAt(extra);
|
|
8297
9544
|
const planned = attempt(() => collectMembers(declaredKeys));
|
|
8298
|
-
const declared = planned
|
|
9545
|
+
const declared = isSuccess(planned) ? planned.value : void 0;
|
|
8299
9546
|
const maskable = entries.length <= 31;
|
|
8300
9547
|
const positions = buildPositions(maskable ? INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(entries), [(entry) => entry.key]) : []);
|
|
8301
9548
|
return (value, path) => {
|
|
@@ -8355,7 +9602,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8355
9602
|
if (additional !== void 0) appendEntries(faults, additional(observed, pathOf(path, key)));
|
|
8356
9603
|
}
|
|
8357
9604
|
});
|
|
8358
|
-
if (
|
|
9605
|
+
if (isFailure(outcome)) {
|
|
8359
9606
|
if (isContractError(outcome.error)) throw outcome.error;
|
|
8360
9607
|
return readValue(() => {
|
|
8361
9608
|
throw outcome.error;
|
|
@@ -8374,7 +9621,11 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8374
9621
|
const plans = INTRINSICS.reflect.apply(INTRINSICS.expand, readIndexView(owned.variants), [(variant) => variant === void 0 ? [] : [this.#auditAt(variant)]]);
|
|
8375
9622
|
const exclusive = owned.mode === "oneOf";
|
|
8376
9623
|
const count = owned.variants.length;
|
|
9624
|
+
const discriminated = this.#discriminate(owned);
|
|
8377
9625
|
return (value, path) => {
|
|
9626
|
+
const selected = discriminated?.select(value);
|
|
9627
|
+
const arm = selected === void 0 ? void 0 : plans[selected];
|
|
9628
|
+
if (arm !== void 0) return arm(value, path);
|
|
8378
9629
|
const perVariant = [];
|
|
8379
9630
|
for (const index2 of iterateIndices(() => plans.length)) {
|
|
8380
9631
|
const plan = plans[index2];
|
|
@@ -8391,18 +9642,21 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8391
9642
|
if (matched > 1) return [{
|
|
8392
9643
|
reason: "oneOf",
|
|
8393
9644
|
path,
|
|
8394
|
-
matched
|
|
9645
|
+
matched,
|
|
9646
|
+
...discriminated?.hint
|
|
8395
9647
|
}];
|
|
8396
9648
|
}
|
|
8397
9649
|
const closest = selectClosestFaults(perVariant);
|
|
8398
9650
|
const report = [exclusive ? {
|
|
8399
9651
|
reason: "oneOf",
|
|
8400
9652
|
path,
|
|
8401
|
-
matched: 0
|
|
9653
|
+
matched: 0,
|
|
9654
|
+
...discriminated?.hint
|
|
8402
9655
|
} : {
|
|
8403
9656
|
reason: "variant",
|
|
8404
9657
|
path,
|
|
8405
|
-
variants: count
|
|
9658
|
+
variants: count,
|
|
9659
|
+
...discriminated?.hint
|
|
8406
9660
|
}];
|
|
8407
9661
|
appendEntries(report, closest);
|
|
8408
9662
|
return limitEntries(report, 64);
|
|
@@ -8428,7 +9682,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8428
9682
|
const ready = this.#reporter;
|
|
8429
9683
|
if (ready !== void 0) return ready;
|
|
8430
9684
|
this.#prepare();
|
|
8431
|
-
if (this.#unions()) this.#buildGuard();
|
|
9685
|
+
if (this.#unions() || this.#defaults()) this.#buildGuard();
|
|
8432
9686
|
for (const step of iterateIndices(() => this.#order.length)) {
|
|
8433
9687
|
const index = this.#order[step];
|
|
8434
9688
|
if (index === void 0) continue;
|
|
@@ -8514,6 +9768,12 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8514
9768
|
received: preview(value)
|
|
8515
9769
|
}];
|
|
8516
9770
|
}
|
|
9771
|
+
case "shape": return (value, path) => ContractCompiler.#acceptShape(value) ? [] : [{
|
|
9772
|
+
reason: "type",
|
|
9773
|
+
path,
|
|
9774
|
+
expected: "shape",
|
|
9775
|
+
received: preview(value)
|
|
9776
|
+
}];
|
|
8517
9777
|
case "array": {
|
|
8518
9778
|
const node = owned;
|
|
8519
9779
|
const item = this.#reportAt(owned.items);
|
|
@@ -8525,7 +9785,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8525
9785
|
received: preview(value)
|
|
8526
9786
|
}];
|
|
8527
9787
|
const entries = readArrayEntries(value);
|
|
8528
|
-
if (
|
|
9788
|
+
if (isFailure(entries)) return [{
|
|
8529
9789
|
reason: "type",
|
|
8530
9790
|
path,
|
|
8531
9791
|
expected: "array",
|
|
@@ -8559,7 +9819,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8559
9819
|
const tail = extra === void 0 || extra === true || extra === false ? void 0 : this.#reportAt(extra);
|
|
8560
9820
|
const open = extra === true || tail !== void 0;
|
|
8561
9821
|
const planned = attempt(() => collectMembers(names));
|
|
8562
|
-
const known = planned
|
|
9822
|
+
const known = isSuccess(planned) ? planned.value : void 0;
|
|
8563
9823
|
const maskable = entries.length <= 31;
|
|
8564
9824
|
const positions = buildPositions(maskable ? INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(entries), [(entry) => entry.key]) : []);
|
|
8565
9825
|
return (value, path) => {
|
|
@@ -8578,7 +9838,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8578
9838
|
received: preview(value)
|
|
8579
9839
|
}];
|
|
8580
9840
|
const faults = [];
|
|
8581
|
-
if (
|
|
9841
|
+
if (isFailure(attempt(() => {
|
|
8582
9842
|
const present = maskable ? void 0 : collectMembers(keys);
|
|
8583
9843
|
const vocabulary = known ?? collectMembers(names);
|
|
8584
9844
|
const seen = present === void 0 ? readPresence(keys, positions) : 0;
|
|
@@ -8613,7 +9873,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8613
9873
|
const observed = record[key];
|
|
8614
9874
|
if (tail !== void 0) appendEntries(faults, tail(observed, pathOf(path, key)));
|
|
8615
9875
|
}
|
|
8616
|
-
})
|
|
9876
|
+
}))) return [{
|
|
8617
9877
|
reason: "type",
|
|
8618
9878
|
path,
|
|
8619
9879
|
expected: "object",
|
|
@@ -8631,7 +9891,11 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8631
9891
|
const guards = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(variants), [(variant) => variant.guard]);
|
|
8632
9892
|
const exclusive = owned.mode === "oneOf";
|
|
8633
9893
|
const count = owned.variants.length;
|
|
9894
|
+
const discriminated = this.#discriminate(owned);
|
|
8634
9895
|
return (value, path) => {
|
|
9896
|
+
const selected = discriminated?.select(value);
|
|
9897
|
+
const arm = selected === void 0 ? void 0 : plans[selected];
|
|
9898
|
+
if (arm !== void 0) return arm(value, path);
|
|
8635
9899
|
const perVariant = [];
|
|
8636
9900
|
for (const index2 of iterateIndices(() => plans.length)) {
|
|
8637
9901
|
const plan = plans[index2];
|
|
@@ -8648,7 +9912,8 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8648
9912
|
const report = [{
|
|
8649
9913
|
reason: "oneOf",
|
|
8650
9914
|
path,
|
|
8651
|
-
matched: 0
|
|
9915
|
+
matched: 0,
|
|
9916
|
+
...discriminated?.hint
|
|
8652
9917
|
}];
|
|
8653
9918
|
appendEntries(report, closest);
|
|
8654
9919
|
return limitEntries(report, 64);
|
|
@@ -8656,13 +9921,15 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8656
9921
|
return [{
|
|
8657
9922
|
reason: "oneOf",
|
|
8658
9923
|
path,
|
|
8659
|
-
matched
|
|
9924
|
+
matched,
|
|
9925
|
+
...discriminated?.hint
|
|
8660
9926
|
}];
|
|
8661
9927
|
}
|
|
8662
9928
|
const report = [{
|
|
8663
9929
|
reason: "variant",
|
|
8664
9930
|
path,
|
|
8665
|
-
variants: count
|
|
9931
|
+
variants: count,
|
|
9932
|
+
...discriminated?.hint
|
|
8666
9933
|
}];
|
|
8667
9934
|
appendEntries(report, closest);
|
|
8668
9935
|
return limitEntries(report, 64);
|
|
@@ -8688,7 +9955,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8688
9955
|
const ready = this.#generator;
|
|
8689
9956
|
if (ready !== void 0) return ready;
|
|
8690
9957
|
this.#prepare();
|
|
8691
|
-
if (this.#unions()) this.#buildGuard();
|
|
9958
|
+
if (this.#unions() || this.#defaults()) this.#buildGuard();
|
|
8692
9959
|
for (const step of iterateIndices(() => this.#order.length)) {
|
|
8693
9960
|
const index = this.#order[step];
|
|
8694
9961
|
if (index === void 0) continue;
|
|
@@ -8755,6 +10022,11 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8755
10022
|
}
|
|
8756
10023
|
case "boolean": return (random) => drawRandom(random, "boolean") >= .5;
|
|
8757
10024
|
case "null": return () => null;
|
|
10025
|
+
case "shape": return () => ({
|
|
10026
|
+
category: "object",
|
|
10027
|
+
properties: {},
|
|
10028
|
+
additionalProperties: false
|
|
10029
|
+
});
|
|
8758
10030
|
case "json": return (random) => {
|
|
8759
10031
|
const pick = INTRINSICS.floor(drawRandom(random, "json") * 5);
|
|
8760
10032
|
if (pick === 0) return null;
|
|
@@ -8838,8 +10110,8 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8838
10110
|
const plan = plans[(start + attemptIndex) % count];
|
|
8839
10111
|
if (plan === void 0) continue;
|
|
8840
10112
|
const outcome = attempt(() => plan(random));
|
|
8841
|
-
if (
|
|
8842
|
-
if (outcome
|
|
10113
|
+
if (isFailure(outcome) && isContractError(outcome.error) && outcome.error.code === "random") throw outcome.error;
|
|
10114
|
+
if (isSuccess(outcome) && guard(outcome.value)) return outcome.value;
|
|
8843
10115
|
}
|
|
8844
10116
|
throw new ContractError("compileGenerator: no union candidate satisfied the compiled guard", {
|
|
8845
10117
|
code: "generate",
|
|
@@ -8921,14 +10193,15 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8921
10193
|
* eager well-formedness pass — there is no second prepass and no alias for it:
|
|
8922
10194
|
* it enforces every bound domain and range used by the artifacts, including
|
|
8923
10195
|
* non-empty literal/union vocabularies, finite literal numbers, integer-range
|
|
8924
|
-
* satisfiability, unflagged string patterns, optional-shape
|
|
8925
|
-
* recursively supported raw-schema vocabulary where every
|
|
8926
|
-
* dense union member is a record.
|
|
10196
|
+
* satisfiability, unflagged string patterns, optional-shape and default
|
|
10197
|
+
* placement, and the recursively supported raw-schema vocabulary where every
|
|
10198
|
+
* present property or dense union member is a record. It judges a default's
|
|
10199
|
+
* structure only; each compiler refuses a default its inner guard refuses.
|
|
8927
10200
|
* Active ancestors are tracked so shared children remain legal. Every
|
|
8928
10201
|
* standalone compiler reaches this same validation once, over its owned
|
|
8929
10202
|
* snapshot, through {@link ContractCompiler} preparation.
|
|
8930
|
-
*
|
|
8931
|
-
*
|
|
10203
|
+
* Cyclic graphs refuse before depth measurement, with structure before cycle.
|
|
10204
|
+
* Acyclic graphs report depth before structure, then field and vocabulary policy.
|
|
8932
10205
|
*
|
|
8933
10206
|
* @param shape - The shape graph to gate
|
|
8934
10207
|
* @returns Nothing; successful return means recursive compilation is structurally safe, depth-safe, and bounded in emitted nodes
|
|
@@ -9070,9 +10343,104 @@ function compileReporter(shape, value, path = []) {
|
|
|
9070
10343
|
function compileAuditor(shape, value, path = []) {
|
|
9071
10344
|
return contain(() => new ContractCompiler(shape).auditor(value, path), "compileAuditor");
|
|
9072
10345
|
}
|
|
10346
|
+
// src/core/LazyContract.ts
|
|
10347
|
+
/**
|
|
10348
|
+
* Owns a contract whose function members compile together on the first call to any.
|
|
10349
|
+
*
|
|
10350
|
+
* @remarks
|
|
10351
|
+
* For a declaration made entirely by builders, where preparation and the schema have
|
|
10352
|
+
* already asked everything that can refuse. A module that declares its contracts as
|
|
10353
|
+
* data then pays for a contract only when something uses it, and each member is a
|
|
10354
|
+
* plain function with one identity, so destructuring and spreading compile nothing.
|
|
10355
|
+
*
|
|
10356
|
+
* A compile can still fail for the host's reasons: a first call near the stack limit,
|
|
10357
|
+
* or a host whose members were replaced after this contract was created, which it
|
|
10358
|
+
* refuses to compile on since the result would outlive the replacement. Neither says
|
|
10359
|
+
* anything about a later call, which compiles afresh. Until then a guard answers
|
|
10360
|
+
* `false` and a parser `undefined`, and the diagnostic and seed members publish the
|
|
10361
|
+
* failure as their doors do.
|
|
10362
|
+
*/
|
|
10363
|
+
var LazyContract = class {
|
|
10364
|
+
#shape;
|
|
10365
|
+
#pending;
|
|
10366
|
+
#settled;
|
|
10367
|
+
#bundle;
|
|
10368
|
+
/**
|
|
10369
|
+
* Retains a declaration and the compiler already prepared from it.
|
|
10370
|
+
*
|
|
10371
|
+
* @param shape - The sealed declaration
|
|
10372
|
+
* @param compiler - The compiler that owns its prepared index
|
|
10373
|
+
*/
|
|
10374
|
+
constructor(shape, compiler) {
|
|
10375
|
+
this.#shape = shape;
|
|
10376
|
+
this.#pending = compiler;
|
|
10377
|
+
}
|
|
10378
|
+
/**
|
|
10379
|
+
* Compiles the schema, which refuses a bad declaration, and publishes the bundle.
|
|
10380
|
+
*
|
|
10381
|
+
* @param schema - The schema the prepared compiler emitted
|
|
10382
|
+
* @returns The frozen bundle, in the key order the compiler's own bundle uses
|
|
10383
|
+
*/
|
|
10384
|
+
publish(schema) {
|
|
10385
|
+
return INTRINSICS.freeze({
|
|
10386
|
+
schema,
|
|
10387
|
+
is: this.#is(),
|
|
10388
|
+
parse: this.#parse(),
|
|
10389
|
+
audit: this.#audit(),
|
|
10390
|
+
explain: this.#explain(),
|
|
10391
|
+
generate: this.#generate()
|
|
10392
|
+
});
|
|
10393
|
+
}
|
|
10394
|
+
#is() {
|
|
10395
|
+
return (value) => {
|
|
10396
|
+
const bundle = this.#bundle ?? this.#attempt();
|
|
10397
|
+
return bundle !== void 0 && bundle.is(value);
|
|
10398
|
+
};
|
|
10399
|
+
}
|
|
10400
|
+
#parse() {
|
|
10401
|
+
return (value) => {
|
|
10402
|
+
const bundle = this.#bundle ?? this.#attempt();
|
|
10403
|
+
return bundle === void 0 ? void 0 : bundle.parse(value);
|
|
10404
|
+
};
|
|
10405
|
+
}
|
|
10406
|
+
#audit() {
|
|
10407
|
+
return (value, path = []) => (this.#settled ?? this.#settle("compileAuditor")).auditor(value, path);
|
|
10408
|
+
}
|
|
10409
|
+
#explain() {
|
|
10410
|
+
return (value, path = []) => (this.#settled ?? this.#settle("compileReporter")).reporter(value, path);
|
|
10411
|
+
}
|
|
10412
|
+
#generate() {
|
|
10413
|
+
return (random) => (this.#settled ?? this.#settle("compileGenerator")).generator(random);
|
|
10414
|
+
}
|
|
10415
|
+
#attempt() {
|
|
10416
|
+
attempt(() => this.#settle("createContract"));
|
|
10417
|
+
return this.#bundle;
|
|
10418
|
+
}
|
|
10419
|
+
#settle(door) {
|
|
10420
|
+
return contain(() => {
|
|
10421
|
+
if (ShapePass.tampered()) throw new ContractError(`${door}: a host member this contract compiles on was replaced after it was created`, {
|
|
10422
|
+
code: "structure",
|
|
10423
|
+
context: {
|
|
10424
|
+
path: [],
|
|
10425
|
+
shape: "contract"
|
|
10426
|
+
}
|
|
10427
|
+
});
|
|
10428
|
+
const compiler = this.#pending ?? new ContractCompiler(this.#shape);
|
|
10429
|
+
this.#pending = void 0;
|
|
10430
|
+
this.#bundle = compiler.contract;
|
|
10431
|
+
this.#settled = compiler;
|
|
10432
|
+
return compiler;
|
|
10433
|
+
}, door);
|
|
10434
|
+
}
|
|
10435
|
+
};
|
|
9073
10436
|
// src/core/factories.ts
|
|
9074
10437
|
function createContract(shape) {
|
|
9075
|
-
return contain(() =>
|
|
10438
|
+
return contain(() => {
|
|
10439
|
+
const compiler = new ContractCompiler(shape);
|
|
10440
|
+
const expansion = ShapePass.expansionOf(shape);
|
|
10441
|
+
if (expansion === void 0 || expansion === 1) return compiler.contract;
|
|
10442
|
+
return new LazyContract(shape, compiler).publish(compiler.schema);
|
|
10443
|
+
}, "createContract");
|
|
9076
10444
|
}
|
|
9077
10445
|
// src/core/ValueInferer.ts
|
|
9078
10446
|
/**
|
|
@@ -9153,14 +10521,14 @@ var ValueInferer = class ValueInferer {
|
|
|
9153
10521
|
admitVisited(this.#visited, value);
|
|
9154
10522
|
const outcome = attempt(() => {
|
|
9155
10523
|
const snapshot = readArrayEntries(value);
|
|
9156
|
-
if (
|
|
10524
|
+
if (isFailure(snapshot)) throw snapshot.error;
|
|
9157
10525
|
if (!snapshot.value.dense) return void 0;
|
|
9158
10526
|
const entries = snapshot.value.entries;
|
|
9159
10527
|
const length = INTRINSICS.ceil(INTRINSICS.min(entries.length, this.#breadth));
|
|
9160
10528
|
return INTRINSICS.from(iterateIndices(() => length), (index) => this.#infer(entries[index], depth - 1));
|
|
9161
10529
|
});
|
|
9162
10530
|
omitVisited(this.#visited, value);
|
|
9163
|
-
if (
|
|
10531
|
+
if (isFailure(outcome)) throw outcome.error;
|
|
9164
10532
|
const sampled = outcome.value;
|
|
9165
10533
|
const schema = sampled === void 0 ? {} : sampled.length > 0 ? {
|
|
9166
10534
|
type: "array",
|
|
@@ -9204,7 +10572,7 @@ var ValueInferer = class ValueInferer {
|
|
|
9204
10572
|
};
|
|
9205
10573
|
});
|
|
9206
10574
|
omitVisited(this.#visited, value);
|
|
9207
|
-
if (
|
|
10575
|
+
if (isFailure(outcome)) throw outcome.error;
|
|
9208
10576
|
const { properties, required, partial } = outcome.value;
|
|
9209
10577
|
const schema = {
|
|
9210
10578
|
type: "object",
|
|
@@ -9331,7 +10699,7 @@ var SampleInferer = class {
|
|
|
9331
10699
|
dropped: INTRINSICS.reflect.apply(INTRINSICS.some, observed, [(entry) => entry.dropped])
|
|
9332
10700
|
};
|
|
9333
10701
|
});
|
|
9334
|
-
if (
|
|
10702
|
+
if (isFailure(valuesOutcome)) throw valuesOutcome.error;
|
|
9335
10703
|
const { values, dropped } = valuesOutcome.value;
|
|
9336
10704
|
return {
|
|
9337
10705
|
key,
|
|
@@ -9394,7 +10762,7 @@ function unifySchemas(schemas) {
|
|
|
9394
10762
|
return contain(() => {
|
|
9395
10763
|
return readValue(() => {
|
|
9396
10764
|
const snapshot = readArrayEntries(schemas);
|
|
9397
|
-
if (
|
|
10765
|
+
if (isFailure(snapshot)) throw snapshot.error;
|
|
9398
10766
|
if (!snapshot.value.dense) throw new INTRINSICS.error("unifySchemas: schemas must be dense");
|
|
9399
10767
|
const owned = snapshot.value.entries;
|
|
9400
10768
|
if (owned.length === 0) return {};
|
|
@@ -9452,7 +10820,7 @@ function stringToFormat(value) {
|
|
|
9452
10820
|
if (!isString(value)) return void 0;
|
|
9453
10821
|
if (value.length > 128) return void 0;
|
|
9454
10822
|
const outcome = attempt(() => classifyFormat(value));
|
|
9455
|
-
return outcome
|
|
10823
|
+
return isSuccess(outcome) ? outcome.value : void 0;
|
|
9456
10824
|
}
|
|
9457
10825
|
/**
|
|
9458
10826
|
* Classifies a list of sample values against the {@link SchemaFormat}
|
|
@@ -9481,7 +10849,7 @@ function samplesToFormat(values) {
|
|
|
9481
10849
|
return contain(() => {
|
|
9482
10850
|
return readValue(() => {
|
|
9483
10851
|
const snapshot = readArrayEntries(values);
|
|
9484
|
-
if (
|
|
10852
|
+
if (isFailure(snapshot)) throw snapshot.error;
|
|
9485
10853
|
if (!snapshot.value.dense) throw new INTRINSICS.error("samplesToFormat: values must be dense");
|
|
9486
10854
|
const owned = snapshot.value.entries;
|
|
9487
10855
|
if (owned.length === 0) return void 0;
|
|
@@ -9532,7 +10900,7 @@ function inferPrimitiveEnum(values, limit) {
|
|
|
9532
10900
|
return contain(() => {
|
|
9533
10901
|
return readValue(() => {
|
|
9534
10902
|
const snapshot = readArrayEntries(values);
|
|
9535
|
-
if (
|
|
10903
|
+
if (isFailure(snapshot)) throw snapshot.error;
|
|
9536
10904
|
if (!snapshot.value.dense) throw new INTRINSICS.error("inferPrimitiveEnum: values must be dense");
|
|
9537
10905
|
const owned = snapshot.value.entries;
|
|
9538
10906
|
if (owned.length < 2) return void 0;
|
|
@@ -9662,7 +11030,7 @@ function samplesToSchema(samples, options) {
|
|
|
9662
11030
|
const enumOn = optionsSnapshot?.enum ?? false;
|
|
9663
11031
|
const read = readValue(() => {
|
|
9664
11032
|
const snapshot = readArrayEntries(samples);
|
|
9665
|
-
if (
|
|
11033
|
+
if (isFailure(snapshot)) throw snapshot.error;
|
|
9666
11034
|
return snapshot.value;
|
|
9667
11035
|
}, "samplesToSchema", { subject: "samples" });
|
|
9668
11036
|
if (!read.dense) throw new ContractError("samplesToSchema: samples must be a dense array", { code: "structure" });
|
|
@@ -9679,7 +11047,9 @@ function samplesToSchema(samples, options) {
|
|
|
9679
11047
|
* rather than parameters, so no caller can pre-populate either and change what
|
|
9680
11048
|
* the conversion widens. Construction observes nothing; `shape` runs the whole
|
|
9681
11049
|
* walk and every refusal it raises is published by that door under the door's
|
|
9682
|
-
* own name.
|
|
11050
|
+
* own name. A strict walk refuses where a lenient one widens, through the one
|
|
11051
|
+
* `#widen` both share, and its per-node `#inspect` refuses every keyword the
|
|
11052
|
+
* lenient precedence would read past.
|
|
9683
11053
|
*
|
|
9684
11054
|
* The recursion is `#` private for the same reason it is bounded: it is the
|
|
9685
11055
|
* class's defining spine, and its state has no meaning outside one call. The
|
|
@@ -9695,73 +11065,314 @@ var SchemaShaper = class SchemaShaper {
|
|
|
9695
11065
|
static #weakSet = WeakSet;
|
|
9696
11066
|
static #weakMap = WeakMap;
|
|
9697
11067
|
#source;
|
|
11068
|
+
#strict;
|
|
9698
11069
|
#visited;
|
|
9699
11070
|
#memo;
|
|
9700
|
-
|
|
11071
|
+
#enclosed;
|
|
11072
|
+
#exclusive;
|
|
11073
|
+
#path;
|
|
11074
|
+
#oneOfs;
|
|
11075
|
+
#built;
|
|
11076
|
+
#shared;
|
|
11077
|
+
constructor(schema, strict = false) {
|
|
9701
11078
|
this.#source = schema;
|
|
11079
|
+
this.#strict = strict;
|
|
9702
11080
|
this.#visited = new SchemaShaper.#weakSet();
|
|
9703
11081
|
this.#memo = new SchemaShaper.#weakMap();
|
|
11082
|
+
this.#enclosed = new SchemaShaper.#weakMap();
|
|
11083
|
+
this.#exclusive = new SchemaShaper.#weakMap();
|
|
11084
|
+
this.#path = [];
|
|
11085
|
+
this.#oneOfs = 0;
|
|
11086
|
+
this.#built = 0;
|
|
11087
|
+
this.#shared = false;
|
|
9704
11088
|
}
|
|
9705
11089
|
/**
|
|
9706
11090
|
* Converts the retained schema into a validating contract shape.
|
|
9707
11091
|
*
|
|
9708
|
-
* @returns The built shape, widened wherever
|
|
9709
|
-
* @throws {ContractError} When schema traversal fails
|
|
11092
|
+
* @returns The built shape, widened wherever a lenient schema is inexpressible
|
|
11093
|
+
* @throws {ContractError} When schema traversal fails, or when a strict conversion refuses
|
|
9710
11094
|
*/
|
|
9711
11095
|
shape() {
|
|
9712
|
-
|
|
11096
|
+
if (this.#strict && isRecord(this.#source) && INTRINSICS.own(this.#source, "default")) throw this.#refusal("a default needs an optional property to fill", "default");
|
|
11097
|
+
const shape = this.#convert(this.#source, 32, false);
|
|
11098
|
+
return !this.#strict && this.#exceeds(shape) ? this.#shallow(shape) : shape;
|
|
11099
|
+
}
|
|
11100
|
+
#exceeds(shape) {
|
|
11101
|
+
if (!this.#shared && this.#built <= 1024) return false;
|
|
11102
|
+
const validator = new ShapeValidator(shape);
|
|
11103
|
+
validator.validate();
|
|
11104
|
+
return (validator.expansion ?? 0) > COMPILE_NODE_LIMIT;
|
|
11105
|
+
}
|
|
11106
|
+
#shallow(shape) {
|
|
11107
|
+
const raw = rawShape({});
|
|
11108
|
+
switch (shape.category) {
|
|
11109
|
+
case "nullable": return nullableShape(this.#shallow(shape.inner));
|
|
11110
|
+
case "array": return arrayShape(raw, INTRINSICS.rebase({
|
|
11111
|
+
...shape.min === void 0 ? {} : { min: shape.min },
|
|
11112
|
+
...shape.max === void 0 ? {} : { max: shape.max },
|
|
11113
|
+
...shape.description === void 0 ? {} : { description: shape.description }
|
|
11114
|
+
}, null));
|
|
11115
|
+
case "object": {
|
|
11116
|
+
const pairs = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(INTRINSICS.keys(shape.properties)), [(key) => [key, shape.properties[key]?.category === "optional" ? optionalShape(raw) : raw]]);
|
|
11117
|
+
const properties = INTRINSICS.rebase(INTRINSICS.record({ [Symbol.iterator]: INTRINSICS.reflect.apply(INTRINSICS.bind, INTRINSICS.iterator, [pairs]) }), null);
|
|
11118
|
+
const extra = shape.additionalProperties;
|
|
11119
|
+
return objectShape(properties, INTRINSICS.rebase({
|
|
11120
|
+
additionalProperties: isObject(extra) ? raw : extra ?? false,
|
|
11121
|
+
...shape.description === void 0 ? {} : { description: shape.description }
|
|
11122
|
+
}, null));
|
|
11123
|
+
}
|
|
11124
|
+
default: return raw;
|
|
11125
|
+
}
|
|
9713
11126
|
}
|
|
9714
|
-
#convert(schema, depth) {
|
|
9715
|
-
if (!(depth > 0)) return
|
|
9716
|
-
if (!matchesRecordBrand(schema)) return
|
|
9717
|
-
if (matchesVisited(this.#visited, schema)) return
|
|
9718
|
-
const
|
|
11127
|
+
#convert(schema, depth, enclosed) {
|
|
11128
|
+
if (!(depth > 0)) return this.#widen("a schema may not nest past the depth limit", void 0);
|
|
11129
|
+
if (!matchesRecordBrand(schema)) return this.#widen("every schema node must be a plain record", void 0);
|
|
11130
|
+
if (matchesVisited(this.#visited, schema)) return this.#widen("a schema may not contain a cycle", void 0);
|
|
11131
|
+
const memo = this.#oneOfs > 0 ? this.#exclusive : enclosed ? this.#enclosed : this.#memo;
|
|
11132
|
+
const depthMemo = INTRINSICS.reflect.apply(INTRINSICS.recall, memo, [schema]);
|
|
9719
11133
|
const cached = depthMemo === void 0 ? void 0 : INTRINSICS.reflect.apply(INTRINSICS.fetch, depthMemo, [depth]);
|
|
9720
|
-
if (cached)
|
|
11134
|
+
if (cached) {
|
|
11135
|
+
this.#shared = true;
|
|
11136
|
+
return cached;
|
|
11137
|
+
}
|
|
9721
11138
|
admitVisited(this.#visited, schema);
|
|
9722
11139
|
try {
|
|
9723
|
-
|
|
9724
|
-
|
|
11140
|
+
if (this.#strict) this.#inspect(schema);
|
|
11141
|
+
const built = this.#build(schema, depth, enclosed);
|
|
11142
|
+
const shape = INTRINSICS.own(schema, "nullable") && INTRINSICS.reflect.read(schema, "nullable") === true ? nullableShape(built) : built;
|
|
11143
|
+
retainDepth(memo, schema, depth, shape);
|
|
9725
11144
|
return shape;
|
|
9726
11145
|
} finally {
|
|
9727
11146
|
omitVisited(this.#visited, schema);
|
|
9728
11147
|
}
|
|
9729
11148
|
}
|
|
9730
|
-
#
|
|
9731
|
-
|
|
9732
|
-
if (
|
|
11149
|
+
#descend(schema, depth, enclosed, first, second) {
|
|
11150
|
+
this.#path[this.#path.length] = first;
|
|
11151
|
+
if (second !== void 0) this.#path[this.#path.length] = second;
|
|
11152
|
+
if (this.#strict && first !== "properties" && isRecord(schema) && INTRINSICS.own(schema, "default")) throw this.#refusal("a default needs an optional property to fill", "default");
|
|
11153
|
+
const shape = this.#convert(schema, depth, enclosed);
|
|
11154
|
+
this.#path.length -= second === void 0 ? 1 : 2;
|
|
11155
|
+
return shape;
|
|
11156
|
+
}
|
|
11157
|
+
#widen(message, description, first, second) {
|
|
11158
|
+
if (this.#strict) throw this.#refusal(message, first, second);
|
|
11159
|
+
this.#built += 1;
|
|
11160
|
+
return rawShape(description === void 0 ? {} : { description });
|
|
11161
|
+
}
|
|
11162
|
+
#describe(shape, description) {
|
|
11163
|
+
if (description === void 0) return shape;
|
|
11164
|
+
switch (shape.category) {
|
|
11165
|
+
case "optional": return shape;
|
|
11166
|
+
case "nullable": return nullableShape(this.#describe(shape.inner, description));
|
|
11167
|
+
case "raw": return rawShape({
|
|
11168
|
+
...shape.schema,
|
|
11169
|
+
description
|
|
11170
|
+
});
|
|
11171
|
+
default: return INTRINSICS.freeze({
|
|
11172
|
+
...shape,
|
|
11173
|
+
description
|
|
11174
|
+
});
|
|
11175
|
+
}
|
|
11176
|
+
}
|
|
11177
|
+
#refusal(message, first, second, third) {
|
|
11178
|
+
const path = pathOf(this.#path, first, second, third);
|
|
11179
|
+
return new ContractError(`schemaToShape: ${message} at ${truncateFaultText(renderPath(path) || "value", 160)}`, {
|
|
11180
|
+
code: "structure",
|
|
11181
|
+
context: {
|
|
11182
|
+
path,
|
|
11183
|
+
shape: "schema"
|
|
11184
|
+
}
|
|
11185
|
+
});
|
|
11186
|
+
}
|
|
11187
|
+
#inspect(schema) {
|
|
11188
|
+
const keys = INTRINSICS.keys(schema);
|
|
11189
|
+
for (const index of iterateIndices(() => keys.length)) {
|
|
11190
|
+
const key = keys[index];
|
|
11191
|
+
if (key === void 0) continue;
|
|
11192
|
+
if (!INTRINSICS.reflect.apply(INTRINSICS.some, STRICT_SCHEMA_KEYWORDS, [(keyword) => keyword === key])) throw this.#refusal("a strict conversion refuses a keyword it cannot honour", key);
|
|
11193
|
+
}
|
|
11194
|
+
this.#inspectAnnotations(schema);
|
|
11195
|
+
if (INTRINSICS.own(schema, "default") && !isJSONValue(schema.default)) throw this.#refusal("a default must be a JSON value", "default");
|
|
11196
|
+
const types = this.#inspectType(schema);
|
|
11197
|
+
this.#inspectForm(schema, types);
|
|
11198
|
+
const hasForm = INTRINSICS.own(schema, "enum") || INTRINSICS.own(schema, "oneOf") || INTRINSICS.own(schema, "anyOf");
|
|
11199
|
+
const object = types.length === 0 && INTRINSICS.own(schema, "properties") ? ["object"] : types;
|
|
11200
|
+
const effective = hasForm ? [] : object;
|
|
11201
|
+
this.#confine(schema, ["minLength", "maxLength"], this.#names(effective, "string"));
|
|
11202
|
+
this.#confine(schema, ["minimum", "maximum"], this.#names(effective, "number", "integer"));
|
|
11203
|
+
this.#confine(schema, [
|
|
11204
|
+
"items",
|
|
11205
|
+
"minItems",
|
|
11206
|
+
"maxItems"
|
|
11207
|
+
], this.#names(effective, "array"));
|
|
11208
|
+
this.#confine(schema, [
|
|
11209
|
+
"properties",
|
|
11210
|
+
"required",
|
|
11211
|
+
"additionalProperties"
|
|
11212
|
+
], this.#names(effective, "object"));
|
|
11213
|
+
this.#inspectLength(INTRINSICS.own(schema, "minLength") ? schema.minLength : void 0, INTRINSICS.own(schema, "maxLength") ? schema.maxLength : void 0, "minLength", "maxLength");
|
|
11214
|
+
this.#inspectLength(INTRINSICS.own(schema, "minItems") ? schema.minItems : void 0, INTRINSICS.own(schema, "maxItems") ? schema.maxItems : void 0, "minItems", "maxItems");
|
|
11215
|
+
this.#inspectRange(schema, this.#names(effective, "integer"));
|
|
11216
|
+
this.#inspectObject(schema);
|
|
11217
|
+
}
|
|
11218
|
+
#inspectAnnotations(schema) {
|
|
11219
|
+
const texts = [
|
|
11220
|
+
"title",
|
|
11221
|
+
"description",
|
|
11222
|
+
"$schema",
|
|
11223
|
+
"$id",
|
|
11224
|
+
"$comment"
|
|
11225
|
+
];
|
|
11226
|
+
for (const index of iterateIndices(() => texts.length)) {
|
|
11227
|
+
const keyword = texts[index];
|
|
11228
|
+
if (keyword === void 0) continue;
|
|
11229
|
+
const value = INTRINSICS.own(schema, keyword) ? INTRINSICS.reflect.read(schema, keyword) : void 0;
|
|
11230
|
+
if (value !== void 0 && !isString(value)) throw this.#refusal(`${keyword} must be a string`, keyword);
|
|
11231
|
+
}
|
|
11232
|
+
const flags = [
|
|
11233
|
+
"deprecated",
|
|
11234
|
+
"readOnly",
|
|
11235
|
+
"writeOnly"
|
|
11236
|
+
];
|
|
11237
|
+
for (const index of iterateIndices(() => flags.length)) {
|
|
11238
|
+
const keyword = flags[index];
|
|
11239
|
+
if (keyword === void 0) continue;
|
|
11240
|
+
const value = INTRINSICS.own(schema, keyword) ? INTRINSICS.reflect.read(schema, keyword) : void 0;
|
|
11241
|
+
if (value !== void 0 && !isBoolean(value)) throw this.#refusal(`${keyword} must be a boolean`, keyword);
|
|
11242
|
+
}
|
|
11243
|
+
const examples = INTRINSICS.own(schema, "examples") ? INTRINSICS.reflect.read(schema, "examples") : void 0;
|
|
11244
|
+
if (examples !== void 0 && !isArray(examples)) throw this.#refusal("examples must be an array", "examples");
|
|
11245
|
+
}
|
|
11246
|
+
#inspectType(schema) {
|
|
11247
|
+
const declared = INTRINSICS.own(schema, "type") ? schema.type : void 0;
|
|
11248
|
+
if (declared === void 0) return [];
|
|
11249
|
+
const names = isArray(declared) ? this.#entries(declared, "type") : [declared];
|
|
11250
|
+
const known = names.length > 0 && this.#unique(names);
|
|
11251
|
+
const types = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(JSON_SCHEMA_TYPES), [(type) => INTRINSICS.reflect.apply(INTRINSICS.some, names, [(name) => name === type])]);
|
|
11252
|
+
if (!known || types.length !== names.length) throw this.#refusal("type must name the supported types", "type");
|
|
11253
|
+
return types;
|
|
11254
|
+
}
|
|
11255
|
+
#inspectForm(schema, types) {
|
|
11256
|
+
if (INTRINSICS.own(schema, "enum")) {
|
|
11257
|
+
const members = this.#entries(schema.enum, "enum");
|
|
11258
|
+
const malformed = INTRINSICS.reflect.apply(INTRINSICS.some, members, [(entry) => entry !== null && (!isLiteralValue(entry) || typeof entry === "number" && !isFiniteNumber(entry))]);
|
|
11259
|
+
if (members.length === 0 || malformed || !this.#unique(members)) throw this.#refusal("enum must list unique finite primitives", "enum");
|
|
11260
|
+
if (INTRINSICS.reflect.apply(INTRINSICS.some, members, [(entry) => types.length > 0 && !this.#fits(entry, types)])) throw this.#refusal("an enum member contradicts the type", "enum");
|
|
11261
|
+
if (INTRINSICS.own(schema, "oneOf")) throw this.#refusal("a strict conversion refuses oneOf beside enum", "oneOf");
|
|
11262
|
+
if (INTRINSICS.own(schema, "anyOf")) throw this.#refusal("a strict conversion refuses anyOf beside enum", "anyOf");
|
|
11263
|
+
return;
|
|
11264
|
+
}
|
|
11265
|
+
const keywords = ["oneOf", "anyOf"];
|
|
11266
|
+
for (const index of iterateIndices(() => keywords.length)) {
|
|
11267
|
+
const keyword = keywords[index];
|
|
11268
|
+
if (keyword === void 0 || !INTRINSICS.own(schema, keyword)) continue;
|
|
11269
|
+
const variants = this.#entries(INTRINSICS.reflect.read(schema, keyword), keyword);
|
|
11270
|
+
if (variants.length === 0) throw this.#refusal(`${keyword} needs a variant`, keyword);
|
|
11271
|
+
for (const variantIndex of iterateIndices(() => variants.length)) if (!isRecord(variants[variantIndex])) throw this.#refusal("every variant must be a schema", keyword, INTRINSICS.text(variantIndex));
|
|
11272
|
+
}
|
|
11273
|
+
const union = INTRINSICS.own(schema, "oneOf") || INTRINSICS.own(schema, "anyOf");
|
|
11274
|
+
if (INTRINSICS.own(schema, "oneOf") && INTRINSICS.own(schema, "anyOf")) throw this.#refusal("a strict conversion refuses anyOf beside oneOf", "anyOf");
|
|
11275
|
+
if (union && types.length > 0) throw this.#refusal("a strict conversion refuses type beside a union", "type");
|
|
11276
|
+
}
|
|
11277
|
+
#inspectLength(min, max, minKey, maxKey) {
|
|
11278
|
+
if (min !== void 0 && !(INTRINSICS.safe(min) && INTRINSICS.numeric(min) >= 0)) throw this.#refusal("a length bound must be a non-negative safe integer", minKey);
|
|
11279
|
+
if (max !== void 0 && !(INTRINSICS.safe(max) && INTRINSICS.numeric(max) >= 0)) throw this.#refusal("a length bound must be a non-negative safe integer", maxKey);
|
|
11280
|
+
if (isFiniteNumber(min) && isFiniteNumber(max) && min > max) throw this.#refusal("a length range may not be contradictory", minKey);
|
|
11281
|
+
}
|
|
11282
|
+
#inspectRange(schema, integer) {
|
|
11283
|
+
const min = INTRINSICS.own(schema, "minimum") ? schema.minimum : void 0;
|
|
11284
|
+
const max = INTRINSICS.own(schema, "maximum") ? schema.maximum : void 0;
|
|
11285
|
+
if (min !== void 0 && !isFiniteNumber(min)) throw this.#refusal("a range bound must be a finite number", "minimum");
|
|
11286
|
+
if (max !== void 0 && !isFiniteNumber(max)) throw this.#refusal("a range bound must be a finite number", "maximum");
|
|
11287
|
+
if (isFiniteNumber(min) && isFiniteNumber(max)) {
|
|
11288
|
+
if (min > max) throw this.#refusal("a range may not be contradictory", "minimum");
|
|
11289
|
+
if (integer && INTRINSICS.ceil(min) > INTRINSICS.floor(max)) throw this.#refusal("an integer range may not be empty", "minimum");
|
|
11290
|
+
}
|
|
11291
|
+
}
|
|
11292
|
+
#inspectObject(schema) {
|
|
11293
|
+
const properties = INTRINSICS.own(schema, "properties") ? schema.properties : void 0;
|
|
11294
|
+
if (properties !== void 0 && !isRecord(properties)) throw this.#refusal("properties must be a plain record", "properties");
|
|
11295
|
+
const extra = INTRINSICS.own(schema, "additionalProperties") ? schema.additionalProperties : void 0;
|
|
11296
|
+
if (extra !== void 0 && !isBoolean(extra) && !isRecord(extra)) throw this.#refusal("additionalProperties must be a boolean or a schema", "additionalProperties");
|
|
11297
|
+
if (!INTRINSICS.own(schema, "required")) return;
|
|
11298
|
+
const names = this.#entries(schema.required, "required");
|
|
11299
|
+
if (INTRINSICS.reflect.apply(INTRINSICS.some, names, [(name) => !isString(name) || !isRecord(properties) || !INTRINSICS.own(properties, name)]) || !this.#unique(names)) throw this.#refusal("required must list unique declared properties", "required");
|
|
11300
|
+
}
|
|
11301
|
+
#confine(schema, keywords, applies) {
|
|
11302
|
+
if (applies) return;
|
|
11303
|
+
for (const index of iterateIndices(() => keywords.length)) {
|
|
11304
|
+
const keyword = keywords[index];
|
|
11305
|
+
if (keyword !== void 0 && INTRINSICS.own(schema, keyword)) throw this.#refusal("a strict conversion refuses a keyword this node never reads", keyword);
|
|
11306
|
+
}
|
|
11307
|
+
}
|
|
11308
|
+
#names(types, ...names) {
|
|
11309
|
+
return INTRINSICS.reflect.apply(INTRINSICS.some, types, [(type) => INTRINSICS.reflect.apply(INTRINSICS.some, names, [(name) => name === type])]);
|
|
11310
|
+
}
|
|
11311
|
+
#fits(entry, types) {
|
|
11312
|
+
if (entry === null) return this.#names(types, "null");
|
|
11313
|
+
if (isString(entry)) return this.#names(types, "string");
|
|
11314
|
+
if (isBoolean(entry)) return this.#names(types, "boolean");
|
|
11315
|
+
return isInteger(entry) ? this.#names(types, "number", "integer") : this.#names(types, "number");
|
|
11316
|
+
}
|
|
11317
|
+
#entries(value, keyword) {
|
|
11318
|
+
if (!isArray(value)) throw this.#refusal(`${keyword} must be an array`, keyword);
|
|
11319
|
+
const snapshot = readArrayEntries(value);
|
|
11320
|
+
if (isFailure(snapshot)) throw snapshot.error;
|
|
11321
|
+
if (!snapshot.value.dense) throw this.#refusal(`${keyword} must be a dense array`, keyword);
|
|
11322
|
+
return snapshot.value.entries;
|
|
11323
|
+
}
|
|
11324
|
+
#unique(entries) {
|
|
11325
|
+
const seen = collectMembers([]);
|
|
11326
|
+
for (const index of iterateIndices(() => entries.length)) {
|
|
11327
|
+
const entry = entries[index];
|
|
11328
|
+
if (matchesMember(seen, entry)) return false;
|
|
11329
|
+
admitMember(seen, entry);
|
|
11330
|
+
}
|
|
11331
|
+
return true;
|
|
11332
|
+
}
|
|
11333
|
+
#build(schema, depth, enclosed) {
|
|
11334
|
+
this.#built += 1;
|
|
11335
|
+
const description = INTRINSICS.own(schema, "description") && isString(schema.description) ? schema.description : void 0;
|
|
11336
|
+
if (INTRINSICS.own(schema, "$ref")) return this.#widen("a reference is not resolved", description, "$ref");
|
|
11337
|
+
if (INTRINSICS.own(schema, "enum") && isArray(schema.enum)) {
|
|
9733
11338
|
const observed = INTRINSICS.from(iterateIndices(() => INTRINSICS.reflect.apply(INTRINSICS.reflect.read, void 0, [schema, "enum"]).length), (index) => INTRINSICS.reflect.apply(INTRINSICS.reflect.read, void 0, [schema, "enum"])[index]);
|
|
9734
|
-
|
|
11339
|
+
if (INTRINSICS.reflect.apply(INTRINSICS.some, observed, [(entry) => entry !== null && (!isLiteralValue(entry) || typeof entry === "number" && !isFiniteNumber(entry))])) return this.#widen("an enum entry has no literal form", description, "enum");
|
|
11340
|
+
const valid = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(observed), [(entry) => entry !== null]);
|
|
11341
|
+
const nullable = valid.length < observed.length;
|
|
9735
11342
|
const pairs = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(valid), [(entry, index) => [`${typeof entry}:${INTRINSICS.text(entry)}`, index]]);
|
|
9736
11343
|
const reversed = INTRINSICS.reflect.apply(INTRINSICS.flip, pairs, []);
|
|
9737
11344
|
const first = INTRINSICS.record({ [Symbol.iterator]: INTRINSICS.reflect.apply(INTRINSICS.bind, INTRINSICS.iterator, [reversed]) });
|
|
9738
11345
|
const literals = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(valid), [(entry, index) => first[`${typeof entry}:${INTRINSICS.text(entry)}`] === index]);
|
|
9739
|
-
if (literals.length > 0)
|
|
11346
|
+
if (literals.length > 0) {
|
|
11347
|
+
const literal = literalShape(literals, description === void 0 ? void 0 : { description });
|
|
11348
|
+
return nullable ? nullableShape(literal) : literal;
|
|
11349
|
+
}
|
|
11350
|
+
if (nullable) return nullShape(description === void 0 ? void 0 : { description });
|
|
9740
11351
|
}
|
|
9741
|
-
if (isArray(schema.oneOf)) {
|
|
9742
|
-
const
|
|
9743
|
-
|
|
9744
|
-
if (records.length > 256) return rawShape(description === void 0 ? {} : { description });
|
|
9745
|
-
const variants = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(records), [(entry) => this.#convert(entry, depth - 1)]);
|
|
9746
|
-
if (variants.length > 0) return INTRINSICS.reflect.apply(oneOfShape, void 0, variants);
|
|
11352
|
+
if (INTRINSICS.own(schema, "oneOf") && isArray(schema.oneOf)) {
|
|
11353
|
+
const union = this.#buildUnion(schema.oneOf, "oneOf", depth, description, enclosed);
|
|
11354
|
+
if (union !== void 0) return union;
|
|
9747
11355
|
}
|
|
9748
|
-
if (isArray(schema.anyOf)) {
|
|
9749
|
-
const
|
|
9750
|
-
|
|
9751
|
-
if (records.length > 256) return rawShape(description === void 0 ? {} : { description });
|
|
9752
|
-
const variants = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(records), [(entry) => this.#convert(entry, depth - 1)]);
|
|
9753
|
-
if (variants.length > 0) return INTRINSICS.reflect.apply(unionShape, void 0, variants);
|
|
11356
|
+
if (INTRINSICS.own(schema, "anyOf") && isArray(schema.anyOf)) {
|
|
11357
|
+
const union = this.#buildUnion(schema.anyOf, "anyOf", depth, description, enclosed);
|
|
11358
|
+
if (union !== void 0) return union;
|
|
9754
11359
|
}
|
|
9755
|
-
const category = schema.type;
|
|
11360
|
+
const category = INTRINSICS.own(schema, "type") ? schema.type : void 0;
|
|
9756
11361
|
if (isArray(category)) {
|
|
9757
11362
|
const observed = INTRINSICS.from(parseEnumValues(category, JSON_SCHEMA_TYPES));
|
|
9758
|
-
if (INTRINSICS.reflect.apply(INTRINSICS.some, observed, [(entry) => entry === void 0])) return
|
|
11363
|
+
if (INTRINSICS.reflect.apply(INTRINSICS.some, observed, [(entry) => entry === void 0])) return this.#widen("type must name the supported types", description, "type");
|
|
9759
11364
|
const members = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(observed), [(member, index) => INTRINSICS.reflect.apply(INTRINSICS.find, observed, [(entry) => entry === member]) === index]);
|
|
9760
|
-
if (members.length === 0 || members.length > 256) return
|
|
11365
|
+
if (members.length === 0 || members.length > 256) return this.#widen("type must name the supported types", description, "type");
|
|
11366
|
+
const others = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(members), [(member) => member !== "null"]);
|
|
11367
|
+
const other = others[0];
|
|
11368
|
+
if (members.length === 2 && others.length === 1 && other !== void 0) return nullableShape(this.#build({
|
|
11369
|
+
...schema,
|
|
11370
|
+
type: other
|
|
11371
|
+
}, depth, enclosed));
|
|
9761
11372
|
const variants = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(members), [(member) => this.#build({
|
|
9762
11373
|
...schema,
|
|
9763
11374
|
type: member
|
|
9764
|
-
}, depth)]);
|
|
11375
|
+
}, depth, enclosed || members.length > 1)]);
|
|
9765
11376
|
const first = variants[0];
|
|
9766
11377
|
if (variants.length === 1 && first !== void 0) return first;
|
|
9767
11378
|
const union = INTRINSICS.reflect.apply(unionShape, void 0, variants);
|
|
@@ -9770,45 +11381,88 @@ var SchemaShaper = class SchemaShaper {
|
|
|
9770
11381
|
description
|
|
9771
11382
|
});
|
|
9772
11383
|
}
|
|
9773
|
-
const type = isString(category) ? schema.type : void 0;
|
|
9774
|
-
if (type === "string")
|
|
9775
|
-
|
|
9776
|
-
|
|
9777
|
-
|
|
9778
|
-
|
|
9779
|
-
|
|
9780
|
-
|
|
9781
|
-
|
|
11384
|
+
const type = isString(category) && INTRINSICS.own(schema, "type") ? schema.type : void 0;
|
|
11385
|
+
if (type === "string") {
|
|
11386
|
+
const bounds = deriveLengthBounds(INTRINSICS.own(schema, "minLength") ? schema.minLength : void 0, INTRINSICS.own(schema, "maxLength") ? schema.maxLength : void 0);
|
|
11387
|
+
return stringShape(INTRINSICS.rebase({
|
|
11388
|
+
...bounds,
|
|
11389
|
+
...description === void 0 ? {} : { description }
|
|
11390
|
+
}, null));
|
|
11391
|
+
}
|
|
11392
|
+
if (type === "number") {
|
|
11393
|
+
const bounds = deriveRangeBounds(INTRINSICS.own(schema, "minimum") ? schema.minimum : void 0, INTRINSICS.own(schema, "maximum") ? schema.maximum : void 0);
|
|
11394
|
+
return numberShape(INTRINSICS.rebase({
|
|
11395
|
+
...bounds,
|
|
11396
|
+
...description === void 0 ? {} : { description }
|
|
11397
|
+
}, null));
|
|
11398
|
+
}
|
|
9782
11399
|
if (type === "integer") {
|
|
9783
|
-
const bounds = deriveRangeBounds(schema.minimum, schema.maximum);
|
|
9784
|
-
return integerShape(INTRINSICS.ceil(bounds.min ?? Number.NEGATIVE_INFINITY) > INTRINSICS.floor(bounds.max ?? Number.POSITIVE_INFINITY) ? description === void 0 ? void 0 : { description } : {
|
|
11400
|
+
const bounds = deriveRangeBounds(INTRINSICS.own(schema, "minimum") ? schema.minimum : void 0, INTRINSICS.own(schema, "maximum") ? schema.maximum : void 0);
|
|
11401
|
+
return integerShape(INTRINSICS.ceil(bounds.min ?? Number.NEGATIVE_INFINITY) > INTRINSICS.floor(bounds.max ?? Number.POSITIVE_INFINITY) ? description === void 0 ? void 0 : INTRINSICS.rebase({ description }, null) : INTRINSICS.rebase({
|
|
9785
11402
|
...bounds,
|
|
9786
11403
|
...description === void 0 ? {} : { description }
|
|
9787
|
-
});
|
|
11404
|
+
}, null));
|
|
9788
11405
|
}
|
|
9789
11406
|
if (type === "boolean") return booleanShape(description === void 0 ? void 0 : { description });
|
|
9790
11407
|
if (type === "null") return nullShape(description === void 0 ? void 0 : { description });
|
|
9791
|
-
if (type === "array")
|
|
9792
|
-
|
|
9793
|
-
|
|
9794
|
-
|
|
9795
|
-
|
|
9796
|
-
|
|
11408
|
+
if (type === "array") {
|
|
11409
|
+
const items = INTRINSICS.own(schema, "prefixItems") ? rawShape({}) : INTRINSICS.own(schema, "items") && isRecord(schema.items) ? this.#descend(schema.items, depth - 1, enclosed, "items") : this.#widen("an array needs an items schema", void 0, "items");
|
|
11410
|
+
const bounds = deriveLengthBounds(INTRINSICS.own(schema, "minItems") ? schema.minItems : void 0, INTRINSICS.own(schema, "maxItems") ? schema.maxItems : void 0);
|
|
11411
|
+
return arrayShape(items, INTRINSICS.rebase({
|
|
11412
|
+
...bounds,
|
|
11413
|
+
...description === void 0 ? {} : { description }
|
|
11414
|
+
}, null));
|
|
11415
|
+
}
|
|
11416
|
+
if (type === "object" || this.#strict && type === void 0 && INTRINSICS.own(schema, "properties") && isRecord(schema.properties)) return this.#buildObject(schema, depth, description, enclosed);
|
|
11417
|
+
if (type === void 0 && INTRINSICS.own(schema, "properties") && isRecord(schema.properties)) return this.#describe(unionShape(this.#buildObject(schema, depth, void 0, true), stringShape(), numberShape(), booleanShape(), nullShape(), arrayShape(rawShape({}))), description);
|
|
11418
|
+
return this.#widen("a schema must name a type, an enum, or a union", description);
|
|
11419
|
+
}
|
|
11420
|
+
#buildUnion(list, keyword, depth, description, enclosed) {
|
|
11421
|
+
const observed = INTRINSICS.from(iterateIndices(() => list.length), (index) => list[index]);
|
|
11422
|
+
const arms = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(observed), [(entry) => entry === true || isRecord(entry)]);
|
|
11423
|
+
if (arms.length > 256) return this.#widen(`${keyword} exceeds the breadth limit`, description, keyword);
|
|
11424
|
+
const nulls = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(arms), [(entry) => entry !== true && INTRINSICS.own(entry, "type") && entry.type === "null" && !INTRINSICS.own(entry, "$ref") && !INTRINSICS.own(entry, "enum") && !INTRINSICS.own(entry, "oneOf") && !INTRINSICS.own(entry, "anyOf")]);
|
|
11425
|
+
const hasTrue = INTRINSICS.reflect.apply(INTRINSICS.some, arms, [(entry) => entry === true]);
|
|
11426
|
+
const paired = arms.length === 2 && nulls.length === 1 && !hasTrue && !(this.#strict && keyword === "oneOf");
|
|
11427
|
+
const exclusive = this.#strict && keyword === "oneOf" ? 1 : 0;
|
|
11428
|
+
this.#oneOfs += exclusive;
|
|
11429
|
+
const variants = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(arms), [(entry, index) => {
|
|
11430
|
+
if (entry !== true) {
|
|
11431
|
+
const position = paired ? enclosed : true;
|
|
11432
|
+
return this.#descend(entry, depth - 1, position, keyword, INTRINSICS.text(index));
|
|
11433
|
+
}
|
|
11434
|
+
this.#built += 1;
|
|
11435
|
+
return rawShape({});
|
|
11436
|
+
}]);
|
|
11437
|
+
this.#oneOfs -= exclusive;
|
|
11438
|
+
const other = variants[arms[0] === nulls[0] ? 1 : 0];
|
|
11439
|
+
if (paired && other !== void 0) return nullableShape(this.#describe(other, description));
|
|
11440
|
+
if (variants.length > 0) {
|
|
11441
|
+
const build = this.#strict && keyword === "oneOf" ? oneOfShape : unionShape;
|
|
11442
|
+
return this.#describe(INTRINSICS.reflect.apply(build, void 0, variants), description);
|
|
11443
|
+
}
|
|
11444
|
+
return INTRINSICS.reflect.apply(INTRINSICS.some, observed, [(entry) => entry === false]) ? this.#widen(`${keyword} admits nothing`, description, keyword) : void 0;
|
|
9797
11445
|
}
|
|
9798
|
-
#buildObject(schema, depth, description) {
|
|
9799
|
-
|
|
9800
|
-
const
|
|
11446
|
+
#buildObject(schema, depth, description, enclosed) {
|
|
11447
|
+
if (this.#oneOfs > 0 && !INTRINSICS.own(schema, "additionalProperties")) throw this.#refusal("an object below a oneOf must declare additionalProperties, because closing it could change which arms match", "additionalProperties");
|
|
11448
|
+
const propertiesSource = INTRINSICS.own(schema, "properties") && isRecord(schema.properties) ? schema.properties : void 0;
|
|
11449
|
+
const requiredSource = INTRINSICS.own(schema, "required") && isArray(schema.required) ? INTRINSICS.from(iterateIndices(() => INTRINSICS.reflect.apply(INTRINSICS.reflect.read, void 0, [schema, "required"]).length), (index) => INTRINSICS.reflect.apply(INTRINSICS.reflect.read, void 0, [schema, "required"])[index]) : [];
|
|
9801
11450
|
const allKeys = propertiesSource === void 0 ? [] : INTRINSICS.keys(propertiesSource);
|
|
9802
11451
|
const truncated = allKeys.length > 256;
|
|
11452
|
+
if (this.#strict && truncated) throw this.#refusal("properties exceeds the breadth limit", "properties");
|
|
9803
11453
|
const keys = limitEntries(allKeys, 256);
|
|
11454
|
+
const extra = INTRINSICS.own(schema, "additionalProperties") ? schema.additionalProperties : void 0;
|
|
11455
|
+
const unread = truncated || INTRINSICS.own(schema, "patternProperties");
|
|
11456
|
+
const open = unread || extra !== false && (!this.#strict || extra !== void 0);
|
|
9804
11457
|
const pairs = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(keys), [(key) => {
|
|
9805
|
-
const child = propertiesSource
|
|
9806
|
-
const childShape = isRecord(child) ? this.#
|
|
9807
|
-
|
|
11458
|
+
const child = propertiesSource !== void 0 && INTRINSICS.own(propertiesSource, key) ? propertiesSource[key] : void 0;
|
|
11459
|
+
const childShape = isRecord(child) ? this.#descend(child, depth - 1, enclosed, "properties", key) : this.#widen("every property schema must be a plain record", void 0, "properties", key);
|
|
11460
|
+
if (!INTRINSICS.reflect.apply(INTRINSICS.some, requiredSource, [(entry) => isString(entry) && entry === key])) return [key, this.#wrap(key, child, childShape, open || enclosed)];
|
|
11461
|
+
if (this.#strict && isRecord(child) && INTRINSICS.own(child, "default")) throw this.#refusal("a default needs an optional property to fill", "properties", key, "default");
|
|
11462
|
+
return [key, childShape];
|
|
9808
11463
|
}]);
|
|
9809
11464
|
const properties = INTRINSICS.rebase(INTRINSICS.record({ [Symbol.iterator]: INTRINSICS.reflect.apply(INTRINSICS.bind, INTRINSICS.iterator, [pairs]) }), null);
|
|
9810
|
-
const
|
|
9811
|
-
const additionalProperties = truncated ? true : extra === false ? false : isRecord(extra) ? this.#convert(extra, depth - 1) : true;
|
|
11465
|
+
const additionalProperties = unread ? true : isRecord(extra) ? this.#descend(extra, depth - 1, enclosed, "additionalProperties") : open;
|
|
9812
11466
|
return INTRINSICS.freeze({
|
|
9813
11467
|
category: "object",
|
|
9814
11468
|
properties: INTRINSICS.freeze(properties),
|
|
@@ -9816,9 +11470,115 @@ var SchemaShaper = class SchemaShaper {
|
|
|
9816
11470
|
...description === void 0 ? {} : { description }
|
|
9817
11471
|
});
|
|
9818
11472
|
}
|
|
11473
|
+
#wrap(key, child, shape, sealed) {
|
|
11474
|
+
this.#built += 1;
|
|
11475
|
+
if (!isRecord(child) || !INTRINSICS.own(child, "default")) return optionalShape(shape);
|
|
11476
|
+
const value = child.default;
|
|
11477
|
+
if (!isJSONValue(value)) return optionalShape(shape);
|
|
11478
|
+
if (this.#strict) {
|
|
11479
|
+
if (sealed) throw new ContractError("schemaToShape: a default cannot fill under a union or on a property of an open object; close the object or remove the default", {
|
|
11480
|
+
code: "placement",
|
|
11481
|
+
context: {
|
|
11482
|
+
path: pathOf(this.#path, "properties", key, "default"),
|
|
11483
|
+
shape: "schema"
|
|
11484
|
+
}
|
|
11485
|
+
});
|
|
11486
|
+
if (new ContractCompiler(shape).guard(value)) return optionalShape(shape, { default: value });
|
|
11487
|
+
throw this.#refusal("a default must satisfy its schema", "properties", key, "default");
|
|
11488
|
+
}
|
|
11489
|
+
const leaf = shape.category === "string" || shape.category === "number" || shape.category === "boolean" || shape.category === "null" || shape.category === "literal";
|
|
11490
|
+
return !sealed && leaf && new ContractCompiler(shape).guard(value) ? optionalShape(shape, { default: value }) : optionalShape(shape);
|
|
11491
|
+
}
|
|
11492
|
+
};
|
|
11493
|
+
// src/core/FaultShaper.ts
|
|
11494
|
+
/**
|
|
11495
|
+
* Retains a lazily built fault declaration without building shapes during module evaluation.
|
|
11496
|
+
*
|
|
11497
|
+
* @example
|
|
11498
|
+
* ```ts
|
|
11499
|
+
* FaultShaper.shape === FaultShaper.shape // true
|
|
11500
|
+
* ```
|
|
11501
|
+
*/
|
|
11502
|
+
var FaultShaper = class FaultShaper {
|
|
11503
|
+
static #shape;
|
|
11504
|
+
/** Returns the shared fault declaration, building it on the first read. */
|
|
11505
|
+
static get shape() {
|
|
11506
|
+
return FaultShaper.#shape ??= new FaultShaper().build();
|
|
11507
|
+
}
|
|
11508
|
+
/**
|
|
11509
|
+
* Builds a closed fault union whose inferred values satisfy the audit fault contract.
|
|
11510
|
+
*
|
|
11511
|
+
* @example
|
|
11512
|
+
* ```ts
|
|
11513
|
+
* createContract(new FaultShaper().build()).is({ reason: 'extra', path: [] }) // true
|
|
11514
|
+
* ```
|
|
11515
|
+
*/
|
|
11516
|
+
build() {
|
|
11517
|
+
const path = unionShape(stringShape(), arrayShape(stringShape()));
|
|
11518
|
+
const expected = literalShape(FAULT_KINDS);
|
|
11519
|
+
const hints = arrayShape(unionShape(stringShape(), numberShape(), booleanShape()));
|
|
11520
|
+
return unionShape(objectShape({
|
|
11521
|
+
reason: literalShape(["type"]),
|
|
11522
|
+
path,
|
|
11523
|
+
expected,
|
|
11524
|
+
received: stringShape()
|
|
11525
|
+
}), objectShape({
|
|
11526
|
+
reason: literalShape(["missing"]),
|
|
11527
|
+
path,
|
|
11528
|
+
expected
|
|
11529
|
+
}), objectShape({
|
|
11530
|
+
reason: literalShape(["constraint"]),
|
|
11531
|
+
path,
|
|
11532
|
+
expected,
|
|
11533
|
+
constraint: literalShape(FAULT_CONSTRAINTS),
|
|
11534
|
+
limit: optionalShape(unionShape(numberShape(), stringShape())),
|
|
11535
|
+
received: stringShape()
|
|
11536
|
+
}), objectShape({
|
|
11537
|
+
reason: literalShape(["variant"]),
|
|
11538
|
+
path,
|
|
11539
|
+
variants: integerShape({ min: 0 })
|
|
11540
|
+
}), objectShape({
|
|
11541
|
+
reason: literalShape(["variant"]),
|
|
11542
|
+
path,
|
|
11543
|
+
variants: integerShape({ min: 0 }),
|
|
11544
|
+
key: stringShape(),
|
|
11545
|
+
expected: hints
|
|
11546
|
+
}), objectShape({
|
|
11547
|
+
reason: literalShape(["oneOf"]),
|
|
11548
|
+
path,
|
|
11549
|
+
matched: integerShape({ min: 0 })
|
|
11550
|
+
}), objectShape({
|
|
11551
|
+
reason: literalShape(["oneOf"]),
|
|
11552
|
+
path,
|
|
11553
|
+
matched: integerShape({ min: 0 }),
|
|
11554
|
+
key: stringShape(),
|
|
11555
|
+
expected: hints
|
|
11556
|
+
}), objectShape({
|
|
11557
|
+
reason: literalShape(["extra"]),
|
|
11558
|
+
path
|
|
11559
|
+
}));
|
|
11560
|
+
}
|
|
11561
|
+
static {
|
|
11562
|
+
pinMembers(FaultShaper.prototype, "FaultShaper");
|
|
11563
|
+
}
|
|
9819
11564
|
};
|
|
9820
11565
|
// src/core/shapers.ts
|
|
9821
11566
|
/**
|
|
11567
|
+
* Returns the shared closed audit-fault shape, building it on the first call.
|
|
11568
|
+
*
|
|
11569
|
+
* @remarks
|
|
11570
|
+
* Numeric bounds, counts, and discriminator literals must be finite. Discriminator hints
|
|
11571
|
+
* carry both key and expected together. The inferred union is assignable to AuditFault.
|
|
11572
|
+
*
|
|
11573
|
+
* @example
|
|
11574
|
+
* ```ts
|
|
11575
|
+
* createContract(auditFaultShape()).is({ reason: 'extra', path: ['typo'] }) // true
|
|
11576
|
+
* ```
|
|
11577
|
+
*/
|
|
11578
|
+
function auditFaultShape() {
|
|
11579
|
+
return contain(() => FaultShaper.shape, "auditFaultShape");
|
|
11580
|
+
}
|
|
11581
|
+
/**
|
|
9822
11582
|
* Builds a string {@link StringShape}.
|
|
9823
11583
|
*
|
|
9824
11584
|
* @remarks
|
|
@@ -9895,8 +11655,9 @@ function stringShape(options) {
|
|
|
9895
11655
|
...patternSnapshot === void 0 ? {} : { pattern: new INTRINSICS.pattern(patternSnapshot.source) },
|
|
9896
11656
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
9897
11657
|
};
|
|
11658
|
+
if (patternSnapshot === void 0 && !ShapePass.polluted()) return ShapePass.seal(shape);
|
|
9898
11659
|
new ShapeValidator(shape).validate();
|
|
9899
|
-
return cloneShape(shape);
|
|
11660
|
+
return ShapePass.seal(cloneShape(shape));
|
|
9900
11661
|
}, "stringShape");
|
|
9901
11662
|
}
|
|
9902
11663
|
/**
|
|
@@ -9948,8 +11709,7 @@ function numberShape(options) {
|
|
|
9948
11709
|
...safe?.integer === void 0 ? {} : { integer: safe.integer },
|
|
9949
11710
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
9950
11711
|
};
|
|
9951
|
-
|
|
9952
|
-
return INTRINSICS.freeze(result);
|
|
11712
|
+
return ShapePass.seal(result);
|
|
9953
11713
|
}, "numberShape");
|
|
9954
11714
|
}
|
|
9955
11715
|
/**
|
|
@@ -9993,8 +11753,7 @@ function booleanShape(options) {
|
|
|
9993
11753
|
category: "boolean",
|
|
9994
11754
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
9995
11755
|
};
|
|
9996
|
-
|
|
9997
|
-
return INTRINSICS.freeze(shape);
|
|
11756
|
+
return ShapePass.seal(shape);
|
|
9998
11757
|
}, "booleanShape");
|
|
9999
11758
|
}
|
|
10000
11759
|
/**
|
|
@@ -10015,25 +11774,24 @@ function nullShape(options) {
|
|
|
10015
11774
|
category: "null",
|
|
10016
11775
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10017
11776
|
};
|
|
10018
|
-
|
|
10019
|
-
return INTRINSICS.freeze(shape);
|
|
11777
|
+
return ShapePass.seal(shape);
|
|
10020
11778
|
}, "nullShape");
|
|
10021
11779
|
}
|
|
10022
11780
|
function literalShape(values, options) {
|
|
10023
11781
|
return contain(() => {
|
|
10024
11782
|
const input = values;
|
|
10025
11783
|
const array = attempt(() => INTRINSICS.array(input));
|
|
10026
|
-
if (
|
|
11784
|
+
if (isFailure(array) || !array.value) throw new ContractError("literalShape: values must be an array", {
|
|
10027
11785
|
code: "structure",
|
|
10028
11786
|
context: {
|
|
10029
11787
|
path: ["values"],
|
|
10030
11788
|
shape: "literal"
|
|
10031
11789
|
},
|
|
10032
|
-
|
|
11790
|
+
...isFailure(array) ? { cause: array.error } : {}
|
|
10033
11791
|
});
|
|
10034
11792
|
const safe = readOptions(options, ["description"], "literalShape", "literal");
|
|
10035
11793
|
const snapshot = readArrayEntries(values);
|
|
10036
|
-
if (
|
|
11794
|
+
if (isFailure(snapshot)) throw new ContractError("literalShape: values could not be copied", {
|
|
10037
11795
|
code: "structure",
|
|
10038
11796
|
context: {
|
|
10039
11797
|
path: ["values"],
|
|
@@ -10064,8 +11822,7 @@ function literalShape(values, options) {
|
|
|
10064
11822
|
values: INTRINSICS.freeze(literals),
|
|
10065
11823
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10066
11824
|
};
|
|
10067
|
-
|
|
10068
|
-
return INTRINSICS.freeze(shape);
|
|
11825
|
+
return ShapePass.seal(shape);
|
|
10069
11826
|
}, "literalShape");
|
|
10070
11827
|
}
|
|
10071
11828
|
/**
|
|
@@ -10111,8 +11868,7 @@ function arrayShape(items, options) {
|
|
|
10111
11868
|
...safe?.max === void 0 ? {} : { max: safe.max },
|
|
10112
11869
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10113
11870
|
};
|
|
10114
|
-
|
|
10115
|
-
return INTRINSICS.freeze(shape);
|
|
11871
|
+
return ShapePass.seal(shape);
|
|
10116
11872
|
}, "arrayShape");
|
|
10117
11873
|
}
|
|
10118
11874
|
/**
|
|
@@ -10182,8 +11938,7 @@ function objectShape(properties, options) {
|
|
|
10182
11938
|
...safe?.additionalProperties === void 0 ? {} : { additionalProperties: safe.additionalProperties },
|
|
10183
11939
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10184
11940
|
};
|
|
10185
|
-
|
|
10186
|
-
return INTRINSICS.freeze(shape);
|
|
11941
|
+
return ShapePass.seal(shape);
|
|
10187
11942
|
}, "objectShape");
|
|
10188
11943
|
}
|
|
10189
11944
|
/**
|
|
@@ -10220,13 +11975,19 @@ function recordShape(values, options) {
|
|
|
10220
11975
|
additionalProperties: values,
|
|
10221
11976
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10222
11977
|
};
|
|
10223
|
-
|
|
10224
|
-
return INTRINSICS.freeze(shape);
|
|
11978
|
+
return ShapePass.seal(shape);
|
|
10225
11979
|
}, "recordShape");
|
|
10226
11980
|
}
|
|
10227
11981
|
/**
|
|
10228
11982
|
* Builds a {@link UnionShape} from a list of variant shapes (`anyOf` in JSON Schema).
|
|
10229
11983
|
*
|
|
11984
|
+
* @remarks
|
|
11985
|
+
* When every arm is an object declaring one required literal property, and a value's
|
|
11986
|
+
* own property exactly equals a literal only one arm holds, that arm answers: `explain`
|
|
11987
|
+
* and `audit` report its faults at their real paths, and `parse` repairs through it
|
|
11988
|
+
* alone. Any other value reports `variant`, with `key` and `expected` saying what would
|
|
11989
|
+
* have named an arm. `is` does not change.
|
|
11990
|
+
*
|
|
10230
11991
|
* @param variants - The candidate shapes; the first match wins at runtime
|
|
10231
11992
|
* @returns A union shape whose `Infer` is the union of the variants
|
|
10232
11993
|
*
|
|
@@ -10238,11 +11999,7 @@ function recordShape(values, options) {
|
|
|
10238
11999
|
*/
|
|
10239
12000
|
function unionShape(...variants) {
|
|
10240
12001
|
return contain(() => {
|
|
10241
|
-
|
|
10242
|
-
category: "union",
|
|
10243
|
-
variants
|
|
10244
|
-
}).validate();
|
|
10245
|
-
return INTRINSICS.freeze({
|
|
12002
|
+
return ShapePass.seal({
|
|
10246
12003
|
category: "union",
|
|
10247
12004
|
variants: INTRINSICS.freeze(variants)
|
|
10248
12005
|
});
|
|
@@ -10261,9 +12018,11 @@ function unionShape(...variants) {
|
|
|
10261
12018
|
* {@link unionShape}'s guard.
|
|
10262
12019
|
* - **Parser**: judged on the RAW input's guard matches only, with NO
|
|
10263
12020
|
* coercion fallback for an ambiguous input. When exactly one variant's
|
|
10264
|
-
* guard accepts the raw value,
|
|
12021
|
+
* guard accepts the raw value, the input itself is returned. Zero matches or
|
|
10265
12022
|
* two-or-more matches both parse to `undefined` — a value ambiguous
|
|
10266
|
-
* between variants has no well-defined coercion target.
|
|
12023
|
+
* between variants has no well-defined coercion target. A value that names one
|
|
12024
|
+
* arm through a required literal is not ambiguous, so as in {@link unionShape}
|
|
12025
|
+
* that arm answers alone, and its parser may coerce.
|
|
10267
12026
|
*
|
|
10268
12027
|
* Prefer {@link unionShape} when a value may legitimately satisfy more than
|
|
10269
12028
|
* one variant (for example overlapping shapes) and any match is acceptable. Prefer
|
|
@@ -10282,15 +12041,10 @@ function unionShape(...variants) {
|
|
|
10282
12041
|
*/
|
|
10283
12042
|
function oneOfShape(...variants) {
|
|
10284
12043
|
return contain(() => {
|
|
10285
|
-
|
|
12044
|
+
return ShapePass.seal({
|
|
10286
12045
|
category: "union",
|
|
10287
|
-
variants,
|
|
12046
|
+
variants: INTRINSICS.freeze(variants),
|
|
10288
12047
|
mode: "oneOf"
|
|
10289
|
-
};
|
|
10290
|
-
new ShapeValidator(shape).validate();
|
|
10291
|
-
return INTRINSICS.freeze({
|
|
10292
|
-
...shape,
|
|
10293
|
-
variants: INTRINSICS.freeze(variants)
|
|
10294
12048
|
});
|
|
10295
12049
|
}, "oneOfShape");
|
|
10296
12050
|
}
|
|
@@ -10299,22 +12053,55 @@ function oneOfShape(...variants) {
|
|
|
10299
12053
|
*
|
|
10300
12054
|
* @remarks
|
|
10301
12055
|
* As an {@link objectShape} property, the field becomes a true optional property
|
|
10302
|
-
* in the inferred type.
|
|
12056
|
+
* in the inferred type. A `default` is captured as an owned frozen copy, and the
|
|
12057
|
+
* compiled parser fills it where the property is absent or `undefined`; the guard,
|
|
12058
|
+
* the audit, the report, and the generator keep treating the property as optional.
|
|
12059
|
+
* The compilers refuse a default `inner` refuses, a default under a union, and a
|
|
12060
|
+
* default on a property of an open object.
|
|
10303
12061
|
*
|
|
10304
12062
|
* @param inner - The wrapped shape
|
|
12063
|
+
* @param options - Optional `default`, a JSON value
|
|
10305
12064
|
* @returns An optional shape
|
|
12065
|
+
* @throws {ContractError} When a present `default` is not a JSON value, `undefined` included
|
|
12066
|
+
*
|
|
12067
|
+
* @example
|
|
12068
|
+
* ```ts
|
|
12069
|
+
* const retries = optionalShape(integerShape({ min: 0 }), { default: 3 })
|
|
12070
|
+
* ```
|
|
10306
12071
|
*/
|
|
10307
|
-
function optionalShape(inner) {
|
|
12072
|
+
function optionalShape(inner, options) {
|
|
10308
12073
|
return contain(() => {
|
|
12074
|
+
const safe = readOptions(options, ["default"], "optionalShape", "optional");
|
|
12075
|
+
refuseOptionKeys(options, ["default"], "optionalShape", "optional");
|
|
12076
|
+
const present = options !== void 0 && INTRINSICS.own(options, "default");
|
|
12077
|
+
const value = present ? safe?.default : void 0;
|
|
12078
|
+
if (present && !isJSONValue(value)) throw new ContractError("optionalShape: default must be a JSON value", {
|
|
12079
|
+
code: "structure",
|
|
12080
|
+
context: {
|
|
12081
|
+
path: ["default"],
|
|
12082
|
+
shape: "optional",
|
|
12083
|
+
received: preview(value)
|
|
12084
|
+
}
|
|
12085
|
+
});
|
|
12086
|
+
const owned = value === void 0 ? void 0 : attempt(() => cloneJSONValue(value));
|
|
12087
|
+
if (owned !== void 0 && isFailure(owned)) throw new ContractError("optionalShape: default must be a JSON value", {
|
|
12088
|
+
code: "structure",
|
|
12089
|
+
context: {
|
|
12090
|
+
path: ["default"],
|
|
12091
|
+
shape: "optional",
|
|
12092
|
+
received: preview(value)
|
|
12093
|
+
},
|
|
12094
|
+
cause: owned.error
|
|
12095
|
+
});
|
|
10309
12096
|
const shape = {
|
|
10310
12097
|
category: "optional",
|
|
10311
|
-
inner
|
|
12098
|
+
inner,
|
|
12099
|
+
...owned === void 0 ? {} : { default: owned.value }
|
|
10312
12100
|
};
|
|
10313
|
-
|
|
12101
|
+
return ShapePass.seal(shape, {
|
|
10314
12102
|
category: "object",
|
|
10315
12103
|
properties: { value: shape }
|
|
10316
|
-
})
|
|
10317
|
-
return INTRINSICS.freeze(shape);
|
|
12104
|
+
});
|
|
10318
12105
|
}, "optionalShape");
|
|
10319
12106
|
}
|
|
10320
12107
|
/**
|
|
@@ -10335,8 +12122,7 @@ function nullableShape(inner) {
|
|
|
10335
12122
|
category: "nullable",
|
|
10336
12123
|
inner
|
|
10337
12124
|
};
|
|
10338
|
-
|
|
10339
|
-
return INTRINSICS.freeze(shape);
|
|
12125
|
+
return ShapePass.seal(shape);
|
|
10340
12126
|
}, "nullableShape");
|
|
10341
12127
|
}
|
|
10342
12128
|
/**
|
|
@@ -10368,17 +12154,39 @@ function jsonShape(options) {
|
|
|
10368
12154
|
category: "json",
|
|
10369
12155
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10370
12156
|
};
|
|
10371
|
-
|
|
10372
|
-
return INTRINSICS.freeze(shape);
|
|
12157
|
+
return ShapePass.seal(shape);
|
|
10373
12158
|
}, "jsonShape");
|
|
10374
12159
|
}
|
|
10375
12160
|
/**
|
|
12161
|
+
* Builds a {@link ShapeShape}.
|
|
12162
|
+
*
|
|
12163
|
+
* @param options - Optional description
|
|
12164
|
+
* @returns A shape-valued declaration
|
|
12165
|
+
*
|
|
12166
|
+
* @example
|
|
12167
|
+
* ```ts
|
|
12168
|
+
* const definition = objectShape({ shape: shapeShape() })
|
|
12169
|
+
* ```
|
|
12170
|
+
*/
|
|
12171
|
+
function shapeShape(options) {
|
|
12172
|
+
return contain(() => {
|
|
12173
|
+
const safe = readOptions(options, ["description"], "shapeShape", "shape");
|
|
12174
|
+
const shape = {
|
|
12175
|
+
category: "shape",
|
|
12176
|
+
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
12177
|
+
};
|
|
12178
|
+
return ShapePass.seal(shape);
|
|
12179
|
+
}, "shapeShape");
|
|
12180
|
+
}
|
|
12181
|
+
/**
|
|
10376
12182
|
* Builds a {@link RawShape} from a supported JSON Schema fragment.
|
|
10377
12183
|
*
|
|
10378
12184
|
* @remarks
|
|
10379
12185
|
* For values the shape DSL can't express. The fragment is recursively checked
|
|
10380
12186
|
* against the lean {@link JSONSchema} vocabulary before ownership is taken;
|
|
10381
12187
|
* malformed or unsupported keywords throw a coded {@link ContractError}. The
|
|
12188
|
+
* refusal path starts at `schema` and names the nested keyword or member.
|
|
12189
|
+
* A non-record root is refused at `schema` itself. The
|
|
10382
12190
|
* compiled guard accepts every
|
|
10383
12191
|
* DEFINED value — `undefined` alone fails, because it is the parser's failure
|
|
10384
12192
|
* sentinel; wrap the shape in {@link optionalShape} to admit absence. The
|
|
@@ -10406,22 +12214,18 @@ function rawShape(schema) {
|
|
|
10406
12214
|
category: "raw",
|
|
10407
12215
|
schema
|
|
10408
12216
|
}).validate();
|
|
10409
|
-
|
|
10410
|
-
new ShapeValidator({
|
|
10411
|
-
category: "raw",
|
|
10412
|
-
schema: owned
|
|
10413
|
-
}).validate();
|
|
10414
|
-
return INTRINSICS.freeze({
|
|
12217
|
+
return ShapePass.seal({
|
|
10415
12218
|
category: "raw",
|
|
10416
|
-
schema:
|
|
12219
|
+
schema: cloneSchema(schema)
|
|
10417
12220
|
});
|
|
10418
12221
|
}, "rawShape");
|
|
10419
12222
|
}
|
|
10420
12223
|
/**
|
|
10421
12224
|
* Converts a runtime `JSONSchema` value into a validating {@link ContractShape}
|
|
10422
12225
|
* — the inverse of {@link compileSchema}. Unlike direct {@link rawShape}
|
|
10423
|
-
* construction, which rejects malformed supported-vocabulary keywords,
|
|
10424
|
-
* conversion is total and widens an inexpressible input to a valid raw
|
|
12226
|
+
* construction, which rejects malformed supported-vocabulary keywords, the
|
|
12227
|
+
* lenient conversion is total and widens an inexpressible input to a valid raw
|
|
12228
|
+
* `{}`, and a strict conversion refuses that input.
|
|
10425
12229
|
*
|
|
10426
12230
|
* @remarks
|
|
10427
12231
|
* Readable malformed, cyclic, or deeply nested schema nodes widen to
|
|
@@ -10472,9 +12276,36 @@ function rawShape(schema) {
|
|
|
10472
12276
|
* when the conversion widened anywhere — `schema` / `is` / `parse` / `explain`
|
|
10473
12277
|
* stay total.
|
|
10474
12278
|
*
|
|
12279
|
+
* LENIENT LAW: a value a JSON Schema accepts is never refused by the shape a
|
|
12280
|
+
* lenient conversion builds from it, because the walk widens what it cannot read.
|
|
12281
|
+
* So `oneOf` builds an inclusive union, a `true` arm is a raw variant and a `false`
|
|
12282
|
+
* arm is dropped, an `enum` holding `null` admits it, `nullable: true` wraps the
|
|
12283
|
+
* node, a present `patternProperties` opens the object, a present `prefixItems`
|
|
12284
|
+
* widens the elements, and a `$ref` widens the node before anything beside it is read.
|
|
12285
|
+
* A schema past the compile limit keeps only its root's type, an array's bounds, and an
|
|
12286
|
+
* object's required keys and closure, with every value below them raw.
|
|
12287
|
+
*
|
|
12288
|
+
* DEFAULTS: a lenient conversion carries a property's `default` when the key is
|
|
12289
|
+
* not `required`, its object is closed with no union ancestor, and the value is
|
|
12290
|
+
* one the property's primitive or literal guard accepts. It drops every other
|
|
12291
|
+
* `default`, because carrying it would make the shape refuse to compile.
|
|
12292
|
+
*
|
|
12293
|
+
* STRICT: `{ strict: true }` is the door for a foreign schema. It refuses, with
|
|
12294
|
+
* code `structure`, every node the lenient walk would widen, every keyword it
|
|
12295
|
+
* would ignore (`pattern`, `format`, `nullable`, `patternProperties`,
|
|
12296
|
+
* `propertyNames`, `prefixItems`, and anything else outside
|
|
12297
|
+
* {@link STRICT_SCHEMA_KEYWORDS}), a malformed keyword or annotation, a boolean schema, an `enum` entry
|
|
12298
|
+
* that is neither a finite primitive nor `null`, a contradictory bound, a `required` name not in
|
|
12299
|
+
* `properties`, breadth past {@link INFER_BREADTH_LIMIT}, and a `default` with no
|
|
12300
|
+
* optional property to fill. An object with no `additionalProperties` closes
|
|
12301
|
+
* rather than opens, and `oneOf` keeps exactly-one, because a strict walk reads
|
|
12302
|
+
* every keyword that could keep two arms apart. Below a `oneOf` that object refuses
|
|
12303
|
+
* instead, since closing it could change which arms match.
|
|
12304
|
+
*
|
|
10475
12305
|
* @param schema - The JSON Schema value to convert
|
|
12306
|
+
* @param options - Optional `strict`, default `false`
|
|
10476
12307
|
* @returns The built {@link ContractShape}
|
|
10477
|
-
* @throws {ContractError} When schema traversal fails
|
|
12308
|
+
* @throws {ContractError} When schema traversal fails, or when a strict conversion refuses
|
|
10478
12309
|
*
|
|
10479
12310
|
* @example
|
|
10480
12311
|
* ```ts
|
|
@@ -10484,14 +12315,36 @@ function rawShape(schema) {
|
|
|
10484
12315
|
* contract.parse({ id: 'nope' }) // undefined
|
|
10485
12316
|
* ```
|
|
10486
12317
|
*/
|
|
10487
|
-
function schemaToShape(schema) {
|
|
12318
|
+
function schemaToShape(schema, options) {
|
|
10488
12319
|
return contain(() => {
|
|
10489
|
-
|
|
12320
|
+
const safe = readOptions(options, ["strict"], "schemaToShape", "schema");
|
|
12321
|
+
refuseOptionKeys(options, ["strict"], "schemaToShape", "schema");
|
|
12322
|
+
const strict = options !== void 0 && INTRINSICS.own(options, "strict") ? safe?.strict : void 0;
|
|
12323
|
+
if (strict !== void 0 && typeof strict !== "boolean") throw new ContractError("schemaToShape: strict must be a boolean", {
|
|
12324
|
+
code: "structure",
|
|
12325
|
+
context: {
|
|
12326
|
+
path: ["strict"],
|
|
12327
|
+
shape: "schema",
|
|
12328
|
+
received: preview(strict)
|
|
12329
|
+
}
|
|
12330
|
+
});
|
|
12331
|
+
const shaper = new SchemaShaper(schema, strict === true);
|
|
12332
|
+
if (strict !== true) return readValue(() => shaper.shape(), "schemaToShape", {
|
|
12333
|
+
subject: "schema",
|
|
12334
|
+
context: { shape: "schema" }
|
|
12335
|
+
});
|
|
12336
|
+
const outcome = attempt(() => shaper.shape());
|
|
12337
|
+
if (isSuccess(outcome)) return outcome.value;
|
|
12338
|
+
if (isContractError(outcome.error)) throw outcome.error;
|
|
12339
|
+
return readValue(() => {
|
|
12340
|
+
throw outcome.error;
|
|
12341
|
+
}, "schemaToShape", {
|
|
10490
12342
|
subject: "schema",
|
|
10491
12343
|
context: { shape: "schema" }
|
|
10492
12344
|
});
|
|
10493
12345
|
}, "schemaToShape");
|
|
10494
12346
|
}
|
|
12347
|
+
exports.CHOICE_LIMIT = CHOICE_LIMIT;
|
|
10495
12348
|
exports.CLONE_NODE_LIMIT = CLONE_NODE_LIMIT;
|
|
10496
12349
|
exports.COMPILE_DEPTH_LIMIT = COMPILE_DEPTH_LIMIT;
|
|
10497
12350
|
exports.COMPILE_NODE_LIMIT = COMPILE_NODE_LIMIT;
|
|
@@ -10499,7 +12352,12 @@ exports.CONTRACT_CODES = CONTRACT_CODES;
|
|
|
10499
12352
|
exports.CONTRACT_ERROR_BRAND = CONTRACT_ERROR_BRAND;
|
|
10500
12353
|
exports.ContractCompiler = ContractCompiler;
|
|
10501
12354
|
exports.ContractError = ContractError;
|
|
12355
|
+
exports.FAULT_CONSTRAINTS = FAULT_CONSTRAINTS;
|
|
12356
|
+
exports.FAULT_KINDS = FAULT_KINDS;
|
|
10502
12357
|
exports.FAULT_LIMIT = FAULT_LIMIT;
|
|
12358
|
+
exports.FAULT_LIST_LIMIT = FAULT_LIST_LIMIT;
|
|
12359
|
+
exports.FAULT_SCHEMA_LIMIT = FAULT_SCHEMA_LIMIT;
|
|
12360
|
+
exports.FAULT_TEXT_LIMIT = FAULT_TEXT_LIMIT;
|
|
10503
12361
|
exports.FORMAT_MAX_LENGTH = FORMAT_MAX_LENGTH;
|
|
10504
12362
|
exports.FORMAT_PATTERNS = FORMAT_PATTERNS;
|
|
10505
12363
|
exports.GENERATION_ATTEMPT_LIMIT = GENERATION_ATTEMPT_LIMIT;
|
|
@@ -10510,18 +12368,26 @@ exports.INFER_ENUM_LIMIT = INFER_ENUM_LIMIT;
|
|
|
10510
12368
|
exports.INTRINSICS = INTRINSICS;
|
|
10511
12369
|
exports.JSONCloner = JSONCloner;
|
|
10512
12370
|
exports.JSON_SCHEMA_TYPES = JSON_SCHEMA_TYPES;
|
|
12371
|
+
exports.NAME_LIMIT = NAME_LIMIT;
|
|
12372
|
+
exports.NODE_LIMIT = NODE_LIMIT;
|
|
10513
12373
|
exports.PRESENCE_MASK_LIMIT = PRESENCE_MASK_LIMIT;
|
|
10514
12374
|
exports.PREVIEW_LIMIT = PREVIEW_LIMIT;
|
|
12375
|
+
exports.SHAPE_FIELDS = SHAPE_FIELDS;
|
|
12376
|
+
exports.STRICT_SCHEMA_KEYWORDS = STRICT_SCHEMA_KEYWORDS;
|
|
12377
|
+
exports.STRING_LIMIT = STRING_LIMIT;
|
|
10515
12378
|
exports.SchemaCloner = SchemaCloner;
|
|
10516
12379
|
exports.ShapeCloner = ShapeCloner;
|
|
10517
12380
|
exports.ShapeValidator = ShapeValidator;
|
|
12381
|
+
exports.TEXT_LIMIT = TEXT_LIMIT;
|
|
10518
12382
|
exports.admitMember = admitMember;
|
|
12383
|
+
exports.admitValue = admitValue;
|
|
10519
12384
|
exports.admitVisited = admitVisited;
|
|
10520
12385
|
exports.andOf = andOf;
|
|
10521
12386
|
exports.appendEntries = appendEntries;
|
|
10522
12387
|
exports.arrayOf = arrayOf;
|
|
10523
12388
|
exports.arrayShape = arrayShape;
|
|
10524
12389
|
exports.attempt = attempt;
|
|
12390
|
+
exports.auditFaultShape = auditFaultShape;
|
|
10525
12391
|
exports.booleanShape = booleanShape;
|
|
10526
12392
|
exports.boundsOf = boundsOf;
|
|
10527
12393
|
exports.buildArrayFaults = buildArrayFaults;
|
|
@@ -10537,7 +12403,7 @@ exports.cloneSchema = cloneSchema;
|
|
|
10537
12403
|
exports.cloneShape = cloneShape;
|
|
10538
12404
|
exports.collectEntries = collectEntries;
|
|
10539
12405
|
exports.collectMembers = collectMembers;
|
|
10540
|
-
exports.
|
|
12406
|
+
exports.comparePrimitives = comparePrimitives;
|
|
10541
12407
|
exports.compileAuditor = compileAuditor;
|
|
10542
12408
|
exports.compileGenerator = compileGenerator;
|
|
10543
12409
|
exports.compileGuard = compileGuard;
|
|
@@ -10545,16 +12411,25 @@ exports.compileParser = compileParser;
|
|
|
10545
12411
|
exports.compileReporter = compileReporter;
|
|
10546
12412
|
exports.compileSchema = compileSchema;
|
|
10547
12413
|
exports.complementOf = complementOf;
|
|
12414
|
+
exports.computeSurrogatePairs = computeSurrogatePairs;
|
|
10548
12415
|
exports.contain = contain;
|
|
10549
12416
|
exports.createContract = createContract;
|
|
10550
12417
|
exports.deriveLengthBounds = deriveLengthBounds;
|
|
10551
12418
|
exports.deriveRangeBounds = deriveRangeBounds;
|
|
12419
|
+
exports.describeFaultLine = describeFaultLine;
|
|
12420
|
+
exports.describeFaults = describeFaults;
|
|
10552
12421
|
exports.drawRandom = drawRandom;
|
|
10553
12422
|
exports.encodeLeaf = encodeLeaf;
|
|
10554
12423
|
exports.encodeParts = encodeParts;
|
|
10555
12424
|
exports.enumOf = enumOf;
|
|
10556
12425
|
exports.enumerableKeys = enumerableKeys;
|
|
10557
12426
|
exports.enumerableSymbolCount = enumerableSymbolCount;
|
|
12427
|
+
exports.errorToMessage = errorToMessage;
|
|
12428
|
+
exports.escapeRegExp = escapeRegExp;
|
|
12429
|
+
exports.extractForms = extractForms;
|
|
12430
|
+
exports.extractPath = extractPath;
|
|
12431
|
+
exports.extractSchema = extractSchema;
|
|
12432
|
+
exports.failure = failure;
|
|
10558
12433
|
exports.holds = holds;
|
|
10559
12434
|
exports.inferPrimitiveEnum = inferPrimitiveEnum;
|
|
10560
12435
|
exports.instanceOf = instanceOf;
|
|
@@ -10566,6 +12441,7 @@ exports.isArrayBufferView = isArrayBufferView;
|
|
|
10566
12441
|
exports.isAsyncFunction = isAsyncFunction;
|
|
10567
12442
|
exports.isAsyncGeneratorFunction = isAsyncGeneratorFunction;
|
|
10568
12443
|
exports.isAsyncIterable = isAsyncIterable;
|
|
12444
|
+
exports.isAuditFault = isAuditFault;
|
|
10569
12445
|
exports.isBigInt = isBigInt;
|
|
10570
12446
|
exports.isBigInt64Array = isBigInt64Array;
|
|
10571
12447
|
exports.isBigUint64Array = isBigUint64Array;
|
|
@@ -10583,7 +12459,10 @@ exports.isEmptyObject = isEmptyObject;
|
|
|
10583
12459
|
exports.isEmptySet = isEmptySet;
|
|
10584
12460
|
exports.isEmptyString = isEmptyString;
|
|
10585
12461
|
exports.isError = isError;
|
|
12462
|
+
exports.isFailure = isFailure;
|
|
10586
12463
|
exports.isFalse = isFalse;
|
|
12464
|
+
exports.isFault = isFault;
|
|
12465
|
+
exports.isFaultPath = isFaultPath;
|
|
10587
12466
|
exports.isFiniteNumber = isFiniteNumber;
|
|
10588
12467
|
exports.isFloat32Array = isFloat32Array;
|
|
10589
12468
|
exports.isFloat64Array = isFloat64Array;
|
|
@@ -10619,6 +12498,7 @@ exports.isRegExp = isRegExp;
|
|
|
10619
12498
|
exports.isSet = isSet;
|
|
10620
12499
|
exports.isSharedArrayBuffer = isSharedArrayBuffer;
|
|
10621
12500
|
exports.isString = isString;
|
|
12501
|
+
exports.isSuccess = isSuccess;
|
|
10622
12502
|
exports.isSymbol = isSymbol;
|
|
10623
12503
|
exports.isTrue = isTrue;
|
|
10624
12504
|
exports.isUint16Array = isUint16Array;
|
|
@@ -10641,12 +12521,16 @@ exports.literalOf = literalOf;
|
|
|
10641
12521
|
exports.literalShape = literalShape;
|
|
10642
12522
|
exports.mapOf = mapOf;
|
|
10643
12523
|
exports.matchOf = matchOf;
|
|
12524
|
+
exports.matchesExactKeys = matchesExactKeys;
|
|
10644
12525
|
exports.matchesISOInstant = matchesISOInstant;
|
|
10645
12526
|
exports.matchesJSONDepth = matchesJSONDepth;
|
|
10646
12527
|
exports.matchesJSONValue = matchesJSONValue;
|
|
12528
|
+
exports.matchesLosslessRepair = matchesLosslessRepair;
|
|
10647
12529
|
exports.matchesMember = matchesMember;
|
|
10648
12530
|
exports.matchesPattern = matchesPattern;
|
|
10649
12531
|
exports.matchesRecordBrand = matchesRecordBrand;
|
|
12532
|
+
exports.matchesStringLength = matchesStringLength;
|
|
12533
|
+
exports.matchesUndefinedField = matchesUndefinedField;
|
|
10650
12534
|
exports.matchesVisited = matchesVisited;
|
|
10651
12535
|
exports.notOf = notOf;
|
|
10652
12536
|
exports.nullShape = nullShape;
|
|
@@ -10705,6 +12589,10 @@ exports.readValue = readValue;
|
|
|
10705
12589
|
exports.recordOf = recordOf;
|
|
10706
12590
|
exports.recordShape = recordShape;
|
|
10707
12591
|
exports.refuseExpansion = refuseExpansion;
|
|
12592
|
+
exports.refuseOptionKeys = refuseOptionKeys;
|
|
12593
|
+
exports.renderForm = renderForm;
|
|
12594
|
+
exports.renderList = renderList;
|
|
12595
|
+
exports.renderPath = renderPath;
|
|
10708
12596
|
exports.resolveField = resolveField;
|
|
10709
12597
|
exports.retainDepth = retainDepth;
|
|
10710
12598
|
exports.samplesToFormat = samplesToFormat;
|
|
@@ -10717,12 +12605,15 @@ exports.schemaToShape = schemaToShape;
|
|
|
10717
12605
|
exports.seededRandom = seededRandom;
|
|
10718
12606
|
exports.selectClosestFaults = selectClosestFaults;
|
|
10719
12607
|
exports.setOf = setOf;
|
|
12608
|
+
exports.shapeShape = shapeShape;
|
|
10720
12609
|
exports.shapeToKind = shapeToKind;
|
|
10721
12610
|
exports.sortValues = sortValues;
|
|
10722
12611
|
exports.stringOf = stringOf;
|
|
10723
12612
|
exports.stringShape = stringShape;
|
|
10724
12613
|
exports.stringToFormat = stringToFormat;
|
|
12614
|
+
exports.success = success;
|
|
10725
12615
|
exports.transformOf = transformOf;
|
|
12616
|
+
exports.truncateFaultText = truncateFaultText;
|
|
10726
12617
|
exports.tupleOf = tupleOf;
|
|
10727
12618
|
exports.unifySchemas = unifySchemas;
|
|
10728
12619
|
exports.unionOf = unionOf;
|