@atscript/typescript 0.1.101 → 0.1.103

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/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
2
- const require_plugin = require('./plugin-Cu6Nez-H.cjs');
2
+ const require_plugin = require('./plugin-2TNKdtc6.cjs');
3
3
 
4
4
  exports.default = require_plugin.tsPlugin;
5
5
  exports.tsPlugin = require_plugin.tsPlugin;
package/dist/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { t as tsPlugin } from "./plugin-DG3jlbSN.mjs";
1
+ import { t as tsPlugin } from "./plugin-BN6tnRsJ.mjs";
2
2
 
3
3
  export { tsPlugin as default, tsPlugin };
@@ -1,3 +1,221 @@
1
+ //#region packages/typescript/src/runtime/validator-fast-check.ts
2
+ /**
3
+ * Allocation-free boolean pre-check used by {@link Validator.validate} before the
4
+ * error-collecting walk.
5
+ *
6
+ * It reads the type tree and metadata LIVE on every call (nothing derived from the
7
+ * type is cached), so `annotate()` / `metadata.set()` / prop mutations made after a
8
+ * validator was created are always honoured — the same as the full walk.
9
+ *
10
+ * Result contract (relative to the full walk over the same node and value):
11
+ * - `PASS` — the walk returns `true`, collects no errors and has no side effects.
12
+ * - `FAIL` — the walk returns `false`; every node the walk would visit was visited
13
+ * here too (so a walk that would throw makes this throw first) and the walk has
14
+ * no side effects for this node.
15
+ * - `BAIL` — not decidable here; the caller must run the full walk.
16
+ *
17
+ * Union branches are tried in declared order, continuing past `FAIL` and stopping
18
+ * at `BAIL`, which keeps union results identical to the walk.
19
+ */ const FAIL = 0;
20
+ const PASS = 1;
21
+ const BAIL = 2;
22
+ /** Node is at walk depth 0 (only object props / array & tuple items increase depth). */ const TOP = 1;
23
+ /** Node is inside a union branch — failures must be exhaustive (see contract). */ const IN_UNION = 2;
24
+ /**
25
+ * `true` only when the full walk over `value` is guaranteed to pass with no errors
26
+ * and no side effects. Any other outcome (including a throw) means "run the walk".
27
+ */ function fastCheckPasses(def, value, opts) {
28
+ try {
29
+ return fastCheck(def, value, TOP, opts) === PASS;
30
+ } catch {
31
+ return false;
32
+ }
33
+ }
34
+ const DECIMAL_RE = /^[+-]?\d+(\.\d+)?$/;
35
+ const regexCache = /* @__PURE__ */ new Map();
36
+ /** Compiled `@expect.pattern` regex, cached per (pattern, flags). */ function getRegex(pattern, flags) {
37
+ let byFlags = regexCache.get(pattern);
38
+ if (!byFlags) {
39
+ byFlags = /* @__PURE__ */ new Map();
40
+ regexCache.set(pattern, byFlags);
41
+ }
42
+ const flagsKey = flags || "";
43
+ let regex = byFlags.get(flagsKey);
44
+ if (!regex) {
45
+ regex = new RegExp(pattern, flags);
46
+ byFlags.set(flagsKey, regex);
47
+ }
48
+ return regex;
49
+ }
50
+ /**
51
+ * `true` when `def` (an optional field holding `null`) carries `@meta.required` and its
52
+ * type does not itself accept `null`. Shared by the walk and the fast pre-check.
53
+ */ function rejectsNullAsRequired(def) {
54
+ return def.metadata.size > 0 && !!def.metadata.get("meta.required") && !acceptsNull(def);
55
+ }
56
+ function acceptsNull(def) {
57
+ const type = def.type;
58
+ if (type.kind === "") {
59
+ const designType = type.designType;
60
+ return designType === "null" || designType === "any";
61
+ }
62
+ if (type.kind === "union") {
63
+ for (const item of type.items) if (item.optional || acceptsNull(item)) return true;
64
+ }
65
+ return false;
66
+ }
67
+ function fastCheck(def, value, flags, opts) {
68
+ if (def.optional && (value === void 0 || value === null)) return value === null && rejectsNullAsRequired(def) ? FAIL : PASS;
69
+ const type = def.type;
70
+ switch (type.kind) {
71
+ case "": return fastCheckFinal(def, value);
72
+ case "object": return fastCheckObject(def, value, flags, opts);
73
+ case "array": return fastCheckArray(def, value, flags, opts);
74
+ case "union": {
75
+ const branchFlags = flags | IN_UNION;
76
+ for (const item of type.items) {
77
+ const result = fastCheck(item, value, branchFlags, opts);
78
+ if (result !== FAIL) return result === PASS && item.type.value === void 0 ? fastCheckUnionConstraints(def.metadata, value) : result;
79
+ }
80
+ return FAIL;
81
+ }
82
+ case "intersection":
83
+ for (const item of type.items) {
84
+ const result = fastCheck(item, value, flags, opts);
85
+ if (result !== PASS) return result;
86
+ }
87
+ return PASS;
88
+ case "tuple": {
89
+ const items = type.items;
90
+ if (!Array.isArray(value) || value.length !== items.length) return FAIL;
91
+ const itemFlags = flags & IN_UNION;
92
+ for (let i = 0; i < items.length; i++) {
93
+ const result = fastCheck(items[i], value[i], itemFlags, opts);
94
+ if (result !== PASS) return result;
95
+ }
96
+ return PASS;
97
+ }
98
+ default: return BAIL;
99
+ }
100
+ }
101
+ function fastCheckFinal(def, value) {
102
+ const type = def.type;
103
+ const designType = type.designType;
104
+ if (designType === "phantom") return PASS;
105
+ if (type.value !== void 0) return value === type.value ? PASS : FAIL;
106
+ switch (designType) {
107
+ case "string": return typeof value === "string" ? fastCheckString(def.metadata, value) : FAIL;
108
+ case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def.metadata, value) : FAIL;
109
+ case "boolean": return typeof value === "boolean" ? fastCheckBoolean(def.metadata, value) : FAIL;
110
+ case "any": return PASS;
111
+ case "never": return FAIL;
112
+ case "undefined": return value === void 0 ? PASS : FAIL;
113
+ case "null": return value === null ? PASS : FAIL;
114
+ case "decimal": return typeof value === "string" && DECIMAL_RE.test(value) ? PASS : FAIL;
115
+ default: return BAIL;
116
+ }
117
+ }
118
+ /**
119
+ * Mirrors Validator.validateUnionConstraints: union-level constraints on the matched value
120
+ * (`null` / objects pass; the caller skips literal branches).
121
+ */ function fastCheckUnionConstraints(metadata, value) {
122
+ if (metadata.size === 0) return PASS;
123
+ switch (typeof value) {
124
+ case "string": return fastCheckString(metadata, value);
125
+ case "number": return fastCheckNumber(metadata, value);
126
+ case "boolean": return fastCheckBoolean(metadata, value);
127
+ case "object": return Array.isArray(value) ? fastCheckArrayConstraints(metadata, value) : PASS;
128
+ default: return PASS;
129
+ }
130
+ }
131
+ function fastCheckBoolean(metadata, value) {
132
+ if (metadata.size === 0) return PASS;
133
+ return metadata.get("meta.required") && value !== true ? FAIL : PASS;
134
+ }
135
+ function fastCheckString(metadata, value) {
136
+ if (metadata.size === 0) return PASS;
137
+ if (metadata.get("meta.required") && value.trim().length === 0) return FAIL;
138
+ const minLength = metadata.get("expect.minLength");
139
+ if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
140
+ const maxLength = metadata.get("expect.maxLength");
141
+ if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
142
+ const patterns = metadata.get("expect.pattern");
143
+ if (patterns) for (const { pattern, flags } of patterns) {
144
+ if (!pattern) continue;
145
+ const regex = getRegex(pattern, flags);
146
+ if (regex.global || regex.sticky) return BAIL;
147
+ if (!regex.test(value)) return FAIL;
148
+ }
149
+ return PASS;
150
+ }
151
+ function fastCheckNumber(metadata, value) {
152
+ if (metadata.size === 0) return PASS;
153
+ if (metadata.get("expect.int") && value % 1 !== 0) return FAIL;
154
+ const min = metadata.get("expect.min");
155
+ if (min !== void 0 && value < (typeof min === "number" ? min : min.minValue)) return FAIL;
156
+ const max = metadata.get("expect.max");
157
+ if (max !== void 0 && value > (typeof max === "number" ? max : max.maxValue)) return FAIL;
158
+ return PASS;
159
+ }
160
+ function fastCheckObject(def, value, flags, opts) {
161
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
162
+ const type = def.type;
163
+ if (type.propsPatterns.length > 0) return BAIL;
164
+ const partial = opts.partial;
165
+ const skipUndefined = partial === "deep" || partial === true && (flags & TOP) !== 0;
166
+ const inUnion = (flags & IN_UNION) !== 0;
167
+ const propFlags = flags & IN_UNION;
168
+ let result = PASS;
169
+ for (const [key, item] of type.props) {
170
+ if (isPhantomType(item)) continue;
171
+ const propValue = value[key];
172
+ if (propValue === void 0 && skipUndefined) continue;
173
+ const propResult = fastCheck(item, propValue, propFlags, opts);
174
+ if (propResult !== PASS) {
175
+ if (propResult === BAIL || !inUnion) return propResult;
176
+ result = FAIL;
177
+ }
178
+ }
179
+ if (result !== PASS) return result;
180
+ const unknownProps = opts.unknownProps;
181
+ if (unknownProps === "error" || unknownProps === "strip") {
182
+ const props = type.props;
183
+ for (const key of Object.keys(value)) {
184
+ const known = props.get(key);
185
+ if (known === void 0 || isPhantomType(known)) return unknownProps === "error" ? FAIL : BAIL;
186
+ }
187
+ }
188
+ return PASS;
189
+ }
190
+ function fastCheckArray(def, value, flags, opts) {
191
+ if (!Array.isArray(value)) return FAIL;
192
+ const metadata = def.metadata;
193
+ if (metadata.size > 0) {
194
+ const result = fastCheckArrayConstraints(metadata, value);
195
+ if (result !== PASS) return result;
196
+ }
197
+ const of = def.type.of;
198
+ const inUnion = (flags & IN_UNION) !== 0;
199
+ const itemFlags = flags & IN_UNION;
200
+ let result = PASS;
201
+ for (const item of value) {
202
+ const itemResult = fastCheck(of, item, itemFlags, opts);
203
+ if (itemResult !== PASS) {
204
+ if (itemResult === BAIL || !inUnion) return itemResult;
205
+ result = FAIL;
206
+ }
207
+ }
208
+ return result;
209
+ }
210
+ function fastCheckArrayConstraints(metadata, value) {
211
+ const minLength = metadata.get("expect.minLength");
212
+ if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
213
+ const maxLength = metadata.get("expect.maxLength");
214
+ if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
215
+ return metadata.get("expect.array.uniqueItems") ? BAIL : PASS;
216
+ }
217
+
218
+ //#endregion
1
219
  //#region packages/typescript/src/runtime/validator.ts
2
220
  function _define_property(obj, key, value) {
3
221
  if (key in obj) Object.defineProperty(obj, key, {
@@ -9,7 +227,6 @@ function _define_property(obj, key, value) {
9
227
  else obj[key] = value;
10
228
  return obj;
11
229
  }
12
- const regexCache = /* @__PURE__ */ new Map();
13
230
  /**
14
231
  * Validates values against an {@link TAtscriptAnnotatedType} definition.
15
232
  *
@@ -30,7 +247,7 @@ const regexCache = /* @__PURE__ */ new Map();
30
247
  *
31
248
  * @typeParam T - The annotated type definition.
32
249
  * @typeParam DataType - The TypeScript type that `validate` narrows to (auto-inferred).
33
- */ var Validator = class {
250
+ */ var Validator = class Validator {
34
251
  buildPath() {
35
252
  if (this.depth <= 0) return "";
36
253
  let path = this.pathSegments[0];
@@ -84,6 +301,7 @@ const regexCache = /* @__PURE__ */ new Map();
84
301
  this.stackErrors.length = 0;
85
302
  this.depth = 0;
86
303
  this.limitExceeded = false;
304
+ if (this.shortcuts && !this.opts.skipList && typeof this.opts.partial !== "function" && fastCheckPasses(this.def, value, this.opts)) return true;
87
305
  this.context = context;
88
306
  const passed = this.validateSafe(this.def, value);
89
307
  this.context = void 0;
@@ -93,6 +311,14 @@ const regexCache = /* @__PURE__ */ new Map();
93
311
  }
94
312
  return true;
95
313
  }
314
+ /**
315
+ * Plain `Validator` without plugins / `replace`: eligible for the allocation-free
316
+ * shortcuts. Subclasses may override the protected `validate*` hooks, so they
317
+ * always take the full walk. (A getter, not a constructor-set field: validators are
318
+ * often built per call, so construction stays as cheap as before.)
319
+ */ get shortcuts() {
320
+ return this.constructor === Validator && !this.hasPlugins && !this.hasReplace;
321
+ }
96
322
  validateSafe(def, value) {
97
323
  if (this.limitExceeded) return false;
98
324
  if (this.hasReplace) {
@@ -103,7 +329,13 @@ const regexCache = /* @__PURE__ */ new Map();
103
329
  }
104
330
  def = replaced;
105
331
  }
106
- if (def.optional && (value === void 0 || value === null)) return true;
332
+ if (def.optional && (value === void 0 || value === null)) {
333
+ if (value === null && rejectsNullAsRequired(def)) {
334
+ this.error(requiredMessage(def));
335
+ return false;
336
+ }
337
+ return true;
338
+ }
107
339
  if (this.hasPlugins) for (const plugin of this.opts.plugins) {
108
340
  const result = plugin(this, def, value);
109
341
  if (result === false || result === true) return result;
@@ -128,12 +360,17 @@ const regexCache = /* @__PURE__ */ new Map();
128
360
  }
129
361
  validateUnion(def, value) {
130
362
  const items = def.type.items;
363
+ if (this.shortcuts) for (const item of items) {
364
+ const type = item.type;
365
+ if (item.optional || type.kind !== "" || type.value === void 0 || type.designType === "phantom") break;
366
+ if (type.value === value) return true;
367
+ }
131
368
  let details;
132
369
  for (const item of items) {
133
370
  this.stackErrors.push(null);
134
371
  if (this.validateSafe(item, value)) {
135
372
  this.stackErrors.pop();
136
- return true;
373
+ return this.validateUnionConstraints(def, item, value);
137
374
  }
138
375
  const branchErrors = this.stackErrors.pop();
139
376
  if (this.limitExceeded) this.limitExceeded = false;
@@ -144,6 +381,23 @@ const regexCache = /* @__PURE__ */ new Map();
144
381
  this.error(`Value does not match any of the allowed types: ${expected}`, void 0, details);
145
382
  return false;
146
383
  }
384
+ /**
385
+ * Constraints annotated on the union itself (`@expect.min 0` on `number | null`)
386
+ * apply to the non-null value that matched `branch`, on top of the branch's own.
387
+ * A literal branch (an enum value) is exempt: its value is fixed.
388
+ */ validateUnionConstraints(def, branch, value) {
389
+ const metadata = def.metadata;
390
+ if (metadata.size === 0 || branch.type.value !== void 0) return true;
391
+ switch (typeof value) {
392
+ case "string": return this.validateStringConstraints(metadata, value);
393
+ case "number": return this.validateNumberConstraints(metadata, value);
394
+ case "boolean": return this.validateBooleanConstraints(metadata, value);
395
+ case "object":
396
+ if (Array.isArray(value)) return this.validateArrayConstraints(metadata, value, arrayElementType(branch));
397
+ return true;
398
+ default: return true;
399
+ }
400
+ }
147
401
  validateIntersection(def, value) {
148
402
  for (const item of def.type.items) if (!this.validateSafe(item, value)) return false;
149
403
  return true;
@@ -170,8 +424,23 @@ const regexCache = /* @__PURE__ */ new Map();
170
424
  this.error("Expected array");
171
425
  return false;
172
426
  }
173
- const minLength = def.metadata.get("expect.minLength");
174
- if (minLength) {
427
+ if (def.metadata.size > 0 && !this.validateArrayConstraints(def.metadata, value, def.type.of)) return false;
428
+ let i = 0;
429
+ let passed = true;
430
+ for (const item of value) {
431
+ this.push(String(i));
432
+ if (!this.validateSafe(def.type.of, item)) {
433
+ passed = false;
434
+ this.pop(true);
435
+ if (this.limitExceeded) return false;
436
+ } else this.pop(false);
437
+ i++;
438
+ }
439
+ return passed;
440
+ }
441
+ /** `@expect.minLength` / `maxLength` / `array.uniqueItems` checks for an array value. */ validateArrayConstraints(metadata, value, of) {
442
+ const minLength = metadata.get("expect.minLength");
443
+ if (minLength !== void 0) {
175
444
  const length = typeof minLength === "number" ? minLength : minLength.length;
176
445
  if (value.length < length) {
177
446
  const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
@@ -179,8 +448,8 @@ const regexCache = /* @__PURE__ */ new Map();
179
448
  return false;
180
449
  }
181
450
  }
182
- const maxLength = def.metadata.get("expect.maxLength");
183
- if (maxLength) {
451
+ const maxLength = metadata.get("expect.maxLength");
452
+ if (maxLength !== void 0) {
184
453
  const length = typeof maxLength === "number" ? maxLength : maxLength.length;
185
454
  if (value.length > length) {
186
455
  const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
@@ -188,13 +457,13 @@ const regexCache = /* @__PURE__ */ new Map();
188
457
  return false;
189
458
  }
190
459
  }
191
- const uniqueItems = def.metadata.get("expect.array.uniqueItems");
460
+ const uniqueItems = metadata.get("expect.array.uniqueItems");
192
461
  if (uniqueItems) {
193
462
  const separator = "▼↩";
194
463
  const seen = /* @__PURE__ */ new Set();
195
464
  const keyProps = /* @__PURE__ */ new Set();
196
- if (def.type.of.type.kind === "object") {
197
- for (const [key, val] of def.type.of.type.props.entries()) if (val.metadata.get("expect.array.key")) keyProps.add(key);
465
+ if (of?.type.kind === "object") {
466
+ for (const [key, val] of of.type.props.entries()) if (val.metadata.get("expect.array.key")) keyProps.add(key);
198
467
  }
199
468
  for (let idx = 0; idx < value.length; idx++) {
200
469
  const item = value[idx];
@@ -212,18 +481,7 @@ const regexCache = /* @__PURE__ */ new Map();
212
481
  seen.add(key);
213
482
  }
214
483
  }
215
- let i = 0;
216
- let passed = true;
217
- for (const item of value) {
218
- this.push(String(i));
219
- if (!this.validateSafe(def.type.of, item)) {
220
- passed = false;
221
- this.pop(true);
222
- if (this.limitExceeded) return false;
223
- } else this.pop(false);
224
- i++;
225
- }
226
- return passed;
484
+ return true;
227
485
  }
228
486
  validateObject(def, value) {
229
487
  if (typeof value !== "object" || value === null || Array.isArray(value)) {
@@ -352,7 +610,7 @@ const regexCache = /* @__PURE__ */ new Map();
352
610
  this.error(`Expected string (decimal), got ${typeOfValue}`);
353
611
  return false;
354
612
  }
355
- if (!/^[+-]?\d+(\.\d+)?$/.test(value)) {
613
+ if (!DECIMAL_RE.test(value)) {
356
614
  this.error(`Invalid decimal format: ${JSON.stringify(value)}`);
357
615
  return false;
358
616
  }
@@ -361,8 +619,11 @@ const regexCache = /* @__PURE__ */ new Map();
361
619
  }
362
620
  }
363
621
  validateString(def, value) {
364
- if (def.metadata.size === 0) return true;
365
- const filled = def.metadata.get("meta.required");
622
+ return this.validateStringConstraints(def.metadata, value);
623
+ }
624
+ /** `@meta.required` / `@expect.minLength` / `maxLength` / `pattern` checks for a string value. */ validateStringConstraints(metadata, value) {
625
+ if (metadata.size === 0) return true;
626
+ const filled = metadata.get("meta.required");
366
627
  if (filled) {
367
628
  if (value.trim().length === 0) {
368
629
  const message = typeof filled === "object" && filled.message ? filled.message : `Must not be empty`;
@@ -370,8 +631,8 @@ const regexCache = /* @__PURE__ */ new Map();
370
631
  return false;
371
632
  }
372
633
  }
373
- const minLength = def.metadata.get("expect.minLength");
374
- if (minLength) {
634
+ const minLength = metadata.get("expect.minLength");
635
+ if (minLength !== void 0) {
375
636
  const length = typeof minLength === "number" ? minLength : minLength.length;
376
637
  if (value.length < length) {
377
638
  const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
@@ -379,8 +640,8 @@ const regexCache = /* @__PURE__ */ new Map();
379
640
  return false;
380
641
  }
381
642
  }
382
- const maxLength = def.metadata.get("expect.maxLength");
383
- if (maxLength) {
643
+ const maxLength = metadata.get("expect.maxLength");
644
+ if (maxLength !== void 0) {
384
645
  const length = typeof maxLength === "number" ? maxLength : maxLength.length;
385
646
  if (value.length > length) {
386
647
  const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
@@ -388,16 +649,10 @@ const regexCache = /* @__PURE__ */ new Map();
388
649
  return false;
389
650
  }
390
651
  }
391
- const patterns = def.metadata.get("expect.pattern");
652
+ const patterns = metadata.get("expect.pattern");
392
653
  for (const { pattern, flags, message } of patterns || []) {
393
654
  if (!pattern) continue;
394
- const cacheKey = `${pattern}//${flags || ""}`;
395
- let regex = regexCache.get(cacheKey);
396
- if (!regex) {
397
- regex = new RegExp(pattern, flags);
398
- regexCache.set(cacheKey, regex);
399
- }
400
- if (!regex.test(value)) {
655
+ if (!getRegex(pattern, flags).test(value)) {
401
656
  this.error(message || `Value is expected to match pattern "${pattern}"`);
402
657
  return false;
403
658
  }
@@ -405,15 +660,18 @@ const regexCache = /* @__PURE__ */ new Map();
405
660
  return true;
406
661
  }
407
662
  validateNumber(def, value) {
408
- if (def.metadata.size === 0) return true;
409
- const int = def.metadata.get("expect.int");
663
+ return this.validateNumberConstraints(def.metadata, value);
664
+ }
665
+ /** `@expect.int` / `min` / `max` checks for a number value. */ validateNumberConstraints(metadata, value) {
666
+ if (metadata.size === 0) return true;
667
+ const int = metadata.get("expect.int");
410
668
  if (int && value % 1 !== 0) {
411
669
  const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
412
670
  this.error(message);
413
671
  return false;
414
672
  }
415
- const min = def.metadata.get("expect.min");
416
- if (min) {
673
+ const min = metadata.get("expect.min");
674
+ if (min !== void 0) {
417
675
  const minValue = typeof min === "number" ? min : min.minValue;
418
676
  if (value < minValue) {
419
677
  const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
@@ -421,8 +679,8 @@ const regexCache = /* @__PURE__ */ new Map();
421
679
  return false;
422
680
  }
423
681
  }
424
- const max = def.metadata.get("expect.max");
425
- if (max) {
682
+ const max = metadata.get("expect.max");
683
+ if (max !== void 0) {
426
684
  const maxValue = typeof max === "number" ? max : max.maxValue;
427
685
  if (value > maxValue) {
428
686
  const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
@@ -433,8 +691,11 @@ const regexCache = /* @__PURE__ */ new Map();
433
691
  return true;
434
692
  }
435
693
  validateBoolean(def, value) {
436
- if (def.metadata.size === 0) return true;
437
- const filled = def.metadata.get("meta.required");
694
+ return this.validateBooleanConstraints(def.metadata, value);
695
+ }
696
+ /** `@meta.required` check for a boolean value (must be `true`). */ validateBooleanConstraints(metadata, value) {
697
+ if (metadata.size === 0) return true;
698
+ const filled = metadata.get("meta.required");
438
699
  if (filled) {
439
700
  if (value !== true) {
440
701
  const message = typeof filled === "object" && filled.message ? filled.message : `Must be checked`;
@@ -474,6 +735,23 @@ const regexCache = /* @__PURE__ */ new Map();
474
735
  if (this.hasReplace) this.replaceCache = /* @__PURE__ */ new WeakMap();
475
736
  }
476
737
  };
738
+ /** The `@meta.required` error message for `def` (custom message or the type's default). */ function requiredMessage(def) {
739
+ const filled = def.metadata.get("meta.required");
740
+ if (typeof filled === "object" && filled.message) return filled.message;
741
+ return def.type.kind === "" && def.type.designType === "boolean" ? "Must be checked" : "Must not be empty";
742
+ }
743
+ /**
744
+ * Element type of the array a union branch holds — through nested unions too
745
+ * (`Items | undefined` with `type Items = Item[] | null`), so `@expect.array.key`
746
+ * fields of the elements are found.
747
+ */ function arrayElementType(branch) {
748
+ const type = branch.type;
749
+ if (type.kind === "array") return type.of;
750
+ if (type.kind === "union") for (const item of type.items) {
751
+ const of = arrayElementType(item);
752
+ if (of) return of;
753
+ }
754
+ }
477
755
  /** Error thrown by {@link Validator.validate} when validation fails. Contains structured error details. */ var ValidatorError = class extends Error {
478
756
  constructor(errors) {
479
757
  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";
@@ -872,10 +1150,7 @@ function cloneTypeDef(type) {
872
1150
  type: "array",
873
1151
  items: build(d.type.of)
874
1152
  };
875
- const minLength = meta.get("expect.minLength");
876
- if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
877
- const maxLength = meta.get("expect.maxLength");
878
- if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
1153
+ applyArrayConstraints(schema, meta);
879
1154
  return schema;
880
1155
  },
881
1156
  union(d) {
@@ -895,7 +1170,9 @@ function cloneTypeDef(type) {
895
1170
  }
896
1171
  };
897
1172
  }
898
- return { anyOf: d.type.items.map(build) };
1173
+ const anyOf = d.type.items.map(build);
1174
+ if (meta.size > 0) applyUnionConstraints(anyOf, meta);
1175
+ return { anyOf };
899
1176
  },
900
1177
  intersection(d) {
901
1178
  return { allOf: d.type.items.map(build) };
@@ -913,24 +1190,8 @@ function cloneTypeDef(type) {
913
1190
  if (d.type.designType && d.type.designType !== "any") {
914
1191
  const dt = d.type.designType;
915
1192
  schema.type = dt === "undefined" ? "null" : dt === "decimal" ? "string" : dt;
916
- if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
917
- }
918
- if (schema.type === "string") {
919
- if (meta.get("meta.required")) schema.minLength = 1;
920
- const minLength = meta.get("expect.minLength");
921
- if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
922
- const maxLength = meta.get("expect.maxLength");
923
- if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
924
- const patterns = meta.get("expect.pattern");
925
- if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
926
- else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
927
- }
928
- if (schema.type === "number" || schema.type === "integer") {
929
- const min = meta.get("expect.min");
930
- if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
931
- const max = meta.get("expect.max");
932
- if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
933
1193
  }
1194
+ applyScalarConstraints(schema, meta);
934
1195
  return schema;
935
1196
  }
936
1197
  });
@@ -942,6 +1203,70 @@ function cloneTypeDef(type) {
942
1203
  };
943
1204
  return schema;
944
1205
  }
1206
+ /** `@expect.minLength` / `maxLength` → `minItems` / `maxItems` on an array schema. */ function applyArrayConstraints(schema, meta) {
1207
+ const minLength = meta.get("expect.minLength");
1208
+ if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
1209
+ const maxLength = meta.get("expect.maxLength");
1210
+ if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
1211
+ }
1212
+ /** String / number constraints on a scalar schema whose `type` is already set. */ function applyScalarConstraints(schema, meta) {
1213
+ if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
1214
+ if (schema.type === "string") {
1215
+ if (meta.get("meta.required")) schema.minLength = 1;
1216
+ const minLength = meta.get("expect.minLength");
1217
+ if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
1218
+ const maxLength = meta.get("expect.maxLength");
1219
+ if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
1220
+ const patterns = meta.get("expect.pattern");
1221
+ if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
1222
+ else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
1223
+ }
1224
+ if (schema.type === "number" || schema.type === "integer") {
1225
+ const min = meta.get("expect.min");
1226
+ if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
1227
+ const max = meta.get("expect.max");
1228
+ if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
1229
+ }
1230
+ }
1231
+ const LOWER_BOUNDS = new Set([
1232
+ "minimum",
1233
+ "minLength",
1234
+ "minItems"
1235
+ ]);
1236
+ const UPPER_BOUNDS = new Set([
1237
+ "maximum",
1238
+ "maxLength",
1239
+ "maxItems"
1240
+ ]);
1241
+ /**
1242
+ * Puts union-level constraints onto the matching `anyOf` members (numbers, strings,
1243
+ * arrays; nested `anyOf` members recursively). When a member already has the same
1244
+ * bound, the stricter one wins; patterns accumulate.
1245
+ */ function applyUnionConstraints(members, meta) {
1246
+ for (const member of members) {
1247
+ if (Array.isArray(member.anyOf)) {
1248
+ applyUnionConstraints(member.anyOf, meta);
1249
+ continue;
1250
+ }
1251
+ if (member.const !== void 0) continue;
1252
+ const type = member.type;
1253
+ const extra = { type };
1254
+ if (type === "array") applyArrayConstraints(extra, meta);
1255
+ else if (type === "string" || type === "number" || type === "integer") applyScalarConstraints(extra, meta);
1256
+ else continue;
1257
+ if (extra.type === "integer") member.type = "integer";
1258
+ for (const [key, value] of Object.entries(extra)) {
1259
+ if (key === "type") continue;
1260
+ if (key === "pattern" || key === "allOf") {
1261
+ const patterns = key === "pattern" ? [{ pattern: value }] : value;
1262
+ if (member.pattern === void 0 && key === "pattern" && !member.allOf) member.pattern = value;
1263
+ else member.allOf = (member.allOf || []).concat(patterns);
1264
+ } else if (member[key] === void 0) member[key] = value;
1265
+ else if (LOWER_BOUNDS.has(key)) member[key] = Math.max(member[key], value);
1266
+ else if (UPPER_BOUNDS.has(key)) member[key] = Math.min(member[key], value);
1267
+ }
1268
+ }
1269
+ }
945
1270
  /**
946
1271
  * Converts a JSON Schema object into a {@link TAtscriptAnnotatedType}.
947
1272
  *