@atscript/typescript 0.2.2 → 0.2.4
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/cli.cjs +211 -29
- package/dist/index.cjs +3 -3
- package/dist/index.mjs +2 -2
- package/dist/{plugin-oUSsb3Ri.cjs → plugin-Cqao46Gt.cjs} +62 -23
- package/dist/{plugin-DhesoKce.mjs → plugin-Ct9Ko5JG.mjs} +59 -20
- package/dist/realm-runtime.js +867 -256
- package/dist/test-utils.cjs +3 -3
- package/dist/test-utils.d.ts +179 -4
- package/dist/test-utils.mjs +2 -2
- package/dist/{unique-name-xWd9uO-k.cjs → user-text-BddKk8EU.cjs} +823 -191
- package/dist/{unique-name-hg1CyPIs.mjs → user-text-qL59indq.mjs} +758 -192
- package/dist/utils.cjs +158 -154
- package/dist/utils.d.ts +312 -28
- package/dist/utils.mjs +58 -63
- package/package.json +5 -5
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"expect.maxLength",
|
|
23
23
|
"expect.array.uniqueItems"
|
|
24
24
|
];
|
|
25
|
+
/** Annotations of an object node, in check order (before its props). */ const OBJECT_KEYS = ["expect.minKeys", "expect.maxKeys"];
|
|
25
26
|
/**
|
|
26
27
|
* Every annotation key the validator pre-check reads. The order is the bit order of
|
|
27
28
|
* compiled-check layouts: changing it changes the layout format.
|
|
@@ -35,7 +36,9 @@
|
|
|
35
36
|
"expect.max",
|
|
36
37
|
"expect.exclusiveMin",
|
|
37
38
|
"expect.exclusiveMax",
|
|
38
|
-
"expect.array.uniqueItems"
|
|
39
|
+
"expect.array.uniqueItems",
|
|
40
|
+
"expect.minKeys",
|
|
41
|
+
"expect.maxKeys"
|
|
39
42
|
];
|
|
40
43
|
/**
|
|
41
44
|
* The number a length / bound annotation compares with: the annotation is either the number
|
|
@@ -236,6 +239,32 @@ const notReferred = (key) => ((FLAGS.get(key) ?? 0) & NOT_REFERRED) !== 0;
|
|
|
236
239
|
*/ function moveIntrinsic(from, to) {
|
|
237
240
|
const s = INTRINSIC.get(from);
|
|
238
241
|
if (s) INTRINSIC.set(to, s);
|
|
242
|
+
moveHidden(from, to);
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Keys a map hides from its bases (`@annotate.reset`): read as if no base had them. Kept per map
|
|
246
|
+
* (as `INTRINSIC`), moved with it when a node's map is replaced; `hiding`: some map hides a key
|
|
247
|
+
* (until then the reads skip the lookup).
|
|
248
|
+
*/ const HIDDEN = /* @__PURE__ */ new WeakMap();
|
|
249
|
+
let hiding = false;
|
|
250
|
+
/** Whether map `m` hides `key` from its bases (see {@link HIDDEN}). */ function hides(m, key) {
|
|
251
|
+
return hiding && HIDDEN.get(m)?.has(key) === true;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Hides `key` of map `m`'s bases (`@annotate.reset`): `m` reads as if no base had it — its own
|
|
255
|
+
* value, if any, is what it has. A metadata write (layered views and compiled checks re-read).
|
|
256
|
+
* `metadata.delete(key)` on a layered map ends it: the bases show through again.
|
|
257
|
+
*/ function hideKey(m, key) {
|
|
258
|
+
let s = HIDDEN.get(m);
|
|
259
|
+
if (!s) HIDDEN.set(m, s = /* @__PURE__ */ new Set());
|
|
260
|
+
s.add(key);
|
|
261
|
+
hiding = true;
|
|
262
|
+
touched(m);
|
|
263
|
+
metadataReplaced();
|
|
264
|
+
}
|
|
265
|
+
/** The keys map `m` hides from its bases (see {@link hideKey}), for a map replacing it. */ function moveHidden(from, to) {
|
|
266
|
+
const s = HIDDEN.get(from);
|
|
267
|
+
if (s) HIDDEN.set(to, s);
|
|
239
268
|
}
|
|
240
269
|
const MISS = Symbol("miss");
|
|
241
270
|
/** The view of a layered map without effective entries (never written: views stay internal). */ const NO_ENTRIES = new AnnotationMap();
|
|
@@ -270,6 +299,7 @@ let _Symbol_iterator = Symbol.iterator;
|
|
|
270
299
|
const v = mapGet.call(this, key);
|
|
271
300
|
if (v !== void 0 || mapHas.call(this, key)) return v;
|
|
272
301
|
}
|
|
302
|
+
if (hiding && hides(this, key)) return MISS;
|
|
273
303
|
if (nr && this.r) {
|
|
274
304
|
for (const n of this.b) {
|
|
275
305
|
const x = intrinsicLookup(n.metadata, key);
|
|
@@ -300,10 +330,14 @@ let _Symbol_iterator = Symbol.iterator;
|
|
|
300
330
|
}
|
|
301
331
|
has(key) {
|
|
302
332
|
const view = this.view();
|
|
303
|
-
return view === void 0 ? this.
|
|
333
|
+
return view === void 0 ? this.liveHas(key) : mapHas.call(view, key);
|
|
334
|
+
}
|
|
335
|
+
/** `has` read through the layers (see the file comment). */ liveHas(key) {
|
|
336
|
+
return this.lookup(key, notReferred(key)) !== MISS;
|
|
304
337
|
}
|
|
305
338
|
/** An append key: the bases' values (lowest priority first), then the own ones. */ append(key) {
|
|
306
339
|
const own = mapGet.call(this, key);
|
|
340
|
+
if (hiding && hides(this, key)) return own;
|
|
307
341
|
if (this.r && notReferred(key)) {
|
|
308
342
|
const parts = [];
|
|
309
343
|
const acc = {
|
|
@@ -340,8 +374,20 @@ let _Symbol_iterator = Symbol.iterator;
|
|
|
340
374
|
}
|
|
341
375
|
liveKeys() {
|
|
342
376
|
const out = /* @__PURE__ */ new Set();
|
|
343
|
-
keysOf(this, false, out, /* @__PURE__ */ new Map());
|
|
344
|
-
|
|
377
|
+
return keysOf(this, false, out, /* @__PURE__ */ new Map()) ? [...out].filter((k) => this.liveHas(k)) : [...out];
|
|
378
|
+
}
|
|
379
|
+
/** Deletes the own value of `key`; a key hidden from the bases (`hideKey`) shows them again. */ delete(key) {
|
|
380
|
+
const deleted = super.delete(key);
|
|
381
|
+
if ((hiding ? HIDDEN.get(this) : void 0)?.delete(key)) {
|
|
382
|
+
touched(this);
|
|
383
|
+
metadataReplaced();
|
|
384
|
+
return true;
|
|
385
|
+
}
|
|
386
|
+
return deleted;
|
|
387
|
+
}
|
|
388
|
+
/** Deletes the own values; the keys hidden from the bases (`hideKey`) show them again. */ clear() {
|
|
389
|
+
super.clear();
|
|
390
|
+
HIDDEN.delete(this);
|
|
345
391
|
}
|
|
346
392
|
keys() {
|
|
347
393
|
return this.effectiveKeys()[Symbol.iterator]();
|
|
@@ -392,7 +438,7 @@ Object.defineProperty(LayerMap.prototype, "size", {
|
|
|
392
438
|
if (m instanceof LayerMap ? mapHas.call(m, key) : m.has(key)) return m instanceof LayerMap ? mapGet.call(m, key) : m.get(key);
|
|
393
439
|
}
|
|
394
440
|
const b = m.b;
|
|
395
|
-
if (b !== void 0) for (const n of b) {
|
|
441
|
+
if (b !== void 0 && !hides(m, key)) for (const n of b) {
|
|
396
442
|
const x = intrinsicLookup(n.metadata, key);
|
|
397
443
|
if (x !== MISS) return x;
|
|
398
444
|
}
|
|
@@ -417,7 +463,7 @@ Object.defineProperty(LayerMap.prototype, "size", {
|
|
|
417
463
|
const was = acc.seen.get(m);
|
|
418
464
|
if (!visit(m, filtered, acc.seen)) return;
|
|
419
465
|
const layer = m instanceof LayerMap ? m : void 0;
|
|
420
|
-
if (layer) {
|
|
466
|
+
if (layer && !hides(layer, key)) {
|
|
421
467
|
const below = filtered || layer.r && notReferred(key);
|
|
422
468
|
for (let i = layer.b.length - 1; i >= 0; i--) collect(layer.b[i].metadata, key, acc, below);
|
|
423
469
|
}
|
|
@@ -426,14 +472,16 @@ Object.defineProperty(LayerMap.prototype, "size", {
|
|
|
426
472
|
}
|
|
427
473
|
/**
|
|
428
474
|
* The keys of `m` (own first, then its bases'), each map once (see {@link visit}). `filtered`:
|
|
429
|
-
* reached through a reference layer.
|
|
475
|
+
* reached through a reference layer. Returns whether a map visited hides keys from its bases.
|
|
430
476
|
*/ function keysOf(m, filtered, out, seen) {
|
|
431
|
-
if (!visit(m, filtered, seen)) return;
|
|
477
|
+
if (!visit(m, filtered, seen)) return false;
|
|
478
|
+
let hid = hiding && (HIDDEN.get(m)?.size ?? 0) > 0;
|
|
432
479
|
const layer = m instanceof LayerMap ? m : void 0;
|
|
433
480
|
const own = layer ? Map.prototype.keys.call(m) : m.keys();
|
|
434
481
|
const intrinsic = INTRINSIC.get(m);
|
|
435
482
|
for (const k of own) if (!filtered || !notReferred(k) || intrinsic?.has(k)) out.add(k);
|
|
436
|
-
if (layer) for (const n of layer.b) keysOf(n.metadata, filtered || layer.r, out, seen);
|
|
483
|
+
if (layer) for (const n of layer.b) hid = keysOf(n.metadata, filtered || layer.r, out, seen) || hid;
|
|
484
|
+
return hid;
|
|
437
485
|
}
|
|
438
486
|
/**
|
|
439
487
|
* `m` has no entry (own or through its bases): the validators' `metadata.size === 0` fast path,
|
|
@@ -924,6 +972,51 @@ function caseVariants(ch, unicode) {
|
|
|
924
972
|
};
|
|
925
973
|
}
|
|
926
974
|
|
|
975
|
+
//#endregion
|
|
976
|
+
//#region packages/typescript/src/runtime/discriminator.ts
|
|
977
|
+
/**
|
|
978
|
+
* Detects a discriminator property across union items.
|
|
979
|
+
*
|
|
980
|
+
* Scans all items for object-typed members that share a common property
|
|
981
|
+
* with distinct const/literal values. If exactly one such property exists,
|
|
982
|
+
* it is returned as the discriminator. Returns `null` when no qualifying
|
|
983
|
+
* property exists, or when more than one does (ambiguous).
|
|
984
|
+
*/ function detectDiscriminator(items) {
|
|
985
|
+
if (items.length < 2) return null;
|
|
986
|
+
for (const item of items) if (item.type.kind !== "object") return null;
|
|
987
|
+
const firstObj = items[0].type;
|
|
988
|
+
const candidates = [];
|
|
989
|
+
for (const [propName, propType] of firstObj.props.entries()) if (propType.type.kind === "" && propType.type.value !== void 0) candidates.push(propName);
|
|
990
|
+
let result = null;
|
|
991
|
+
for (const candidate of candidates) {
|
|
992
|
+
const values = /* @__PURE__ */ new Set();
|
|
993
|
+
const indexMapping = {};
|
|
994
|
+
let valid = true;
|
|
995
|
+
for (let i = 0; i < items.length; i++) {
|
|
996
|
+
const prop = items[i].type.props.get(candidate);
|
|
997
|
+
if (!prop || prop.type.kind !== "" || prop.type.value === void 0) {
|
|
998
|
+
valid = false;
|
|
999
|
+
break;
|
|
1000
|
+
}
|
|
1001
|
+
const val = prop.type.value;
|
|
1002
|
+
if (values.has(val)) {
|
|
1003
|
+
valid = false;
|
|
1004
|
+
break;
|
|
1005
|
+
}
|
|
1006
|
+
values.add(val);
|
|
1007
|
+
indexMapping[String(val)] = i;
|
|
1008
|
+
}
|
|
1009
|
+
if (valid) {
|
|
1010
|
+
if (result) return null;
|
|
1011
|
+
result = {
|
|
1012
|
+
propertyName: candidate,
|
|
1013
|
+
indexMapping
|
|
1014
|
+
};
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
return result;
|
|
1018
|
+
}
|
|
1019
|
+
|
|
927
1020
|
//#endregion
|
|
928
1021
|
//#region packages/typescript/src/runtime/compiled-check/layout.ts
|
|
929
1022
|
/**
|
|
@@ -997,11 +1090,14 @@ const B_MAX = bit("expect.max");
|
|
|
997
1090
|
const B_EX_MIN = bit("expect.exclusiveMin");
|
|
998
1091
|
const B_EX_MAX = bit("expect.exclusiveMax");
|
|
999
1092
|
const B_UNIQUE = bit("expect.array.uniqueItems");
|
|
1093
|
+
const B_MIN_KEYS = bit("expect.minKeys");
|
|
1094
|
+
const B_MAX_KEYS = bit("expect.maxKeys");
|
|
1000
1095
|
/** Annotations the pre-check reads per kind of value (see validator-keys.ts). */ const STRING_BITS = bitsOf(STRING_KEYS);
|
|
1001
1096
|
const NUMBER_BITS = bitsOf(NUMBER_KEYS);
|
|
1002
1097
|
const ARRAY_BITS = bitsOf(ARRAY_KEYS);
|
|
1098
|
+
const OBJECT_BITS = bitsOf(OBJECT_KEYS);
|
|
1003
1099
|
const BOOLEAN_BITS = bitsOf(BOOLEAN_KEYS);
|
|
1004
|
-
/** Keys whose value the compiled code reads (one K slot each). */ const VALUE_BITS = B_MIN_LENGTH | B_MAX_LENGTH | B_MIN | B_MAX | B_EX_MIN | B_EX_MAX;
|
|
1100
|
+
/** Keys whose value the compiled code reads (one K slot each). */ const VALUE_BITS = B_MIN_LENGTH | B_MAX_LENGTH | B_MIN | B_MAX | B_EX_MIN | B_EX_MAX | B_MIN_KEYS | B_MAX_KEYS;
|
|
1005
1101
|
const FLAG_BITS = B_REQUIRED | B_INT | B_UNIQUE;
|
|
1006
1102
|
const ALL_BITS = (1 << VALIDATOR_KEYS.length) - 1;
|
|
1007
1103
|
/** The field holding the bound of each value key's object form. */ const BOUND_FIELDS = {
|
|
@@ -1010,7 +1106,9 @@ const ALL_BITS = (1 << VALIDATOR_KEYS.length) - 1;
|
|
|
1010
1106
|
[B_MIN]: "minValue",
|
|
1011
1107
|
[B_MAX]: "maxValue",
|
|
1012
1108
|
[B_EX_MIN]: "exclusiveMinValue",
|
|
1013
|
-
[B_EX_MAX]: "exclusiveMaxValue"
|
|
1109
|
+
[B_EX_MAX]: "exclusiveMaxValue",
|
|
1110
|
+
[B_MIN_KEYS]: "count",
|
|
1111
|
+
[B_MAX_KEYS]: "count"
|
|
1014
1112
|
};
|
|
1015
1113
|
/**
|
|
1016
1114
|
* Validator annotations the pre-check reads for a node of this type def (the rest of the
|
|
@@ -1026,6 +1124,7 @@ const ALL_BITS = (1 << VALIDATOR_KEYS.length) - 1;
|
|
|
1026
1124
|
default: return 0;
|
|
1027
1125
|
}
|
|
1028
1126
|
case "array": return ARRAY_BITS;
|
|
1127
|
+
case "object": return OBJECT_BITS;
|
|
1029
1128
|
case "union": return ALL_BITS;
|
|
1030
1129
|
default: return 0;
|
|
1031
1130
|
}
|
|
@@ -1517,7 +1616,7 @@ function acceptsNull(def) {
|
|
|
1517
1616
|
for (const item of def.type.items) {
|
|
1518
1617
|
const failures = COMPILED_ENV.n;
|
|
1519
1618
|
const result = fastCheck(item, value, branchFlags, opts);
|
|
1520
|
-
if (result !== FAIL) return unionMatchResult(result, item, def.metadata, value);
|
|
1619
|
+
if (result !== FAIL) return unionMatchResult(result, item, def.metadata, value, opts);
|
|
1521
1620
|
if (COMPILED_ENV.n !== failures) unionWaste();
|
|
1522
1621
|
if (nested) COMPILED_ENV.n++;
|
|
1523
1622
|
if (pendingStrips.length !== kept) pendingStrips.length = kept;
|
|
@@ -1536,7 +1635,7 @@ function acceptsNull(def) {
|
|
|
1536
1635
|
for (const child of plan.children) {
|
|
1537
1636
|
const failures = COMPILED_ENV.n;
|
|
1538
1637
|
const result = checkPlan(child, value, branchFlags, opts);
|
|
1539
|
-
if (result !== FAIL) return unionMatchResult(result, child.def, plan.metadata, value);
|
|
1638
|
+
if (result !== FAIL) return unionMatchResult(result, child.def, plan.metadata, value, opts);
|
|
1540
1639
|
if (COMPILED_ENV.n !== failures) unionWaste();
|
|
1541
1640
|
if (nested) COMPILED_ENV.n++;
|
|
1542
1641
|
if (pendingStrips.length !== kept) pendingStrips.length = kept;
|
|
@@ -1549,7 +1648,7 @@ function acceptsNull(def) {
|
|
|
1549
1648
|
for (const item of def.type.items) {
|
|
1550
1649
|
const failures = COMPILED_ENV.n;
|
|
1551
1650
|
const result = fastCheck(item, value, branchFlags, opts);
|
|
1552
|
-
if (result !== FAIL) return unionMatchResult(result, item, def.metadata, value);
|
|
1651
|
+
if (result !== FAIL) return unionMatchResult(result, item, def.metadata, value, opts);
|
|
1553
1652
|
if (COMPILED_ENV.n !== failures) unionWaste();
|
|
1554
1653
|
if (nested) COMPILED_ENV.n++;
|
|
1555
1654
|
if (pendingStrips.length !== kept) pendingStrips.length = kept;
|
|
@@ -1562,7 +1661,7 @@ function acceptsNull(def) {
|
|
|
1562
1661
|
for (const child of plan.children) {
|
|
1563
1662
|
const failures = COMPILED_ENV.n;
|
|
1564
1663
|
const result = checkPlan(child, value, branchFlags, opts);
|
|
1565
|
-
if (result !== FAIL) return unionMatchResult(result, child.def, plan.metadata, value);
|
|
1664
|
+
if (result !== FAIL) return unionMatchResult(result, child.def, plan.metadata, value, opts);
|
|
1566
1665
|
if (COMPILED_ENV.n !== failures) unionWaste();
|
|
1567
1666
|
if (nested) COMPILED_ENV.n++;
|
|
1568
1667
|
if (pendingStrips.length !== kept) pendingStrips.length = kept;
|
|
@@ -1608,7 +1707,7 @@ function fastCheckFinal(def, value) {
|
|
|
1608
1707
|
case "string": return typeof value === "string" ? fastCheckString(def.metadata, value) : FAIL;
|
|
1609
1708
|
case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def.metadata, value) : FAIL;
|
|
1610
1709
|
case "boolean": return typeof value === "boolean" ? fastCheckBoolean(def.metadata, value) : FAIL;
|
|
1611
|
-
case "any": return PASS;
|
|
1710
|
+
case "any": return value === void 0 ? FAIL : PASS;
|
|
1612
1711
|
case "never": return FAIL;
|
|
1613
1712
|
case "undefined": return value === void 0 ? PASS : FAIL;
|
|
1614
1713
|
case "null": return value === null ? PASS : FAIL;
|
|
@@ -1616,20 +1715,22 @@ function fastCheckFinal(def, value) {
|
|
|
1616
1715
|
default: return BAIL;
|
|
1617
1716
|
}
|
|
1618
1717
|
}
|
|
1619
|
-
/** Result of a union whose branch `item` did not fail (`PASS` or `BAIL`). */ function unionMatchResult(result, item, metadata, value) {
|
|
1620
|
-
return result === PASS && item.type.value === void 0 ? fastCheckUnionConstraints(metadata, value) : result;
|
|
1718
|
+
/** Result of a union whose branch `item` did not fail (`PASS` or `BAIL`). */ function unionMatchResult(result, item, metadata, value, opts) {
|
|
1719
|
+
return result === PASS && item.type.value === void 0 ? fastCheckUnionConstraints(metadata, value, item, opts) : result;
|
|
1621
1720
|
}
|
|
1622
1721
|
/**
|
|
1623
1722
|
* Mirrors Validator.validateUnionConstraints: union-level constraints on the matched value
|
|
1624
1723
|
* (`null` / objects pass; the caller skips literal branches).
|
|
1625
|
-
*/ function fastCheckUnionConstraints(meta, value) {
|
|
1724
|
+
*/ function fastCheckUnionConstraints(meta, value, branch, opts) {
|
|
1626
1725
|
const metadata = filledMeta(meta);
|
|
1627
1726
|
if (metadata === void 0) return PASS;
|
|
1628
1727
|
switch (typeof value) {
|
|
1629
1728
|
case "string": return fastCheckString(metadata, value);
|
|
1630
1729
|
case "number": return fastCheckNumber(metadata, value);
|
|
1631
1730
|
case "boolean": return fastCheckBoolean(metadata, value);
|
|
1632
|
-
case "object":
|
|
1731
|
+
case "object":
|
|
1732
|
+
if (Array.isArray(value)) return fastCheckArrayConstraints(metadata, value);
|
|
1733
|
+
return value === null ? PASS : objectKeyCounts(metadata, value, branch, opts);
|
|
1633
1734
|
default: return PASS;
|
|
1634
1735
|
}
|
|
1635
1736
|
}
|
|
@@ -1671,6 +1772,34 @@ function numberConstraints(value, int, min, max, exMin, exMax) {
|
|
|
1671
1772
|
/** `@expect.minLength` / `@expect.maxLength` (strings and arrays). */ function lengthFails(length, minLength, maxLength) {
|
|
1672
1773
|
return minLength !== void 0 && length < (typeof minLength === "number" ? minLength : minLength.length) || maxLength !== void 0 && length > (typeof maxLength === "number" ? maxLength : maxLength.length);
|
|
1673
1774
|
}
|
|
1775
|
+
/** `@expect.minKeys` / `@expect.maxKeys` of an object keeping `count` keys. */ function countFails(count, minKeys, maxKeys) {
|
|
1776
|
+
return minKeys !== void 0 && count < (typeof minKeys === "number" ? minKeys : minKeys.count) || maxKeys !== void 0 && count > (typeof maxKeys === "number" ? maxKeys : maxKeys.count);
|
|
1777
|
+
}
|
|
1778
|
+
/**
|
|
1779
|
+
* The keys of object `value` the walk's result keeps (`Validator.keptKeyCount`): with
|
|
1780
|
+
* `unknownProps: 'strip'`, the keys `props` declares (not phantom); else every own key (with
|
|
1781
|
+
* 'error' an object with an unknown key fails anyway, whatever its count).
|
|
1782
|
+
*/ function keptKeys(props, value, opts) {
|
|
1783
|
+
const keys = Object.keys(value);
|
|
1784
|
+
if (opts.unknownProps !== "strip") return keys.length;
|
|
1785
|
+
let count = 0;
|
|
1786
|
+
for (const key of keys) if (!isUnknownKey(props, key)) count++;
|
|
1787
|
+
return count;
|
|
1788
|
+
}
|
|
1789
|
+
/**
|
|
1790
|
+
* `@expect.minKeys` / `maxKeys` in `metadata` (an object's or a union's) of object `value`,
|
|
1791
|
+
* matched as `branch` (pattern props: the walk).
|
|
1792
|
+
*/ function objectKeyCounts(metadata, value, branch, opts) {
|
|
1793
|
+
const minKeys = metadata.get("expect.minKeys");
|
|
1794
|
+
const maxKeys = metadata.get("expect.maxKeys");
|
|
1795
|
+
if (minKeys === void 0 && maxKeys === void 0) return PASS;
|
|
1796
|
+
const type = branch.type;
|
|
1797
|
+
let count;
|
|
1798
|
+
if (type.kind !== "object") count = Object.keys(value).length;
|
|
1799
|
+
else if (type.propsPatterns.length > 0 && opts.unknownProps !== "ignore") return BAIL;
|
|
1800
|
+
else count = keptKeys(type.props, value, opts);
|
|
1801
|
+
return countFails(count, minKeys, maxKeys) ? FAIL : PASS;
|
|
1802
|
+
}
|
|
1674
1803
|
/**
|
|
1675
1804
|
* {@link fastCheck} of an intersection (a function of its own: `fastCheck` stays small, so the
|
|
1676
1805
|
* engine inlines it in the hot object and prop checks).
|
|
@@ -1707,6 +1836,8 @@ function fastCheckObject(def, value, flags, opts) {
|
|
|
1707
1836
|
}
|
|
1708
1837
|
}
|
|
1709
1838
|
if (result !== PASS) return result;
|
|
1839
|
+
const metadata = filledMeta(def.metadata);
|
|
1840
|
+
if (metadata !== void 0 && objectKeyCounts(metadata, value, def, opts) === FAIL) return FAIL;
|
|
1710
1841
|
if (unknownProps === "strip") {
|
|
1711
1842
|
pendStrips(type.props, value);
|
|
1712
1843
|
return PASS;
|
|
@@ -1779,7 +1910,8 @@ const plans = /* @__PURE__ */ new WeakMap();
|
|
|
1779
1910
|
/**
|
|
1780
1911
|
* Annotation keys the node's check reads, and their values (`undefined` when absent):
|
|
1781
1912
|
* string `meta.required, expect.minLength, maxLength, pattern`; number `expect.int, min,
|
|
1782
|
-
* max, exclusiveMin, exclusiveMax`; boolean `meta.required`; array `expect.minLength, maxLength, array.uniqueItems
|
|
1913
|
+
* max, exclusiveMin, exclusiveMax`; boolean `meta.required`; array `expect.minLength, maxLength, array.uniqueItems`;
|
|
1914
|
+
* object `expect.minKeys, maxKeys`.
|
|
1783
1915
|
*/ _define_property$1(this, "k0", void 0);
|
|
1784
1916
|
_define_property$1(this, "k1", void 0);
|
|
1785
1917
|
_define_property$1(this, "k2", void 0);
|
|
@@ -1947,6 +2079,7 @@ function setMetaKeys(plan, k0, k1, k2, k3, k4) {
|
|
|
1947
2079
|
plan.byKey = byKey;
|
|
1948
2080
|
plan.props = type.props;
|
|
1949
2081
|
plan.propsPatterns = type.propsPatterns;
|
|
2082
|
+
setMetaKeys(plan, ...OBJECT_KEYS);
|
|
1950
2083
|
plan.op = OP_OBJECT;
|
|
1951
2084
|
break;
|
|
1952
2085
|
}
|
|
@@ -2060,16 +2193,28 @@ function setMetaKeys(plan, k0, k1, k2, k3, k4) {
|
|
|
2060
2193
|
}
|
|
2061
2194
|
if (result !== PASS) return result;
|
|
2062
2195
|
const unknownProps = opts.unknownProps;
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2196
|
+
const byKey = plan.byKey;
|
|
2197
|
+
if (plan.metaStamp !== stamp) refreshMeta(plan);
|
|
2198
|
+
if ((plan.m0 !== void 0 || plan.m1 !== void 0) && planKeyCountFails(plan, byKey, value, opts)) return FAIL;
|
|
2199
|
+
if (unknownProps === "error" || unknownProps === "strip") for (const key of Object.keys(value)) {
|
|
2200
|
+
const known = byKey.get(key);
|
|
2201
|
+
if (known !== void 0 && known.op !== OP_PHANTOM) continue;
|
|
2202
|
+
if (unknownProps === "error") return FAIL;
|
|
2203
|
+
pendingStrips.push(value, key);
|
|
2204
|
+
}
|
|
2205
|
+
return PASS;
|
|
2206
|
+
}
|
|
2207
|
+
/** The key counts of an object plan (see `keptKeys`; `byKey` verified). */ function planKeyCountFails(plan, byKey, value, opts) {
|
|
2208
|
+
const keys = Object.keys(value);
|
|
2209
|
+
let count = keys.length;
|
|
2210
|
+
if (opts.unknownProps === "strip") {
|
|
2211
|
+
count = 0;
|
|
2212
|
+
for (const key of keys) {
|
|
2066
2213
|
const known = byKey.get(key);
|
|
2067
|
-
if (known !== void 0 && known.op !== OP_PHANTOM)
|
|
2068
|
-
if (unknownProps === "error") return FAIL;
|
|
2069
|
-
pendingStrips.push(value, key);
|
|
2214
|
+
if (known !== void 0 && known.op !== OP_PHANTOM) count++;
|
|
2070
2215
|
}
|
|
2071
2216
|
}
|
|
2072
|
-
return
|
|
2217
|
+
return countFails(count, plan.m0, plan.m1);
|
|
2073
2218
|
}
|
|
2074
2219
|
/** Plan counterpart of {@link fastCheckArray}. */ function checkArray(plan, value, flags, opts) {
|
|
2075
2220
|
if (!Array.isArray(value)) return FAIL;
|
|
@@ -2079,6 +2224,147 @@ function setMetaKeys(plan, k0, k1, k2, k3, k4) {
|
|
|
2079
2224
|
return checkItems(plan.ofPlan, value, flags, opts);
|
|
2080
2225
|
}
|
|
2081
2226
|
|
|
2227
|
+
//#endregion
|
|
2228
|
+
//#region packages/typescript/src/runtime/validator-messages.ts
|
|
2229
|
+
/**
|
|
2230
|
+
* Validator error codes, their default English templates and the translation helpers.
|
|
2231
|
+
*
|
|
2232
|
+
* The validator is locale-agnostic: every error it reports carries a stable `code` and the
|
|
2233
|
+
* `params` its message needs, and its `message` is the default English text (the code's
|
|
2234
|
+
* template filled with the params) or the app's own text from an annotation (`custom: true`).
|
|
2235
|
+
* Translate after the fact with {@link localizeErrors}, or per validator with the `translate`
|
|
2236
|
+
* option.
|
|
2237
|
+
*/ /**
|
|
2238
|
+
* Default English templates of the built-in error codes. `{name}` is replaced with
|
|
2239
|
+
* `params.name` (see {@link formatValidatorMessage}).
|
|
2240
|
+
*/ const VALIDATOR_MESSAGES = Object.freeze({
|
|
2241
|
+
REQUIRED: "Must not be empty",
|
|
2242
|
+
REQUIRED_CHECKED: "Must be checked",
|
|
2243
|
+
TYPE: "Expected {expected}, got {actual}",
|
|
2244
|
+
TYPE_ARRAY: "Expected array",
|
|
2245
|
+
TYPE_OBJECT: "Expected object",
|
|
2246
|
+
TYPE_DECIMAL: "Expected string (decimal), got {actual}",
|
|
2247
|
+
NOT_FINITE: "Expected finite number, got {actual}",
|
|
2248
|
+
DECIMAL_FORMAT: "Invalid decimal format: \"{value}\"",
|
|
2249
|
+
NEVER: "This type is impossible, must be an internal problem",
|
|
2250
|
+
LITERAL: "Expected {expected}, got {actual}",
|
|
2251
|
+
UNION: "Value does not match any of the allowed types: {expected}",
|
|
2252
|
+
UNION_DISCRIMINATOR: "Expected one of {allowed}, got {actual}",
|
|
2253
|
+
UNEXPECTED_PROPERTY: "Unexpected property",
|
|
2254
|
+
TUPLE_LENGTH: "Expected array of length {length}",
|
|
2255
|
+
MIN_LENGTH: "Expected minimum length of {min} characters, got {actual} characters",
|
|
2256
|
+
MAX_LENGTH: "Expected maximum length of {max} characters, got {actual} characters",
|
|
2257
|
+
MIN_ITEMS: "Expected minimum length of {min} items, got {actual} items",
|
|
2258
|
+
MAX_ITEMS: "Expected maximum length of {max} items, got {actual} items",
|
|
2259
|
+
UNIQUE_ITEMS: "Duplicate items are not allowed",
|
|
2260
|
+
MIN_KEYS: "Expected at least {min} properties, got {actual}",
|
|
2261
|
+
MAX_KEYS: "Expected at most {max} properties, got {actual}",
|
|
2262
|
+
PATTERN: "Value is expected to match pattern \"{pattern}\"",
|
|
2263
|
+
INT: "Expected integer, got {actual}",
|
|
2264
|
+
MIN: "Expected minimum {min}, got {actual}",
|
|
2265
|
+
MAX: "Expected maximum {max}, got {actual}",
|
|
2266
|
+
EXCLUSIVE_MIN: "Expected greater than {min}, got {actual}",
|
|
2267
|
+
EXCLUSIVE_MAX: "Expected less than {max}, got {actual}",
|
|
2268
|
+
EMAIL: "Invalid email format.",
|
|
2269
|
+
PHONE: "Invalid phone number format.",
|
|
2270
|
+
DATE: "Invalid date format.",
|
|
2271
|
+
ISO_DATE: "Invalid ISO date format.",
|
|
2272
|
+
UUID: "Invalid UUID format.",
|
|
2273
|
+
URL: "Invalid URL format.",
|
|
2274
|
+
IPV4: "Invalid IPv4 address.",
|
|
2275
|
+
IPV6: "Invalid IPv6 address.",
|
|
2276
|
+
IP: "Invalid IP address."
|
|
2277
|
+
});
|
|
2278
|
+
/**
|
|
2279
|
+
* The built-in validator error codes by name (`VALIDATOR_ERROR_CODES.MIN_LENGTH === 'MIN_LENGTH'`).
|
|
2280
|
+
* `CUSTOM` is the code of an error a validator plugin reports without a code of its own.
|
|
2281
|
+
*/ const VALIDATOR_ERROR_CODES = Object.freeze(Object.fromEntries([...Object.keys(VALIDATOR_MESSAGES), "CUSTOM"].map((code) => [code, code])));
|
|
2282
|
+
/**
|
|
2283
|
+
* Codes of the format messages built into the `string.*` primitives, by their English text
|
|
2284
|
+
* (the primitives' `@expect.pattern` message).
|
|
2285
|
+
*/ const FORMAT_CODES = new Map([
|
|
2286
|
+
"EMAIL",
|
|
2287
|
+
"PHONE",
|
|
2288
|
+
"DATE",
|
|
2289
|
+
"ISO_DATE",
|
|
2290
|
+
"UUID",
|
|
2291
|
+
"URL",
|
|
2292
|
+
"IPV4",
|
|
2293
|
+
"IPV6",
|
|
2294
|
+
"IP"
|
|
2295
|
+
].map((code) => [VALIDATOR_MESSAGES[code], code]));
|
|
2296
|
+
/** The code of a primitive's built-in format message (`string.email`'s → `EMAIL`), if it is one. */ function formatCodeOf(message) {
|
|
2297
|
+
return FORMAT_CODES.get(message);
|
|
2298
|
+
}
|
|
2299
|
+
/**
|
|
2300
|
+
* Fills a message template: each `{name}` becomes `params.name` (arrays: their items joined
|
|
2301
|
+
* with `, `). A placeholder without a param stays as it is.
|
|
2302
|
+
*/ function formatValidatorMessage(template, params) {
|
|
2303
|
+
if (params === void 0) return template;
|
|
2304
|
+
const parts = BUILTIN_PARTS.get(template);
|
|
2305
|
+
if (parts === void 0) return template.includes("{") ? template.replaceAll(PLACEHOLDER, fill(params)) : template;
|
|
2306
|
+
let out = parts[0];
|
|
2307
|
+
for (let i = 1; i < parts.length; i += 2) out += paramText(params, parts[i]) + parts[i + 1];
|
|
2308
|
+
return out;
|
|
2309
|
+
}
|
|
2310
|
+
const PLACEHOLDER = /\{(\w+)\}/g;
|
|
2311
|
+
/** The replacer of {@link PLACEHOLDER} over `params`. */ function fill(params) {
|
|
2312
|
+
return (_match, name) => paramText(params, name);
|
|
2313
|
+
}
|
|
2314
|
+
/** The text of placeholder `name` (the placeholder itself without the param). */ function paramText(params, name) {
|
|
2315
|
+
if (!Object.hasOwn(params, name)) return `{${name}}`;
|
|
2316
|
+
const value = params[name];
|
|
2317
|
+
return Array.isArray(value) ? value.map(String).join(", ") : String(value);
|
|
2318
|
+
}
|
|
2319
|
+
/**
|
|
2320
|
+
* The {@link VALIDATOR_MESSAGES} templates with placeholders, split at them: text, name, text,
|
|
2321
|
+
* name, …, text (`'a {b} c'.split(/\{(\w+)\}/)`).
|
|
2322
|
+
*/ const BUILTIN_PARTS = new Map(Object.values(VALIDATOR_MESSAGES).filter((template) => template.includes("{")).map((template) => [template, template.split(/\{(\w+)\}/)]));
|
|
2323
|
+
/**
|
|
2324
|
+
* Returns `errors` with translated messages (new objects; `details` too): `translate` gets
|
|
2325
|
+
* each error's code (or its message text, for app text — see {@link TValidatorTranslate}),
|
|
2326
|
+
* params and the error itself. Errors it returns `undefined` for keep their message.
|
|
2327
|
+
*
|
|
2328
|
+
* @example
|
|
2329
|
+
* ```ts
|
|
2330
|
+
* const errors = localizeErrors(validator.errors, (id, params) =>
|
|
2331
|
+
* i18n.exists(id) ? i18n.t(id, params) : undefined
|
|
2332
|
+
* )
|
|
2333
|
+
* ```
|
|
2334
|
+
*/ function localizeErrors(errors, translate) {
|
|
2335
|
+
return errors.map((error) => {
|
|
2336
|
+
const id = error.custom || error.code === void 0 ? error.message : error.code;
|
|
2337
|
+
const out = { ...error };
|
|
2338
|
+
if (error.params !== void 0) out.params = copyValue(error.params);
|
|
2339
|
+
const message = translate(id, out.params ?? {}, out);
|
|
2340
|
+
if (message !== void 0) out.message = message;
|
|
2341
|
+
if (error.details) out.details = localizeErrors(error.details, translate);
|
|
2342
|
+
return out;
|
|
2343
|
+
});
|
|
2344
|
+
}
|
|
2345
|
+
/** A copy of `value`: plain objects and arrays copied, at any depth (error params). */ function copyValue(value) {
|
|
2346
|
+
if (Array.isArray(value)) return value.map(copyValue);
|
|
2347
|
+
if (typeof value === "object" && value !== null) {
|
|
2348
|
+
const out = {};
|
|
2349
|
+
for (const [key, item] of Object.entries(value)) out[key] = copyValue(item);
|
|
2350
|
+
return out;
|
|
2351
|
+
}
|
|
2352
|
+
return value;
|
|
2353
|
+
}
|
|
2354
|
+
/**
|
|
2355
|
+
* A {@link TValidatorTranslate} over a map of message templates keyed by code (and, for app
|
|
2356
|
+
* text, by the text itself): `{name}` placeholders are filled from the error's params. Ids
|
|
2357
|
+
* missing from the map keep their message.
|
|
2358
|
+
*
|
|
2359
|
+
* @example
|
|
2360
|
+
* ```ts
|
|
2361
|
+
* // templates served per locale, e.g. { MIN_LENGTH: 'Mindestens {min} Zeichen', … }
|
|
2362
|
+
* localizeErrors(errors, messagesTranslator(templates))
|
|
2363
|
+
* ```
|
|
2364
|
+
*/ function messagesTranslator(messages) {
|
|
2365
|
+
return (id, params) => Object.hasOwn(messages, id) ? formatValidatorMessage(messages[id], params) : void 0;
|
|
2366
|
+
}
|
|
2367
|
+
|
|
2082
2368
|
//#endregion
|
|
2083
2369
|
//#region packages/typescript/src/runtime/validator.ts
|
|
2084
2370
|
function _define_property(obj, key, value) {
|
|
@@ -2126,36 +2412,67 @@ function _define_property(obj, key, value) {
|
|
|
2126
2412
|
pop(saveErrors) {
|
|
2127
2413
|
this.depth--;
|
|
2128
2414
|
const popped = this.stackErrors.pop();
|
|
2129
|
-
if (saveErrors && popped
|
|
2415
|
+
if (saveErrors && popped) this.reportAll(popped);
|
|
2130
2416
|
return popped;
|
|
2131
2417
|
}
|
|
2132
2418
|
clear() {
|
|
2133
2419
|
this.stackErrors[this.stackErrors.length - 1] = null;
|
|
2134
2420
|
if (this.limitExceeded) this.limitExceeded = false;
|
|
2135
2421
|
}
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
this.stackErrors[this.stackErrors.length - 1] = errors;
|
|
2141
|
-
} else errors = this.errors;
|
|
2422
|
+
/**
|
|
2423
|
+
* Reports an error at `path` (default: the current path). `info` gives its code and params;
|
|
2424
|
+
* without it the error is app text with code `CUSTOM` (a plugin's own message).
|
|
2425
|
+
*/ error(message, path, details, info) {
|
|
2142
2426
|
const error = {
|
|
2143
2427
|
path: path || this.buildPath(),
|
|
2144
|
-
message
|
|
2428
|
+
message,
|
|
2429
|
+
code: info?.code ?? "CUSTOM",
|
|
2430
|
+
params: info?.params ?? {}
|
|
2145
2431
|
};
|
|
2432
|
+
if (info?.custom ?? info?.code === void 0) error.custom = true;
|
|
2146
2433
|
if (details?.length) error.details = details;
|
|
2147
|
-
|
|
2148
|
-
if (errors.length >= this.opts.errorLimit) this.limitExceeded = true;
|
|
2434
|
+
this.report(error);
|
|
2149
2435
|
return error;
|
|
2150
2436
|
}
|
|
2151
|
-
/**
|
|
2437
|
+
/**
|
|
2438
|
+
* Reports built-in error `code`: `custom` (the app's message from an annotation) when given,
|
|
2439
|
+
* else the code's template filled with `params`.
|
|
2440
|
+
*/ fail(code, params, custom) {
|
|
2441
|
+
this.builtin(code, params, custom);
|
|
2442
|
+
return false;
|
|
2443
|
+
}
|
|
2444
|
+
/** Reports built-in error `code` at `path` (see {@link fail}), with `details` (a union's). */ builtin(code, params, custom, path, details) {
|
|
2445
|
+
return custom ? this.error(custom, path, details, {
|
|
2446
|
+
code,
|
|
2447
|
+
params,
|
|
2448
|
+
custom: true
|
|
2449
|
+
}) : this.error(formatValidatorMessage(VALIDATOR_MESSAGES[code], params), path, details, {
|
|
2450
|
+
code,
|
|
2451
|
+
params
|
|
2452
|
+
});
|
|
2453
|
+
}
|
|
2454
|
+
/** {@link fail} of a type mismatch of `def`: its `@expect.message` replaces the message. */ mismatch(def, code, params) {
|
|
2455
|
+
return this.fail(code, params, expectMessage(def));
|
|
2456
|
+
}
|
|
2457
|
+
/** The `@meta.required` error of `def` (custom message or the type's default). */ required(def) {
|
|
2458
|
+
const isBoolean = def.type.kind === "" && def.type.designType === "boolean";
|
|
2459
|
+
return this.fail(isBoolean ? "REQUIRED_CHECKED" : "REQUIRED", {}, messageOf(def.metadata.get("meta.required")));
|
|
2460
|
+
}
|
|
2461
|
+
/**
|
|
2462
|
+
* Adds `error`, already made, to the errors of the current level (as {@link error} does);
|
|
2463
|
+
* `true` once the level holds as many errors as the limit.
|
|
2464
|
+
*/ report(error) {
|
|
2152
2465
|
let errors = this.stackErrors[this.stackErrors.length - 1];
|
|
2153
2466
|
if (!errors) if (this.stackErrors.length > 0) {
|
|
2154
2467
|
errors = [];
|
|
2155
2468
|
this.stackErrors[this.stackErrors.length - 1] = errors;
|
|
2156
2469
|
} else errors = this.errors;
|
|
2157
2470
|
errors.push(error);
|
|
2158
|
-
if (errors.length >= this.opts.errorLimit)
|
|
2471
|
+
if (errors.length >= this.opts.errorLimit) {
|
|
2472
|
+
this.limitExceeded = true;
|
|
2473
|
+
return true;
|
|
2474
|
+
}
|
|
2475
|
+
return false;
|
|
2159
2476
|
}
|
|
2160
2477
|
throw() {
|
|
2161
2478
|
throw new ValidatorError(this.errors);
|
|
@@ -2185,6 +2502,7 @@ function _define_property(obj, key, value) {
|
|
|
2185
2502
|
if (this.unionMemo !== void 0) this.unionMemo = void 0;
|
|
2186
2503
|
if (this.strips.length > 0) performStrips(this.strips, passed);
|
|
2187
2504
|
if (!passed) {
|
|
2505
|
+
if (this.opts.translate) this.errors = localizeErrors(this.errors, this.opts.translate);
|
|
2188
2506
|
if (safe) return false;
|
|
2189
2507
|
this.throw();
|
|
2190
2508
|
}
|
|
@@ -2202,10 +2520,7 @@ function _define_property(obj, key, value) {
|
|
|
2202
2520
|
if (this.limitExceeded) return false;
|
|
2203
2521
|
if (this.hasReplace) def = this.replaced(def);
|
|
2204
2522
|
if (def.optional && (value === void 0 || value === null)) {
|
|
2205
|
-
if (value === null && rejectsNullAsRequired(def))
|
|
2206
|
-
this.error(requiredMessage(def));
|
|
2207
|
-
return false;
|
|
2208
|
-
}
|
|
2523
|
+
if (value === null && rejectsNullAsRequired(def)) return this.required(def);
|
|
2209
2524
|
return true;
|
|
2210
2525
|
}
|
|
2211
2526
|
if (this.hasPlugins) {
|
|
@@ -2219,10 +2534,7 @@ function _define_property(obj, key, value) {
|
|
|
2219
2534
|
* optional `def` passes `undefined` / `null`, a plugin may decide — `undefined` to go on.
|
|
2220
2535
|
*/ preCheck(def, value) {
|
|
2221
2536
|
if (def.optional && (value === void 0 || value === null)) {
|
|
2222
|
-
if (value === null && rejectsNullAsRequired(def))
|
|
2223
|
-
this.error(requiredMessage(def));
|
|
2224
|
-
return false;
|
|
2225
|
-
}
|
|
2537
|
+
if (value === null && rejectsNullAsRequired(def)) return this.required(def);
|
|
2226
2538
|
return true;
|
|
2227
2539
|
}
|
|
2228
2540
|
if (this.hasPlugins) {
|
|
@@ -2244,7 +2556,7 @@ function _define_property(obj, key, value) {
|
|
|
2244
2556
|
if (!passed && this.strips.length !== kept) this.strips.length = kept;
|
|
2245
2557
|
return passed;
|
|
2246
2558
|
},
|
|
2247
|
-
error: (message, path, details) => this.error(message, path, details)
|
|
2559
|
+
error: (message, path, details, info) => this.error(message, path, details, info)
|
|
2248
2560
|
}, {
|
|
2249
2561
|
opts: {
|
|
2250
2562
|
get: () => this.opts,
|
|
@@ -2309,11 +2621,11 @@ function _define_property(obj, key, value) {
|
|
|
2309
2621
|
path = this.buildPath();
|
|
2310
2622
|
const known = this.decided(def, value, path);
|
|
2311
2623
|
if (known !== void 0) {
|
|
2312
|
-
if (known.
|
|
2624
|
+
if (known.errors === void 0) {
|
|
2313
2625
|
stripAgain(this.strips, known.strips);
|
|
2314
2626
|
return this.validateUnionConstraints(def, known.item, value);
|
|
2315
2627
|
}
|
|
2316
|
-
this.
|
|
2628
|
+
this.reportAll(known.errors);
|
|
2317
2629
|
return false;
|
|
2318
2630
|
}
|
|
2319
2631
|
}
|
|
@@ -2322,6 +2634,11 @@ function _define_property(obj, key, value) {
|
|
|
2322
2634
|
} else this.wasted = 0;
|
|
2323
2635
|
const kept = this.strips.length;
|
|
2324
2636
|
let details;
|
|
2637
|
+
/**
|
|
2638
|
+
* Where each failed branch's errors start in `details` (by branch index): plain object
|
|
2639
|
+
* values only, the ones a discriminator can report (see `discriminatedErrors`).
|
|
2640
|
+
*/ let starts;
|
|
2641
|
+
const plainObject = typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2325
2642
|
for (const item of items) {
|
|
2326
2643
|
this.stackErrors.push(null);
|
|
2327
2644
|
const failures = this.nestedFailures;
|
|
@@ -2338,14 +2655,52 @@ function _define_property(obj, key, value) {
|
|
|
2338
2655
|
if (this.limitExceeded) this.limitExceeded = false;
|
|
2339
2656
|
if (this.nestedFailures !== failures) this.wasteBranch();
|
|
2340
2657
|
if (nested) this.nestedFailures++;
|
|
2658
|
+
if (plainObject) (starts ?? (starts = [])).push(details?.length ?? 0);
|
|
2341
2659
|
if (branchErrors) if (details) for (const err of branchErrors) details.push(err);
|
|
2342
2660
|
else details = branchErrors;
|
|
2343
2661
|
}
|
|
2344
|
-
const
|
|
2345
|
-
|
|
2346
|
-
if (path !== void 0) this.decide(def, value, path, kept, void 0, error);
|
|
2662
|
+
const errors = this.discriminatedErrors(def, value, details, starts ?? []) ?? this.unionError(def, details);
|
|
2663
|
+
if (path !== void 0) this.decide(def, value, path, kept, void 0, errors);
|
|
2347
2664
|
return false;
|
|
2348
2665
|
}
|
|
2666
|
+
/** Reports the error of union `def` no branch matched (`details`: the branches' errors). */ unionError(def, details) {
|
|
2667
|
+
const expected = def.type.items.map((item, i) => `[${item.type.kind || item.type.designType}(${i})]`).join(", ");
|
|
2668
|
+
return [this.builtin("UNION", { expected }, expectMessage(def), void 0, details)];
|
|
2669
|
+
}
|
|
2670
|
+
/**
|
|
2671
|
+
* The errors of an object union over object `value` with a shared literal discriminator (see
|
|
2672
|
+
* {@link unionDiscriminator}), reported in place of the union error: the errors of the branch
|
|
2673
|
+
* the value's discriminator picks, or one `UNION_DISCRIMINATOR` error at the discriminator
|
|
2674
|
+
* when it picks none. `undefined` (nothing reported) for other unions and values.
|
|
2675
|
+
*/ discriminatedErrors(def, value, details, starts) {
|
|
2676
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return;
|
|
2677
|
+
const disc = unionDiscriminator(def.type.items);
|
|
2678
|
+
if (disc === void 0) return;
|
|
2679
|
+
const tag = value[disc.property];
|
|
2680
|
+
if (tag === void 0 && this.partialHere()) return;
|
|
2681
|
+
const picked = disc.branches.findIndex(([, literal]) => literal === tag);
|
|
2682
|
+
if (picked < 0) {
|
|
2683
|
+
const params = {
|
|
2684
|
+
property: disc.property,
|
|
2685
|
+
allowed: disc.branches.map(([, literal]) => literal),
|
|
2686
|
+
actual: clip(printValue(tag))
|
|
2687
|
+
};
|
|
2688
|
+
const at = this.depth > 0 ? `${this.buildPath()}.${disc.property}` : disc.property;
|
|
2689
|
+
return [this.builtin("UNION_DISCRIMINATOR", params, expectMessage(def), at)];
|
|
2690
|
+
}
|
|
2691
|
+
const index = disc.branches[picked][0];
|
|
2692
|
+
const errors = details?.slice(starts[index], starts[index + 1] ?? details.length);
|
|
2693
|
+
if (errors === void 0 || errors.length === 0) return;
|
|
2694
|
+
this.reportAll(errors);
|
|
2695
|
+
return errors;
|
|
2696
|
+
}
|
|
2697
|
+
/** Reports `errors`, made already, until the error limit is reached. */ reportAll(errors) {
|
|
2698
|
+
for (const error of errors) if (this.report(error)) break;
|
|
2699
|
+
}
|
|
2700
|
+
/** Whether missing props are allowed at the current depth (the `partial` option). */ partialHere() {
|
|
2701
|
+
const partial = this.opts.partial;
|
|
2702
|
+
return partial === "deep" || partial === true && this.depth === 0 || typeof partial === "function";
|
|
2703
|
+
}
|
|
2349
2704
|
/** A union branch failed after branches of nested unions over objects did (see `unionMemo`). */ wasteBranch() {
|
|
2350
2705
|
if (++this.wasted === WASTE_BEFORE_MEMO && this.unionMemo === void 0) this.unionMemo = /* @__PURE__ */ new Map();
|
|
2351
2706
|
}
|
|
@@ -2359,13 +2714,13 @@ function _define_property(obj, key, value) {
|
|
|
2359
2714
|
/**
|
|
2360
2715
|
* Records the outcome of union `def` over `value` at `path`, entered with `kept` strips (the
|
|
2361
2716
|
* ones after them are its own).
|
|
2362
|
-
*/ decide(def, value, path, kept, item,
|
|
2717
|
+
*/ decide(def, value, path, kept, item, errors) {
|
|
2363
2718
|
const outcome = {
|
|
2364
2719
|
def,
|
|
2365
2720
|
path,
|
|
2366
2721
|
unknownProps: this.opts.unknownProps,
|
|
2367
2722
|
item,
|
|
2368
|
-
|
|
2723
|
+
errors,
|
|
2369
2724
|
strips: stripsSince(this.strips, kept)
|
|
2370
2725
|
};
|
|
2371
2726
|
const outcomes = this.unionMemo.get(value);
|
|
@@ -2385,7 +2740,7 @@ function _define_property(obj, key, value) {
|
|
|
2385
2740
|
case "boolean": return this.validateBooleanConstraints(metadata, value);
|
|
2386
2741
|
case "object":
|
|
2387
2742
|
if (Array.isArray(value)) return this.validateArrayConstraints(metadata, value, arrayElementType(branch));
|
|
2388
|
-
return
|
|
2743
|
+
return value === null || this.validateObjectConstraints(metadata, value, branch, this.skipListHere());
|
|
2389
2744
|
default: return true;
|
|
2390
2745
|
}
|
|
2391
2746
|
}
|
|
@@ -2399,7 +2754,7 @@ function _define_property(obj, key, value) {
|
|
|
2399
2754
|
*/ unknownKey(key, keysToStrip) {
|
|
2400
2755
|
if (this.opts.unknownProps === "error") {
|
|
2401
2756
|
this.push(key);
|
|
2402
|
-
this.
|
|
2757
|
+
this.fail("UNEXPECTED_PROPERTY", { property: clip(key) });
|
|
2403
2758
|
this.pop(true);
|
|
2404
2759
|
} else if (this.opts.unknownProps === "strip") (keysToStrip ?? (keysToStrip = [])).push(key);
|
|
2405
2760
|
return keysToStrip;
|
|
@@ -2412,10 +2767,7 @@ function _define_property(obj, key, value) {
|
|
|
2412
2767
|
return skipList;
|
|
2413
2768
|
}
|
|
2414
2769
|
validateTuple(def, value) {
|
|
2415
|
-
if (!Array.isArray(value) || value.length !== def.type.items.length) {
|
|
2416
|
-
this.error(`Expected array of length ${def.type.items.length}`);
|
|
2417
|
-
return false;
|
|
2418
|
-
}
|
|
2770
|
+
if (!Array.isArray(value) || value.length !== def.type.items.length) return this.mismatch(def, "TUPLE_LENGTH", { length: def.type.items.length });
|
|
2419
2771
|
let i = 0;
|
|
2420
2772
|
for (const item of def.type.items) {
|
|
2421
2773
|
this.push(String(i));
|
|
@@ -2429,10 +2781,7 @@ function _define_property(obj, key, value) {
|
|
|
2429
2781
|
return true;
|
|
2430
2782
|
}
|
|
2431
2783
|
validateArray(def, value) {
|
|
2432
|
-
if (!Array.isArray(value)) {
|
|
2433
|
-
this.error("Expected array");
|
|
2434
|
-
return false;
|
|
2435
|
-
}
|
|
2784
|
+
if (!Array.isArray(value)) return this.mismatch(def, "TYPE_ARRAY", { actual: typeOf(value) });
|
|
2436
2785
|
if (!metaEmpty(def.metadata) && !this.validateArrayConstraints(def.metadata, value, def.type.of)) return false;
|
|
2437
2786
|
let i = 0;
|
|
2438
2787
|
let passed = true;
|
|
@@ -2451,21 +2800,19 @@ function _define_property(obj, key, value) {
|
|
|
2451
2800
|
const metadata = readable(meta);
|
|
2452
2801
|
const minLength = metadata.get("expect.minLength");
|
|
2453
2802
|
if (minLength !== void 0) {
|
|
2454
|
-
const length =
|
|
2455
|
-
if (value.length < length) {
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
}
|
|
2803
|
+
const length = boundOf(minLength, "length");
|
|
2804
|
+
if (value.length < length) return this.fail("MIN_ITEMS", {
|
|
2805
|
+
min: length,
|
|
2806
|
+
actual: value.length
|
|
2807
|
+
}, messageOf(minLength));
|
|
2460
2808
|
}
|
|
2461
2809
|
const maxLength = metadata.get("expect.maxLength");
|
|
2462
2810
|
if (maxLength !== void 0) {
|
|
2463
|
-
const length =
|
|
2464
|
-
if (value.length > length) {
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
}
|
|
2811
|
+
const length = boundOf(maxLength, "length");
|
|
2812
|
+
if (value.length > length) return this.fail("MAX_ITEMS", {
|
|
2813
|
+
max: length,
|
|
2814
|
+
actual: value.length
|
|
2815
|
+
}, messageOf(maxLength));
|
|
2469
2816
|
}
|
|
2470
2817
|
const uniqueItems = metadata.get("expect.array.uniqueItems");
|
|
2471
2818
|
if (uniqueItems) {
|
|
@@ -2481,7 +2828,7 @@ function _define_property(obj, key, value) {
|
|
|
2481
2828
|
} else key = JSON.stringify(item);
|
|
2482
2829
|
if (seen.has(key)) {
|
|
2483
2830
|
this.push(String(idx));
|
|
2484
|
-
this.
|
|
2831
|
+
this.fail("UNIQUE_ITEMS", {}, messageOf(uniqueItems));
|
|
2485
2832
|
this.pop(true);
|
|
2486
2833
|
return false;
|
|
2487
2834
|
}
|
|
@@ -2491,13 +2838,14 @@ function _define_property(obj, key, value) {
|
|
|
2491
2838
|
return true;
|
|
2492
2839
|
}
|
|
2493
2840
|
validateObject(def, value) {
|
|
2494
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
2495
|
-
|
|
2496
|
-
return false;
|
|
2497
|
-
}
|
|
2841
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return this.mismatch(def, "TYPE_OBJECT", { actual: typeOf(value) });
|
|
2842
|
+
const skipList = this.opts.skipList ? this.skipListHere() : void 0;
|
|
2498
2843
|
let passed = true;
|
|
2844
|
+
if (!this.validateObjectConstraints(def.metadata, value, def, skipList)) {
|
|
2845
|
+
if (this.limitExceeded) return false;
|
|
2846
|
+
passed = false;
|
|
2847
|
+
}
|
|
2499
2848
|
let keysToStrip;
|
|
2500
|
-
const skipList = this.opts.skipList ? this.skipListHere() : void 0;
|
|
2501
2849
|
let partialFunctionMatched = false;
|
|
2502
2850
|
if (typeof this.opts.partial === "function") partialFunctionMatched = this.opts.partial(def, this.buildPath());
|
|
2503
2851
|
for (const [key, item] of def.type.props.entries()) {
|
|
@@ -2557,63 +2905,58 @@ function _define_property(obj, key, value) {
|
|
|
2557
2905
|
}
|
|
2558
2906
|
validatePrimitive(def, value) {
|
|
2559
2907
|
if (def.type.value !== void 0) {
|
|
2560
|
-
if (value !== def.type.value) {
|
|
2561
|
-
|
|
2562
|
-
|
|
2563
|
-
}
|
|
2908
|
+
if (value !== def.type.value) return this.mismatch(def, "LITERAL", {
|
|
2909
|
+
expected: def.type.value,
|
|
2910
|
+
actual: clip(printValue(value))
|
|
2911
|
+
});
|
|
2564
2912
|
return true;
|
|
2565
2913
|
}
|
|
2566
|
-
const typeOfValue =
|
|
2567
|
-
|
|
2568
|
-
|
|
2569
|
-
|
|
2570
|
-
|
|
2571
|
-
|
|
2914
|
+
const typeOfValue = typeOf(value);
|
|
2915
|
+
const designType = def.type.designType;
|
|
2916
|
+
switch (designType) {
|
|
2917
|
+
case "never": return this.fail("NEVER", {});
|
|
2918
|
+
case "any":
|
|
2919
|
+
if (value === void 0) return this.mismatch(def, "TYPE", {
|
|
2920
|
+
expected: "unknown",
|
|
2921
|
+
actual: typeOfValue
|
|
2922
|
+
});
|
|
2923
|
+
return true;
|
|
2572
2924
|
case "string":
|
|
2573
|
-
if (typeOfValue !==
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
}
|
|
2925
|
+
if (typeOfValue !== designType) return this.mismatch(def, "TYPE", {
|
|
2926
|
+
expected: designType,
|
|
2927
|
+
actual: typeOfValue
|
|
2928
|
+
});
|
|
2577
2929
|
return this.validateString(def, value);
|
|
2578
2930
|
case "number":
|
|
2579
|
-
if (typeOfValue !==
|
|
2580
|
-
|
|
2581
|
-
|
|
2582
|
-
}
|
|
2583
|
-
if (!Number.isFinite(value)) {
|
|
2584
|
-
this.error(`Expected finite number, got ${value}`);
|
|
2585
|
-
return false;
|
|
2586
|
-
}
|
|
2931
|
+
if (typeOfValue !== designType) return this.mismatch(def, "TYPE", {
|
|
2932
|
+
expected: designType,
|
|
2933
|
+
actual: typeOfValue
|
|
2934
|
+
});
|
|
2935
|
+
if (!Number.isFinite(value)) return this.mismatch(def, "NOT_FINITE", { actual: String(value) });
|
|
2587
2936
|
return this.validateNumber(def, value);
|
|
2588
2937
|
case "boolean":
|
|
2589
|
-
if (typeOfValue !==
|
|
2590
|
-
|
|
2591
|
-
|
|
2592
|
-
}
|
|
2938
|
+
if (typeOfValue !== designType) return this.mismatch(def, "TYPE", {
|
|
2939
|
+
expected: designType,
|
|
2940
|
+
actual: typeOfValue
|
|
2941
|
+
});
|
|
2593
2942
|
return this.validateBoolean(def, value);
|
|
2594
2943
|
case "undefined":
|
|
2595
|
-
if (value !== void 0) {
|
|
2596
|
-
|
|
2597
|
-
|
|
2598
|
-
}
|
|
2944
|
+
if (value !== void 0) return this.mismatch(def, "TYPE", {
|
|
2945
|
+
expected: designType,
|
|
2946
|
+
actual: typeOfValue
|
|
2947
|
+
});
|
|
2599
2948
|
return true;
|
|
2600
2949
|
case "null":
|
|
2601
|
-
if (value !== null) {
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
}
|
|
2950
|
+
if (value !== null) return this.mismatch(def, "TYPE", {
|
|
2951
|
+
expected: designType,
|
|
2952
|
+
actual: typeOfValue
|
|
2953
|
+
});
|
|
2605
2954
|
return true;
|
|
2606
2955
|
case "decimal":
|
|
2607
|
-
if (typeOfValue !== "string") {
|
|
2608
|
-
|
|
2609
|
-
return false;
|
|
2610
|
-
}
|
|
2611
|
-
if (!DECIMAL_RE.test(value)) {
|
|
2612
|
-
this.error(`Invalid decimal format: ${JSON.stringify(value)}`);
|
|
2613
|
-
return false;
|
|
2614
|
-
}
|
|
2956
|
+
if (typeOfValue !== "string") return this.mismatch(def, "TYPE_DECIMAL", { actual: typeOfValue });
|
|
2957
|
+
if (!DECIMAL_RE.test(value)) return this.mismatch(def, "DECIMAL_FORMAT", { value: clip(value) });
|
|
2615
2958
|
return true;
|
|
2616
|
-
default: throw new Error(`Unknown type "${
|
|
2959
|
+
default: throw new Error(`Unknown type "${designType}"`);
|
|
2617
2960
|
}
|
|
2618
2961
|
}
|
|
2619
2962
|
validateString(def, value) {
|
|
@@ -2624,36 +2967,33 @@ function _define_property(obj, key, value) {
|
|
|
2624
2967
|
if (metadata === void 0) return true;
|
|
2625
2968
|
const filled = metadata.get("meta.required");
|
|
2626
2969
|
if (filled) {
|
|
2627
|
-
if (value.trim().length === 0) {
|
|
2628
|
-
const message = typeof filled === "object" && filled.message ? filled.message : `Must not be empty`;
|
|
2629
|
-
this.error(message);
|
|
2630
|
-
return false;
|
|
2631
|
-
}
|
|
2970
|
+
if (value.trim().length === 0) return this.fail("REQUIRED", {}, messageOf(filled));
|
|
2632
2971
|
}
|
|
2633
2972
|
const minLength = metadata.get("expect.minLength");
|
|
2634
2973
|
if (minLength !== void 0) {
|
|
2635
|
-
const length =
|
|
2636
|
-
if (value.length < length) {
|
|
2637
|
-
|
|
2638
|
-
|
|
2639
|
-
|
|
2640
|
-
}
|
|
2974
|
+
const length = boundOf(minLength, "length");
|
|
2975
|
+
if (value.length < length) return this.fail("MIN_LENGTH", {
|
|
2976
|
+
min: length,
|
|
2977
|
+
actual: value.length
|
|
2978
|
+
}, messageOf(minLength));
|
|
2641
2979
|
}
|
|
2642
2980
|
const maxLength = metadata.get("expect.maxLength");
|
|
2643
2981
|
if (maxLength !== void 0) {
|
|
2644
|
-
const length =
|
|
2645
|
-
if (value.length > length) {
|
|
2646
|
-
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
}
|
|
2982
|
+
const length = boundOf(maxLength, "length");
|
|
2983
|
+
if (value.length > length) return this.fail("MAX_LENGTH", {
|
|
2984
|
+
max: length,
|
|
2985
|
+
actual: value.length
|
|
2986
|
+
}, messageOf(maxLength));
|
|
2650
2987
|
}
|
|
2651
2988
|
const patterns = metadata.get("expect.pattern");
|
|
2652
2989
|
for (const { pattern, flags, message } of patterns || []) {
|
|
2653
2990
|
if (!pattern) continue;
|
|
2654
2991
|
if (!getRegex(pattern, flags).test(value)) {
|
|
2655
|
-
|
|
2656
|
-
return
|
|
2992
|
+
const format = message ? formatCodeOf(message) : void 0;
|
|
2993
|
+
return format === void 0 ? this.fail("PATTERN", flags ? {
|
|
2994
|
+
pattern,
|
|
2995
|
+
flags
|
|
2996
|
+
} : { pattern }, message) : this.fail(format, {});
|
|
2657
2997
|
}
|
|
2658
2998
|
}
|
|
2659
2999
|
return true;
|
|
@@ -2665,38 +3005,41 @@ function _define_property(obj, key, value) {
|
|
|
2665
3005
|
const metadata = filledMeta(meta);
|
|
2666
3006
|
if (metadata === void 0) return true;
|
|
2667
3007
|
const int = metadata.get("expect.int");
|
|
2668
|
-
if (int && value % 1 !== 0) {
|
|
2669
|
-
const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
|
|
2670
|
-
this.error(message);
|
|
2671
|
-
return false;
|
|
2672
|
-
}
|
|
3008
|
+
if (int && value % 1 !== 0) return this.fail("INT", { actual: value }, messageOf(int));
|
|
2673
3009
|
const min = metadata.get("expect.min");
|
|
2674
3010
|
if (min !== void 0) {
|
|
2675
|
-
const
|
|
2676
|
-
if (value <
|
|
3011
|
+
const bound = boundOf(min, "minValue");
|
|
3012
|
+
if (value < bound) return this.fail("MIN", {
|
|
3013
|
+
min: bound,
|
|
3014
|
+
actual: value
|
|
3015
|
+
}, messageOf(min));
|
|
2677
3016
|
}
|
|
2678
3017
|
const max = metadata.get("expect.max");
|
|
2679
3018
|
if (max !== void 0) {
|
|
2680
|
-
const
|
|
2681
|
-
if (value >
|
|
3019
|
+
const bound = boundOf(max, "maxValue");
|
|
3020
|
+
if (value > bound) return this.fail("MAX", {
|
|
3021
|
+
max: bound,
|
|
3022
|
+
actual: value
|
|
3023
|
+
}, messageOf(max));
|
|
2682
3024
|
}
|
|
2683
3025
|
const exMin = metadata.get("expect.exclusiveMin");
|
|
2684
3026
|
if (exMin !== void 0) {
|
|
2685
|
-
const bound =
|
|
2686
|
-
if (value <= bound) return this.
|
|
3027
|
+
const bound = boundOf(exMin, "exclusiveMinValue");
|
|
3028
|
+
if (value <= bound) return this.fail("EXCLUSIVE_MIN", {
|
|
3029
|
+
min: bound,
|
|
3030
|
+
actual: value
|
|
3031
|
+
}, messageOf(exMin));
|
|
2687
3032
|
}
|
|
2688
3033
|
const exMax = metadata.get("expect.exclusiveMax");
|
|
2689
3034
|
if (exMax !== void 0) {
|
|
2690
|
-
const bound =
|
|
2691
|
-
if (value >= bound) return this.
|
|
3035
|
+
const bound = boundOf(exMax, "exclusiveMaxValue");
|
|
3036
|
+
if (value >= bound) return this.fail("EXCLUSIVE_MAX", {
|
|
3037
|
+
max: bound,
|
|
3038
|
+
actual: value
|
|
3039
|
+
}, messageOf(exMax));
|
|
2692
3040
|
}
|
|
2693
3041
|
return true;
|
|
2694
3042
|
}
|
|
2695
|
-
/** Reports a failed number bound: its custom message, else `Expected <label> <bound>, got <value>`. */ numberBoundFailed(annotation, label, bound, value) {
|
|
2696
|
-
const message = typeof annotation === "object" && annotation.message ? annotation.message : `Expected ${label} ${bound}, got ${value}`;
|
|
2697
|
-
this.error(message);
|
|
2698
|
-
return false;
|
|
2699
|
-
}
|
|
2700
3043
|
validateBoolean(def, value) {
|
|
2701
3044
|
return this.validateBooleanConstraints(def.metadata, value);
|
|
2702
3045
|
}
|
|
@@ -2705,14 +3048,59 @@ function _define_property(obj, key, value) {
|
|
|
2705
3048
|
if (metadata === void 0) return true;
|
|
2706
3049
|
const filled = metadata.get("meta.required");
|
|
2707
3050
|
if (filled) {
|
|
2708
|
-
if (value !== true) {
|
|
2709
|
-
|
|
2710
|
-
|
|
2711
|
-
|
|
2712
|
-
|
|
3051
|
+
if (value !== true) return this.fail("REQUIRED_CHECKED", {}, messageOf(filled));
|
|
3052
|
+
}
|
|
3053
|
+
return true;
|
|
3054
|
+
}
|
|
3055
|
+
/**
|
|
3056
|
+
* `@expect.minKeys` / `maxKeys` checks for an object value, matched as `shape`: the keys the
|
|
3057
|
+
* result keeps are counted (see {@link keptKeyCount}; `skipList`: the skip list keys here).
|
|
3058
|
+
*/ validateObjectConstraints(meta, value, shape, skipList) {
|
|
3059
|
+
const metadata = filledMeta(meta);
|
|
3060
|
+
if (metadata === void 0) return true;
|
|
3061
|
+
const minKeys = metadata.get("expect.minKeys");
|
|
3062
|
+
const maxKeys = metadata.get("expect.maxKeys");
|
|
3063
|
+
if (minKeys === void 0 && maxKeys === void 0) return true;
|
|
3064
|
+
const count = this.keptKeyCount(shape, value, skipList);
|
|
3065
|
+
if (minKeys !== void 0) {
|
|
3066
|
+
const bound = boundOf(minKeys, "count");
|
|
3067
|
+
if (count < bound) return this.fail("MIN_KEYS", {
|
|
3068
|
+
min: bound,
|
|
3069
|
+
actual: count
|
|
3070
|
+
}, messageOf(minKeys));
|
|
3071
|
+
}
|
|
3072
|
+
if (maxKeys !== void 0) {
|
|
3073
|
+
const bound = boundOf(maxKeys, "count");
|
|
3074
|
+
if (count > bound) return this.fail("MAX_KEYS", {
|
|
3075
|
+
max: bound,
|
|
3076
|
+
actual: count
|
|
3077
|
+
}, messageOf(maxKeys));
|
|
2713
3078
|
}
|
|
2714
3079
|
return true;
|
|
2715
3080
|
}
|
|
3081
|
+
/**
|
|
3082
|
+
* The keys of object `value` (matched as `shape`) the result keeps: every own key with
|
|
3083
|
+
* `unknownProps: 'ignore'`; else (and with 'strip', and 'error', where the others are errors
|
|
3084
|
+
* of their own) the keys `shape` declares, its pattern props match or the skip list names.
|
|
3085
|
+
* Matched as anything other than an object type, every own key.
|
|
3086
|
+
*/ keptKeyCount(shape, value, skipList) {
|
|
3087
|
+
const keys = Object.keys(value);
|
|
3088
|
+
if (this.opts.unknownProps === "ignore" || shape.type.kind !== "object") return keys.length;
|
|
3089
|
+
const type = shape.type;
|
|
3090
|
+
let count = 0;
|
|
3091
|
+
for (const key of keys) {
|
|
3092
|
+
const prop = type.props.get(key);
|
|
3093
|
+
if (prop !== void 0 && !isPhantomType(prop) || skipList?.has(key)) {
|
|
3094
|
+
count++;
|
|
3095
|
+
continue;
|
|
3096
|
+
}
|
|
3097
|
+
for (const { pattern } of type.propsPatterns) if (pattern.test(key)) {
|
|
3098
|
+
count++;
|
|
3099
|
+
break;
|
|
3100
|
+
}
|
|
3101
|
+
}
|
|
3102
|
+
return count;
|
|
3103
|
+
}
|
|
2716
3104
|
constructor(def, opts) {
|
|
2717
3105
|
_define_property(this, "def", void 0);
|
|
2718
3106
|
_define_property(this, "opts", void 0);
|
|
@@ -2767,10 +3155,78 @@ function _define_property(obj, key, value) {
|
|
|
2767
3155
|
if (this.hasReplace) this.replaceCache = /* @__PURE__ */ new WeakMap();
|
|
2768
3156
|
}
|
|
2769
3157
|
};
|
|
2770
|
-
/** The
|
|
2771
|
-
|
|
2772
|
-
|
|
2773
|
-
|
|
3158
|
+
/** The message argument of a constraint annotation (`{ message }`), if any. */ function messageOf(annotation) {
|
|
3159
|
+
return typeof annotation === "object" && annotation !== null ? annotation.message || void 0 : void 0;
|
|
3160
|
+
}
|
|
3161
|
+
/**
|
|
3162
|
+
* Decided discriminators, by union `items` (reported errors only: a union's discriminator is
|
|
3163
|
+
* looked for once its value failed). An entry is reused while the items, their type defs and the
|
|
3164
|
+
* discriminator props are the same objects.
|
|
3165
|
+
*/ const discCache = /* @__PURE__ */ new WeakMap();
|
|
3166
|
+
/**
|
|
3167
|
+
* The discriminator of a union over objects (see {@link detectDiscriminator}), required in every
|
|
3168
|
+
* object branch: the property and, per object branch, its index in `items` and its literal. The
|
|
3169
|
+
* other branches must reject any object (scalars, arrays, tuples), so only the object branches
|
|
3170
|
+
* can match an object value.
|
|
3171
|
+
*/ function unionDiscriminator(items) {
|
|
3172
|
+
const cached = discCache.get(items);
|
|
3173
|
+
if (cached !== void 0 && sameDiscSource(cached, items)) return cached.disc;
|
|
3174
|
+
const entry = {
|
|
3175
|
+
nodes: [...items],
|
|
3176
|
+
types: items.map((item) => item.type),
|
|
3177
|
+
props: [],
|
|
3178
|
+
disc: findDiscriminator(items)
|
|
3179
|
+
};
|
|
3180
|
+
if (entry.disc !== void 0) {
|
|
3181
|
+
const property = entry.disc.property;
|
|
3182
|
+
entry.props = entry.disc.branches.map(([i]) => items[i].type.props.get(property));
|
|
3183
|
+
}
|
|
3184
|
+
discCache.set(items, entry);
|
|
3185
|
+
return entry.disc;
|
|
3186
|
+
}
|
|
3187
|
+
/** Whether `entry` was decided from `items` as they are. */ function sameDiscSource(entry, items) {
|
|
3188
|
+
if (entry.nodes.length !== items.length) return false;
|
|
3189
|
+
for (let i = 0; i < items.length; i++) if (entry.nodes[i] !== items[i] || entry.types[i] !== items[i].type) return false;
|
|
3190
|
+
const disc = entry.disc;
|
|
3191
|
+
if (disc !== void 0) {
|
|
3192
|
+
const branches = disc.branches;
|
|
3193
|
+
for (let j = 0; j < branches.length; j++) if (items[branches[j][0]].type.props.get(disc.property) !== entry.props[j]) return false;
|
|
3194
|
+
}
|
|
3195
|
+
return true;
|
|
3196
|
+
}
|
|
3197
|
+
/** {@link unionDiscriminator}, decided anew. */ function findDiscriminator(items) {
|
|
3198
|
+
const objects = [];
|
|
3199
|
+
const indexes = [];
|
|
3200
|
+
for (const [i, item] of items.entries()) {
|
|
3201
|
+
const type = item.type;
|
|
3202
|
+
if (type.kind === "object") {
|
|
3203
|
+
objects.push(item);
|
|
3204
|
+
indexes.push(i);
|
|
3205
|
+
} else if (type.kind !== "array" && type.kind !== "tuple" && (type.kind !== "" || type.designType === "any" || isPhantomType(item))) return;
|
|
3206
|
+
}
|
|
3207
|
+
const disc = detectDiscriminator(objects);
|
|
3208
|
+
if (!disc) return;
|
|
3209
|
+
const property = disc.propertyName;
|
|
3210
|
+
const branches = [];
|
|
3211
|
+
for (const [j, item] of objects.entries()) {
|
|
3212
|
+
const prop = item.type.props.get(property);
|
|
3213
|
+
if (prop === void 0 || prop.optional) return;
|
|
3214
|
+
branches.push([indexes[j], prop.type.value]);
|
|
3215
|
+
}
|
|
3216
|
+
return {
|
|
3217
|
+
property,
|
|
3218
|
+
branches
|
|
3219
|
+
};
|
|
3220
|
+
}
|
|
3221
|
+
/** The most characters of input copied into params (see {@link clip}). */ const CLIP = 100;
|
|
3222
|
+
/** Input copied into params: at most {@link CLIP} characters (then an ellipsis). */ function clip(text) {
|
|
3223
|
+
return text.length > CLIP ? `${text.slice(0, CLIP)}…` : text;
|
|
3224
|
+
}
|
|
3225
|
+
/** `def`'s `@expect.message` (the message of its type mismatches), if any. */ function expectMessage(def) {
|
|
3226
|
+
return def.metadata.get("expect.message") || void 0;
|
|
3227
|
+
}
|
|
3228
|
+
/** The type name an error reports for `value` (`array` for arrays, else `typeof`). */ function typeOf(value) {
|
|
3229
|
+
return Array.isArray(value) ? "array" : typeof value;
|
|
2774
3230
|
}
|
|
2775
3231
|
/**
|
|
2776
3232
|
* Element type of the array a union branch holds — through nested unions too
|
|
@@ -3321,10 +3777,25 @@ function recordsOf(m) {
|
|
|
3321
3777
|
* A block write of an append key: the own list is the elements written before any block (by
|
|
3322
3778
|
* the builder, or a raw write since), then every block's part by rank.
|
|
3323
3779
|
*/ function appendKey(m, key, values, rank) {
|
|
3780
|
+
const r = appendRecord(m, key);
|
|
3781
|
+
const { parts } = r;
|
|
3782
|
+
let at = 0;
|
|
3783
|
+
while (at < parts.length && compare(parts[at].rank, rank) < 0) at++;
|
|
3784
|
+
if (at < parts.length && compare(parts[at].rank, rank) === 0) parts[at].values = values;
|
|
3785
|
+
else parts.splice(at, 0, {
|
|
3786
|
+
rank,
|
|
3787
|
+
values
|
|
3788
|
+
});
|
|
3789
|
+
writeAppend(m, key, r);
|
|
3790
|
+
}
|
|
3791
|
+
/**
|
|
3792
|
+
* The block record of append key `key` of `m`: a new one when a raw write since changed the own
|
|
3793
|
+
* list (the elements written before any block: the builder's, or that raw write).
|
|
3794
|
+
*/ function appendRecord(m, key) {
|
|
3324
3795
|
const recs = recordsOf(m);
|
|
3325
3796
|
let r = recs.get(key);
|
|
3326
3797
|
const own = ownValue(m, key);
|
|
3327
|
-
if (!r || own !== r.value || r.value.length !== r.length) {
|
|
3798
|
+
if (!r || own !== r.value || r.value !== void 0 && r.value.length !== r.length) {
|
|
3328
3799
|
const before = own === void 0 ? [] : Array.isArray(own) ? [...own] : [own];
|
|
3329
3800
|
r = {
|
|
3330
3801
|
before,
|
|
@@ -3332,28 +3803,59 @@ function recordsOf(m) {
|
|
|
3332
3803
|
value: before,
|
|
3333
3804
|
length: 0
|
|
3334
3805
|
};
|
|
3806
|
+
recs.set(key, r);
|
|
3807
|
+
}
|
|
3808
|
+
return r;
|
|
3809
|
+
}
|
|
3810
|
+
/** Writes append key `key`'s own list from its record: the elements, then the parts by rank. */ function writeAppend(m, key, r) {
|
|
3811
|
+
const reset = r.reset;
|
|
3812
|
+
const value = reset ? [] : [...r.before];
|
|
3813
|
+
for (const p of r.parts) if (!reset || compare(p.rank, reset) >= 0) value.push(...p.values);
|
|
3814
|
+
if (value.length > 0) {
|
|
3815
|
+
m.set(key, value);
|
|
3816
|
+
r.value = value;
|
|
3817
|
+
} else {
|
|
3818
|
+
m.delete(key);
|
|
3819
|
+
r.value = void 0;
|
|
3335
3820
|
}
|
|
3336
|
-
const { parts } = r;
|
|
3337
|
-
let at = 0;
|
|
3338
|
-
while (at < parts.length && compare(parts[at].rank, rank) < 0) at++;
|
|
3339
|
-
if (at < parts.length && compare(parts[at].rank, rank) === 0) parts[at].values = values;
|
|
3340
|
-
else parts.splice(at, 0, {
|
|
3341
|
-
rank,
|
|
3342
|
-
values
|
|
3343
|
-
});
|
|
3344
|
-
const value = [...r.before];
|
|
3345
|
-
for (const p of parts) value.push(...p.values);
|
|
3346
|
-
m.set(key, value);
|
|
3347
|
-
r.value = value;
|
|
3348
3821
|
r.length = value.length;
|
|
3349
|
-
|
|
3822
|
+
if (reset) hideKey(m, key);
|
|
3823
|
+
}
|
|
3824
|
+
/**
|
|
3825
|
+
* `@annotate.reset 'key'` by a block of rank `rank` at the node of map `m`: the node reads as if
|
|
3826
|
+
* the key had no value below that block — none from its bases (the types it references, the
|
|
3827
|
+
* target of an `as` copy: hidden, see `hideKey`), none written on the node itself (its
|
|
3828
|
+
* declaration, lower-ranked blocks). A block ranked at or above it (the resetting block's own
|
|
3829
|
+
* values included) writes as usual; one ranked below is ignored, whatever the load order.
|
|
3830
|
+
*/ function resetKey(m, key, rank) {
|
|
3831
|
+
if (isAppendKey(key)) {
|
|
3832
|
+
const r = appendRecord(m, key);
|
|
3833
|
+
if (!r.reset || compare(r.reset, rank) < 0) r.reset = rank;
|
|
3834
|
+
writeAppend(m, key, r);
|
|
3835
|
+
return;
|
|
3836
|
+
}
|
|
3837
|
+
const recs = recordsOf(m);
|
|
3838
|
+
const r = recs.get(key);
|
|
3839
|
+
if (!(r && ownValue(m, key) === r.value && compare(r.rank, rank) > 0)) {
|
|
3840
|
+
m.delete(key);
|
|
3841
|
+
recs.set(key, {
|
|
3842
|
+
rank,
|
|
3843
|
+
value: void 0
|
|
3844
|
+
});
|
|
3845
|
+
}
|
|
3846
|
+
hideKey(m, key);
|
|
3350
3847
|
}
|
|
3848
|
+
/** The block annotation resetting the annotation it names at its node (`@annotate.reset`). */ const RESET = "annotate.reset";
|
|
3351
3849
|
function writeEntry(m, anns, rank) {
|
|
3850
|
+
for (const [key, value] of anns) if (key === RESET) resetKey(m, value, rank);
|
|
3352
3851
|
const values = /* @__PURE__ */ new Map();
|
|
3353
|
-
for (const [key, value, list] of anns)
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3852
|
+
for (const [key, value, list] of anns) {
|
|
3853
|
+
if (key === RESET) continue;
|
|
3854
|
+
if (list) {
|
|
3855
|
+
const xs = values.get(key);
|
|
3856
|
+
values.set(key, xs ? [...xs, value] : [value]);
|
|
3857
|
+
} else values.set(key, value);
|
|
3858
|
+
}
|
|
3357
3859
|
for (const [key, value] of values) if (isAppendKey(key)) appendKey(m, key, Array.isArray(value) ? value : [value], rank);
|
|
3358
3860
|
else writeKey(m, key, value, rank);
|
|
3359
3861
|
}
|
|
@@ -3448,4 +3950,68 @@ const queue = [];
|
|
|
3448
3950
|
}
|
|
3449
3951
|
|
|
3450
3952
|
//#endregion
|
|
3451
|
-
|
|
3953
|
+
//#region packages/typescript/src/runtime/user-text.ts
|
|
3954
|
+
/**
|
|
3955
|
+
* The annotations holding user-facing text (labels, descriptions, error messages): the arguments
|
|
3956
|
+
* their specs flag `userText`. Core's are known from the start ({@link CORE_USER_TEXT}); a plugin's
|
|
3957
|
+
* are registered by the generated modules that write them (`$ut`), or by the plugin itself
|
|
3958
|
+
* ({@link registerUserText}), so a translator finds them at runtime without the specs.
|
|
3959
|
+
*/ /** `true`: the value itself is the text; a list: those fields of the value object are. */ /**
|
|
3960
|
+
* The user-facing text of `@atscript/core`'s annotations (its specs' `userText` arguments; a test
|
|
3961
|
+
* keeps them equal).
|
|
3962
|
+
*/ const CORE_USER_TEXT = new Map([
|
|
3963
|
+
["meta.label", true],
|
|
3964
|
+
["meta.description", true],
|
|
3965
|
+
["meta.required", ["message"]],
|
|
3966
|
+
["expect.minLength", ["message"]],
|
|
3967
|
+
["expect.maxLength", ["message"]],
|
|
3968
|
+
["expect.min", ["message"]],
|
|
3969
|
+
["expect.max", ["message"]],
|
|
3970
|
+
["expect.exclusiveMin", ["message"]],
|
|
3971
|
+
["expect.exclusiveMax", ["message"]],
|
|
3972
|
+
["expect.int", ["message"]],
|
|
3973
|
+
["expect.minKeys", ["message"]],
|
|
3974
|
+
["expect.maxKeys", ["message"]],
|
|
3975
|
+
["expect.message", true],
|
|
3976
|
+
["expect.pattern", ["message"]],
|
|
3977
|
+
["expect.array.uniqueItems", ["message"]],
|
|
3978
|
+
["expect.array.key", ["message"]]
|
|
3979
|
+
]);
|
|
3980
|
+
const USER_TEXT = new Map(CORE_USER_TEXT);
|
|
3981
|
+
/**
|
|
3982
|
+
* Registers metadata key `key` as holding user-facing text: `true` (default) — its value is the
|
|
3983
|
+
* text; a list of field names — those fields of its value object are. Fields add up across calls.
|
|
3984
|
+
* For plugins that write such metadata at runtime; generated modules register what they write.
|
|
3985
|
+
*/ function registerUserText(key, fields = true) {
|
|
3986
|
+
const known = USER_TEXT.get(key);
|
|
3987
|
+
if (known === true || fields === true) {
|
|
3988
|
+
USER_TEXT.set(key, true);
|
|
3989
|
+
return;
|
|
3990
|
+
}
|
|
3991
|
+
const added = fields.filter((f) => !known?.includes(f));
|
|
3992
|
+
if (added.length > 0 || !known) USER_TEXT.set(key, [...known ?? [], ...added]);
|
|
3993
|
+
}
|
|
3994
|
+
/**
|
|
3995
|
+
* Registers user-text keys written by a generated module (see {@link registerUserText}): `key` —
|
|
3996
|
+
* the value itself; `key:field` — that field of the value object.
|
|
3997
|
+
*/ function $ut(...entries) {
|
|
3998
|
+
for (const entry of entries) {
|
|
3999
|
+
const at = entry.indexOf(":");
|
|
4000
|
+
if (at === -1) registerUserText(entry);
|
|
4001
|
+
else registerUserText(entry.slice(0, at), [entry.slice(at + 1)]);
|
|
4002
|
+
}
|
|
4003
|
+
}
|
|
4004
|
+
/**
|
|
4005
|
+
* Where metadata key `key` holds user-facing text: `true` — its value is the text (a string; for
|
|
4006
|
+
* a `multiple` annotation, each item of the list); a list of field names — those fields of its
|
|
4007
|
+
* value object (each item's, for a list); `undefined` — no user-facing text. Core keys are known
|
|
4008
|
+
* from the start; a plugin's once a module writing them is loaded (or the plugin registered them).
|
|
4009
|
+
*/ function userTextOf(key) {
|
|
4010
|
+
return USER_TEXT.get(key);
|
|
4011
|
+
}
|
|
4012
|
+
/** Every metadata key holding user-facing text known so far (see {@link userTextOf}). */ function userTextKeys() {
|
|
4013
|
+
return USER_TEXT;
|
|
4014
|
+
}
|
|
4015
|
+
|
|
4016
|
+
//#endregion
|
|
4017
|
+
export { boundOf as $, propAtChain as A, jsonSchemaPattern as B, createAnnotatedTypeNode as C, isAnnotatedType as D, defineAnnotatedType as E, formatValidatorMessage as F, metaEmpty as G, $ak as H, localizeErrors as I, metadataWriteCount as J, watchLayer as K, messagesTranslator as L, ValidatorError as M, VALIDATOR_ERROR_CODES as N, isAnnotatedTypeOfPrimitive as O, VALIDATOR_MESSAGES as P, arrayKeyProps as Q, registerCompiledCheck as R, annotate as S, createObjectTypeDef as T, $nr as U, statelessRegExp as V, LayerMap as W, DECIMAL_RE as X, touched as Y, VALIDATOR_KEYS as Z, pendingStatements as _, userTextOf as a, cutKey as b, $blk as c, $fc as d, $ft as f, appliedBlockModules as g, $sh as h, userTextKeys as i, Validator as j, isPhantomType as k, $end as l, $q as m, CORE_USER_TEXT as n, uniqueName as o, $ps as p, invalidateCompiledValidators as q, registerUserText as r, $alias as s, $ut as t, $f as u, stageEnds as v, createMetadataMap as w, enterCut as x, cutAs as y, detectDiscriminator as z };
|