@atscript/typescript 0.1.107 → 0.1.109

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.
@@ -1,37 +1,93 @@
1
1
 
2
2
  //#region packages/typescript/src/runtime/validator-fast-check.ts
3
+ function _define_property$1(obj, key, value) {
4
+ if (key in obj) Object.defineProperty(obj, key, {
5
+ value,
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true
9
+ });
10
+ else obj[key] = value;
11
+ return obj;
12
+ }
3
13
  /**
4
14
  * Allocation-free boolean pre-check used by {@link Validator.validate} before the
5
15
  * error-collecting walk.
6
16
  *
7
- * It reads the type tree and metadata LIVE on every call (nothing derived from the
8
- * type is cached), so `annotate()` / `metadata.set()` / prop mutations made after a
9
- * validator was created are always honoured — the same as the full walk.
17
+ * Nodes are checked by the generic functions below, which read the type tree and metadata
18
+ * LIVE on every visit. Array items — the same type visited many times per call — go
19
+ * through a compiled {@link Plan} of the item type instead (cached per node, shared by every
20
+ * validator and option set). A plan snapshots what its node's check reads and re-verifies
21
+ * every snapshot against the live node lazily, once per call, right before first using it
22
+ * (rebuilding it when anything differs); constraint objects and pattern arrays are read
23
+ * live on every visit. Either way `annotate()` / `metadata.set()` / prop mutations made
24
+ * after a validator was created are always honoured — the same as the full walk.
10
25
  *
11
26
  * Result contract (relative to the full walk over the same node and value):
12
- * - `PASS` — the walk returns `true`, collects no errors and has no side effects.
13
- * - `FAIL` — the walk returns `false`; every node the walk would visit was visited
14
- * here too (so a walk that would throw makes this throw first) and the walk has
15
- * no side effects for this node.
27
+ * - `PASS` — the walk returns `true`, collects no errors and has no side effects other
28
+ * than the strips recorded here (see below).
29
+ * - `FAIL` — the walk returns `false`. Inside a union, every node the walk would visit
30
+ * was visited here too (so a walk that would throw makes this throw first) and the
31
+ * walk's only side effects for this node are the strips recorded here. Outside unions
32
+ * a `FAIL` always ends in the walk, so it may be returned early.
16
33
  * - `BAIL` — not decidable here; the caller must run the full walk.
17
34
  *
18
35
  * Union branches are tried in declared order, continuing past `FAIL` and stopping
19
36
  * at `BAIL`, which keeps union results identical to the walk.
37
+ *
38
+ * `unknownProps: 'strip'`: the walk deletes an object's unknown keys right after the object
39
+ * passes (even when an ancestor fails later). This check records those deletions only for
40
+ * the top-level object (`pendingKeys`), whose strip is the walk's last read of the input:
41
+ * deferring an earlier strip past later reads could be observed by them (a prototype, a
42
+ * proxy or an accessor over the stripped object), so a nested object with unknown keys
43
+ * bails, as does any object visit once a strip is pending (a later intersection member).
44
+ * The recorded keys are deleted, in the walk's order, only when the whole value passes —
45
+ * so a `PASS` result still has exactly the walk's side effects, and `FAIL` / `BAIL` leave
46
+ * the value untouched for the walk.
20
47
  */ const FAIL = 0;
21
48
  const PASS = 1;
22
49
  const BAIL = 2;
23
50
  /** Node is at walk depth 0 (only object props / array & tuple items increase depth). */ const TOP = 1;
24
51
  /** Node is inside a union branch — failures must be exhaustive (see contract). */ const IN_UNION = 2;
52
+ /** Incremented per {@link fastCheckPasses} call; snapshots verified in older calls re-verify. */ let stamp = 0;
53
+ /** A 'strip' {@link fastCheckPasses} call is in progress (the pending strips below are its own). */ let stripping = false;
25
54
  /**
26
- * `true` only when the full walk over `value` is guaranteed to pass with no errors
27
- * and no side effects. Any other outcome (including a throw) means "run the walk".
55
+ * Unknown keys of the top-level object the walk would delete (`unknownProps: 'strip'`), in
56
+ * the walk's deletion order. Deleted when the call passes, dropped otherwise.
57
+ */ const pendingKeys = [];
58
+ /**
59
+ * `true` only when the full walk over `value` is guaranteed to pass with no errors and
60
+ * no side effects other than its 'strip' deletions, which are then performed here, as the
61
+ * walk would. Any other outcome (including a throw) leaves `value` untouched and means
62
+ * "run the walk".
28
63
  */ function fastCheckPasses(def, value, opts) {
64
+ if (opts.unknownProps === "strip") return fastCheckStripping(def, value, opts);
65
+ stamp++;
29
66
  try {
30
67
  return fastCheck(def, value, TOP, opts) === PASS;
31
68
  } catch {
32
69
  return false;
33
70
  }
34
71
  }
72
+ /** {@link fastCheckPasses} for `unknownProps: 'strip'`: performs the recorded strips on a pass. */ function fastCheckStripping(def, value, opts) {
73
+ if (stripping) return false;
74
+ stamp++;
75
+ stripping = true;
76
+ let passed;
77
+ try {
78
+ passed = fastCheck(def, value, TOP, opts) === PASS;
79
+ } catch {
80
+ passed = false;
81
+ } finally {
82
+ stripping = false;
83
+ }
84
+ try {
85
+ if (passed) for (const key of pendingKeys) delete value[key];
86
+ } finally {
87
+ pendingKeys.length = 0;
88
+ }
89
+ return passed;
90
+ }
35
91
  const DECIMAL_RE = /^[+-]?\d+(\.\d+)?$/;
36
92
  const regexCache = /* @__PURE__ */ new Map();
37
93
  /** Compiled `@expect.pattern` regex, cached per (pattern, flags). */ function getRegex(pattern, flags) {
@@ -76,7 +132,8 @@ function fastCheck(def, value, flags, opts) {
76
132
  const branchFlags = flags | IN_UNION;
77
133
  for (const item of type.items) {
78
134
  const result = fastCheck(item, value, branchFlags, opts);
79
- if (result !== FAIL) return result === PASS && item.type.value === void 0 ? fastCheckUnionConstraints(def.metadata, value) : result;
135
+ if (result !== FAIL) return unionMatchResult(result, item, def.metadata, value);
136
+ if (pendingKeys.length > 0) return BAIL;
80
137
  }
81
138
  return FAIL;
82
139
  }
@@ -116,6 +173,9 @@ function fastCheckFinal(def, value) {
116
173
  default: return BAIL;
117
174
  }
118
175
  }
176
+ /** Result of a union whose branch `item` did not fail (`PASS` or `BAIL`). */ function unionMatchResult(result, item, metadata, value) {
177
+ return result === PASS && item.type.value === void 0 ? fastCheckUnionConstraints(metadata, value) : result;
178
+ }
119
179
  /**
120
180
  * Mirrors Validator.validateUnionConstraints: union-level constraints on the matched value
121
181
  * (`null` / objects pass; the caller skips literal branches).
@@ -130,8 +190,7 @@ function fastCheckFinal(def, value) {
130
190
  }
131
191
  }
132
192
  function fastCheckBoolean(metadata, value) {
133
- if (metadata.size === 0) return PASS;
134
- return metadata.get("meta.required") && value !== true ? FAIL : PASS;
193
+ return metadata.size === 0 ? PASS : booleanConstraints(value, metadata.get("meta.required"));
135
194
  }
136
195
  function fastCheckString(metadata, value) {
137
196
  if (metadata.size === 0) return PASS;
@@ -151,20 +210,36 @@ function fastCheckString(metadata, value) {
151
210
  }
152
211
  function fastCheckNumber(metadata, value) {
153
212
  if (metadata.size === 0) return PASS;
154
- if (metadata.get("expect.int") && value % 1 !== 0) return FAIL;
155
- const min = metadata.get("expect.min");
213
+ return numberConstraints(value, metadata.get("expect.int"), metadata.get("expect.min"), metadata.get("expect.max"), metadata.get("expect.exclusiveMin"), metadata.get("expect.exclusiveMax"));
214
+ }
215
+ function booleanConstraints(value, required) {
216
+ return required && value !== true ? FAIL : PASS;
217
+ }
218
+ function numberConstraints(value, int, min, max, exMin, exMax) {
219
+ if (int && value % 1 !== 0) return FAIL;
156
220
  if (min !== void 0 && value < (typeof min === "number" ? min : min.minValue)) return FAIL;
157
- const max = metadata.get("expect.max");
158
221
  if (max !== void 0 && value > (typeof max === "number" ? max : max.maxValue)) return FAIL;
222
+ if (exMin !== void 0 && value <= (typeof exMin === "number" ? exMin : exMin.exclusiveMinValue)) return FAIL;
223
+ if (exMax !== void 0 && value >= (typeof exMax === "number" ? exMax : exMax.exclusiveMaxValue)) return FAIL;
159
224
  return PASS;
160
225
  }
226
+ /** `@expect.minLength` / `@expect.maxLength` (strings and arrays). */ function lengthFails(length, minLength, maxLength) {
227
+ return minLength !== void 0 && length < (typeof minLength === "number" ? minLength : minLength.length) || maxLength !== void 0 && length > (typeof maxLength === "number" ? maxLength : maxLength.length);
228
+ }
229
+ /** Array constraints; duplicate detection (`uniqueItems`) is left to the walk. */ function arrayConstraints(length, minLength, maxLength, uniqueItems) {
230
+ if (lengthFails(length, minLength, maxLength)) return FAIL;
231
+ return uniqueItems ? BAIL : PASS;
232
+ }
161
233
  function fastCheckObject(def, value, flags, opts) {
162
234
  if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
163
235
  const type = def.type;
164
236
  if (type.propsPatterns.length > 0) return BAIL;
237
+ if (pendingKeys.length > 0) return BAIL;
238
+ const unknownProps = opts.unknownProps;
239
+ const inUnion = (flags & IN_UNION) !== 0;
240
+ if (unknownProps === "error" && !inUnion && hasUnknownKey(type.props, value)) return FAIL;
165
241
  const partial = opts.partial;
166
242
  const skipUndefined = partial === "deep" || partial === true && (flags & TOP) !== 0;
167
- const inUnion = (flags & IN_UNION) !== 0;
168
243
  const propFlags = flags & IN_UNION;
169
244
  let result = PASS;
170
245
  for (const [key, item] of type.props) {
@@ -178,16 +253,28 @@ function fastCheckObject(def, value, flags, opts) {
178
253
  }
179
254
  }
180
255
  if (result !== PASS) return result;
181
- const unknownProps = opts.unknownProps;
182
- if (unknownProps === "error" || unknownProps === "strip") {
183
- const props = type.props;
184
- for (const key of Object.keys(value)) {
185
- const known = props.get(key);
186
- if (known === void 0 || isPhantomType(known)) return unknownProps === "error" ? FAIL : BAIL;
187
- }
256
+ if (unknownProps === "strip") return pendStrips(type.props, value, flags);
257
+ return unknownProps === "error" && inUnion && hasUnknownKey(type.props, value) ? FAIL : PASS;
258
+ }
259
+ /**
260
+ * Records the unknown keys of a passing top-level object: the walk deletes them at this
261
+ * point, after its last read of the input. A nested object's strip would be followed by
262
+ * more reads, so it is left to the walk.
263
+ */ function pendStrips(props, value, flags) {
264
+ for (const key of Object.keys(value)) if (isUnknownKey(props, key)) {
265
+ if ((flags & TOP) === 0) return BAIL;
266
+ pendingKeys.push(key);
188
267
  }
189
268
  return PASS;
190
269
  }
270
+ /** `true` when `value` has a key the walk treats as unknown. */ function hasUnknownKey(props, value) {
271
+ for (const key of Object.keys(value)) if (isUnknownKey(props, key)) return true;
272
+ return false;
273
+ }
274
+ /** `true` when the walk treats `key` as unknown (no prop, or a phantom one). */ function isUnknownKey(props, key) {
275
+ const known = props.get(key);
276
+ return known === void 0 || isPhantomType(known);
277
+ }
191
278
  function fastCheckArray(def, value, flags, opts) {
192
279
  if (!Array.isArray(value)) return FAIL;
193
280
  const metadata = def.metadata;
@@ -195,12 +282,14 @@ function fastCheckArray(def, value, flags, opts) {
195
282
  const result = fastCheckArrayConstraints(metadata, value);
196
283
  if (result !== PASS) return result;
197
284
  }
198
- const of = def.type.of;
285
+ return checkItems(planFor(def.type.of), value, flags, opts);
286
+ }
287
+ /** Checks every array item against the item type's plan. */ function checkItems(of, value, flags, opts) {
199
288
  const inUnion = (flags & IN_UNION) !== 0;
200
289
  const itemFlags = flags & IN_UNION;
201
290
  let result = PASS;
202
291
  for (const item of value) {
203
- const itemResult = fastCheck(of, item, itemFlags, opts);
292
+ const itemResult = checkPlan(of, item, itemFlags, opts);
204
293
  if (itemResult !== PASS) {
205
294
  if (itemResult === BAIL || !inUnion) return itemResult;
206
295
  result = FAIL;
@@ -209,11 +298,344 @@ function fastCheckArray(def, value, flags, opts) {
209
298
  return result;
210
299
  }
211
300
  function fastCheckArrayConstraints(metadata, value) {
212
- const minLength = metadata.get("expect.minLength");
213
- if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
214
- const maxLength = metadata.get("expect.maxLength");
215
- if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
216
- return metadata.get("expect.array.uniqueItems") ? BAIL : PASS;
301
+ return arrayConstraints(value.length, metadata.get("expect.minLength"), metadata.get("expect.maxLength"), metadata.get("expect.array.uniqueItems"));
302
+ }
303
+ const OP_GENERIC = 0;
304
+ const OP_PHANTOM = 1;
305
+ const OP_STRING = 2;
306
+ const OP_NUMBER = 3;
307
+ const OP_BOOLEAN = 4;
308
+ /** Any other final node: checked by {@link fastCheckFinal}. */ const OP_FINAL = 5;
309
+ const OP_OBJECT = 6;
310
+ const OP_ARRAY = 7;
311
+ const OP_UNION = 8;
312
+ function isFinalOp(op) {
313
+ return op !== OP_GENERIC && op <= OP_FINAL;
314
+ }
315
+ /** Shared placeholder for the per-kind arrays of plans that don't use them. */ const EMPTY = Object.freeze([]);
316
+ const plans = /* @__PURE__ */ new WeakMap();
317
+ /** One shape for every plan, so plan field access stays monomorphic. */ let Plan = class Plan {
318
+ constructor(def) {
319
+ _define_property$1(this, "def", void 0);
320
+ /** Call in which the node structure was last verified (`-1`: must rebuild). */ _define_property$1(this, "stamp", void 0);
321
+ /** Call in which the annotation values (`m0`–`m4`) were last verified. */ _define_property$1(this, "metaStamp", void 0);
322
+ _define_property$1(this, "op", void 0);
323
+ _define_property$1(this, "type", void 0);
324
+ _define_property$1(this, "optional", void 0);
325
+ _define_property$1(this, "metadata", void 0);
326
+ _define_property$1(this, "kind", void 0);
327
+ _define_property$1(this, "designType", void 0);
328
+ _define_property$1(this, "literal", void 0);
329
+ /** String plans: compiled `@expect.pattern` regexes by position (checked against the live entry). */ _define_property$1(this, "regexes", void 0);
330
+ /**
331
+ * Annotation keys the node's check reads, and their values (`undefined` when absent):
332
+ * string `meta.required, expect.minLength, maxLength, pattern`; number `expect.int, min,
333
+ * max, exclusiveMin, exclusiveMax`; boolean `meta.required`; array `expect.minLength, maxLength, array.uniqueItems`.
334
+ */ _define_property$1(this, "k0", void 0);
335
+ _define_property$1(this, "k1", void 0);
336
+ _define_property$1(this, "k2", void 0);
337
+ _define_property$1(this, "k3", void 0);
338
+ _define_property$1(this, "k4", void 0);
339
+ _define_property$1(this, "m0", void 0);
340
+ _define_property$1(this, "m1", void 0);
341
+ _define_property$1(this, "m2", void 0);
342
+ _define_property$1(this, "m3", void 0);
343
+ _define_property$1(this, "m4", void 0);
344
+ _define_property$1(this, "props", void 0);
345
+ _define_property$1(this, "propsPatterns", void 0);
346
+ _define_property$1(this, "keys", void 0);
347
+ /** Call in which `props.get(keys[i]) === nodes[i]` was last verified. */ _define_property$1(this, "edges", void 0);
348
+ /** Prop plans by key (the entries of `props`, verified per visit via `edges`). */ _define_property$1(this, "byKey", void 0);
349
+ _define_property$1(this, "nodes", void 0);
350
+ _define_property$1(this, "children", void 0);
351
+ _define_property$1(this, "of", void 0);
352
+ _define_property$1(this, "ofPlan", void 0);
353
+ _define_property$1(this, "items", void 0);
354
+ this.def = def;
355
+ this.stamp = -1;
356
+ this.metaStamp = -1;
357
+ this.op = OP_GENERIC;
358
+ this.type = void 0;
359
+ this.optional = void 0;
360
+ this.metadata = void 0;
361
+ this.kind = void 0;
362
+ this.designType = void 0;
363
+ this.literal = void 0;
364
+ this.regexes = EMPTY;
365
+ this.k0 = void 0;
366
+ this.k1 = void 0;
367
+ this.k2 = void 0;
368
+ this.k3 = void 0;
369
+ this.k4 = void 0;
370
+ this.m0 = void 0;
371
+ this.m1 = void 0;
372
+ this.m2 = void 0;
373
+ this.m3 = void 0;
374
+ this.m4 = void 0;
375
+ this.props = void 0;
376
+ this.propsPatterns = void 0;
377
+ this.keys = EMPTY;
378
+ this.edges = EMPTY;
379
+ this.byKey = void 0;
380
+ this.nodes = EMPTY;
381
+ this.children = EMPTY;
382
+ this.of = void 0;
383
+ this.ofPlan = void 0;
384
+ this.items = void 0;
385
+ }
386
+ };
387
+ function planFor(def) {
388
+ let plan = plans.get(def);
389
+ if (plan === void 0) {
390
+ plan = new Plan(def);
391
+ plans.set(def, plan);
392
+ }
393
+ return plan;
394
+ }
395
+ /** Brings the plan's structure up to date with its live node (once per call). */ function refresh(plan) {
396
+ if (!(isFinalOp(plan.op) ? verifyFinal(plan) : verify(plan))) build(plan);
397
+ plan.stamp = stamp;
398
+ }
399
+ /** Structure check for final ops; small enough to be inlined into the hot loops. */ function verifyFinal(plan) {
400
+ const def = plan.def;
401
+ const type = def.type;
402
+ return plan.stamp !== -1 && type === plan.type && def.optional === plan.optional && def.metadata === plan.metadata && type.kind === "" && type.designType === plan.designType && type.value === plan.literal;
403
+ }
404
+ /** Structure check for objects, arrays, unions and generic nodes. */ function verify(plan) {
405
+ if (plan.stamp === -1) return false;
406
+ const def = plan.def;
407
+ const type = def.type;
408
+ if (type !== plan.type || def.optional !== plan.optional || def.metadata !== plan.metadata || type.kind !== plan.kind) return false;
409
+ switch (plan.op) {
410
+ case OP_OBJECT: return type.props === plan.props && plan.props.size === plan.keys.length && type.propsPatterns === plan.propsPatterns && plan.propsPatterns.length === 0;
411
+ case OP_ARRAY: return type.of === plan.of;
412
+ case OP_UNION: {
413
+ const items = plan.items;
414
+ const nodes = plan.nodes;
415
+ if (type.items !== items || items.length !== nodes.length) return false;
416
+ for (let i = 0; i < nodes.length; i++) if (items[i] !== nodes[i]) return false;
417
+ return true;
418
+ }
419
+ default: return type.kind !== "object" || type.propsPatterns.length > 0;
420
+ }
421
+ }
422
+ /** Brings the annotation values up to date (once per call, when a constraint is read). */ function refreshMeta(plan) {
423
+ const metadata = plan.metadata;
424
+ if (!(metadata.size === 0 ? plan.m0 === void 0 && plan.m1 === void 0 && plan.m2 === void 0 && plan.m3 === void 0 && plan.m4 === void 0 : metadata.get(plan.k0) === plan.m0 && (plan.k1 === void 0 || metadata.get(plan.k1) === plan.m1) && (plan.k2 === void 0 || metadata.get(plan.k2) === plan.m2) && (plan.k3 === void 0 || metadata.get(plan.k3) === plan.m3) && (plan.k4 === void 0 || metadata.get(plan.k4) === plan.m4))) snapshotMeta(plan);
425
+ plan.metaStamp = stamp;
426
+ }
427
+ function snapshotMeta(plan) {
428
+ const metadata = plan.metadata;
429
+ plan.m0 = metadata.get(plan.k0);
430
+ plan.m1 = metadata.get(plan.k1);
431
+ plan.m2 = metadata.get(plan.k2);
432
+ plan.m3 = metadata.get(plan.k3);
433
+ plan.m4 = metadata.get(plan.k4);
434
+ }
435
+ function setMetaKeys(plan, k0, k1, k2, k3, k4) {
436
+ plan.k0 = k0;
437
+ plan.k1 = k1;
438
+ plan.k2 = k2;
439
+ plan.k3 = k3;
440
+ plan.k4 = k4;
441
+ snapshotMeta(plan);
442
+ plan.metaStamp = stamp;
443
+ }
444
+ /** (Re)compiles `plan` from its live node. */ function build(plan) {
445
+ plan.stamp = -1;
446
+ plan.op = OP_GENERIC;
447
+ const def = plan.def;
448
+ const type = def.type;
449
+ plan.type = type;
450
+ plan.optional = def.optional;
451
+ plan.metadata = def.metadata;
452
+ plan.kind = type.kind;
453
+ plan.designType = type.designType;
454
+ plan.literal = type.value;
455
+ plan.regexes = EMPTY;
456
+ plan.props = void 0;
457
+ plan.propsPatterns = void 0;
458
+ plan.keys = EMPTY;
459
+ plan.edges = EMPTY;
460
+ plan.byKey = void 0;
461
+ plan.nodes = EMPTY;
462
+ plan.children = EMPTY;
463
+ plan.of = void 0;
464
+ plan.ofPlan = void 0;
465
+ plan.items = void 0;
466
+ setMetaKeys(plan);
467
+ switch (type.kind) {
468
+ case "":
469
+ if (type.designType === "phantom") plan.op = OP_PHANTOM;
470
+ else if (type.value !== void 0) plan.op = OP_FINAL;
471
+ else if (type.designType === "string") {
472
+ setMetaKeys(plan, "meta.required", "expect.minLength", "expect.maxLength", "expect.pattern");
473
+ plan.regexes = [];
474
+ plan.op = OP_STRING;
475
+ } else if (type.designType === "number") {
476
+ setMetaKeys(plan, "expect.int", "expect.min", "expect.max", "expect.exclusiveMin", "expect.exclusiveMax");
477
+ plan.op = OP_NUMBER;
478
+ } else if (type.designType === "boolean") {
479
+ setMetaKeys(plan, "meta.required");
480
+ plan.op = OP_BOOLEAN;
481
+ } else plan.op = OP_FINAL;
482
+ break;
483
+ case "object": {
484
+ if (type.propsPatterns.length > 0) break;
485
+ const byKey = /* @__PURE__ */ new Map();
486
+ plan.keys = [];
487
+ plan.edges = [];
488
+ plan.nodes = [];
489
+ plan.children = [];
490
+ for (const [key, node] of type.props) {
491
+ const child = planFor(node);
492
+ plan.keys.push(key);
493
+ plan.edges.push(stamp);
494
+ plan.nodes.push(node);
495
+ plan.children.push(child);
496
+ byKey.set(key, child);
497
+ }
498
+ plan.byKey = byKey;
499
+ plan.props = type.props;
500
+ plan.propsPatterns = type.propsPatterns;
501
+ plan.op = OP_OBJECT;
502
+ break;
503
+ }
504
+ case "array":
505
+ setMetaKeys(plan, "expect.minLength", "expect.maxLength", "expect.array.uniqueItems");
506
+ plan.of = type.of;
507
+ plan.ofPlan = planFor(type.of);
508
+ plan.op = OP_ARRAY;
509
+ break;
510
+ case "union":
511
+ plan.nodes = [];
512
+ plan.children = [];
513
+ for (const node of type.items) {
514
+ plan.nodes.push(node);
515
+ plan.children.push(planFor(node));
516
+ }
517
+ plan.items = type.items;
518
+ plan.op = OP_UNION;
519
+ break;
520
+ default:
521
+ }
522
+ }
523
+ /** Plan counterpart of {@link fastCheck}. */ function checkPlan(plan, value, flags, opts) {
524
+ if (plan.stamp !== stamp) refresh(plan);
525
+ if (plan.optional && (value === void 0 || value === null)) return value === null && rejectsNullAsRequired(plan.def) ? FAIL : PASS;
526
+ switch (plan.op) {
527
+ case OP_STRING: return typeof value === "string" ? checkString(plan, value) : FAIL;
528
+ case OP_NUMBER: return typeof value === "number" && Number.isFinite(value) ? checkNumber(plan, value) : FAIL;
529
+ case OP_OBJECT: return checkObject(plan, value, flags, opts);
530
+ case OP_ARRAY: return checkArray(plan, value, flags, opts);
531
+ case OP_UNION: {
532
+ const branchFlags = flags | IN_UNION;
533
+ for (const child of plan.children) {
534
+ const result = checkPlan(child, value, branchFlags, opts);
535
+ if (result !== FAIL) return unionMatchResult(result, child.def, plan.metadata, value);
536
+ }
537
+ return FAIL;
538
+ }
539
+ case OP_BOOLEAN:
540
+ if (typeof value !== "boolean") return FAIL;
541
+ if (plan.metaStamp !== stamp) refreshMeta(plan);
542
+ return booleanConstraints(value, plan.m0);
543
+ case OP_PHANTOM:
544
+ case OP_FINAL: return fastCheckFinal(plan.def, value);
545
+ default: return fastCheck(plan.def, value, flags, opts);
546
+ }
547
+ }
548
+ /** Plan counterpart of {@link fastCheckString}; constraint objects are read live. */ function checkString(plan, value) {
549
+ if (plan.metaStamp !== stamp) refreshMeta(plan);
550
+ if (plan.m0 && value.trim().length === 0) return FAIL;
551
+ if (lengthFails(value.length, plan.m1, plan.m2)) return FAIL;
552
+ const patterns = plan.m3;
553
+ if (patterns) {
554
+ if (!Array.isArray(patterns)) return fastCheckString(plan.metadata, value);
555
+ for (let i = 0; i < patterns.length; i++) {
556
+ const { pattern, flags } = patterns[i];
557
+ if (!pattern) continue;
558
+ let entry = plan.regexes[i];
559
+ if (entry === void 0 || entry.pattern !== pattern || entry.flags !== flags) {
560
+ const re = getRegex(pattern, flags);
561
+ entry = {
562
+ pattern,
563
+ flags,
564
+ re,
565
+ stateful: re.global || re.sticky
566
+ };
567
+ plan.regexes[i] = entry;
568
+ }
569
+ if (entry.stateful) return BAIL;
570
+ if (!entry.re.test(value)) return FAIL;
571
+ }
572
+ }
573
+ return PASS;
574
+ }
575
+ /** Plan counterpart of {@link fastCheckNumber}; constraint objects are read live. */ function checkNumber(plan, value) {
576
+ if (plan.metaStamp !== stamp) refreshMeta(plan);
577
+ return numberConstraints(value, plan.m0, plan.m1, plan.m2, plan.m3, plan.m4);
578
+ }
579
+ /** Plan counterpart of {@link fastCheckObject}. */ function checkObject(plan, value, flags, opts) {
580
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
581
+ const partial = opts.partial;
582
+ const skipUndefined = partial === "deep" || partial === true && (flags & TOP) !== 0;
583
+ const inUnion = (flags & IN_UNION) !== 0;
584
+ const propFlags = flags & IN_UNION;
585
+ const keys = plan.keys;
586
+ const children = plan.children;
587
+ const edges = plan.edges;
588
+ let result = PASS;
589
+ for (let i = 0; i < keys.length; i++) {
590
+ if (edges[i] !== stamp) {
591
+ if (plan.props.get(keys[i]) !== plan.nodes[i]) {
592
+ plan.stamp = -1;
593
+ return BAIL;
594
+ }
595
+ edges[i] = stamp;
596
+ }
597
+ const child = children[i];
598
+ if (child.stamp !== stamp) refresh(child);
599
+ const op = child.op;
600
+ if (op === OP_PHANTOM) continue;
601
+ const propValue = value[keys[i]];
602
+ if (propValue === void 0 && skipUndefined) continue;
603
+ if (typeof propValue === "string" && op === OP_STRING) {
604
+ const r = checkString(child, propValue);
605
+ if (r === PASS) continue;
606
+ if (r === BAIL || !inUnion) return r;
607
+ result = FAIL;
608
+ continue;
609
+ }
610
+ if (typeof propValue === "number" && op === OP_NUMBER) {
611
+ if (Number.isFinite(propValue) && checkNumber(child, propValue) === PASS) continue;
612
+ if (!inUnion) return FAIL;
613
+ result = FAIL;
614
+ continue;
615
+ }
616
+ const propResult = checkPlan(child, propValue, propFlags, opts);
617
+ if (propResult !== PASS) {
618
+ if (propResult === BAIL || !inUnion) return propResult;
619
+ result = FAIL;
620
+ }
621
+ }
622
+ if (result !== PASS) return result;
623
+ const unknownProps = opts.unknownProps;
624
+ if (unknownProps === "error" || unknownProps === "strip") {
625
+ const byKey = plan.byKey;
626
+ for (const key of Object.keys(value)) {
627
+ const known = byKey.get(key);
628
+ if (known === void 0 || known.op === OP_PHANTOM) return unknownProps === "error" ? FAIL : BAIL;
629
+ }
630
+ }
631
+ return PASS;
632
+ }
633
+ /** Plan counterpart of {@link fastCheckArray}. */ function checkArray(plan, value, flags, opts) {
634
+ if (!Array.isArray(value)) return FAIL;
635
+ if (plan.metaStamp !== stamp) refreshMeta(plan);
636
+ const result = arrayConstraints(value.length, plan.m0, plan.m1, plan.m2);
637
+ if (result !== PASS) return result;
638
+ return checkItems(plan.ofPlan, value, flags, opts);
217
639
  }
218
640
 
219
641
  //#endregion
@@ -564,7 +986,7 @@ function _define_property(obj, key, value) {
564
986
  validatePrimitive(def, value) {
565
987
  if (def.type.value !== void 0) {
566
988
  if (value !== def.type.value) {
567
- this.error(`Expected ${def.type.value}, got ${value}`);
989
+ this.error(`Expected ${def.type.value}, got ${printValue(value)}`);
568
990
  return false;
569
991
  }
570
992
  return true;
@@ -666,7 +1088,7 @@ function _define_property(obj, key, value) {
666
1088
  validateNumber(def, value) {
667
1089
  return this.validateNumberConstraints(def.metadata, value);
668
1090
  }
669
- /** `@expect.int` / `min` / `max` checks for a number value. */ validateNumberConstraints(metadata, value) {
1091
+ /** `@expect.int` / `min` / `max` / `exclusiveMin` / `exclusiveMax` checks for a number value. */ validateNumberConstraints(metadata, value) {
670
1092
  if (metadata.size === 0) return true;
671
1093
  const int = metadata.get("expect.int");
672
1094
  if (int && value % 1 !== 0) {
@@ -677,23 +1099,30 @@ function _define_property(obj, key, value) {
677
1099
  const min = metadata.get("expect.min");
678
1100
  if (min !== void 0) {
679
1101
  const minValue = typeof min === "number" ? min : min.minValue;
680
- if (value < minValue) {
681
- const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
682
- this.error(message);
683
- return false;
684
- }
1102
+ if (value < minValue) return this.numberBoundFailed(min, "minimum", minValue, value);
685
1103
  }
686
1104
  const max = metadata.get("expect.max");
687
1105
  if (max !== void 0) {
688
1106
  const maxValue = typeof max === "number" ? max : max.maxValue;
689
- if (value > maxValue) {
690
- const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
691
- this.error(message);
692
- return false;
693
- }
1107
+ if (value > maxValue) return this.numberBoundFailed(max, "maximum", maxValue, value);
1108
+ }
1109
+ const exMin = metadata.get("expect.exclusiveMin");
1110
+ if (exMin !== void 0) {
1111
+ const bound = typeof exMin === "number" ? exMin : exMin.exclusiveMinValue;
1112
+ if (value <= bound) return this.numberBoundFailed(exMin, "greater than", bound, value);
1113
+ }
1114
+ const exMax = metadata.get("expect.exclusiveMax");
1115
+ if (exMax !== void 0) {
1116
+ const bound = typeof exMax === "number" ? exMax : exMax.exclusiveMaxValue;
1117
+ if (value >= bound) return this.numberBoundFailed(exMax, "less than", bound, value);
694
1118
  }
695
1119
  return true;
696
1120
  }
1121
+ /** Reports a failed number bound: its custom message, else `Expected <label> <bound>, got <value>`. */ numberBoundFailed(annotation, label, bound, value) {
1122
+ const message = typeof annotation === "object" && annotation.message ? annotation.message : `Expected ${label} ${bound}, got ${value}`;
1123
+ this.error(message);
1124
+ return false;
1125
+ }
697
1126
  validateBoolean(def, value) {
698
1127
  return this.validateBooleanConstraints(def.metadata, value);
699
1128
  }
@@ -756,6 +1185,13 @@ function _define_property(obj, key, value) {
756
1185
  if (of) return of;
757
1186
  }
758
1187
  }
1188
+ /** `${value}` throws for symbols and null-prototype objects: `String()` handles symbols, the tag the rest. */ function printValue(value) {
1189
+ try {
1190
+ return String(value);
1191
+ } catch {
1192
+ return Object.prototype.toString.call(value);
1193
+ }
1194
+ }
759
1195
  /** Error thrown by {@link Validator.validate} when validation fails. Contains structured error details. */ var ValidatorError = class extends Error {
760
1196
  constructor(errors) {
761
1197
  super(`${errors[0].path ? errors[0].path + ": " : ""}${errors[0].message}`), _define_property(this, "errors", void 0), _define_property(this, "name", void 0), this.errors = errors, this.name = "Validation Error";
@@ -1227,18 +1663,27 @@ function cloneTypeDef(type) {
1227
1663
  }
1228
1664
  if (schema.type === "number" || schema.type === "integer") {
1229
1665
  const min = meta.get("expect.min");
1230
- if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
1666
+ if (min !== void 0) schema.minimum = boundOf(min, "minValue");
1231
1667
  const max = meta.get("expect.max");
1232
- if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
1668
+ if (max !== void 0) schema.maximum = boundOf(max, "maxValue");
1669
+ const exMin = meta.get("expect.exclusiveMin");
1670
+ if (exMin !== void 0) schema.exclusiveMinimum = boundOf(exMin, "exclusiveMinValue");
1671
+ const exMax = meta.get("expect.exclusiveMax");
1672
+ if (exMax !== void 0) schema.exclusiveMaximum = boundOf(exMax, "exclusiveMaxValue");
1233
1673
  }
1234
1674
  }
1675
+ /** A number-bound annotation's value: the bare number, or its `field` in the object form. */ function boundOf(m, field) {
1676
+ return typeof m === "number" ? m : m[field];
1677
+ }
1235
1678
  const LOWER_BOUNDS = new Set([
1236
1679
  "minimum",
1680
+ "exclusiveMinimum",
1237
1681
  "minLength",
1238
1682
  "minItems"
1239
1683
  ]);
1240
1684
  const UPPER_BOUNDS = new Set([
1241
1685
  "maximum",
1686
+ "exclusiveMaximum",
1242
1687
  "maxLength",
1243
1688
  "maxItems"
1244
1689
  ]);
@@ -1378,14 +1823,12 @@ const UPPER_BOUNDS = new Set([
1378
1823
  if (s.type === "integer") {
1379
1824
  const handle = defineAnnotatedType().designType("number").tags("number");
1380
1825
  handle.annotate("expect.int", true);
1381
- if (typeof s.minimum === "number") handle.annotate("expect.min", { minValue: s.minimum });
1382
- if (typeof s.maximum === "number") handle.annotate("expect.max", { maxValue: s.maximum });
1826
+ annotateNumberBounds(handle, s);
1383
1827
  return handle.$type;
1384
1828
  }
1385
1829
  if (s.type === "number") {
1386
1830
  const handle = defineAnnotatedType().designType("number").tags("number");
1387
- if (typeof s.minimum === "number") handle.annotate("expect.min", { minValue: s.minimum });
1388
- if (typeof s.maximum === "number") handle.annotate("expect.max", { maxValue: s.maximum });
1831
+ annotateNumberBounds(handle, s);
1389
1832
  return handle.$type;
1390
1833
  }
1391
1834
  if (s.type === "boolean") return defineAnnotatedType().designType("boolean").tags("boolean").$type;
@@ -1395,6 +1838,18 @@ const UPPER_BOUNDS = new Set([
1395
1838
  return convert(schema);
1396
1839
  }
1397
1840
  /**
1841
+ * Maps `minimum` / `maximum` / `exclusiveMinimum` / `exclusiveMaximum` onto
1842
+ * `@expect.min` / `max` / `exclusiveMin` / `exclusiveMax`. Also reads the draft-4 form,
1843
+ * where `exclusiveMinimum: true` makes `minimum` exclusive.
1844
+ */ function annotateNumberBounds(handle, s) {
1845
+ if (typeof s.minimum === "number") if (s.exclusiveMinimum === true) handle.annotate("expect.exclusiveMin", { exclusiveMinValue: s.minimum });
1846
+ else handle.annotate("expect.min", { minValue: s.minimum });
1847
+ if (typeof s.maximum === "number") if (s.exclusiveMaximum === true) handle.annotate("expect.exclusiveMax", { exclusiveMaxValue: s.maximum });
1848
+ else handle.annotate("expect.max", { maxValue: s.maximum });
1849
+ if (typeof s.exclusiveMinimum === "number") handle.annotate("expect.exclusiveMin", { exclusiveMinValue: s.exclusiveMinimum });
1850
+ if (typeof s.exclusiveMaximum === "number") handle.annotate("expect.exclusiveMax", { exclusiveMaxValue: s.exclusiveMaximum });
1851
+ }
1852
+ /**
1398
1853
  * Merges multiple annotated types into a combined schema map with shared `$defs`.
1399
1854
  *
1400
1855
  * Each type must have an `id`. The returned `schemas` object contains individual