@orkestrel/contract 0.0.21 → 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 +2135 -423
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +766 -48
- package/dist/src/core/index.d.ts +766 -48
- package/dist/src/core/index.js +2108 -424
- package/dist/src/core/index.js.map +1 -1
- package/package.json +9 -9
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
|
*
|
|
@@ -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
|
*
|
|
@@ -613,6 +742,12 @@ var TEXT_LIMIT = 1048576;
|
|
|
613
742
|
* ```
|
|
614
743
|
*/
|
|
615
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;
|
|
616
751
|
// src/core/errors.ts
|
|
617
752
|
/**
|
|
618
753
|
* Carries a machine-readable contract category, optional context, and an exact optional
|
|
@@ -1263,6 +1398,103 @@ function isObject(value) {
|
|
|
1263
1398
|
function isRecord(value) {
|
|
1264
1399
|
return holds(() => matchesRecordBrand(value));
|
|
1265
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
|
+
}
|
|
1266
1498
|
/** Determines whether a value is a `Map`.
|
|
1267
1499
|
*
|
|
1268
1500
|
* @param value - The value to inspect
|
|
@@ -1906,6 +2138,126 @@ function isJSONPrimitive(value) {
|
|
|
1906
2138
|
}
|
|
1907
2139
|
// src/core/helpers.ts
|
|
1908
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
|
+
/**
|
|
1909
2261
|
* Iterates indices while rereading the upper bound before every step.
|
|
1910
2262
|
*
|
|
1911
2263
|
* @remarks
|
|
@@ -2488,6 +2840,93 @@ function matchesPattern(pattern, value) {
|
|
|
2488
2840
|
return INTRINSICS.reflect.apply(INTRINSICS.captures, pattern, [value]) !== null;
|
|
2489
2841
|
}
|
|
2490
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
|
+
/**
|
|
2491
2930
|
* Escapes every regular-expression metacharacter in a text, so the text
|
|
2492
2931
|
* matches literally inside a larger `RegExp` source.
|
|
2493
2932
|
*
|
|
@@ -2813,7 +3252,7 @@ function readValue(callback, reader, options) {
|
|
|
2813
3252
|
};
|
|
2814
3253
|
const requested = options?.code;
|
|
2815
3254
|
const subject = options?.subject;
|
|
2816
|
-
const code = requested !== void 0 &&
|
|
3255
|
+
const code = requested !== void 0 && INTRINSICS.reflect.apply(INTRINSICS.some, CONTRACT_CODES, [(known) => known === requested]) ? requested : "structure";
|
|
2817
3256
|
return {
|
|
2818
3257
|
reader: isString(reader) ? reader : "readValue",
|
|
2819
3258
|
subject: isString(subject) ? subject : "value",
|
|
@@ -3309,6 +3748,70 @@ function resolveField(record, path) {
|
|
|
3309
3748
|
return isSuccess(outcome) ? outcome.value : void 0;
|
|
3310
3749
|
}
|
|
3311
3750
|
/**
|
|
3751
|
+
* Determines whether an object holds exactly the named own keys.
|
|
3752
|
+
*
|
|
3753
|
+
* @remarks
|
|
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`.
|
|
3756
|
+
*
|
|
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
|
+
/**
|
|
3312
3815
|
* Determines whether a readable value stays within the fixed JSON container-depth limit.
|
|
3313
3816
|
*
|
|
3314
3817
|
* @remarks
|
|
@@ -3416,13 +3919,16 @@ function matchesJSONDepth(value) {
|
|
|
3416
3919
|
*
|
|
3417
3920
|
* Beside the ancestor set the walk keeps a walk-local PROVED set, so a node
|
|
3418
3921
|
* whose whole subtree already matched is not re-walked when a second path
|
|
3419
|
-
* 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
|
|
3420
3925
|
* aliases: thirty aliases — thirty-one ordinary records — cost `2^30` visits
|
|
3421
3926
|
* through `isJSONValue`, `parseJSONValue`, `canonicalStringify` and every
|
|
3422
3927
|
* `jsonShape` contract.
|
|
3423
3928
|
*
|
|
3424
3929
|
* @param entry - The value to inspect
|
|
3425
3930
|
* @param ancestors - Objects on the active traversal path
|
|
3931
|
+
* @param budget - Remaining capture nodes; aliases are charged for each copied occurrence when supplied
|
|
3426
3932
|
* @returns True if the value is a cycle-free JSON value; false otherwise
|
|
3427
3933
|
*
|
|
3428
3934
|
* @example
|
|
@@ -3431,8 +3937,12 @@ function matchesJSONDepth(value) {
|
|
|
3431
3937
|
* matchesJSONValue(Number.NaN, new WeakSet()) // false
|
|
3432
3938
|
* ```
|
|
3433
3939
|
*/
|
|
3434
|
-
function matchesJSONValue(entry, ancestors) {
|
|
3940
|
+
function matchesJSONValue(entry, ancestors, budget) {
|
|
3435
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
|
+
}
|
|
3436
3946
|
const stack = [{
|
|
3437
3947
|
operation: "enter",
|
|
3438
3948
|
value: entry
|
|
@@ -3446,11 +3956,18 @@ function matchesJSONValue(entry, ancestors) {
|
|
|
3446
3956
|
admitVisited(proved, frame.value);
|
|
3447
3957
|
continue;
|
|
3448
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
|
+
}
|
|
3449
3966
|
const node = frame.value;
|
|
3450
3967
|
if (node === null || isString(node) || isBoolean(node) || isFiniteNumber(node)) continue;
|
|
3451
3968
|
if (INTRINSICS.array(node)) {
|
|
3452
3969
|
if (matchesVisited(ancestors, node)) return false;
|
|
3453
|
-
if (matchesVisited(proved, node)) continue;
|
|
3970
|
+
if (budget === void 0 && matchesVisited(proved, node)) continue;
|
|
3454
3971
|
const snapshot = readArrayEntries(node);
|
|
3455
3972
|
if (isFailure(snapshot)) throw snapshot.error;
|
|
3456
3973
|
if (!snapshot.value.dense) return false;
|
|
@@ -3471,7 +3988,7 @@ function matchesJSONValue(entry, ancestors) {
|
|
|
3471
3988
|
}
|
|
3472
3989
|
if (!isRecord(node)) return false;
|
|
3473
3990
|
if (matchesVisited(ancestors, node)) return false;
|
|
3474
|
-
if (matchesVisited(proved, node)) continue;
|
|
3991
|
+
if (budget === void 0 && matchesVisited(proved, node)) continue;
|
|
3475
3992
|
admitVisited(ancestors, node);
|
|
3476
3993
|
stack[stack.length] = {
|
|
3477
3994
|
operation: "exit",
|
|
@@ -3581,7 +4098,7 @@ function schemaToObject(schema) {
|
|
|
3581
4098
|
return contain(() => {
|
|
3582
4099
|
return readValue(() => {
|
|
3583
4100
|
INTRINSICS.values(schema);
|
|
3584
|
-
if (schema.type === "object") return schema;
|
|
4101
|
+
if (INTRINSICS.own(schema, "type") && schema.type === "object") return schema;
|
|
3585
4102
|
return {
|
|
3586
4103
|
type: "object",
|
|
3587
4104
|
properties: { value: schema },
|
|
@@ -4050,7 +4567,7 @@ function preview(value) {
|
|
|
4050
4567
|
function buildStringFaults(shape, value, path, pattern) {
|
|
4051
4568
|
return readValue(() => {
|
|
4052
4569
|
const faults = [];
|
|
4053
|
-
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] = {
|
|
4054
4571
|
reason: "constraint",
|
|
4055
4572
|
path,
|
|
4056
4573
|
expected: "string",
|
|
@@ -4058,7 +4575,7 @@ function buildStringFaults(shape, value, path, pattern) {
|
|
|
4058
4575
|
limit: shape.min,
|
|
4059
4576
|
received: preview(value)
|
|
4060
4577
|
};
|
|
4061
|
-
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] = {
|
|
4062
4579
|
reason: "constraint",
|
|
4063
4580
|
path,
|
|
4064
4581
|
expected: "string",
|
|
@@ -4066,7 +4583,7 @@ function buildStringFaults(shape, value, path, pattern) {
|
|
|
4066
4583
|
limit: shape.max,
|
|
4067
4584
|
received: preview(value)
|
|
4068
4585
|
};
|
|
4069
|
-
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));
|
|
4070
4587
|
if (stateless !== void 0 && !matchesPattern(stateless, value)) {
|
|
4071
4588
|
const limit = readPatternSource(stateless);
|
|
4072
4589
|
faults[faults.length] = {
|
|
@@ -4106,16 +4623,16 @@ function buildStringFaults(shape, value, path, pattern) {
|
|
|
4106
4623
|
*/
|
|
4107
4624
|
function buildNumberFaults(shape, value, path) {
|
|
4108
4625
|
return readValue(() => {
|
|
4109
|
-
const expected = shape.integer === true ? "integer" : "number";
|
|
4626
|
+
const expected = INTRINSICS.own(shape, "integer") && shape.integer === true ? "integer" : "number";
|
|
4110
4627
|
const faults = [];
|
|
4111
|
-
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] = {
|
|
4112
4629
|
reason: "constraint",
|
|
4113
4630
|
path,
|
|
4114
4631
|
expected,
|
|
4115
4632
|
constraint: "integer",
|
|
4116
4633
|
received: preview(value)
|
|
4117
4634
|
};
|
|
4118
|
-
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] = {
|
|
4119
4636
|
reason: "constraint",
|
|
4120
4637
|
path,
|
|
4121
4638
|
expected,
|
|
@@ -4123,7 +4640,7 @@ function buildNumberFaults(shape, value, path) {
|
|
|
4123
4640
|
limit: shape.min,
|
|
4124
4641
|
received: preview(value)
|
|
4125
4642
|
};
|
|
4126
|
-
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] = {
|
|
4127
4644
|
reason: "constraint",
|
|
4128
4645
|
path,
|
|
4129
4646
|
expected,
|
|
@@ -4161,7 +4678,7 @@ function buildNumberFaults(shape, value, path) {
|
|
|
4161
4678
|
function buildArrayFaults(shape, length, path) {
|
|
4162
4679
|
return readValue(() => {
|
|
4163
4680
|
const faults = [];
|
|
4164
|
-
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] = {
|
|
4165
4682
|
reason: "constraint",
|
|
4166
4683
|
path,
|
|
4167
4684
|
expected: "array",
|
|
@@ -4169,7 +4686,7 @@ function buildArrayFaults(shape, length, path) {
|
|
|
4169
4686
|
limit: shape.min,
|
|
4170
4687
|
received: INTRINSICS.text(length)
|
|
4171
4688
|
};
|
|
4172
|
-
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] = {
|
|
4173
4690
|
reason: "constraint",
|
|
4174
4691
|
path,
|
|
4175
4692
|
expected: "array",
|
|
@@ -4243,7 +4760,7 @@ function shapeToKind(shape) {
|
|
|
4243
4760
|
return readValue(() => {
|
|
4244
4761
|
switch (shape.category) {
|
|
4245
4762
|
case "string": return "string";
|
|
4246
|
-
case "number": return shape.integer === true ? "integer" : "number";
|
|
4763
|
+
case "number": return INTRINSICS.own(shape, "integer") && shape.integer === true ? "integer" : "number";
|
|
4247
4764
|
case "boolean": return "boolean";
|
|
4248
4765
|
case "null": return "null";
|
|
4249
4766
|
case "literal": return "literal";
|
|
@@ -4251,6 +4768,7 @@ function shapeToKind(shape) {
|
|
|
4251
4768
|
case "object": return "object";
|
|
4252
4769
|
case "union": return "union";
|
|
4253
4770
|
case "json": return "json";
|
|
4771
|
+
case "shape": return "shape";
|
|
4254
4772
|
case "optional": return shapeToKind(shape.inner);
|
|
4255
4773
|
case "nullable": return shapeToKind(shape.inner);
|
|
4256
4774
|
case "raw": return "json";
|
|
@@ -4395,6 +4913,233 @@ for (const generator of [iterateIndices, encodeParts]) {
|
|
|
4395
4913
|
INTRINSICS.freeze(generator.prototype);
|
|
4396
4914
|
INTRINSICS.freeze(generator);
|
|
4397
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
|
+
}
|
|
4398
5143
|
// src/core/JSONCloner.ts
|
|
4399
5144
|
/**
|
|
4400
5145
|
* Owns the state of one exact JSON snapshot operation.
|
|
@@ -4426,7 +5171,6 @@ var JSONCloner = class JSONCloner {
|
|
|
4426
5171
|
#owned;
|
|
4427
5172
|
#active;
|
|
4428
5173
|
#pending;
|
|
4429
|
-
#produced;
|
|
4430
5174
|
#poison;
|
|
4431
5175
|
#terminal;
|
|
4432
5176
|
constructor(value) {
|
|
@@ -4434,17 +5178,17 @@ var JSONCloner = class JSONCloner {
|
|
|
4434
5178
|
this.#owned = new JSONCloner.#weakSet();
|
|
4435
5179
|
this.#active = new JSONCloner.#weakSet();
|
|
4436
5180
|
this.#pending = [];
|
|
4437
|
-
this.#produced = 0;
|
|
4438
5181
|
this.#poison = void 0;
|
|
4439
5182
|
this.#terminal = void 0;
|
|
4440
5183
|
}
|
|
4441
5184
|
/**
|
|
4442
5185
|
* Clones the retained source into exact, deeply frozen JSON data.
|
|
4443
5186
|
*
|
|
5187
|
+
* @param budget - Remaining nodes shared with an enclosing ownership operation
|
|
4444
5188
|
* @returns The settled JSON snapshot
|
|
4445
5189
|
* @throws {ContractError} When the source is inexact, cyclic, unreadable, or cloning is reentered
|
|
4446
5190
|
*/
|
|
4447
|
-
clone() {
|
|
5191
|
+
clone(budget = { left: CLONE_NODE_LIMIT }) {
|
|
4448
5192
|
const terminal = this.#terminal;
|
|
4449
5193
|
if (terminal !== void 0) {
|
|
4450
5194
|
if (isSuccess(terminal)) return terminal.value;
|
|
@@ -4456,7 +5200,7 @@ var JSONCloner = class JSONCloner {
|
|
|
4456
5200
|
throw poison;
|
|
4457
5201
|
}
|
|
4458
5202
|
this.#poison = this.#create("JSONCloner.clone: JSON cloning may not be reentered");
|
|
4459
|
-
const outcome = attempt(() => this.#execute());
|
|
5203
|
+
const outcome = attempt(() => this.#execute(budget));
|
|
4460
5204
|
const interruption = this.#terminal;
|
|
4461
5205
|
if (interruption !== void 0) {
|
|
4462
5206
|
if (isSuccess(interruption)) return interruption.value;
|
|
@@ -4474,12 +5218,14 @@ var JSONCloner = class JSONCloner {
|
|
|
4474
5218
|
this.#pending = JSONCloner.#emptyPending;
|
|
4475
5219
|
throw error;
|
|
4476
5220
|
}
|
|
4477
|
-
#execute() {
|
|
4478
|
-
|
|
4479
|
-
|
|
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);
|
|
4480
5226
|
return root;
|
|
4481
5227
|
}
|
|
4482
|
-
#drain() {
|
|
5228
|
+
#drain(budget) {
|
|
4483
5229
|
while (this.#pending.length > 0) {
|
|
4484
5230
|
const frame = this.#pending[this.#pending.length - 1];
|
|
4485
5231
|
if (frame === void 0) continue;
|
|
@@ -4492,7 +5238,7 @@ var JSONCloner = class JSONCloner {
|
|
|
4492
5238
|
continue;
|
|
4493
5239
|
}
|
|
4494
5240
|
frame.index += 1;
|
|
4495
|
-
const clone = this.#capture(entry[1]);
|
|
5241
|
+
const clone = this.#capture(entry[1], budget);
|
|
4496
5242
|
INTRINSICS.define(frame.clone, entry[0], {
|
|
4497
5243
|
value: clone,
|
|
4498
5244
|
enumerable: true,
|
|
@@ -4501,9 +5247,9 @@ var JSONCloner = class JSONCloner {
|
|
|
4501
5247
|
});
|
|
4502
5248
|
}
|
|
4503
5249
|
}
|
|
4504
|
-
#capture(value) {
|
|
4505
|
-
|
|
4506
|
-
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);
|
|
4507
5253
|
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
|
|
4508
5254
|
if (typeof value === "number") {
|
|
4509
5255
|
if (INTRINSICS.finite(value)) return value;
|
|
@@ -4566,10 +5312,13 @@ var JSONCloner = class JSONCloner {
|
|
|
4566
5312
|
return [key, descriptor.value];
|
|
4567
5313
|
});
|
|
4568
5314
|
}
|
|
4569
|
-
#create(message) {
|
|
5315
|
+
#create(message, limit) {
|
|
4570
5316
|
const error = new ContractError(message, {
|
|
4571
5317
|
code: "clone",
|
|
4572
|
-
context: {
|
|
5318
|
+
context: {
|
|
5319
|
+
shape: "json",
|
|
5320
|
+
...limit === void 0 ? {} : { limit }
|
|
5321
|
+
}
|
|
4573
5322
|
});
|
|
4574
5323
|
admitVisited(this.#owned, error);
|
|
4575
5324
|
return error;
|
|
@@ -4577,10 +5326,10 @@ var JSONCloner = class JSONCloner {
|
|
|
4577
5326
|
#owns(error) {
|
|
4578
5327
|
return isObject(error) && matchesVisited(this.#owned, error);
|
|
4579
5328
|
}
|
|
4580
|
-
#refuse(message) {
|
|
5329
|
+
#refuse(message, limit) {
|
|
4581
5330
|
const terminal = this.#terminal;
|
|
4582
5331
|
if (terminal !== void 0 && isFailure(terminal)) throw terminal.error;
|
|
4583
|
-
throw this.#create(message);
|
|
5332
|
+
throw this.#create(message, limit);
|
|
4584
5333
|
}
|
|
4585
5334
|
static {
|
|
4586
5335
|
INTRINSICS.freeze(JSONCloner.#emptyPending);
|
|
@@ -4786,7 +5535,23 @@ var SchemaCloner = class SchemaCloner {
|
|
|
4786
5535
|
var ShapePass = class ShapePass {
|
|
4787
5536
|
static #weakSet = WeakSet;
|
|
4788
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();
|
|
4789
5553
|
#source;
|
|
5554
|
+
#budget;
|
|
4790
5555
|
#stack = [];
|
|
4791
5556
|
#path = [];
|
|
4792
5557
|
#active = new ShapePass.#weakSet();
|
|
@@ -4796,25 +5561,96 @@ var ShapePass = class ShapePass {
|
|
|
4796
5561
|
#height = [];
|
|
4797
5562
|
#reach = [];
|
|
4798
5563
|
#via = [];
|
|
5564
|
+
#within = [];
|
|
4799
5565
|
#counts = [];
|
|
4800
5566
|
#schemas = new ShapePass.#weakMap();
|
|
4801
5567
|
#children = [];
|
|
4802
5568
|
#category;
|
|
4803
5569
|
#raw = 0;
|
|
5570
|
+
#defaulted = false;
|
|
5571
|
+
#open = false;
|
|
4804
5572
|
#structure;
|
|
4805
5573
|
#cycle;
|
|
4806
5574
|
#domain;
|
|
4807
|
-
#clock = 0;
|
|
4808
|
-
#oldest = 0;
|
|
4809
|
-
#trail = [];
|
|
4810
5575
|
#poison;
|
|
4811
|
-
constructor(shape) {
|
|
5576
|
+
constructor(shape, budget) {
|
|
4812
5577
|
this.#source = shape;
|
|
5578
|
+
this.#budget = budget;
|
|
4813
5579
|
}
|
|
4814
|
-
/**
|
|
4815
|
-
|
|
4816
|
-
|
|
4817
|
-
|
|
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) === "");
|
|
5649
|
+
}
|
|
5650
|
+
/** Poisons this pass and throws the same refusal on every reentry. */
|
|
5651
|
+
interrupt() {
|
|
5652
|
+
this.#poison ??= new ContractError("ShapeValidator.validate: shape validation may not be reentered", {
|
|
5653
|
+
code: "structure",
|
|
4818
5654
|
context: { path: [] }
|
|
4819
5655
|
});
|
|
4820
5656
|
throw this.#poison;
|
|
@@ -4831,6 +5667,34 @@ var ShapePass = class ShapePass {
|
|
|
4831
5667
|
cause: outcome.error
|
|
4832
5668
|
});
|
|
4833
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
|
+
}
|
|
4834
5698
|
#execute() {
|
|
4835
5699
|
this.#stack[this.#stack.length] = {
|
|
4836
5700
|
operation: "enter",
|
|
@@ -4852,9 +5716,11 @@ var ShapePass = class ShapePass {
|
|
|
4852
5716
|
this.#path.length -= frame.segments;
|
|
4853
5717
|
return;
|
|
4854
5718
|
}
|
|
4855
|
-
const
|
|
4856
|
-
|
|
4857
|
-
|
|
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;
|
|
4858
5724
|
const current = frame.shape;
|
|
4859
5725
|
if (typeof current !== "object" || current === null || !isRecord(current)) {
|
|
4860
5726
|
this.#structure ??= new ContractError("validateShape: every structural child must be a shape", {
|
|
@@ -4881,7 +5747,10 @@ var ShapePass = class ShapePass {
|
|
|
4881
5747
|
this.#children = [];
|
|
4882
5748
|
this.#category = void 0;
|
|
4883
5749
|
this.#raw = 0;
|
|
5750
|
+
this.#defaulted = false;
|
|
5751
|
+
this.#open = false;
|
|
4884
5752
|
const outcome = attempt(() => this.#observe(current, frame.depth));
|
|
5753
|
+
this.#spend(0);
|
|
4885
5754
|
if (isFailure(outcome) || !outcome.value) {
|
|
4886
5755
|
this.#structure ??= new ContractError("validateShape: every node must be a recognized shape", {
|
|
4887
5756
|
code: "structure",
|
|
@@ -4898,7 +5767,7 @@ var ShapePass = class ShapePass {
|
|
|
4898
5767
|
if (descriptor === void 0 || !INTRINSICS.own(descriptor, "value")) return this.#refuse("validateShape: every node must be a recognized shape");
|
|
4899
5768
|
const category = current.category;
|
|
4900
5769
|
if (current.category !== category || descriptor.value !== category) return this.#refuse("validateShape: every node must be a recognized shape");
|
|
4901
|
-
const fields =
|
|
5770
|
+
const fields = typeof category === "string" && INTRINSICS.own(SHAPE_FIELDS, category) ? SHAPE_FIELDS[category] : void 0;
|
|
4902
5771
|
if (fields === void 0) return this.#refuse("validateShape: every node must be a recognized shape");
|
|
4903
5772
|
this.#category = category;
|
|
4904
5773
|
if (!this.#scan(current, fields)) return false;
|
|
@@ -4927,45 +5796,6 @@ var ShapePass = class ShapePass {
|
|
|
4927
5796
|
}
|
|
4928
5797
|
return this.#populate(current);
|
|
4929
5798
|
}
|
|
4930
|
-
#recognize(category) {
|
|
4931
|
-
switch (category) {
|
|
4932
|
-
case "string": return [
|
|
4933
|
-
"min",
|
|
4934
|
-
"max",
|
|
4935
|
-
"pattern",
|
|
4936
|
-
"description"
|
|
4937
|
-
];
|
|
4938
|
-
case "number": return [
|
|
4939
|
-
"integer",
|
|
4940
|
-
"min",
|
|
4941
|
-
"max",
|
|
4942
|
-
"description"
|
|
4943
|
-
];
|
|
4944
|
-
case "boolean":
|
|
4945
|
-
case "null":
|
|
4946
|
-
case "json": return ["description"];
|
|
4947
|
-
case "literal": return ["values", "description"];
|
|
4948
|
-
case "array": return [
|
|
4949
|
-
"items",
|
|
4950
|
-
"min",
|
|
4951
|
-
"max",
|
|
4952
|
-
"description"
|
|
4953
|
-
];
|
|
4954
|
-
case "object": return [
|
|
4955
|
-
"properties",
|
|
4956
|
-
"additionalProperties",
|
|
4957
|
-
"description"
|
|
4958
|
-
];
|
|
4959
|
-
case "union": return [
|
|
4960
|
-
"variants",
|
|
4961
|
-
"mode",
|
|
4962
|
-
"description"
|
|
4963
|
-
];
|
|
4964
|
-
case "optional":
|
|
4965
|
-
case "nullable": return ["inner"];
|
|
4966
|
-
case "raw": return ["schema"];
|
|
4967
|
-
}
|
|
4968
|
-
}
|
|
4969
5799
|
#scan(current, fields) {
|
|
4970
5800
|
for (const fieldIndex of iterateIndices(() => fields.length)) {
|
|
4971
5801
|
const field = fields[fieldIndex];
|
|
@@ -4988,6 +5818,10 @@ var ShapePass = class ShapePass {
|
|
|
4988
5818
|
}
|
|
4989
5819
|
#constrain(current) {
|
|
4990
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
|
+
}
|
|
4991
5825
|
if (current.category === "string") {
|
|
4992
5826
|
if (current.min !== void 0 && typeof current.min !== "number") return this.#refuse("validateShape: string min must be a number", "min");
|
|
4993
5827
|
if (current.max !== void 0 && typeof current.max !== "number") return this.#refuse("validateShape: string max must be a number", "max");
|
|
@@ -5023,7 +5857,7 @@ var ShapePass = class ShapePass {
|
|
|
5023
5857
|
received: INTRINSICS.text(current.max)
|
|
5024
5858
|
}
|
|
5025
5859
|
});
|
|
5026
|
-
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", {
|
|
5027
5861
|
code: "range",
|
|
5028
5862
|
context: {
|
|
5029
5863
|
path: pathOf(this.#path),
|
|
@@ -5042,7 +5876,7 @@ var ShapePass = class ShapePass {
|
|
|
5042
5876
|
}
|
|
5043
5877
|
if (current.category === "array" && current.min !== void 0 && typeof current.min !== "number") return this.#refuse("validateShape: array min must be a number", "min");
|
|
5044
5878
|
if (current.category === "array" && current.max !== void 0 && typeof current.max !== "number") return this.#refuse("validateShape: array max must be a number", "max");
|
|
5045
|
-
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", {
|
|
5046
5880
|
code: "bound",
|
|
5047
5881
|
context: {
|
|
5048
5882
|
path: pathOf(this.#path),
|
|
@@ -5051,7 +5885,7 @@ var ShapePass = class ShapePass {
|
|
|
5051
5885
|
received: INTRINSICS.text(current.min)
|
|
5052
5886
|
}
|
|
5053
5887
|
});
|
|
5054
|
-
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", {
|
|
5055
5889
|
code: "bound",
|
|
5056
5890
|
context: {
|
|
5057
5891
|
path: pathOf(this.#path),
|
|
@@ -5060,7 +5894,7 @@ var ShapePass = class ShapePass {
|
|
|
5060
5894
|
received: INTRINSICS.text(current.max)
|
|
5061
5895
|
}
|
|
5062
5896
|
});
|
|
5063
|
-
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", {
|
|
5064
5898
|
code: "range",
|
|
5065
5899
|
context: {
|
|
5066
5900
|
path: pathOf(this.#path),
|
|
@@ -5082,7 +5916,7 @@ var ShapePass = class ShapePass {
|
|
|
5082
5916
|
});
|
|
5083
5917
|
}
|
|
5084
5918
|
#restrict(current, source = "", flags = "") {
|
|
5085
|
-
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", {
|
|
5086
5920
|
code: "bound",
|
|
5087
5921
|
context: {
|
|
5088
5922
|
path: pathOf(this.#path),
|
|
@@ -5091,7 +5925,7 @@ var ShapePass = class ShapePass {
|
|
|
5091
5925
|
received: INTRINSICS.text(current.min)
|
|
5092
5926
|
}
|
|
5093
5927
|
});
|
|
5094
|
-
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", {
|
|
5095
5929
|
code: "bound",
|
|
5096
5930
|
context: {
|
|
5097
5931
|
path: pathOf(this.#path),
|
|
@@ -5108,7 +5942,7 @@ var ShapePass = class ShapePass {
|
|
|
5108
5942
|
received: `/${source}/${flags}`
|
|
5109
5943
|
}
|
|
5110
5944
|
});
|
|
5111
|
-
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", {
|
|
5112
5946
|
code: "range",
|
|
5113
5947
|
context: {
|
|
5114
5948
|
path: pathOf(this.#path),
|
|
@@ -5117,6 +5951,7 @@ var ShapePass = class ShapePass {
|
|
|
5117
5951
|
});
|
|
5118
5952
|
}
|
|
5119
5953
|
#inspect(source) {
|
|
5954
|
+
if (this.#structure !== void 0) return true;
|
|
5120
5955
|
if (!isRecord(source)) return this.#refuse("validateShape: raw schema must be a plain record", "schema");
|
|
5121
5956
|
const active = new ShapePass.#weakSet();
|
|
5122
5957
|
const stack = [{
|
|
@@ -5131,24 +5966,27 @@ var ShapePass = class ShapePass {
|
|
|
5131
5966
|
this.#settleSchema(frame.schema, frame.nested);
|
|
5132
5967
|
continue;
|
|
5133
5968
|
}
|
|
5969
|
+
const first = INTRINSICS.own(frame, "first") ? frame.first : void 0;
|
|
5970
|
+
const second = INTRINSICS.own(frame, "second") ? frame.second : void 0;
|
|
5134
5971
|
const schema = frame.schema;
|
|
5135
|
-
if (!isRecord(schema)) return this.#
|
|
5136
|
-
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);
|
|
5137
5974
|
if (INTRINSICS.reflect.apply(INTRINSICS.recall, this.#schemas, [schema]) !== void 0) continue;
|
|
5138
5975
|
admitVisited(active, schema);
|
|
5139
5976
|
const keyList = INTRINSICS.keys(schema);
|
|
5140
5977
|
for (const keyIndex of iterateIndices(() => keyList.length)) {
|
|
5141
5978
|
const key = keyList[keyIndex];
|
|
5142
5979
|
if (key === void 0) continue;
|
|
5143
|
-
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);
|
|
5144
5981
|
}
|
|
5145
|
-
const category = schema.type;
|
|
5146
|
-
if (category !== void 0 && category !== "null" && category !== "boolean" && category !== "object" && category !== "array" && category !== "number" && category !== "integer" && category !== "string") return this.#
|
|
5147
|
-
if (schema.description !== void 0 && typeof schema.description !== "string") return this.#
|
|
5148
|
-
if (schema
|
|
5149
|
-
if (schema.
|
|
5150
|
-
|
|
5151
|
-
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");
|
|
5152
5990
|
}
|
|
5153
5991
|
const lengthList = [
|
|
5154
5992
|
"minLength",
|
|
@@ -5159,77 +5997,95 @@ var ShapePass = class ShapePass {
|
|
|
5159
5997
|
for (const lengthIndex of iterateIndices(() => lengthList.length)) {
|
|
5160
5998
|
const key = lengthList[lengthIndex];
|
|
5161
5999
|
if (key === void 0) continue;
|
|
5162
|
-
const value = schema[key];
|
|
5163
|
-
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);
|
|
5164
6002
|
}
|
|
5165
6003
|
const rangeList = ["minimum", "maximum"];
|
|
5166
6004
|
for (const rangeIndex of iterateIndices(() => rangeList.length)) {
|
|
5167
6005
|
const key = rangeList[rangeIndex];
|
|
5168
6006
|
if (key === void 0) continue;
|
|
5169
|
-
const value = schema[key];
|
|
5170
|
-
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);
|
|
5171
6009
|
}
|
|
5172
|
-
const population = schema.enum;
|
|
6010
|
+
const population = INTRINSICS.own(schema, "enum") ? schema.enum : void 0;
|
|
5173
6011
|
if (population !== void 0) {
|
|
5174
|
-
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");
|
|
5175
6013
|
const snapshot = readArrayEntries(population);
|
|
5176
|
-
if (isFailure(snapshot) || snapshot.value.entries.length === 0) return this.#
|
|
5177
|
-
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");
|
|
5178
6016
|
const values = collectMembers([]);
|
|
5179
6017
|
for (const valueIndex of iterateIndices(() => snapshot.value.entries.length)) {
|
|
5180
6018
|
const value = snapshot.value.entries[valueIndex];
|
|
5181
|
-
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));
|
|
5182
6020
|
admitMember(values, value);
|
|
5183
6021
|
}
|
|
5184
6022
|
}
|
|
5185
|
-
const names = schema.required;
|
|
6023
|
+
const names = INTRINSICS.own(schema, "required") ? schema.required : void 0;
|
|
5186
6024
|
if (names !== void 0) {
|
|
5187
|
-
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");
|
|
5188
6026
|
const snapshot = readArrayEntries(names);
|
|
5189
|
-
if (isFailure(snapshot)) return this.#
|
|
5190
|
-
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");
|
|
5191
6029
|
const required = collectMembers([]);
|
|
5192
6030
|
for (const valueIndex of iterateIndices(() => snapshot.value.entries.length)) {
|
|
5193
6031
|
const value = snapshot.value.entries[valueIndex];
|
|
5194
6032
|
if (value === void 0) continue;
|
|
5195
|
-
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));
|
|
5196
6034
|
admitMember(required, value);
|
|
5197
6035
|
}
|
|
5198
6036
|
}
|
|
5199
6037
|
const nested = [];
|
|
5200
|
-
|
|
5201
|
-
if (schema.
|
|
5202
|
-
|
|
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");
|
|
5203
6045
|
const propertyList = INTRINSICS.keys(schema.properties);
|
|
5204
6046
|
for (const propertyIndex of iterateIndices(() => propertyList.length)) {
|
|
5205
6047
|
const key = propertyList[propertyIndex];
|
|
5206
6048
|
if (key === void 0) continue;
|
|
5207
|
-
|
|
6049
|
+
edges[nested.length] = ["properties", key];
|
|
6050
|
+
nested[nested.length] = INTRINSICS.own(schema.properties, key) ? schema.properties[key] : void 0;
|
|
5208
6051
|
}
|
|
5209
6052
|
}
|
|
5210
|
-
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
|
+
}
|
|
5211
6057
|
const unionList = ["anyOf", "oneOf"];
|
|
5212
6058
|
for (const unionIndex of iterateIndices(() => unionList.length)) {
|
|
5213
6059
|
const key = unionList[unionIndex];
|
|
5214
6060
|
if (key === void 0) continue;
|
|
5215
|
-
const variants = schema[key];
|
|
6061
|
+
const variants = INTRINSICS.own(schema, key) ? schema[key] : void 0;
|
|
5216
6062
|
if (variants === void 0) continue;
|
|
5217
|
-
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);
|
|
5218
6064
|
const snapshot = readArrayEntries(variants);
|
|
5219
|
-
if (isFailure(snapshot) || snapshot.value.entries.length === 0) return this.#
|
|
5220
|
-
if (!snapshot.value.dense) return this.#
|
|
5221
|
-
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
|
+
}
|
|
5222
6071
|
}
|
|
5223
6072
|
stack[stack.length] = {
|
|
5224
6073
|
operation: "exit",
|
|
5225
6074
|
schema,
|
|
5226
|
-
nested
|
|
6075
|
+
nested,
|
|
6076
|
+
...first === void 0 ? {} : { first },
|
|
6077
|
+
...second === void 0 ? {} : { second }
|
|
5227
6078
|
};
|
|
5228
6079
|
for (const offset of iterateIndices(() => nested.length)) {
|
|
5229
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;
|
|
5230
6084
|
stack[stack.length] = {
|
|
5231
6085
|
operation: "enter",
|
|
5232
|
-
schema: nested[index]
|
|
6086
|
+
schema: nested[index],
|
|
6087
|
+
...childFirst === void 0 ? {} : { first: childFirst },
|
|
6088
|
+
...childSecond === void 0 ? {} : { second: childSecond }
|
|
5233
6089
|
};
|
|
5234
6090
|
}
|
|
5235
6091
|
}
|
|
@@ -5246,6 +6102,7 @@ var ShapePass = class ShapePass {
|
|
|
5246
6102
|
#populate(current) {
|
|
5247
6103
|
switch (current.category) {
|
|
5248
6104
|
case "array":
|
|
6105
|
+
this.#spend(1);
|
|
5249
6106
|
this.#children[this.#children.length] = {
|
|
5250
6107
|
shape: current.items,
|
|
5251
6108
|
first: "items",
|
|
@@ -5259,6 +6116,7 @@ var ShapePass = class ShapePass {
|
|
|
5259
6116
|
for (const keyIndex of iterateIndices(() => keys.length)) {
|
|
5260
6117
|
const key = keys[keyIndex];
|
|
5261
6118
|
if (key === void 0) continue;
|
|
6119
|
+
this.#spend(1);
|
|
5262
6120
|
const descriptorOutcome = attempt(() => INTRINSICS.describe(properties, key));
|
|
5263
6121
|
if (isFailure(descriptorOutcome)) return this.#refuse("validateShape: every node must be a recognized shape", "properties", key);
|
|
5264
6122
|
const descriptor = descriptorOutcome.value;
|
|
@@ -5271,10 +6129,16 @@ var ShapePass = class ShapePass {
|
|
|
5271
6129
|
};
|
|
5272
6130
|
continue;
|
|
5273
6131
|
}
|
|
5274
|
-
const childOutcome = attempt(() =>
|
|
6132
|
+
const childOutcome = attempt(() => {
|
|
6133
|
+
const population = current.properties;
|
|
6134
|
+
return isRecord(population) ? population[key] : void 0;
|
|
6135
|
+
});
|
|
5275
6136
|
if (isFailure(childOutcome)) return this.#refuse("validateShape: every node must be a recognized shape", "properties", key);
|
|
5276
6137
|
const child = childOutcome.value;
|
|
5277
|
-
const stable = attempt(() =>
|
|
6138
|
+
const stable = attempt(() => {
|
|
6139
|
+
const population = current.properties;
|
|
6140
|
+
return isRecord(population) && INTRINSICS.same(population[key], child) && INTRINSICS.same(descriptor.value, child);
|
|
6141
|
+
});
|
|
5278
6142
|
if (isFailure(stable)) return this.#refuse("validateShape: every node must be a recognized shape", "properties", key);
|
|
5279
6143
|
if (!stable.value) {
|
|
5280
6144
|
this.#children[this.#children.length] = {
|
|
@@ -5293,11 +6157,15 @@ var ShapePass = class ShapePass {
|
|
|
5293
6157
|
};
|
|
5294
6158
|
}
|
|
5295
6159
|
const extra = current.additionalProperties;
|
|
5296
|
-
|
|
5297
|
-
|
|
5298
|
-
|
|
5299
|
-
|
|
5300
|
-
|
|
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
|
+
}
|
|
5301
6169
|
break;
|
|
5302
6170
|
}
|
|
5303
6171
|
case "union": {
|
|
@@ -5316,6 +6184,7 @@ var ShapePass = class ShapePass {
|
|
|
5316
6184
|
});
|
|
5317
6185
|
for (const index of iterateIndices(() => length)) {
|
|
5318
6186
|
const key = INTRINSICS.text(index);
|
|
6187
|
+
this.#spend(1);
|
|
5319
6188
|
const descriptorOutcome = attempt(() => INTRINSICS.describe(current.variants, key));
|
|
5320
6189
|
if (isFailure(descriptorOutcome)) return this.#refuse("validateShape: every node must be a recognized shape", "variants", key);
|
|
5321
6190
|
const descriptor = descriptorOutcome.value;
|
|
@@ -5329,7 +6198,10 @@ var ShapePass = class ShapePass {
|
|
|
5329
6198
|
continue;
|
|
5330
6199
|
}
|
|
5331
6200
|
const variant = variants[index];
|
|
5332
|
-
const stable = attempt(() =>
|
|
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
|
+
});
|
|
5333
6205
|
if (isFailure(stable)) return this.#refuse("validateShape: every node must be a recognized shape", "variants", key);
|
|
5334
6206
|
if (!stable.value) {
|
|
5335
6207
|
this.#children[this.#children.length] = {
|
|
@@ -5371,7 +6243,10 @@ var ShapePass = class ShapePass {
|
|
|
5371
6243
|
const descriptor = descriptorOutcome.value;
|
|
5372
6244
|
if (descriptor === void 0 || !INTRINSICS.own(descriptor, "value")) return this.#refuse("validateShape: values must be a dense data array", "values", key);
|
|
5373
6245
|
const value = literals[index];
|
|
5374
|
-
const stable = attempt(() =>
|
|
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
|
+
});
|
|
5375
6250
|
if (isFailure(stable)) return this.#refuse("validateShape: every node must be a recognized shape", "values", key);
|
|
5376
6251
|
if (!stable.value) return this.#refuse("validateShape: values must be a stable data array", "values", key);
|
|
5377
6252
|
if (!isLiteralValue(value)) return this.#refuse("validateShape: every literal value must be a string, number, or boolean", "values", key);
|
|
@@ -5389,14 +6264,28 @@ var ShapePass = class ShapePass {
|
|
|
5389
6264
|
break;
|
|
5390
6265
|
}
|
|
5391
6266
|
case "optional":
|
|
5392
|
-
case "nullable":
|
|
5393
|
-
|
|
5394
|
-
|
|
5395
|
-
|
|
5396
|
-
|
|
6267
|
+
case "nullable":
|
|
6268
|
+
this.#spend(1);
|
|
6269
|
+
this.#children[this.#children.length] = {
|
|
6270
|
+
shape: current.inner,
|
|
6271
|
+
first: "inner",
|
|
6272
|
+
optional: false
|
|
6273
|
+
};
|
|
5397
6274
|
}
|
|
5398
6275
|
return true;
|
|
5399
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
|
+
}
|
|
5400
6289
|
#schedule(current, depth, segments) {
|
|
5401
6290
|
admitVisited(this.#active, current);
|
|
5402
6291
|
const children = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(this.#children), [(child) => child !== void 0]);
|
|
@@ -5415,7 +6304,7 @@ var ShapePass = class ShapePass {
|
|
|
5415
6304
|
depth: depth + 1,
|
|
5416
6305
|
optional: child.optional,
|
|
5417
6306
|
first: child.first,
|
|
5418
|
-
...child.second
|
|
6307
|
+
...INTRINSICS.own(child, "second") && child.second !== void 0 ? { second: child.second } : {}
|
|
5419
6308
|
};
|
|
5420
6309
|
}
|
|
5421
6310
|
return captured;
|
|
@@ -5426,25 +6315,27 @@ var ShapePass = class ShapePass {
|
|
|
5426
6315
|
shape,
|
|
5427
6316
|
category: this.#category,
|
|
5428
6317
|
children,
|
|
5429
|
-
raw: this.#raw
|
|
6318
|
+
raw: this.#raw,
|
|
6319
|
+
defaulted: this.#defaulted,
|
|
6320
|
+
open: this.#open
|
|
5430
6321
|
};
|
|
5431
6322
|
INTRINSICS.reflect.apply(INTRINSICS.retain, this.#index, [shape, index]);
|
|
5432
6323
|
return index;
|
|
5433
6324
|
}
|
|
5434
6325
|
#locate(shape) {
|
|
5435
|
-
if (shape ===
|
|
6326
|
+
if (typeof shape !== "object" || shape === null) return void 0;
|
|
5436
6327
|
return INTRINSICS.reflect.apply(INTRINSICS.recall, this.#index, [shape]);
|
|
5437
6328
|
}
|
|
5438
6329
|
#measure() {
|
|
6330
|
+
if (this.#cycle !== void 0) {
|
|
6331
|
+
this.#finish();
|
|
6332
|
+
return;
|
|
6333
|
+
}
|
|
5439
6334
|
for (const index of iterateIndices(() => this.#captures.length)) {
|
|
5440
6335
|
this.#height[index] = 0;
|
|
5441
6336
|
this.#reach[index] = 0;
|
|
5442
6337
|
this.#counts[index] = 1;
|
|
5443
6338
|
}
|
|
5444
|
-
if (this.#cycle !== void 0) {
|
|
5445
|
-
this.#trace();
|
|
5446
|
-
return;
|
|
5447
|
-
}
|
|
5448
6339
|
for (const step of iterateIndices(() => this.#post.length)) {
|
|
5449
6340
|
const index = this.#post[step];
|
|
5450
6341
|
if (index === void 0) continue;
|
|
@@ -5482,7 +6373,7 @@ var ShapePass = class ShapePass {
|
|
|
5482
6373
|
this.#via[located] = {
|
|
5483
6374
|
parent: index,
|
|
5484
6375
|
first: child.first,
|
|
5485
|
-
...child.second
|
|
6376
|
+
...INTRINSICS.own(child, "second") && child.second !== void 0 ? { second: child.second } : {}
|
|
5486
6377
|
};
|
|
5487
6378
|
}
|
|
5488
6379
|
}
|
|
@@ -5495,11 +6386,52 @@ var ShapePass = class ShapePass {
|
|
|
5495
6386
|
context: { path: pathOf(this.#ascend(index), "schema") }
|
|
5496
6387
|
});
|
|
5497
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);
|
|
5498
6430
|
}
|
|
5499
6431
|
#ascend(index) {
|
|
5500
6432
|
if (index === 0) return [];
|
|
5501
|
-
const edge = this.#via[index];
|
|
5502
|
-
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);
|
|
5503
6435
|
}
|
|
5504
6436
|
#witness(index, depth, path) {
|
|
5505
6437
|
if (depth <= 512) {
|
|
@@ -5510,7 +6442,7 @@ var ShapePass = class ShapePass {
|
|
|
5510
6442
|
const reached = located === void 0 ? 0 : this.#height[located] ?? 0;
|
|
5511
6443
|
return depth + 1 + reached > 512;
|
|
5512
6444
|
}])];
|
|
5513
|
-
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));
|
|
5514
6446
|
}
|
|
5515
6447
|
throw new ContractError("validateShape: a shape exceeds the compilation depth limit", {
|
|
5516
6448
|
code: "depth",
|
|
@@ -5520,86 +6452,24 @@ var ShapePass = class ShapePass {
|
|
|
5520
6452
|
}
|
|
5521
6453
|
});
|
|
5522
6454
|
}
|
|
5523
|
-
#trace() {
|
|
5524
|
-
const settled = [];
|
|
5525
|
-
const order = [];
|
|
5526
|
-
const active = new ShapePass.#weakSet();
|
|
5527
|
-
const stack = [{
|
|
5528
|
-
operation: "enter",
|
|
5529
|
-
index: 0,
|
|
5530
|
-
depth: 0
|
|
5531
|
-
}];
|
|
5532
|
-
while (stack.length > 0) {
|
|
5533
|
-
const frame = stack.pop();
|
|
5534
|
-
if (frame === void 0) continue;
|
|
5535
|
-
if (frame.operation === "exit") {
|
|
5536
|
-
const capture = this.#captures[frame.index];
|
|
5537
|
-
if (capture !== void 0) omitVisited(active, capture.shape);
|
|
5538
|
-
if (this.#oldest >= (order[frame.index] ?? 0) && (settled[frame.index] ?? -1) < frame.depth) settled[frame.index] = frame.depth;
|
|
5539
|
-
if (frame.outer < this.#oldest) this.#oldest = frame.outer;
|
|
5540
|
-
this.#trail.length -= frame.segments;
|
|
5541
|
-
continue;
|
|
5542
|
-
}
|
|
5543
|
-
const segments = (frame.first === void 0 ? 0 : 1) + (frame.second === void 0 ? 0 : 1);
|
|
5544
|
-
if (frame.first !== void 0) this.#trail[this.#trail.length] = frame.first;
|
|
5545
|
-
if (frame.second !== void 0) this.#trail[this.#trail.length] = frame.second;
|
|
5546
|
-
if (frame.depth > 512) throw new ContractError("validateShape: a shape exceeds the compilation depth limit", {
|
|
5547
|
-
code: "depth",
|
|
5548
|
-
context: {
|
|
5549
|
-
path: pathOf(this.#trail),
|
|
5550
|
-
limit: 512
|
|
5551
|
-
}
|
|
5552
|
-
});
|
|
5553
|
-
const index = frame.index;
|
|
5554
|
-
const capture = index === void 0 ? void 0 : this.#captures[index];
|
|
5555
|
-
if (index === void 0 || capture === void 0) {
|
|
5556
|
-
this.#trail.length -= segments;
|
|
5557
|
-
continue;
|
|
5558
|
-
}
|
|
5559
|
-
if (matchesVisited(active, capture.shape)) {
|
|
5560
|
-
const blocker = order[index] ?? 0;
|
|
5561
|
-
if (blocker < this.#oldest) this.#oldest = blocker;
|
|
5562
|
-
this.#trail.length -= segments;
|
|
5563
|
-
continue;
|
|
5564
|
-
}
|
|
5565
|
-
const recorded = settled[index];
|
|
5566
|
-
if (recorded !== void 0 && frame.depth <= recorded) {
|
|
5567
|
-
this.#trail.length -= segments;
|
|
5568
|
-
continue;
|
|
5569
|
-
}
|
|
5570
|
-
admitVisited(active, capture.shape);
|
|
5571
|
-
order[index] = this.#clock;
|
|
5572
|
-
this.#clock += 1;
|
|
5573
|
-
stack[stack.length] = {
|
|
5574
|
-
operation: "exit",
|
|
5575
|
-
index,
|
|
5576
|
-
depth: frame.depth,
|
|
5577
|
-
segments,
|
|
5578
|
-
outer: this.#oldest
|
|
5579
|
-
};
|
|
5580
|
-
this.#oldest = this.#clock;
|
|
5581
|
-
for (const offset of iterateIndices(() => capture.children.length)) {
|
|
5582
|
-
const edge = capture.children.length - 1 - offset;
|
|
5583
|
-
const child = capture.children[edge];
|
|
5584
|
-
if (child === void 0) continue;
|
|
5585
|
-
stack[stack.length] = {
|
|
5586
|
-
operation: "enter",
|
|
5587
|
-
index: this.#locate(child.shape),
|
|
5588
|
-
depth: frame.depth + 1,
|
|
5589
|
-
first: child.first,
|
|
5590
|
-
...child.second === void 0 ? {} : { second: child.second }
|
|
5591
|
-
};
|
|
5592
|
-
}
|
|
5593
|
-
}
|
|
5594
|
-
}
|
|
5595
6455
|
#finish() {
|
|
5596
6456
|
if (this.#structure !== void 0) throw this.#structure;
|
|
5597
6457
|
if (this.#cycle !== void 0) throw this.#cycle;
|
|
5598
6458
|
if (this.#domain !== void 0) throw this.#domain;
|
|
5599
6459
|
}
|
|
5600
|
-
#
|
|
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) {
|
|
5601
6471
|
if (this.#structure === void 0) {
|
|
5602
|
-
const path = pathOf(
|
|
6472
|
+
const path = pathOf(prefix, first, second);
|
|
5603
6473
|
this.#structure = new ContractError(message, {
|
|
5604
6474
|
code: "structure",
|
|
5605
6475
|
context: { path }
|
|
@@ -5667,7 +6537,7 @@ var ShapeValidator = class ShapeValidator {
|
|
|
5667
6537
|
*/
|
|
5668
6538
|
validate() {
|
|
5669
6539
|
INTRINSICS.reflect.apply(INTRINSICS.sweep, this.#passes, [(pass) => pass.interrupt()]);
|
|
5670
|
-
const pass = new ShapePass(this.#source);
|
|
6540
|
+
const pass = new ShapePass(this.#source, { left: CLONE_NODE_LIMIT });
|
|
5671
6541
|
INTRINSICS.reflect.apply(INTRINSICS.admit, this.#passes, [pass]);
|
|
5672
6542
|
this.#expansion = void 0;
|
|
5673
6543
|
try {
|
|
@@ -5770,6 +6640,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5770
6640
|
return this.#settle(failure(error));
|
|
5771
6641
|
}
|
|
5772
6642
|
#execute() {
|
|
6643
|
+
const budget = { left: CLONE_NODE_LIMIT };
|
|
5773
6644
|
const paths = this.#paths;
|
|
5774
6645
|
const memo = this.#memo;
|
|
5775
6646
|
if (paths === void 0 || memo === void 0) throw this.#unavailable();
|
|
@@ -5778,7 +6649,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5778
6649
|
shape: this.#source,
|
|
5779
6650
|
depth: 0
|
|
5780
6651
|
};
|
|
5781
|
-
this.#drain();
|
|
6652
|
+
this.#drain(budget);
|
|
5782
6653
|
this.#wireNodes();
|
|
5783
6654
|
const root = INTRINSICS.reflect.apply(INTRINSICS.fetch, memo, [this.#source]);
|
|
5784
6655
|
if (root === void 0) throw this.#create("ShapeCloner.clone: captured root is unavailable", {
|
|
@@ -5790,19 +6661,19 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5790
6661
|
if (this.#fidelity !== void 0) throw this.#fidelity;
|
|
5791
6662
|
return root;
|
|
5792
6663
|
}
|
|
5793
|
-
#drain() {
|
|
6664
|
+
#drain(budget) {
|
|
5794
6665
|
const memo = this.#memo;
|
|
5795
6666
|
if (memo === void 0) throw this.#unavailable();
|
|
5796
6667
|
for (const cursor of iterateIndices(() => this.#pending.length)) {
|
|
5797
6668
|
const entry = this.#pending[cursor];
|
|
5798
6669
|
if (entry === void 0) continue;
|
|
5799
6670
|
if (INTRINSICS.reflect.apply(INTRINSICS.keyed, memo, [entry.shape])) continue;
|
|
5800
|
-
if (entry.depth > 512) this.#refuseDepth();
|
|
5801
|
-
this.#captureNode(entry.shape, entry.depth);
|
|
6671
|
+
if (entry.depth > 512) this.#refuseDepth(budget);
|
|
6672
|
+
this.#captureNode(entry.shape, entry.depth, budget);
|
|
5802
6673
|
}
|
|
5803
6674
|
}
|
|
5804
|
-
#refuseDepth() {
|
|
5805
|
-
this.#validateShape(this.#source);
|
|
6675
|
+
#refuseDepth(budget) {
|
|
6676
|
+
this.#validateShape(this.#source, [], budget);
|
|
5806
6677
|
throw this.#create("validateShape: a shape exceeds the compilation depth limit", {
|
|
5807
6678
|
code: "depth",
|
|
5808
6679
|
context: {
|
|
@@ -5811,7 +6682,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5811
6682
|
}
|
|
5812
6683
|
});
|
|
5813
6684
|
}
|
|
5814
|
-
#captureShell(source, path) {
|
|
6685
|
+
#captureShell(source, path, budget) {
|
|
5815
6686
|
if (!matchesRecordBrand(source)) throw this.#create("validateShape: every structural child must be a shape", {
|
|
5816
6687
|
code: "structure",
|
|
5817
6688
|
context: { path }
|
|
@@ -5833,13 +6704,14 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5833
6704
|
case "boolean": return this.#captureSimple(source, path, "boolean");
|
|
5834
6705
|
case "null": return this.#captureSimple(source, path, "null");
|
|
5835
6706
|
case "literal": return this.#captureLiteral(source, path);
|
|
5836
|
-
case "array": return this.#captureArray(source, path);
|
|
5837
|
-
case "object": return this.#captureObject(source, path);
|
|
5838
|
-
case "union": return this.#captureUnion(source, path);
|
|
5839
|
-
case "optional": return this.#
|
|
5840
|
-
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);
|
|
5841
6712
|
case "json": return this.#captureSimple(source, path, "json");
|
|
5842
|
-
case "
|
|
6713
|
+
case "shape": return this.#captureSimple(source, path, "shape");
|
|
6714
|
+
case "raw": return this.#captureRaw(source, path, budget);
|
|
5843
6715
|
default: throw this.#create("validateShape: every node must be a recognized shape", {
|
|
5844
6716
|
code: "structure",
|
|
5845
6717
|
context: { path }
|
|
@@ -5927,12 +6799,12 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5927
6799
|
flags
|
|
5928
6800
|
};
|
|
5929
6801
|
}
|
|
5930
|
-
#captureNode(source, depth) {
|
|
6802
|
+
#captureNode(source, depth, budget) {
|
|
5931
6803
|
const paths = this.#paths;
|
|
5932
6804
|
const memo = this.#memo;
|
|
5933
6805
|
if (paths === void 0 || memo === void 0) throw this.#unavailable();
|
|
5934
6806
|
const path = INTRINSICS.reflect.apply(INTRINSICS.fetch, paths, [source]) ?? [];
|
|
5935
|
-
const clone = this.#captureShell(source, path);
|
|
6807
|
+
const clone = this.#captureShell(source, path, budget);
|
|
5936
6808
|
INTRINSICS.reflect.apply(INTRINSICS.store, memo, [source, clone]);
|
|
5937
6809
|
this.#sources[this.#sources.length] = source;
|
|
5938
6810
|
this.#scheduleNode(source, clone, path, depth);
|
|
@@ -5981,6 +6853,10 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5981
6853
|
category: "null",
|
|
5982
6854
|
...description === void 0 ? {} : { description }
|
|
5983
6855
|
};
|
|
6856
|
+
if (category === "shape") return {
|
|
6857
|
+
category: "shape",
|
|
6858
|
+
...description === void 0 ? {} : { description }
|
|
6859
|
+
};
|
|
5984
6860
|
return {
|
|
5985
6861
|
category: "json",
|
|
5986
6862
|
...description === void 0 ? {} : { description }
|
|
@@ -5995,7 +6871,8 @@ var ShapeCloner = class ShapeCloner {
|
|
|
5995
6871
|
...description === void 0 ? {} : { description }
|
|
5996
6872
|
};
|
|
5997
6873
|
}
|
|
5998
|
-
#captureArray(source, path) {
|
|
6874
|
+
#captureArray(source, path, budget) {
|
|
6875
|
+
this.#spend(budget, 1, path);
|
|
5999
6876
|
const items = this.#captureField(source, "items", path);
|
|
6000
6877
|
const min = this.#captureField(source, "min", path);
|
|
6001
6878
|
const max = this.#captureField(source, "max", path);
|
|
@@ -6012,7 +6889,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
6012
6889
|
...description === void 0 ? {} : { description }
|
|
6013
6890
|
};
|
|
6014
6891
|
}
|
|
6015
|
-
#captureObject(source, path) {
|
|
6892
|
+
#captureObject(source, path, budget) {
|
|
6016
6893
|
const propertySource = this.#captureField(source, "properties", path);
|
|
6017
6894
|
const additional = this.#captureField(source, "additionalProperties", path);
|
|
6018
6895
|
const description = this.#captureField(source, "description", path);
|
|
@@ -6020,7 +6897,8 @@ var ShapeCloner = class ShapeCloner {
|
|
|
6020
6897
|
code: "structure",
|
|
6021
6898
|
context: { path: pathOf(path, "properties") }
|
|
6022
6899
|
});
|
|
6023
|
-
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);
|
|
6024
6902
|
return {
|
|
6025
6903
|
category: "object",
|
|
6026
6904
|
properties: INTRINSICS.create(null),
|
|
@@ -6028,11 +6906,11 @@ var ShapeCloner = class ShapeCloner {
|
|
|
6028
6906
|
...description === void 0 ? {} : { description }
|
|
6029
6907
|
};
|
|
6030
6908
|
}
|
|
6031
|
-
#captureUnion(source, path) {
|
|
6909
|
+
#captureUnion(source, path, budget) {
|
|
6032
6910
|
const variantSource = this.#captureField(source, "variants", path);
|
|
6033
6911
|
const mode = this.#captureField(source, "mode", path);
|
|
6034
6912
|
const description = this.#captureField(source, "description", path);
|
|
6035
|
-
this.#captureVariants(source, variantSource, path);
|
|
6913
|
+
this.#captureVariants(source, variantSource, path, budget);
|
|
6036
6914
|
return {
|
|
6037
6915
|
category: "union",
|
|
6038
6916
|
variants: [],
|
|
@@ -6040,21 +6918,41 @@ var ShapeCloner = class ShapeCloner {
|
|
|
6040
6918
|
...description === void 0 ? {} : { description }
|
|
6041
6919
|
};
|
|
6042
6920
|
}
|
|
6043
|
-
#
|
|
6044
|
-
const inner = this.#
|
|
6045
|
-
|
|
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", {
|
|
6046
6931
|
code: "structure",
|
|
6047
|
-
context: { path: pathOf(path, "
|
|
6932
|
+
context: { path: pathOf(path, "default") }
|
|
6048
6933
|
});
|
|
6049
|
-
return
|
|
6934
|
+
return {
|
|
6050
6935
|
category: "optional",
|
|
6051
|
-
inner
|
|
6052
|
-
|
|
6936
|
+
inner,
|
|
6937
|
+
default: owned.value
|
|
6938
|
+
};
|
|
6939
|
+
}
|
|
6940
|
+
#captureNullable(source, path, budget) {
|
|
6941
|
+
return {
|
|
6053
6942
|
category: "nullable",
|
|
6054
|
-
inner
|
|
6943
|
+
inner: this.#captureInner(source, path, budget)
|
|
6055
6944
|
};
|
|
6056
6945
|
}
|
|
6057
|
-
#
|
|
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) {
|
|
6058
6956
|
const schema = this.#captureField(source, "schema", path);
|
|
6059
6957
|
if (!isRecord(schema)) throw this.#create("validateShape: raw schema must be a plain record", {
|
|
6060
6958
|
code: "structure",
|
|
@@ -6063,10 +6961,10 @@ var ShapeCloner = class ShapeCloner {
|
|
|
6063
6961
|
this.#validateShape({
|
|
6064
6962
|
category: "raw",
|
|
6065
6963
|
schema
|
|
6066
|
-
});
|
|
6964
|
+
}, path, budget);
|
|
6067
6965
|
const outcome = attempt(() => new SchemaCloner(schema).clone());
|
|
6068
6966
|
if (isFailure(outcome)) {
|
|
6069
|
-
if (isContractError(outcome.error))
|
|
6967
|
+
if (isContractError(outcome.error)) throw this.#prefix(outcome.error, pathOf(path, "schema"));
|
|
6070
6968
|
throw outcome.error;
|
|
6071
6969
|
}
|
|
6072
6970
|
return {
|
|
@@ -6109,9 +7007,10 @@ var ShapeCloner = class ShapeCloner {
|
|
|
6109
7007
|
});
|
|
6110
7008
|
return value;
|
|
6111
7009
|
}
|
|
6112
|
-
#captureProperties(source, properties, path) {
|
|
7010
|
+
#captureProperties(source, properties, path, budget) {
|
|
6113
7011
|
const keys = INTRINSICS.keys(properties);
|
|
6114
7012
|
const snapshot = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(keys), [(key) => {
|
|
7013
|
+
this.#spend(budget, 1, path);
|
|
6115
7014
|
const descriptor = INTRINSICS.describe(properties, key);
|
|
6116
7015
|
if (descriptor === void 0 || !INTRINSICS.own(descriptor, "value")) throw this.#create("validateShape: every structural child must be a shape", {
|
|
6117
7016
|
code: "structure",
|
|
@@ -6138,7 +7037,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
6138
7037
|
if (captured === void 0) throw this.#unavailable();
|
|
6139
7038
|
INTRINSICS.reflect.apply(INTRINSICS.store, captured, [source, snapshot]);
|
|
6140
7039
|
}
|
|
6141
|
-
#captureVariants(source, variants, path) {
|
|
7040
|
+
#captureVariants(source, variants, path, budget) {
|
|
6142
7041
|
if (!INTRINSICS.array(variants)) throw this.#create("validateShape: variants must be a finite array", {
|
|
6143
7042
|
code: "structure",
|
|
6144
7043
|
context: { path: pathOf(path, "variants") }
|
|
@@ -6153,6 +7052,7 @@ var ShapeCloner = class ShapeCloner {
|
|
|
6153
7052
|
context: { path: pathOf(path, "variants") }
|
|
6154
7053
|
});
|
|
6155
7054
|
const entries = snapshot.value.entries;
|
|
7055
|
+
this.#spend(budget, entries.length, path);
|
|
6156
7056
|
const captured = this.#variants;
|
|
6157
7057
|
if (captured === void 0) throw this.#unavailable();
|
|
6158
7058
|
INTRINSICS.reflect.apply(INTRINSICS.store, captured, [source, entries]);
|
|
@@ -6336,12 +7236,40 @@ var ShapeCloner = class ShapeCloner {
|
|
|
6336
7236
|
if (clone !== void 0) INTRINSICS.freeze(clone);
|
|
6337
7237
|
}
|
|
6338
7238
|
}
|
|
6339
|
-
#validateShape(shape) {
|
|
6340
|
-
const outcome = attempt(() => new
|
|
7239
|
+
#validateShape(shape, path = [], budget) {
|
|
7240
|
+
const outcome = attempt(() => new ShapePass(shape, budget).validate());
|
|
6341
7241
|
if (isSuccess(outcome)) return;
|
|
6342
|
-
if (isContractError(outcome.error))
|
|
7242
|
+
if (isContractError(outcome.error)) throw this.#prefix(outcome.error, path);
|
|
6343
7243
|
throw outcome.error;
|
|
6344
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
|
+
}
|
|
6345
7273
|
#unavailable() {
|
|
6346
7274
|
return this.#create("ShapeCloner.clone: the capture state is unavailable", {
|
|
6347
7275
|
code: "clone",
|
|
@@ -6507,7 +7435,7 @@ function ownShape(shape) {
|
|
|
6507
7435
|
const frozen = holds(() => INTRINSICS.frozen(shape));
|
|
6508
7436
|
const fidelity = attempt(() => cloneShape(shape));
|
|
6509
7437
|
if (isSuccess(fidelity)) return fidelity.value;
|
|
6510
|
-
if (isContractError(fidelity.error) && fidelity.error.code !== "clone") throw fidelity.error;
|
|
7438
|
+
if (isContractError(fidelity.error) && (fidelity.error.code !== "clone" || fidelity.error.context?.limit === 262144)) throw fidelity.error;
|
|
6511
7439
|
if (!frozen) throw fidelity.error;
|
|
6512
7440
|
const validation = attempt(() => new ShapeValidator(shape).validate());
|
|
6513
7441
|
if (isFailure(validation) && isContractError(validation.error)) throw validation.error;
|
|
@@ -6984,8 +7912,9 @@ function matchOf(pattern) {
|
|
|
6984
7912
|
* refinements — `min` / `max` length and a `pattern`.
|
|
6985
7913
|
*
|
|
6986
7914
|
* @remarks
|
|
6987
|
-
* Composes {@link isString} with {@link
|
|
6988
|
-
*
|
|
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).
|
|
6989
7918
|
* When `min`, `max`, and `pattern` are all absent it returns the bare {@link isString} guard
|
|
6990
7919
|
* (the unconstrained fast path), so an unrefined string leaf pays no wrapping
|
|
6991
7920
|
* cost. The single source of the string refinement shared by the compiled guard
|
|
@@ -7012,14 +7941,13 @@ function stringOf(options) {
|
|
|
7012
7941
|
"max",
|
|
7013
7942
|
"pattern"
|
|
7014
7943
|
], "stringOf", "string");
|
|
7015
|
-
const min = safe
|
|
7016
|
-
const max = safe
|
|
7017
|
-
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;
|
|
7018
7947
|
if (source !== void 0 && !isRegExp(source)) throw new ContractError("stringOf: pattern must be a RegExp", { code: "pattern" });
|
|
7019
7948
|
const pattern = source === void 0 ? void 0 : ownPattern(source, "stringOf");
|
|
7020
7949
|
if (min === void 0 && max === void 0 && pattern === void 0) return isString;
|
|
7021
|
-
|
|
7022
|
-
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)));
|
|
7023
7951
|
}, "stringOf");
|
|
7024
7952
|
}
|
|
7025
7953
|
/**
|
|
@@ -7544,7 +8472,9 @@ function seededRandom(seed) {
|
|
|
7544
8472
|
* chain pays a hundred clones of shrinking graphs and a hundred validations —
|
|
7545
8473
|
* quadratic work for a linear declaration. Here ownership runs once, validation runs once over
|
|
7546
8474
|
* that owned result, and each unique node and structural edge is indexed once
|
|
7547
|
-
* 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
|
|
7548
8478
|
* postorder pass whose entries are keyed by node identity, so a shared child is
|
|
7549
8479
|
* compiled once however many parents point at it. The same asymmetry exists on
|
|
7550
8480
|
* the VALUE side and is answered the same way: `guard`, `auditor` and `reporter`
|
|
@@ -7556,7 +8486,9 @@ function seededRandom(seed) {
|
|
|
7556
8486
|
* getters after it replay. A getter builds its own family and no other, except
|
|
7557
8487
|
* that `parser`, `reporter` and `generator` build the guard plan when the graph
|
|
7558
8488
|
* holds a union, because a union's membership question IS a guard question in
|
|
7559
|
-
* 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.
|
|
7560
8492
|
*
|
|
7561
8493
|
* One terminal lifecycle covers preparation and every family. A failure settles
|
|
7562
8494
|
* the compiler: later getters rethrow that exact error, while an artifact
|
|
@@ -7776,10 +8708,15 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7776
8708
|
shape: "contract"
|
|
7777
8709
|
}
|
|
7778
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
|
+
}
|
|
7779
8718
|
const owned = ownShape(source);
|
|
7780
|
-
|
|
7781
|
-
validator.validate();
|
|
7782
|
-
refuseExpansion(validator.expansion);
|
|
8719
|
+
refuseExpansion(new ShapePass(owned).validate());
|
|
7783
8720
|
this.#discover(owned);
|
|
7784
8721
|
this.#source = void 0;
|
|
7785
8722
|
}
|
|
@@ -7808,7 +8745,9 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7808
8745
|
const shape = frame.shape;
|
|
7809
8746
|
if (INTRINSICS.reflect.apply(INTRINSICS.recall, known, [shape]) !== void 0) continue;
|
|
7810
8747
|
const index = this.#nodes.length;
|
|
7811
|
-
|
|
8748
|
+
const node = INTRINSICS.rebase({ ...shape }, null);
|
|
8749
|
+
this.#nodes[index] = node;
|
|
8750
|
+
INTRINSICS.reflect.apply(INTRINSICS.retain, known, [node, index]);
|
|
7812
8751
|
INTRINSICS.reflect.apply(INTRINSICS.retain, known, [shape, index]);
|
|
7813
8752
|
this.#stack[this.#stack.length] = {
|
|
7814
8753
|
operation: "exit",
|
|
@@ -7899,6 +8838,54 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7899
8838
|
for (const index of iterateIndices(() => this.#nodes.length)) if (this.#nodes[index]?.category === "union") return true;
|
|
7900
8839
|
return false;
|
|
7901
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
|
+
}
|
|
8888
|
+
}
|
|
7902
8889
|
#repeats(index) {
|
|
7903
8890
|
const category = this.#node(index).category;
|
|
7904
8891
|
if (category === "array" || category === "object" || category === "union") return true;
|
|
@@ -7910,10 +8897,64 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7910
8897
|
#trackFaults(plan) {
|
|
7911
8898
|
return (value, path) => EvaluationScope.run(plan, value, path, true);
|
|
7912
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
|
+
}
|
|
7913
8953
|
#buildSchema() {
|
|
7914
8954
|
const ready = this.#schema;
|
|
7915
8955
|
if (ready !== void 0) return ready;
|
|
7916
8956
|
this.#prepare();
|
|
8957
|
+
if (this.#defaults()) this.#buildGuard();
|
|
7917
8958
|
for (const step of iterateIndices(() => this.#order.length)) {
|
|
7918
8959
|
const index = this.#order[step];
|
|
7919
8960
|
if (index === void 0) continue;
|
|
@@ -7937,6 +8978,13 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7937
8978
|
#schemaOf(index) {
|
|
7938
8979
|
const owned = this.#node(index);
|
|
7939
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
|
+
});
|
|
7940
8988
|
case "string": {
|
|
7941
8989
|
const text = owned.pattern === void 0 ? void 0 : readPatternSource(owned.pattern);
|
|
7942
8990
|
if (owned.pattern !== void 0 && text === void 0) throw new ContractError("compileSchema: pattern source could not be read", {
|
|
@@ -7986,7 +9034,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
7986
9034
|
if (child === void 0) return [];
|
|
7987
9035
|
return [{
|
|
7988
9036
|
key,
|
|
7989
|
-
schema: this.#schemaAt(child
|
|
9037
|
+
schema: this.#schemaAt(child),
|
|
7990
9038
|
required: child.category !== "optional"
|
|
7991
9039
|
}];
|
|
7992
9040
|
}]);
|
|
@@ -8010,7 +9058,14 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8010
9058
|
...owned.description !== void 0 ? { description: owned.description } : {}
|
|
8011
9059
|
});
|
|
8012
9060
|
}
|
|
8013
|
-
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
|
+
}
|
|
8014
9069
|
case "nullable": return INTRINSICS.freeze({ anyOf: INTRINSICS.freeze([this.#schemaAt(owned.inner), INTRINSICS.freeze({ type: "null" })]) });
|
|
8015
9070
|
case "raw": return cloneSchema(owned.schema);
|
|
8016
9071
|
}
|
|
@@ -8026,6 +9081,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8026
9081
|
this.#guards[index] = this.#repeats(index) ? this.#trackGuard(plan) : plan;
|
|
8027
9082
|
}
|
|
8028
9083
|
const root = this.#guardAt(this.#node(0));
|
|
9084
|
+
if (this.#defaults()) this.#checkDefaults(this.#node(0), []);
|
|
8029
9085
|
this.#guard = root;
|
|
8030
9086
|
return root;
|
|
8031
9087
|
}
|
|
@@ -8040,14 +9096,24 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8040
9096
|
});
|
|
8041
9097
|
return compiled;
|
|
8042
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
|
+
}
|
|
8043
9113
|
#guardOf(index) {
|
|
8044
9114
|
const owned = this.#node(index);
|
|
8045
9115
|
switch (owned.category) {
|
|
8046
|
-
case "string": return
|
|
8047
|
-
...owned.min === void 0 ? {} : { min: owned.min },
|
|
8048
|
-
...owned.max === void 0 ? {} : { max: owned.max },
|
|
8049
|
-
...owned.pattern === void 0 ? {} : { pattern: owned.pattern }
|
|
8050
|
-
});
|
|
9116
|
+
case "string": return this.#stringGuard(owned);
|
|
8051
9117
|
case "number": {
|
|
8052
9118
|
const base = owned.integer === true ? isInteger : isFiniteNumber;
|
|
8053
9119
|
if (owned.min === void 0 && owned.max === void 0) return base;
|
|
@@ -8056,6 +9122,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8056
9122
|
case "boolean": return isBoolean;
|
|
8057
9123
|
case "null": return isNull;
|
|
8058
9124
|
case "json": return isJSONValue;
|
|
9125
|
+
case "shape": return (value) => ContractCompiler.#acceptShape(value);
|
|
8059
9126
|
case "literal": return literalOf(owned.values);
|
|
8060
9127
|
case "array": {
|
|
8061
9128
|
const base = arrayOf(this.#guardAt(owned.items));
|
|
@@ -8132,7 +9199,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8132
9199
|
const ready = this.#parser;
|
|
8133
9200
|
if (ready !== void 0) return ready;
|
|
8134
9201
|
this.#prepare();
|
|
8135
|
-
if (this.#unions()) this.#buildGuard();
|
|
9202
|
+
if (this.#unions() || this.#defaults()) this.#buildGuard();
|
|
8136
9203
|
for (const step of iterateIndices(() => this.#order.length)) {
|
|
8137
9204
|
const index = this.#order[step];
|
|
8138
9205
|
if (index === void 0) continue;
|
|
@@ -8158,11 +9225,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8158
9225
|
switch (owned.category) {
|
|
8159
9226
|
case "string": {
|
|
8160
9227
|
if (owned.min === void 0 && owned.max === void 0 && owned.pattern === void 0) return parseString;
|
|
8161
|
-
const guard =
|
|
8162
|
-
...owned.min === void 0 ? {} : { min: owned.min },
|
|
8163
|
-
...owned.max === void 0 ? {} : { max: owned.max },
|
|
8164
|
-
...owned.pattern === void 0 ? {} : { pattern: owned.pattern }
|
|
8165
|
-
});
|
|
9228
|
+
const guard = this.#stringGuard(owned);
|
|
8166
9229
|
return (value) => {
|
|
8167
9230
|
const parsed = parseString(value);
|
|
8168
9231
|
return parsed !== void 0 && guard(parsed) ? parsed : void 0;
|
|
@@ -8180,6 +9243,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8180
9243
|
case "boolean": return parseBoolean;
|
|
8181
9244
|
case "null": return (value) => value === null ? null : void 0;
|
|
8182
9245
|
case "json": return parseJSONValue;
|
|
9246
|
+
case "shape": return (value) => ContractCompiler.#acceptShape(value) ? value : void 0;
|
|
8183
9247
|
case "literal": {
|
|
8184
9248
|
const allowed = literalOf(owned.values);
|
|
8185
9249
|
return (value) => {
|
|
@@ -8227,7 +9291,8 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8227
9291
|
return [{
|
|
8228
9292
|
key,
|
|
8229
9293
|
parse: this.#parserAt(inner),
|
|
8230
|
-
optional
|
|
9294
|
+
optional,
|
|
9295
|
+
fill: optional && INTRINSICS.own(child, "default") ? child.default : void 0
|
|
8231
9296
|
}];
|
|
8232
9297
|
}]);
|
|
8233
9298
|
const known = readValue(() => collectMembers(INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(entries), [(entry) => entry.key])), "compileParser", {
|
|
@@ -8251,14 +9316,14 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8251
9316
|
for (const entryIndex of iterateIndices(() => entries.length)) {
|
|
8252
9317
|
const entry = entries[entryIndex];
|
|
8253
9318
|
if (entry === void 0) continue;
|
|
8254
|
-
|
|
8255
|
-
if (entry.optional) continue;
|
|
8256
|
-
return;
|
|
8257
|
-
}
|
|
8258
|
-
const raw = record[entry.key];
|
|
9319
|
+
const raw = (present === void 0 ? (seen & 1 << entryIndex) === 0 : !matchesMember(present, entry.key)) ? void 0 : record[entry.key];
|
|
8259
9320
|
if (raw === void 0) {
|
|
8260
|
-
if (entry.optional)
|
|
8261
|
-
|
|
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;
|
|
8262
9327
|
}
|
|
8263
9328
|
const parsed = entry.parse(raw);
|
|
8264
9329
|
if (parsed === void 0) return void 0;
|
|
@@ -8292,12 +9357,19 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8292
9357
|
parse: this.#parserAt(variant),
|
|
8293
9358
|
guard: this.#guardAt(variant)
|
|
8294
9359
|
}]]);
|
|
9360
|
+
const discriminated = this.#discriminate(owned);
|
|
8295
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);
|
|
8296
9365
|
const matching = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(variants), [(variant) => variant.guard(value)]);
|
|
8297
9366
|
const only = matching[0];
|
|
8298
|
-
return matching.length === 1 && only !== void 0 ?
|
|
9367
|
+
return matching.length === 1 && only !== void 0 ? value : void 0;
|
|
8299
9368
|
};
|
|
8300
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);
|
|
8301
9373
|
for (const variantIndex of iterateIndices(() => variants.length)) {
|
|
8302
9374
|
const variant = variants[variantIndex];
|
|
8303
9375
|
if (variant === void 0) continue;
|
|
@@ -8326,6 +9398,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8326
9398
|
const ready = this.#auditor;
|
|
8327
9399
|
if (ready !== void 0) return ready;
|
|
8328
9400
|
this.#prepare();
|
|
9401
|
+
if (this.#defaults()) this.#buildGuard();
|
|
8329
9402
|
for (const step of iterateIndices(() => this.#order.length)) {
|
|
8330
9403
|
const index = this.#order[step];
|
|
8331
9404
|
if (index === void 0) continue;
|
|
@@ -8400,6 +9473,12 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8400
9473
|
expected: "json",
|
|
8401
9474
|
received: preview(value)
|
|
8402
9475
|
}];
|
|
9476
|
+
case "shape": return (value, path) => ContractCompiler.#acceptShape(value) ? [] : [{
|
|
9477
|
+
reason: "type",
|
|
9478
|
+
path,
|
|
9479
|
+
expected: "shape",
|
|
9480
|
+
received: preview(value)
|
|
9481
|
+
}];
|
|
8403
9482
|
case "literal": {
|
|
8404
9483
|
const allowed = literalOf(owned.values);
|
|
8405
9484
|
return (value, path) => allowed(value) ? [] : [{
|
|
@@ -8542,7 +9621,11 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8542
9621
|
const plans = INTRINSICS.reflect.apply(INTRINSICS.expand, readIndexView(owned.variants), [(variant) => variant === void 0 ? [] : [this.#auditAt(variant)]]);
|
|
8543
9622
|
const exclusive = owned.mode === "oneOf";
|
|
8544
9623
|
const count = owned.variants.length;
|
|
9624
|
+
const discriminated = this.#discriminate(owned);
|
|
8545
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);
|
|
8546
9629
|
const perVariant = [];
|
|
8547
9630
|
for (const index2 of iterateIndices(() => plans.length)) {
|
|
8548
9631
|
const plan = plans[index2];
|
|
@@ -8559,18 +9642,21 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8559
9642
|
if (matched > 1) return [{
|
|
8560
9643
|
reason: "oneOf",
|
|
8561
9644
|
path,
|
|
8562
|
-
matched
|
|
9645
|
+
matched,
|
|
9646
|
+
...discriminated?.hint
|
|
8563
9647
|
}];
|
|
8564
9648
|
}
|
|
8565
9649
|
const closest = selectClosestFaults(perVariant);
|
|
8566
9650
|
const report = [exclusive ? {
|
|
8567
9651
|
reason: "oneOf",
|
|
8568
9652
|
path,
|
|
8569
|
-
matched: 0
|
|
9653
|
+
matched: 0,
|
|
9654
|
+
...discriminated?.hint
|
|
8570
9655
|
} : {
|
|
8571
9656
|
reason: "variant",
|
|
8572
9657
|
path,
|
|
8573
|
-
variants: count
|
|
9658
|
+
variants: count,
|
|
9659
|
+
...discriminated?.hint
|
|
8574
9660
|
}];
|
|
8575
9661
|
appendEntries(report, closest);
|
|
8576
9662
|
return limitEntries(report, 64);
|
|
@@ -8596,7 +9682,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8596
9682
|
const ready = this.#reporter;
|
|
8597
9683
|
if (ready !== void 0) return ready;
|
|
8598
9684
|
this.#prepare();
|
|
8599
|
-
if (this.#unions()) this.#buildGuard();
|
|
9685
|
+
if (this.#unions() || this.#defaults()) this.#buildGuard();
|
|
8600
9686
|
for (const step of iterateIndices(() => this.#order.length)) {
|
|
8601
9687
|
const index = this.#order[step];
|
|
8602
9688
|
if (index === void 0) continue;
|
|
@@ -8682,6 +9768,12 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8682
9768
|
received: preview(value)
|
|
8683
9769
|
}];
|
|
8684
9770
|
}
|
|
9771
|
+
case "shape": return (value, path) => ContractCompiler.#acceptShape(value) ? [] : [{
|
|
9772
|
+
reason: "type",
|
|
9773
|
+
path,
|
|
9774
|
+
expected: "shape",
|
|
9775
|
+
received: preview(value)
|
|
9776
|
+
}];
|
|
8685
9777
|
case "array": {
|
|
8686
9778
|
const node = owned;
|
|
8687
9779
|
const item = this.#reportAt(owned.items);
|
|
@@ -8799,7 +9891,11 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8799
9891
|
const guards = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(variants), [(variant) => variant.guard]);
|
|
8800
9892
|
const exclusive = owned.mode === "oneOf";
|
|
8801
9893
|
const count = owned.variants.length;
|
|
9894
|
+
const discriminated = this.#discriminate(owned);
|
|
8802
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);
|
|
8803
9899
|
const perVariant = [];
|
|
8804
9900
|
for (const index2 of iterateIndices(() => plans.length)) {
|
|
8805
9901
|
const plan = plans[index2];
|
|
@@ -8816,7 +9912,8 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8816
9912
|
const report = [{
|
|
8817
9913
|
reason: "oneOf",
|
|
8818
9914
|
path,
|
|
8819
|
-
matched: 0
|
|
9915
|
+
matched: 0,
|
|
9916
|
+
...discriminated?.hint
|
|
8820
9917
|
}];
|
|
8821
9918
|
appendEntries(report, closest);
|
|
8822
9919
|
return limitEntries(report, 64);
|
|
@@ -8824,13 +9921,15 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8824
9921
|
return [{
|
|
8825
9922
|
reason: "oneOf",
|
|
8826
9923
|
path,
|
|
8827
|
-
matched
|
|
9924
|
+
matched,
|
|
9925
|
+
...discriminated?.hint
|
|
8828
9926
|
}];
|
|
8829
9927
|
}
|
|
8830
9928
|
const report = [{
|
|
8831
9929
|
reason: "variant",
|
|
8832
9930
|
path,
|
|
8833
|
-
variants: count
|
|
9931
|
+
variants: count,
|
|
9932
|
+
...discriminated?.hint
|
|
8834
9933
|
}];
|
|
8835
9934
|
appendEntries(report, closest);
|
|
8836
9935
|
return limitEntries(report, 64);
|
|
@@ -8856,7 +9955,7 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8856
9955
|
const ready = this.#generator;
|
|
8857
9956
|
if (ready !== void 0) return ready;
|
|
8858
9957
|
this.#prepare();
|
|
8859
|
-
if (this.#unions()) this.#buildGuard();
|
|
9958
|
+
if (this.#unions() || this.#defaults()) this.#buildGuard();
|
|
8860
9959
|
for (const step of iterateIndices(() => this.#order.length)) {
|
|
8861
9960
|
const index = this.#order[step];
|
|
8862
9961
|
if (index === void 0) continue;
|
|
@@ -8923,6 +10022,11 @@ var ContractCompiler = class ContractCompiler {
|
|
|
8923
10022
|
}
|
|
8924
10023
|
case "boolean": return (random) => drawRandom(random, "boolean") >= .5;
|
|
8925
10024
|
case "null": return () => null;
|
|
10025
|
+
case "shape": return () => ({
|
|
10026
|
+
category: "object",
|
|
10027
|
+
properties: {},
|
|
10028
|
+
additionalProperties: false
|
|
10029
|
+
});
|
|
8926
10030
|
case "json": return (random) => {
|
|
8927
10031
|
const pick = INTRINSICS.floor(drawRandom(random, "json") * 5);
|
|
8928
10032
|
if (pick === 0) return null;
|
|
@@ -9089,14 +10193,15 @@ var ContractCompiler = class ContractCompiler {
|
|
|
9089
10193
|
* eager well-formedness pass — there is no second prepass and no alias for it:
|
|
9090
10194
|
* it enforces every bound domain and range used by the artifacts, including
|
|
9091
10195
|
* non-empty literal/union vocabularies, finite literal numbers, integer-range
|
|
9092
|
-
* satisfiability, unflagged string patterns, optional-shape
|
|
9093
|
-
* recursively supported raw-schema vocabulary where every
|
|
9094
|
-
* 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.
|
|
9095
10200
|
* Active ancestors are tracked so shared children remain legal. Every
|
|
9096
10201
|
* standalone compiler reaches this same validation once, over its owned
|
|
9097
10202
|
* snapshot, through {@link ContractCompiler} preparation.
|
|
9098
|
-
*
|
|
9099
|
-
*
|
|
10203
|
+
* Cyclic graphs refuse before depth measurement, with structure before cycle.
|
|
10204
|
+
* Acyclic graphs report depth before structure, then field and vocabulary policy.
|
|
9100
10205
|
*
|
|
9101
10206
|
* @param shape - The shape graph to gate
|
|
9102
10207
|
* @returns Nothing; successful return means recursive compilation is structurally safe, depth-safe, and bounded in emitted nodes
|
|
@@ -9238,9 +10343,104 @@ function compileReporter(shape, value, path = []) {
|
|
|
9238
10343
|
function compileAuditor(shape, value, path = []) {
|
|
9239
10344
|
return contain(() => new ContractCompiler(shape).auditor(value, path), "compileAuditor");
|
|
9240
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
|
+
};
|
|
9241
10436
|
// src/core/factories.ts
|
|
9242
10437
|
function createContract(shape) {
|
|
9243
|
-
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");
|
|
9244
10444
|
}
|
|
9245
10445
|
// src/core/ValueInferer.ts
|
|
9246
10446
|
/**
|
|
@@ -9847,7 +11047,9 @@ function samplesToSchema(samples, options) {
|
|
|
9847
11047
|
* rather than parameters, so no caller can pre-populate either and change what
|
|
9848
11048
|
* the conversion widens. Construction observes nothing; `shape` runs the whole
|
|
9849
11049
|
* walk and every refusal it raises is published by that door under the door's
|
|
9850
|
-
* 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.
|
|
9851
11053
|
*
|
|
9852
11054
|
* The recursion is `#` private for the same reason it is bounded: it is the
|
|
9853
11055
|
* class's defining spine, and its state has no meaning outside one call. The
|
|
@@ -9863,73 +11065,314 @@ var SchemaShaper = class SchemaShaper {
|
|
|
9863
11065
|
static #weakSet = WeakSet;
|
|
9864
11066
|
static #weakMap = WeakMap;
|
|
9865
11067
|
#source;
|
|
11068
|
+
#strict;
|
|
9866
11069
|
#visited;
|
|
9867
11070
|
#memo;
|
|
9868
|
-
|
|
11071
|
+
#enclosed;
|
|
11072
|
+
#exclusive;
|
|
11073
|
+
#path;
|
|
11074
|
+
#oneOfs;
|
|
11075
|
+
#built;
|
|
11076
|
+
#shared;
|
|
11077
|
+
constructor(schema, strict = false) {
|
|
9869
11078
|
this.#source = schema;
|
|
11079
|
+
this.#strict = strict;
|
|
9870
11080
|
this.#visited = new SchemaShaper.#weakSet();
|
|
9871
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;
|
|
9872
11088
|
}
|
|
9873
11089
|
/**
|
|
9874
11090
|
* Converts the retained schema into a validating contract shape.
|
|
9875
11091
|
*
|
|
9876
|
-
* @returns The built shape, widened wherever
|
|
9877
|
-
* @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
|
|
9878
11094
|
*/
|
|
9879
11095
|
shape() {
|
|
9880
|
-
|
|
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;
|
|
9881
11099
|
}
|
|
9882
|
-
#
|
|
9883
|
-
if (!
|
|
9884
|
-
|
|
9885
|
-
|
|
9886
|
-
|
|
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
|
+
}
|
|
11126
|
+
}
|
|
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]);
|
|
9887
11133
|
const cached = depthMemo === void 0 ? void 0 : INTRINSICS.reflect.apply(INTRINSICS.fetch, depthMemo, [depth]);
|
|
9888
|
-
if (cached)
|
|
11134
|
+
if (cached) {
|
|
11135
|
+
this.#shared = true;
|
|
11136
|
+
return cached;
|
|
11137
|
+
}
|
|
9889
11138
|
admitVisited(this.#visited, schema);
|
|
9890
11139
|
try {
|
|
9891
|
-
|
|
9892
|
-
|
|
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);
|
|
9893
11144
|
return shape;
|
|
9894
11145
|
} finally {
|
|
9895
11146
|
omitVisited(this.#visited, schema);
|
|
9896
11147
|
}
|
|
9897
11148
|
}
|
|
9898
|
-
#
|
|
9899
|
-
|
|
9900
|
-
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)) {
|
|
9901
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]);
|
|
9902
|
-
|
|
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;
|
|
9903
11342
|
const pairs = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(valid), [(entry, index) => [`${typeof entry}:${INTRINSICS.text(entry)}`, index]]);
|
|
9904
11343
|
const reversed = INTRINSICS.reflect.apply(INTRINSICS.flip, pairs, []);
|
|
9905
11344
|
const first = INTRINSICS.record({ [Symbol.iterator]: INTRINSICS.reflect.apply(INTRINSICS.bind, INTRINSICS.iterator, [reversed]) });
|
|
9906
11345
|
const literals = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(valid), [(entry, index) => first[`${typeof entry}:${INTRINSICS.text(entry)}`] === index]);
|
|
9907
|
-
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 });
|
|
9908
11351
|
}
|
|
9909
|
-
if (isArray(schema.oneOf)) {
|
|
9910
|
-
const
|
|
9911
|
-
|
|
9912
|
-
if (records.length > 256) return rawShape(description === void 0 ? {} : { description });
|
|
9913
|
-
const variants = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(records), [(entry) => this.#convert(entry, depth - 1)]);
|
|
9914
|
-
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;
|
|
9915
11355
|
}
|
|
9916
|
-
if (isArray(schema.anyOf)) {
|
|
9917
|
-
const
|
|
9918
|
-
|
|
9919
|
-
if (records.length > 256) return rawShape(description === void 0 ? {} : { description });
|
|
9920
|
-
const variants = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(records), [(entry) => this.#convert(entry, depth - 1)]);
|
|
9921
|
-
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;
|
|
9922
11359
|
}
|
|
9923
|
-
const category = schema.type;
|
|
11360
|
+
const category = INTRINSICS.own(schema, "type") ? schema.type : void 0;
|
|
9924
11361
|
if (isArray(category)) {
|
|
9925
11362
|
const observed = INTRINSICS.from(parseEnumValues(category, JSON_SCHEMA_TYPES));
|
|
9926
|
-
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");
|
|
9927
11364
|
const members = INTRINSICS.reflect.apply(INTRINSICS.select, readIndexView(observed), [(member, index) => INTRINSICS.reflect.apply(INTRINSICS.find, observed, [(entry) => entry === member]) === index]);
|
|
9928
|
-
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));
|
|
9929
11372
|
const variants = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(members), [(member) => this.#build({
|
|
9930
11373
|
...schema,
|
|
9931
11374
|
type: member
|
|
9932
|
-
}, depth)]);
|
|
11375
|
+
}, depth, enclosed || members.length > 1)]);
|
|
9933
11376
|
const first = variants[0];
|
|
9934
11377
|
if (variants.length === 1 && first !== void 0) return first;
|
|
9935
11378
|
const union = INTRINSICS.reflect.apply(unionShape, void 0, variants);
|
|
@@ -9938,45 +11381,88 @@ var SchemaShaper = class SchemaShaper {
|
|
|
9938
11381
|
description
|
|
9939
11382
|
});
|
|
9940
11383
|
}
|
|
9941
|
-
const type = isString(category) ? schema.type : void 0;
|
|
9942
|
-
if (type === "string")
|
|
9943
|
-
|
|
9944
|
-
|
|
9945
|
-
|
|
9946
|
-
|
|
9947
|
-
|
|
9948
|
-
|
|
9949
|
-
|
|
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
|
+
}
|
|
9950
11399
|
if (type === "integer") {
|
|
9951
|
-
const bounds = deriveRangeBounds(schema.minimum, schema.maximum);
|
|
9952
|
-
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({
|
|
9953
11402
|
...bounds,
|
|
9954
11403
|
...description === void 0 ? {} : { description }
|
|
9955
|
-
});
|
|
11404
|
+
}, null));
|
|
9956
11405
|
}
|
|
9957
11406
|
if (type === "boolean") return booleanShape(description === void 0 ? void 0 : { description });
|
|
9958
11407
|
if (type === "null") return nullShape(description === void 0 ? void 0 : { description });
|
|
9959
|
-
if (type === "array")
|
|
9960
|
-
|
|
9961
|
-
|
|
9962
|
-
|
|
9963
|
-
|
|
9964
|
-
|
|
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;
|
|
9965
11445
|
}
|
|
9966
|
-
#buildObject(schema, depth, description) {
|
|
9967
|
-
|
|
9968
|
-
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]) : [];
|
|
9969
11450
|
const allKeys = propertiesSource === void 0 ? [] : INTRINSICS.keys(propertiesSource);
|
|
9970
11451
|
const truncated = allKeys.length > 256;
|
|
11452
|
+
if (this.#strict && truncated) throw this.#refusal("properties exceeds the breadth limit", "properties");
|
|
9971
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);
|
|
9972
11457
|
const pairs = INTRINSICS.reflect.apply(INTRINSICS.project, readIndexView(keys), [(key) => {
|
|
9973
|
-
const child = propertiesSource
|
|
9974
|
-
const childShape = isRecord(child) ? this.#
|
|
9975
|
-
|
|
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];
|
|
9976
11463
|
}]);
|
|
9977
11464
|
const properties = INTRINSICS.rebase(INTRINSICS.record({ [Symbol.iterator]: INTRINSICS.reflect.apply(INTRINSICS.bind, INTRINSICS.iterator, [pairs]) }), null);
|
|
9978
|
-
const
|
|
9979
|
-
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;
|
|
9980
11466
|
return INTRINSICS.freeze({
|
|
9981
11467
|
category: "object",
|
|
9982
11468
|
properties: INTRINSICS.freeze(properties),
|
|
@@ -9984,9 +11470,115 @@ var SchemaShaper = class SchemaShaper {
|
|
|
9984
11470
|
...description === void 0 ? {} : { description }
|
|
9985
11471
|
});
|
|
9986
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
|
+
}
|
|
9987
11564
|
};
|
|
9988
11565
|
// src/core/shapers.ts
|
|
9989
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
|
+
/**
|
|
9990
11582
|
* Builds a string {@link StringShape}.
|
|
9991
11583
|
*
|
|
9992
11584
|
* @remarks
|
|
@@ -10063,8 +11655,9 @@ function stringShape(options) {
|
|
|
10063
11655
|
...patternSnapshot === void 0 ? {} : { pattern: new INTRINSICS.pattern(patternSnapshot.source) },
|
|
10064
11656
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10065
11657
|
};
|
|
11658
|
+
if (patternSnapshot === void 0 && !ShapePass.polluted()) return ShapePass.seal(shape);
|
|
10066
11659
|
new ShapeValidator(shape).validate();
|
|
10067
|
-
return cloneShape(shape);
|
|
11660
|
+
return ShapePass.seal(cloneShape(shape));
|
|
10068
11661
|
}, "stringShape");
|
|
10069
11662
|
}
|
|
10070
11663
|
/**
|
|
@@ -10116,8 +11709,7 @@ function numberShape(options) {
|
|
|
10116
11709
|
...safe?.integer === void 0 ? {} : { integer: safe.integer },
|
|
10117
11710
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10118
11711
|
};
|
|
10119
|
-
|
|
10120
|
-
return INTRINSICS.freeze(result);
|
|
11712
|
+
return ShapePass.seal(result);
|
|
10121
11713
|
}, "numberShape");
|
|
10122
11714
|
}
|
|
10123
11715
|
/**
|
|
@@ -10161,8 +11753,7 @@ function booleanShape(options) {
|
|
|
10161
11753
|
category: "boolean",
|
|
10162
11754
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10163
11755
|
};
|
|
10164
|
-
|
|
10165
|
-
return INTRINSICS.freeze(shape);
|
|
11756
|
+
return ShapePass.seal(shape);
|
|
10166
11757
|
}, "booleanShape");
|
|
10167
11758
|
}
|
|
10168
11759
|
/**
|
|
@@ -10183,8 +11774,7 @@ function nullShape(options) {
|
|
|
10183
11774
|
category: "null",
|
|
10184
11775
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10185
11776
|
};
|
|
10186
|
-
|
|
10187
|
-
return INTRINSICS.freeze(shape);
|
|
11777
|
+
return ShapePass.seal(shape);
|
|
10188
11778
|
}, "nullShape");
|
|
10189
11779
|
}
|
|
10190
11780
|
function literalShape(values, options) {
|
|
@@ -10232,8 +11822,7 @@ function literalShape(values, options) {
|
|
|
10232
11822
|
values: INTRINSICS.freeze(literals),
|
|
10233
11823
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10234
11824
|
};
|
|
10235
|
-
|
|
10236
|
-
return INTRINSICS.freeze(shape);
|
|
11825
|
+
return ShapePass.seal(shape);
|
|
10237
11826
|
}, "literalShape");
|
|
10238
11827
|
}
|
|
10239
11828
|
/**
|
|
@@ -10279,8 +11868,7 @@ function arrayShape(items, options) {
|
|
|
10279
11868
|
...safe?.max === void 0 ? {} : { max: safe.max },
|
|
10280
11869
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10281
11870
|
};
|
|
10282
|
-
|
|
10283
|
-
return INTRINSICS.freeze(shape);
|
|
11871
|
+
return ShapePass.seal(shape);
|
|
10284
11872
|
}, "arrayShape");
|
|
10285
11873
|
}
|
|
10286
11874
|
/**
|
|
@@ -10350,8 +11938,7 @@ function objectShape(properties, options) {
|
|
|
10350
11938
|
...safe?.additionalProperties === void 0 ? {} : { additionalProperties: safe.additionalProperties },
|
|
10351
11939
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10352
11940
|
};
|
|
10353
|
-
|
|
10354
|
-
return INTRINSICS.freeze(shape);
|
|
11941
|
+
return ShapePass.seal(shape);
|
|
10355
11942
|
}, "objectShape");
|
|
10356
11943
|
}
|
|
10357
11944
|
/**
|
|
@@ -10388,13 +11975,19 @@ function recordShape(values, options) {
|
|
|
10388
11975
|
additionalProperties: values,
|
|
10389
11976
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10390
11977
|
};
|
|
10391
|
-
|
|
10392
|
-
return INTRINSICS.freeze(shape);
|
|
11978
|
+
return ShapePass.seal(shape);
|
|
10393
11979
|
}, "recordShape");
|
|
10394
11980
|
}
|
|
10395
11981
|
/**
|
|
10396
11982
|
* Builds a {@link UnionShape} from a list of variant shapes (`anyOf` in JSON Schema).
|
|
10397
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
|
+
*
|
|
10398
11991
|
* @param variants - The candidate shapes; the first match wins at runtime
|
|
10399
11992
|
* @returns A union shape whose `Infer` is the union of the variants
|
|
10400
11993
|
*
|
|
@@ -10406,11 +11999,7 @@ function recordShape(values, options) {
|
|
|
10406
11999
|
*/
|
|
10407
12000
|
function unionShape(...variants) {
|
|
10408
12001
|
return contain(() => {
|
|
10409
|
-
|
|
10410
|
-
category: "union",
|
|
10411
|
-
variants
|
|
10412
|
-
}).validate();
|
|
10413
|
-
return INTRINSICS.freeze({
|
|
12002
|
+
return ShapePass.seal({
|
|
10414
12003
|
category: "union",
|
|
10415
12004
|
variants: INTRINSICS.freeze(variants)
|
|
10416
12005
|
});
|
|
@@ -10429,9 +12018,11 @@ function unionShape(...variants) {
|
|
|
10429
12018
|
* {@link unionShape}'s guard.
|
|
10430
12019
|
* - **Parser**: judged on the RAW input's guard matches only, with NO
|
|
10431
12020
|
* coercion fallback for an ambiguous input. When exactly one variant's
|
|
10432
|
-
* guard accepts the raw value,
|
|
12021
|
+
* guard accepts the raw value, the input itself is returned. Zero matches or
|
|
10433
12022
|
* two-or-more matches both parse to `undefined` — a value ambiguous
|
|
10434
|
-
* 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.
|
|
10435
12026
|
*
|
|
10436
12027
|
* Prefer {@link unionShape} when a value may legitimately satisfy more than
|
|
10437
12028
|
* one variant (for example overlapping shapes) and any match is acceptable. Prefer
|
|
@@ -10450,15 +12041,10 @@ function unionShape(...variants) {
|
|
|
10450
12041
|
*/
|
|
10451
12042
|
function oneOfShape(...variants) {
|
|
10452
12043
|
return contain(() => {
|
|
10453
|
-
|
|
12044
|
+
return ShapePass.seal({
|
|
10454
12045
|
category: "union",
|
|
10455
|
-
variants,
|
|
12046
|
+
variants: INTRINSICS.freeze(variants),
|
|
10456
12047
|
mode: "oneOf"
|
|
10457
|
-
};
|
|
10458
|
-
new ShapeValidator(shape).validate();
|
|
10459
|
-
return INTRINSICS.freeze({
|
|
10460
|
-
...shape,
|
|
10461
|
-
variants: INTRINSICS.freeze(variants)
|
|
10462
12048
|
});
|
|
10463
12049
|
}, "oneOfShape");
|
|
10464
12050
|
}
|
|
@@ -10467,22 +12053,55 @@ function oneOfShape(...variants) {
|
|
|
10467
12053
|
*
|
|
10468
12054
|
* @remarks
|
|
10469
12055
|
* As an {@link objectShape} property, the field becomes a true optional property
|
|
10470
|
-
* 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.
|
|
10471
12061
|
*
|
|
10472
12062
|
* @param inner - The wrapped shape
|
|
12063
|
+
* @param options - Optional `default`, a JSON value
|
|
10473
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
|
+
* ```
|
|
10474
12071
|
*/
|
|
10475
|
-
function optionalShape(inner) {
|
|
12072
|
+
function optionalShape(inner, options) {
|
|
10476
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
|
+
});
|
|
10477
12096
|
const shape = {
|
|
10478
12097
|
category: "optional",
|
|
10479
|
-
inner
|
|
12098
|
+
inner,
|
|
12099
|
+
...owned === void 0 ? {} : { default: owned.value }
|
|
10480
12100
|
};
|
|
10481
|
-
|
|
12101
|
+
return ShapePass.seal(shape, {
|
|
10482
12102
|
category: "object",
|
|
10483
12103
|
properties: { value: shape }
|
|
10484
|
-
})
|
|
10485
|
-
return INTRINSICS.freeze(shape);
|
|
12104
|
+
});
|
|
10486
12105
|
}, "optionalShape");
|
|
10487
12106
|
}
|
|
10488
12107
|
/**
|
|
@@ -10503,8 +12122,7 @@ function nullableShape(inner) {
|
|
|
10503
12122
|
category: "nullable",
|
|
10504
12123
|
inner
|
|
10505
12124
|
};
|
|
10506
|
-
|
|
10507
|
-
return INTRINSICS.freeze(shape);
|
|
12125
|
+
return ShapePass.seal(shape);
|
|
10508
12126
|
}, "nullableShape");
|
|
10509
12127
|
}
|
|
10510
12128
|
/**
|
|
@@ -10536,17 +12154,39 @@ function jsonShape(options) {
|
|
|
10536
12154
|
category: "json",
|
|
10537
12155
|
...safe?.description === void 0 ? {} : { description: safe.description }
|
|
10538
12156
|
};
|
|
10539
|
-
|
|
10540
|
-
return INTRINSICS.freeze(shape);
|
|
12157
|
+
return ShapePass.seal(shape);
|
|
10541
12158
|
}, "jsonShape");
|
|
10542
12159
|
}
|
|
10543
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
|
+
/**
|
|
10544
12182
|
* Builds a {@link RawShape} from a supported JSON Schema fragment.
|
|
10545
12183
|
*
|
|
10546
12184
|
* @remarks
|
|
10547
12185
|
* For values the shape DSL can't express. The fragment is recursively checked
|
|
10548
12186
|
* against the lean {@link JSONSchema} vocabulary before ownership is taken;
|
|
10549
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
|
|
10550
12190
|
* compiled guard accepts every
|
|
10551
12191
|
* DEFINED value — `undefined` alone fails, because it is the parser's failure
|
|
10552
12192
|
* sentinel; wrap the shape in {@link optionalShape} to admit absence. The
|
|
@@ -10574,22 +12214,18 @@ function rawShape(schema) {
|
|
|
10574
12214
|
category: "raw",
|
|
10575
12215
|
schema
|
|
10576
12216
|
}).validate();
|
|
10577
|
-
|
|
10578
|
-
new ShapeValidator({
|
|
10579
|
-
category: "raw",
|
|
10580
|
-
schema: owned
|
|
10581
|
-
}).validate();
|
|
10582
|
-
return INTRINSICS.freeze({
|
|
12217
|
+
return ShapePass.seal({
|
|
10583
12218
|
category: "raw",
|
|
10584
|
-
schema:
|
|
12219
|
+
schema: cloneSchema(schema)
|
|
10585
12220
|
});
|
|
10586
12221
|
}, "rawShape");
|
|
10587
12222
|
}
|
|
10588
12223
|
/**
|
|
10589
12224
|
* Converts a runtime `JSONSchema` value into a validating {@link ContractShape}
|
|
10590
12225
|
* — the inverse of {@link compileSchema}. Unlike direct {@link rawShape}
|
|
10591
|
-
* construction, which rejects malformed supported-vocabulary keywords,
|
|
10592
|
-
* 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.
|
|
10593
12229
|
*
|
|
10594
12230
|
* @remarks
|
|
10595
12231
|
* Readable malformed, cyclic, or deeply nested schema nodes widen to
|
|
@@ -10640,9 +12276,36 @@ function rawShape(schema) {
|
|
|
10640
12276
|
* when the conversion widened anywhere — `schema` / `is` / `parse` / `explain`
|
|
10641
12277
|
* stay total.
|
|
10642
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
|
+
*
|
|
10643
12305
|
* @param schema - The JSON Schema value to convert
|
|
12306
|
+
* @param options - Optional `strict`, default `false`
|
|
10644
12307
|
* @returns The built {@link ContractShape}
|
|
10645
|
-
* @throws {ContractError} When schema traversal fails
|
|
12308
|
+
* @throws {ContractError} When schema traversal fails, or when a strict conversion refuses
|
|
10646
12309
|
*
|
|
10647
12310
|
* @example
|
|
10648
12311
|
* ```ts
|
|
@@ -10652,9 +12315,30 @@ function rawShape(schema) {
|
|
|
10652
12315
|
* contract.parse({ id: 'nope' }) // undefined
|
|
10653
12316
|
* ```
|
|
10654
12317
|
*/
|
|
10655
|
-
function schemaToShape(schema) {
|
|
12318
|
+
function schemaToShape(schema, options) {
|
|
10656
12319
|
return contain(() => {
|
|
10657
|
-
|
|
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", {
|
|
10658
12342
|
subject: "schema",
|
|
10659
12343
|
context: { shape: "schema" }
|
|
10660
12344
|
});
|
|
@@ -10668,7 +12352,12 @@ exports.CONTRACT_CODES = CONTRACT_CODES;
|
|
|
10668
12352
|
exports.CONTRACT_ERROR_BRAND = CONTRACT_ERROR_BRAND;
|
|
10669
12353
|
exports.ContractCompiler = ContractCompiler;
|
|
10670
12354
|
exports.ContractError = ContractError;
|
|
12355
|
+
exports.FAULT_CONSTRAINTS = FAULT_CONSTRAINTS;
|
|
12356
|
+
exports.FAULT_KINDS = FAULT_KINDS;
|
|
10671
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;
|
|
10672
12361
|
exports.FORMAT_MAX_LENGTH = FORMAT_MAX_LENGTH;
|
|
10673
12362
|
exports.FORMAT_PATTERNS = FORMAT_PATTERNS;
|
|
10674
12363
|
exports.GENERATION_ATTEMPT_LIMIT = GENERATION_ATTEMPT_LIMIT;
|
|
@@ -10683,18 +12372,22 @@ exports.NAME_LIMIT = NAME_LIMIT;
|
|
|
10683
12372
|
exports.NODE_LIMIT = NODE_LIMIT;
|
|
10684
12373
|
exports.PRESENCE_MASK_LIMIT = PRESENCE_MASK_LIMIT;
|
|
10685
12374
|
exports.PREVIEW_LIMIT = PREVIEW_LIMIT;
|
|
12375
|
+
exports.SHAPE_FIELDS = SHAPE_FIELDS;
|
|
12376
|
+
exports.STRICT_SCHEMA_KEYWORDS = STRICT_SCHEMA_KEYWORDS;
|
|
10686
12377
|
exports.STRING_LIMIT = STRING_LIMIT;
|
|
10687
12378
|
exports.SchemaCloner = SchemaCloner;
|
|
10688
12379
|
exports.ShapeCloner = ShapeCloner;
|
|
10689
12380
|
exports.ShapeValidator = ShapeValidator;
|
|
10690
12381
|
exports.TEXT_LIMIT = TEXT_LIMIT;
|
|
10691
12382
|
exports.admitMember = admitMember;
|
|
12383
|
+
exports.admitValue = admitValue;
|
|
10692
12384
|
exports.admitVisited = admitVisited;
|
|
10693
12385
|
exports.andOf = andOf;
|
|
10694
12386
|
exports.appendEntries = appendEntries;
|
|
10695
12387
|
exports.arrayOf = arrayOf;
|
|
10696
12388
|
exports.arrayShape = arrayShape;
|
|
10697
12389
|
exports.attempt = attempt;
|
|
12390
|
+
exports.auditFaultShape = auditFaultShape;
|
|
10698
12391
|
exports.booleanShape = booleanShape;
|
|
10699
12392
|
exports.boundsOf = boundsOf;
|
|
10700
12393
|
exports.buildArrayFaults = buildArrayFaults;
|
|
@@ -10718,10 +12411,13 @@ exports.compileParser = compileParser;
|
|
|
10718
12411
|
exports.compileReporter = compileReporter;
|
|
10719
12412
|
exports.compileSchema = compileSchema;
|
|
10720
12413
|
exports.complementOf = complementOf;
|
|
12414
|
+
exports.computeSurrogatePairs = computeSurrogatePairs;
|
|
10721
12415
|
exports.contain = contain;
|
|
10722
12416
|
exports.createContract = createContract;
|
|
10723
12417
|
exports.deriveLengthBounds = deriveLengthBounds;
|
|
10724
12418
|
exports.deriveRangeBounds = deriveRangeBounds;
|
|
12419
|
+
exports.describeFaultLine = describeFaultLine;
|
|
12420
|
+
exports.describeFaults = describeFaults;
|
|
10725
12421
|
exports.drawRandom = drawRandom;
|
|
10726
12422
|
exports.encodeLeaf = encodeLeaf;
|
|
10727
12423
|
exports.encodeParts = encodeParts;
|
|
@@ -10730,6 +12426,9 @@ exports.enumerableKeys = enumerableKeys;
|
|
|
10730
12426
|
exports.enumerableSymbolCount = enumerableSymbolCount;
|
|
10731
12427
|
exports.errorToMessage = errorToMessage;
|
|
10732
12428
|
exports.escapeRegExp = escapeRegExp;
|
|
12429
|
+
exports.extractForms = extractForms;
|
|
12430
|
+
exports.extractPath = extractPath;
|
|
12431
|
+
exports.extractSchema = extractSchema;
|
|
10733
12432
|
exports.failure = failure;
|
|
10734
12433
|
exports.holds = holds;
|
|
10735
12434
|
exports.inferPrimitiveEnum = inferPrimitiveEnum;
|
|
@@ -10742,6 +12441,7 @@ exports.isArrayBufferView = isArrayBufferView;
|
|
|
10742
12441
|
exports.isAsyncFunction = isAsyncFunction;
|
|
10743
12442
|
exports.isAsyncGeneratorFunction = isAsyncGeneratorFunction;
|
|
10744
12443
|
exports.isAsyncIterable = isAsyncIterable;
|
|
12444
|
+
exports.isAuditFault = isAuditFault;
|
|
10745
12445
|
exports.isBigInt = isBigInt;
|
|
10746
12446
|
exports.isBigInt64Array = isBigInt64Array;
|
|
10747
12447
|
exports.isBigUint64Array = isBigUint64Array;
|
|
@@ -10761,6 +12461,8 @@ exports.isEmptyString = isEmptyString;
|
|
|
10761
12461
|
exports.isError = isError;
|
|
10762
12462
|
exports.isFailure = isFailure;
|
|
10763
12463
|
exports.isFalse = isFalse;
|
|
12464
|
+
exports.isFault = isFault;
|
|
12465
|
+
exports.isFaultPath = isFaultPath;
|
|
10764
12466
|
exports.isFiniteNumber = isFiniteNumber;
|
|
10765
12467
|
exports.isFloat32Array = isFloat32Array;
|
|
10766
12468
|
exports.isFloat64Array = isFloat64Array;
|
|
@@ -10819,12 +12521,16 @@ exports.literalOf = literalOf;
|
|
|
10819
12521
|
exports.literalShape = literalShape;
|
|
10820
12522
|
exports.mapOf = mapOf;
|
|
10821
12523
|
exports.matchOf = matchOf;
|
|
12524
|
+
exports.matchesExactKeys = matchesExactKeys;
|
|
10822
12525
|
exports.matchesISOInstant = matchesISOInstant;
|
|
10823
12526
|
exports.matchesJSONDepth = matchesJSONDepth;
|
|
10824
12527
|
exports.matchesJSONValue = matchesJSONValue;
|
|
12528
|
+
exports.matchesLosslessRepair = matchesLosslessRepair;
|
|
10825
12529
|
exports.matchesMember = matchesMember;
|
|
10826
12530
|
exports.matchesPattern = matchesPattern;
|
|
10827
12531
|
exports.matchesRecordBrand = matchesRecordBrand;
|
|
12532
|
+
exports.matchesStringLength = matchesStringLength;
|
|
12533
|
+
exports.matchesUndefinedField = matchesUndefinedField;
|
|
10828
12534
|
exports.matchesVisited = matchesVisited;
|
|
10829
12535
|
exports.notOf = notOf;
|
|
10830
12536
|
exports.nullShape = nullShape;
|
|
@@ -10883,6 +12589,10 @@ exports.readValue = readValue;
|
|
|
10883
12589
|
exports.recordOf = recordOf;
|
|
10884
12590
|
exports.recordShape = recordShape;
|
|
10885
12591
|
exports.refuseExpansion = refuseExpansion;
|
|
12592
|
+
exports.refuseOptionKeys = refuseOptionKeys;
|
|
12593
|
+
exports.renderForm = renderForm;
|
|
12594
|
+
exports.renderList = renderList;
|
|
12595
|
+
exports.renderPath = renderPath;
|
|
10886
12596
|
exports.resolveField = resolveField;
|
|
10887
12597
|
exports.retainDepth = retainDepth;
|
|
10888
12598
|
exports.samplesToFormat = samplesToFormat;
|
|
@@ -10895,6 +12605,7 @@ exports.schemaToShape = schemaToShape;
|
|
|
10895
12605
|
exports.seededRandom = seededRandom;
|
|
10896
12606
|
exports.selectClosestFaults = selectClosestFaults;
|
|
10897
12607
|
exports.setOf = setOf;
|
|
12608
|
+
exports.shapeShape = shapeShape;
|
|
10898
12609
|
exports.shapeToKind = shapeToKind;
|
|
10899
12610
|
exports.sortValues = sortValues;
|
|
10900
12611
|
exports.stringOf = stringOf;
|
|
@@ -10902,6 +12613,7 @@ exports.stringShape = stringShape;
|
|
|
10902
12613
|
exports.stringToFormat = stringToFormat;
|
|
10903
12614
|
exports.success = success;
|
|
10904
12615
|
exports.transformOf = transformOf;
|
|
12616
|
+
exports.truncateFaultText = truncateFaultText;
|
|
10905
12617
|
exports.tupleOf = tupleOf;
|
|
10906
12618
|
exports.unifySchemas = unifySchemas;
|
|
10907
12619
|
exports.unionOf = unionOf;
|