@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/cli.cjs CHANGED
@@ -666,6 +666,224 @@ function renderPrimitiveTypeDef(def) {
666
666
  }
667
667
  }
668
668
 
669
+ //#endregion
670
+ //#region packages/typescript/src/runtime/validator-fast-check.ts
671
+ /**
672
+ * Allocation-free boolean pre-check used by {@link Validator.validate} before the
673
+ * error-collecting walk.
674
+ *
675
+ * It reads the type tree and metadata LIVE on every call (nothing derived from the
676
+ * type is cached), so `annotate()` / `metadata.set()` / prop mutations made after a
677
+ * validator was created are always honoured — the same as the full walk.
678
+ *
679
+ * Result contract (relative to the full walk over the same node and value):
680
+ * - `PASS` — the walk returns `true`, collects no errors and has no side effects.
681
+ * - `FAIL` — the walk returns `false`; every node the walk would visit was visited
682
+ * here too (so a walk that would throw makes this throw first) and the walk has
683
+ * no side effects for this node.
684
+ * - `BAIL` — not decidable here; the caller must run the full walk.
685
+ *
686
+ * Union branches are tried in declared order, continuing past `FAIL` and stopping
687
+ * at `BAIL`, which keeps union results identical to the walk.
688
+ */ const FAIL = 0;
689
+ const PASS = 1;
690
+ const BAIL = 2;
691
+ /** Node is at walk depth 0 (only object props / array & tuple items increase depth). */ const TOP = 1;
692
+ /** Node is inside a union branch — failures must be exhaustive (see contract). */ const IN_UNION = 2;
693
+ /**
694
+ * `true` only when the full walk over `value` is guaranteed to pass with no errors
695
+ * and no side effects. Any other outcome (including a throw) means "run the walk".
696
+ */ function fastCheckPasses(def, value, opts) {
697
+ try {
698
+ return fastCheck(def, value, TOP, opts) === PASS;
699
+ } catch {
700
+ return false;
701
+ }
702
+ }
703
+ const DECIMAL_RE = /^[+-]?\d+(\.\d+)?$/;
704
+ const regexCache = /* @__PURE__ */ new Map();
705
+ /** Compiled `@expect.pattern` regex, cached per (pattern, flags). */ function getRegex(pattern, flags) {
706
+ let byFlags = regexCache.get(pattern);
707
+ if (!byFlags) {
708
+ byFlags = /* @__PURE__ */ new Map();
709
+ regexCache.set(pattern, byFlags);
710
+ }
711
+ const flagsKey = flags || "";
712
+ let regex = byFlags.get(flagsKey);
713
+ if (!regex) {
714
+ regex = new RegExp(pattern, flags);
715
+ byFlags.set(flagsKey, regex);
716
+ }
717
+ return regex;
718
+ }
719
+ /**
720
+ * `true` when `def` (an optional field holding `null`) carries `@meta.required` and its
721
+ * type does not itself accept `null`. Shared by the walk and the fast pre-check.
722
+ */ function rejectsNullAsRequired(def) {
723
+ return def.metadata.size > 0 && !!def.metadata.get("meta.required") && !acceptsNull(def);
724
+ }
725
+ function acceptsNull(def) {
726
+ const type = def.type;
727
+ if (type.kind === "") {
728
+ const designType = type.designType;
729
+ return designType === "null" || designType === "any";
730
+ }
731
+ if (type.kind === "union") {
732
+ for (const item of type.items) if (item.optional || acceptsNull(item)) return true;
733
+ }
734
+ return false;
735
+ }
736
+ function fastCheck(def, value, flags, opts) {
737
+ if (def.optional && (value === void 0 || value === null)) return value === null && rejectsNullAsRequired(def) ? FAIL : PASS;
738
+ const type = def.type;
739
+ switch (type.kind) {
740
+ case "": return fastCheckFinal(def, value);
741
+ case "object": return fastCheckObject(def, value, flags, opts);
742
+ case "array": return fastCheckArray(def, value, flags, opts);
743
+ case "union": {
744
+ const branchFlags = flags | IN_UNION;
745
+ for (const item of type.items) {
746
+ const result = fastCheck(item, value, branchFlags, opts);
747
+ if (result !== FAIL) return result === PASS && item.type.value === void 0 ? fastCheckUnionConstraints(def.metadata, value) : result;
748
+ }
749
+ return FAIL;
750
+ }
751
+ case "intersection":
752
+ for (const item of type.items) {
753
+ const result = fastCheck(item, value, flags, opts);
754
+ if (result !== PASS) return result;
755
+ }
756
+ return PASS;
757
+ case "tuple": {
758
+ const items = type.items;
759
+ if (!Array.isArray(value) || value.length !== items.length) return FAIL;
760
+ const itemFlags = flags & IN_UNION;
761
+ for (let i = 0; i < items.length; i++) {
762
+ const result = fastCheck(items[i], value[i], itemFlags, opts);
763
+ if (result !== PASS) return result;
764
+ }
765
+ return PASS;
766
+ }
767
+ default: return BAIL;
768
+ }
769
+ }
770
+ function fastCheckFinal(def, value) {
771
+ const type = def.type;
772
+ const designType = type.designType;
773
+ if (designType === "phantom") return PASS;
774
+ if (type.value !== void 0) return value === type.value ? PASS : FAIL;
775
+ switch (designType) {
776
+ case "string": return typeof value === "string" ? fastCheckString(def.metadata, value) : FAIL;
777
+ case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def.metadata, value) : FAIL;
778
+ case "boolean": return typeof value === "boolean" ? fastCheckBoolean(def.metadata, value) : FAIL;
779
+ case "any": return PASS;
780
+ case "never": return FAIL;
781
+ case "undefined": return value === void 0 ? PASS : FAIL;
782
+ case "null": return value === null ? PASS : FAIL;
783
+ case "decimal": return typeof value === "string" && DECIMAL_RE.test(value) ? PASS : FAIL;
784
+ default: return BAIL;
785
+ }
786
+ }
787
+ /**
788
+ * Mirrors Validator.validateUnionConstraints: union-level constraints on the matched value
789
+ * (`null` / objects pass; the caller skips literal branches).
790
+ */ function fastCheckUnionConstraints(metadata, value) {
791
+ if (metadata.size === 0) return PASS;
792
+ switch (typeof value) {
793
+ case "string": return fastCheckString(metadata, value);
794
+ case "number": return fastCheckNumber(metadata, value);
795
+ case "boolean": return fastCheckBoolean(metadata, value);
796
+ case "object": return Array.isArray(value) ? fastCheckArrayConstraints(metadata, value) : PASS;
797
+ default: return PASS;
798
+ }
799
+ }
800
+ function fastCheckBoolean(metadata, value) {
801
+ if (metadata.size === 0) return PASS;
802
+ return metadata.get("meta.required") && value !== true ? FAIL : PASS;
803
+ }
804
+ function fastCheckString(metadata, value) {
805
+ if (metadata.size === 0) return PASS;
806
+ if (metadata.get("meta.required") && value.trim().length === 0) return FAIL;
807
+ const minLength = metadata.get("expect.minLength");
808
+ if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
809
+ const maxLength = metadata.get("expect.maxLength");
810
+ if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
811
+ const patterns = metadata.get("expect.pattern");
812
+ if (patterns) for (const { pattern, flags } of patterns) {
813
+ if (!pattern) continue;
814
+ const regex = getRegex(pattern, flags);
815
+ if (regex.global || regex.sticky) return BAIL;
816
+ if (!regex.test(value)) return FAIL;
817
+ }
818
+ return PASS;
819
+ }
820
+ function fastCheckNumber(metadata, value) {
821
+ if (metadata.size === 0) return PASS;
822
+ if (metadata.get("expect.int") && value % 1 !== 0) return FAIL;
823
+ const min = metadata.get("expect.min");
824
+ if (min !== void 0 && value < (typeof min === "number" ? min : min.minValue)) return FAIL;
825
+ const max = metadata.get("expect.max");
826
+ if (max !== void 0 && value > (typeof max === "number" ? max : max.maxValue)) return FAIL;
827
+ return PASS;
828
+ }
829
+ function fastCheckObject(def, value, flags, opts) {
830
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
831
+ const type = def.type;
832
+ if (type.propsPatterns.length > 0) return BAIL;
833
+ const partial = opts.partial;
834
+ const skipUndefined = partial === "deep" || partial === true && (flags & TOP) !== 0;
835
+ const inUnion = (flags & IN_UNION) !== 0;
836
+ const propFlags = flags & IN_UNION;
837
+ let result = PASS;
838
+ for (const [key, item] of type.props) {
839
+ if (isPhantomType(item)) continue;
840
+ const propValue = value[key];
841
+ if (propValue === void 0 && skipUndefined) continue;
842
+ const propResult = fastCheck(item, propValue, propFlags, opts);
843
+ if (propResult !== PASS) {
844
+ if (propResult === BAIL || !inUnion) return propResult;
845
+ result = FAIL;
846
+ }
847
+ }
848
+ if (result !== PASS) return result;
849
+ const unknownProps = opts.unknownProps;
850
+ if (unknownProps === "error" || unknownProps === "strip") {
851
+ const props = type.props;
852
+ for (const key of Object.keys(value)) {
853
+ const known = props.get(key);
854
+ if (known === void 0 || isPhantomType(known)) return unknownProps === "error" ? FAIL : BAIL;
855
+ }
856
+ }
857
+ return PASS;
858
+ }
859
+ function fastCheckArray(def, value, flags, opts) {
860
+ if (!Array.isArray(value)) return FAIL;
861
+ const metadata = def.metadata;
862
+ if (metadata.size > 0) {
863
+ const result = fastCheckArrayConstraints(metadata, value);
864
+ if (result !== PASS) return result;
865
+ }
866
+ const of = def.type.of;
867
+ const inUnion = (flags & IN_UNION) !== 0;
868
+ const itemFlags = flags & IN_UNION;
869
+ let result = PASS;
870
+ for (const item of value) {
871
+ const itemResult = fastCheck(of, item, itemFlags, opts);
872
+ if (itemResult !== PASS) {
873
+ if (itemResult === BAIL || !inUnion) return itemResult;
874
+ result = FAIL;
875
+ }
876
+ }
877
+ return result;
878
+ }
879
+ function fastCheckArrayConstraints(metadata, value) {
880
+ const minLength = metadata.get("expect.minLength");
881
+ if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
882
+ const maxLength = metadata.get("expect.maxLength");
883
+ if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
884
+ return metadata.get("expect.array.uniqueItems") ? BAIL : PASS;
885
+ }
886
+
669
887
  //#endregion
670
888
  //#region packages/typescript/src/runtime/validator.ts
671
889
  function _define_property$4(obj, key, value) {
@@ -678,7 +896,6 @@ function _define_property$4(obj, key, value) {
678
896
  else obj[key] = value;
679
897
  return obj;
680
898
  }
681
- const regexCache = /* @__PURE__ */ new Map();
682
899
  /**
683
900
  * Validates values against an {@link TAtscriptAnnotatedType} definition.
684
901
  *
@@ -699,7 +916,7 @@ const regexCache = /* @__PURE__ */ new Map();
699
916
  *
700
917
  * @typeParam T - The annotated type definition.
701
918
  * @typeParam DataType - The TypeScript type that `validate` narrows to (auto-inferred).
702
- */ var Validator = class {
919
+ */ var Validator = class Validator {
703
920
  buildPath() {
704
921
  if (this.depth <= 0) return "";
705
922
  let path = this.pathSegments[0];
@@ -753,6 +970,7 @@ const regexCache = /* @__PURE__ */ new Map();
753
970
  this.stackErrors.length = 0;
754
971
  this.depth = 0;
755
972
  this.limitExceeded = false;
973
+ if (this.shortcuts && !this.opts.skipList && typeof this.opts.partial !== "function" && fastCheckPasses(this.def, value, this.opts)) return true;
756
974
  this.context = context;
757
975
  const passed = this.validateSafe(this.def, value);
758
976
  this.context = void 0;
@@ -762,6 +980,14 @@ const regexCache = /* @__PURE__ */ new Map();
762
980
  }
763
981
  return true;
764
982
  }
983
+ /**
984
+ * Plain `Validator` without plugins / `replace`: eligible for the allocation-free
985
+ * shortcuts. Subclasses may override the protected `validate*` hooks, so they
986
+ * always take the full walk. (A getter, not a constructor-set field: validators are
987
+ * often built per call, so construction stays as cheap as before.)
988
+ */ get shortcuts() {
989
+ return this.constructor === Validator && !this.hasPlugins && !this.hasReplace;
990
+ }
765
991
  validateSafe(def, value) {
766
992
  if (this.limitExceeded) return false;
767
993
  if (this.hasReplace) {
@@ -772,7 +998,13 @@ const regexCache = /* @__PURE__ */ new Map();
772
998
  }
773
999
  def = replaced;
774
1000
  }
775
- if (def.optional && (value === void 0 || value === null)) return true;
1001
+ if (def.optional && (value === void 0 || value === null)) {
1002
+ if (value === null && rejectsNullAsRequired(def)) {
1003
+ this.error(requiredMessage(def));
1004
+ return false;
1005
+ }
1006
+ return true;
1007
+ }
776
1008
  if (this.hasPlugins) for (const plugin of this.opts.plugins) {
777
1009
  const result = plugin(this, def, value);
778
1010
  if (result === false || result === true) return result;
@@ -797,12 +1029,17 @@ const regexCache = /* @__PURE__ */ new Map();
797
1029
  }
798
1030
  validateUnion(def, value) {
799
1031
  const items = def.type.items;
1032
+ if (this.shortcuts) for (const item of items) {
1033
+ const type = item.type;
1034
+ if (item.optional || type.kind !== "" || type.value === void 0 || type.designType === "phantom") break;
1035
+ if (type.value === value) return true;
1036
+ }
800
1037
  let details;
801
1038
  for (const item of items) {
802
1039
  this.stackErrors.push(null);
803
1040
  if (this.validateSafe(item, value)) {
804
1041
  this.stackErrors.pop();
805
- return true;
1042
+ return this.validateUnionConstraints(def, item, value);
806
1043
  }
807
1044
  const branchErrors = this.stackErrors.pop();
808
1045
  if (this.limitExceeded) this.limitExceeded = false;
@@ -813,6 +1050,23 @@ const regexCache = /* @__PURE__ */ new Map();
813
1050
  this.error(`Value does not match any of the allowed types: ${expected}`, void 0, details);
814
1051
  return false;
815
1052
  }
1053
+ /**
1054
+ * Constraints annotated on the union itself (`@expect.min 0` on `number | null`)
1055
+ * apply to the non-null value that matched `branch`, on top of the branch's own.
1056
+ * A literal branch (an enum value) is exempt: its value is fixed.
1057
+ */ validateUnionConstraints(def, branch, value) {
1058
+ const metadata = def.metadata;
1059
+ if (metadata.size === 0 || branch.type.value !== void 0) return true;
1060
+ switch (typeof value) {
1061
+ case "string": return this.validateStringConstraints(metadata, value);
1062
+ case "number": return this.validateNumberConstraints(metadata, value);
1063
+ case "boolean": return this.validateBooleanConstraints(metadata, value);
1064
+ case "object":
1065
+ if (Array.isArray(value)) return this.validateArrayConstraints(metadata, value, arrayElementType(branch));
1066
+ return true;
1067
+ default: return true;
1068
+ }
1069
+ }
816
1070
  validateIntersection(def, value) {
817
1071
  for (const item of def.type.items) if (!this.validateSafe(item, value)) return false;
818
1072
  return true;
@@ -839,8 +1093,23 @@ const regexCache = /* @__PURE__ */ new Map();
839
1093
  this.error("Expected array");
840
1094
  return false;
841
1095
  }
842
- const minLength = def.metadata.get("expect.minLength");
843
- if (minLength) {
1096
+ if (def.metadata.size > 0 && !this.validateArrayConstraints(def.metadata, value, def.type.of)) return false;
1097
+ let i = 0;
1098
+ let passed = true;
1099
+ for (const item of value) {
1100
+ this.push(String(i));
1101
+ if (!this.validateSafe(def.type.of, item)) {
1102
+ passed = false;
1103
+ this.pop(true);
1104
+ if (this.limitExceeded) return false;
1105
+ } else this.pop(false);
1106
+ i++;
1107
+ }
1108
+ return passed;
1109
+ }
1110
+ /** `@expect.minLength` / `maxLength` / `array.uniqueItems` checks for an array value. */ validateArrayConstraints(metadata, value, of) {
1111
+ const minLength = metadata.get("expect.minLength");
1112
+ if (minLength !== void 0) {
844
1113
  const length = typeof minLength === "number" ? minLength : minLength.length;
845
1114
  if (value.length < length) {
846
1115
  const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
@@ -848,8 +1117,8 @@ const regexCache = /* @__PURE__ */ new Map();
848
1117
  return false;
849
1118
  }
850
1119
  }
851
- const maxLength = def.metadata.get("expect.maxLength");
852
- if (maxLength) {
1120
+ const maxLength = metadata.get("expect.maxLength");
1121
+ if (maxLength !== void 0) {
853
1122
  const length = typeof maxLength === "number" ? maxLength : maxLength.length;
854
1123
  if (value.length > length) {
855
1124
  const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
@@ -857,13 +1126,13 @@ const regexCache = /* @__PURE__ */ new Map();
857
1126
  return false;
858
1127
  }
859
1128
  }
860
- const uniqueItems = def.metadata.get("expect.array.uniqueItems");
1129
+ const uniqueItems = metadata.get("expect.array.uniqueItems");
861
1130
  if (uniqueItems) {
862
1131
  const separator = "▼↩";
863
1132
  const seen = /* @__PURE__ */ new Set();
864
1133
  const keyProps = /* @__PURE__ */ new Set();
865
- if (def.type.of.type.kind === "object") {
866
- for (const [key, val] of def.type.of.type.props.entries()) if (val.metadata.get("expect.array.key")) keyProps.add(key);
1134
+ if (of?.type.kind === "object") {
1135
+ for (const [key, val] of of.type.props.entries()) if (val.metadata.get("expect.array.key")) keyProps.add(key);
867
1136
  }
868
1137
  for (let idx = 0; idx < value.length; idx++) {
869
1138
  const item = value[idx];
@@ -881,18 +1150,7 @@ const regexCache = /* @__PURE__ */ new Map();
881
1150
  seen.add(key);
882
1151
  }
883
1152
  }
884
- let i = 0;
885
- let passed = true;
886
- for (const item of value) {
887
- this.push(String(i));
888
- if (!this.validateSafe(def.type.of, item)) {
889
- passed = false;
890
- this.pop(true);
891
- if (this.limitExceeded) return false;
892
- } else this.pop(false);
893
- i++;
894
- }
895
- return passed;
1153
+ return true;
896
1154
  }
897
1155
  validateObject(def, value) {
898
1156
  if (typeof value !== "object" || value === null || Array.isArray(value)) {
@@ -1021,7 +1279,7 @@ const regexCache = /* @__PURE__ */ new Map();
1021
1279
  this.error(`Expected string (decimal), got ${typeOfValue}`);
1022
1280
  return false;
1023
1281
  }
1024
- if (!/^[+-]?\d+(\.\d+)?$/.test(value)) {
1282
+ if (!DECIMAL_RE.test(value)) {
1025
1283
  this.error(`Invalid decimal format: ${JSON.stringify(value)}`);
1026
1284
  return false;
1027
1285
  }
@@ -1030,8 +1288,11 @@ const regexCache = /* @__PURE__ */ new Map();
1030
1288
  }
1031
1289
  }
1032
1290
  validateString(def, value) {
1033
- if (def.metadata.size === 0) return true;
1034
- const filled = def.metadata.get("meta.required");
1291
+ return this.validateStringConstraints(def.metadata, value);
1292
+ }
1293
+ /** `@meta.required` / `@expect.minLength` / `maxLength` / `pattern` checks for a string value. */ validateStringConstraints(metadata, value) {
1294
+ if (metadata.size === 0) return true;
1295
+ const filled = metadata.get("meta.required");
1035
1296
  if (filled) {
1036
1297
  if (value.trim().length === 0) {
1037
1298
  const message = typeof filled === "object" && filled.message ? filled.message : `Must not be empty`;
@@ -1039,8 +1300,8 @@ const regexCache = /* @__PURE__ */ new Map();
1039
1300
  return false;
1040
1301
  }
1041
1302
  }
1042
- const minLength = def.metadata.get("expect.minLength");
1043
- if (minLength) {
1303
+ const minLength = metadata.get("expect.minLength");
1304
+ if (minLength !== void 0) {
1044
1305
  const length = typeof minLength === "number" ? minLength : minLength.length;
1045
1306
  if (value.length < length) {
1046
1307
  const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
@@ -1048,8 +1309,8 @@ const regexCache = /* @__PURE__ */ new Map();
1048
1309
  return false;
1049
1310
  }
1050
1311
  }
1051
- const maxLength = def.metadata.get("expect.maxLength");
1052
- if (maxLength) {
1312
+ const maxLength = metadata.get("expect.maxLength");
1313
+ if (maxLength !== void 0) {
1053
1314
  const length = typeof maxLength === "number" ? maxLength : maxLength.length;
1054
1315
  if (value.length > length) {
1055
1316
  const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
@@ -1057,16 +1318,10 @@ const regexCache = /* @__PURE__ */ new Map();
1057
1318
  return false;
1058
1319
  }
1059
1320
  }
1060
- const patterns = def.metadata.get("expect.pattern");
1321
+ const patterns = metadata.get("expect.pattern");
1061
1322
  for (const { pattern, flags, message } of patterns || []) {
1062
1323
  if (!pattern) continue;
1063
- const cacheKey = `${pattern}//${flags || ""}`;
1064
- let regex = regexCache.get(cacheKey);
1065
- if (!regex) {
1066
- regex = new RegExp(pattern, flags);
1067
- regexCache.set(cacheKey, regex);
1068
- }
1069
- if (!regex.test(value)) {
1324
+ if (!getRegex(pattern, flags).test(value)) {
1070
1325
  this.error(message || `Value is expected to match pattern "${pattern}"`);
1071
1326
  return false;
1072
1327
  }
@@ -1074,15 +1329,18 @@ const regexCache = /* @__PURE__ */ new Map();
1074
1329
  return true;
1075
1330
  }
1076
1331
  validateNumber(def, value) {
1077
- if (def.metadata.size === 0) return true;
1078
- const int = def.metadata.get("expect.int");
1332
+ return this.validateNumberConstraints(def.metadata, value);
1333
+ }
1334
+ /** `@expect.int` / `min` / `max` checks for a number value. */ validateNumberConstraints(metadata, value) {
1335
+ if (metadata.size === 0) return true;
1336
+ const int = metadata.get("expect.int");
1079
1337
  if (int && value % 1 !== 0) {
1080
1338
  const message = typeof int === "object" && int.message ? int.message : `Expected integer, got ${value}`;
1081
1339
  this.error(message);
1082
1340
  return false;
1083
1341
  }
1084
- const min = def.metadata.get("expect.min");
1085
- if (min) {
1342
+ const min = metadata.get("expect.min");
1343
+ if (min !== void 0) {
1086
1344
  const minValue = typeof min === "number" ? min : min.minValue;
1087
1345
  if (value < minValue) {
1088
1346
  const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
@@ -1090,8 +1348,8 @@ const regexCache = /* @__PURE__ */ new Map();
1090
1348
  return false;
1091
1349
  }
1092
1350
  }
1093
- const max = def.metadata.get("expect.max");
1094
- if (max) {
1351
+ const max = metadata.get("expect.max");
1352
+ if (max !== void 0) {
1095
1353
  const maxValue = typeof max === "number" ? max : max.maxValue;
1096
1354
  if (value > maxValue) {
1097
1355
  const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
@@ -1102,8 +1360,11 @@ const regexCache = /* @__PURE__ */ new Map();
1102
1360
  return true;
1103
1361
  }
1104
1362
  validateBoolean(def, value) {
1105
- if (def.metadata.size === 0) return true;
1106
- const filled = def.metadata.get("meta.required");
1363
+ return this.validateBooleanConstraints(def.metadata, value);
1364
+ }
1365
+ /** `@meta.required` check for a boolean value (must be `true`). */ validateBooleanConstraints(metadata, value) {
1366
+ if (metadata.size === 0) return true;
1367
+ const filled = metadata.get("meta.required");
1107
1368
  if (filled) {
1108
1369
  if (value !== true) {
1109
1370
  const message = typeof filled === "object" && filled.message ? filled.message : `Must be checked`;
@@ -1143,6 +1404,23 @@ const regexCache = /* @__PURE__ */ new Map();
1143
1404
  if (this.hasReplace) this.replaceCache = /* @__PURE__ */ new WeakMap();
1144
1405
  }
1145
1406
  };
1407
+ /** The `@meta.required` error message for `def` (custom message or the type's default). */ function requiredMessage(def) {
1408
+ const filled = def.metadata.get("meta.required");
1409
+ if (typeof filled === "object" && filled.message) return filled.message;
1410
+ return def.type.kind === "" && def.type.designType === "boolean" ? "Must be checked" : "Must not be empty";
1411
+ }
1412
+ /**
1413
+ * Element type of the array a union branch holds — through nested unions too
1414
+ * (`Items | undefined` with `type Items = Item[] | null`), so `@expect.array.key`
1415
+ * fields of the elements are found.
1416
+ */ function arrayElementType(branch) {
1417
+ const type = branch.type;
1418
+ if (type.kind === "array") return type.of;
1419
+ if (type.kind === "union") for (const item of type.items) {
1420
+ const of = arrayElementType(item);
1421
+ if (of) return of;
1422
+ }
1423
+ }
1146
1424
  /** Error thrown by {@link Validator.validate} when validation fails. Contains structured error details. */ var ValidatorError = class extends Error {
1147
1425
  constructor(errors) {
1148
1426
  super(`${errors[0].path ? errors[0].path + ": " : ""}${errors[0].message}`), _define_property$4(this, "errors", void 0), _define_property$4(this, "name", void 0), this.errors = errors, this.name = "Validation Error";
@@ -1511,10 +1789,7 @@ function cloneTypeDef(type) {
1511
1789
  type: "array",
1512
1790
  items: build(d.type.of)
1513
1791
  };
1514
- const minLength = meta.get("expect.minLength");
1515
- if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
1516
- const maxLength = meta.get("expect.maxLength");
1517
- if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
1792
+ applyArrayConstraints(schema, meta);
1518
1793
  return schema;
1519
1794
  },
1520
1795
  union(d) {
@@ -1534,7 +1809,9 @@ function cloneTypeDef(type) {
1534
1809
  }
1535
1810
  };
1536
1811
  }
1537
- return { anyOf: d.type.items.map(build) };
1812
+ const anyOf = d.type.items.map(build);
1813
+ if (meta.size > 0) applyUnionConstraints(anyOf, meta);
1814
+ return { anyOf };
1538
1815
  },
1539
1816
  intersection(d) {
1540
1817
  return { allOf: d.type.items.map(build) };
@@ -1552,24 +1829,8 @@ function cloneTypeDef(type) {
1552
1829
  if (d.type.designType && d.type.designType !== "any") {
1553
1830
  const dt = d.type.designType;
1554
1831
  schema.type = dt === "undefined" ? "null" : dt === "decimal" ? "string" : dt;
1555
- if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
1556
- }
1557
- if (schema.type === "string") {
1558
- if (meta.get("meta.required")) schema.minLength = 1;
1559
- const minLength = meta.get("expect.minLength");
1560
- if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
1561
- const maxLength = meta.get("expect.maxLength");
1562
- if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
1563
- const patterns = meta.get("expect.pattern");
1564
- if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
1565
- else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
1566
- }
1567
- if (schema.type === "number" || schema.type === "integer") {
1568
- const min = meta.get("expect.min");
1569
- if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
1570
- const max = meta.get("expect.max");
1571
- if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
1572
1832
  }
1833
+ applyScalarConstraints(schema, meta);
1573
1834
  return schema;
1574
1835
  }
1575
1836
  });
@@ -1581,6 +1842,70 @@ function cloneTypeDef(type) {
1581
1842
  };
1582
1843
  return schema;
1583
1844
  }
1845
+ /** `@expect.minLength` / `maxLength` → `minItems` / `maxItems` on an array schema. */ function applyArrayConstraints(schema, meta) {
1846
+ const minLength = meta.get("expect.minLength");
1847
+ if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
1848
+ const maxLength = meta.get("expect.maxLength");
1849
+ if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
1850
+ }
1851
+ /** String / number constraints on a scalar schema whose `type` is already set. */ function applyScalarConstraints(schema, meta) {
1852
+ if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
1853
+ if (schema.type === "string") {
1854
+ if (meta.get("meta.required")) schema.minLength = 1;
1855
+ const minLength = meta.get("expect.minLength");
1856
+ if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
1857
+ const maxLength = meta.get("expect.maxLength");
1858
+ if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
1859
+ const patterns = meta.get("expect.pattern");
1860
+ if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
1861
+ else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
1862
+ }
1863
+ if (schema.type === "number" || schema.type === "integer") {
1864
+ const min = meta.get("expect.min");
1865
+ if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
1866
+ const max = meta.get("expect.max");
1867
+ if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
1868
+ }
1869
+ }
1870
+ const LOWER_BOUNDS = new Set([
1871
+ "minimum",
1872
+ "minLength",
1873
+ "minItems"
1874
+ ]);
1875
+ const UPPER_BOUNDS = new Set([
1876
+ "maximum",
1877
+ "maxLength",
1878
+ "maxItems"
1879
+ ]);
1880
+ /**
1881
+ * Puts union-level constraints onto the matching `anyOf` members (numbers, strings,
1882
+ * arrays; nested `anyOf` members recursively). When a member already has the same
1883
+ * bound, the stricter one wins; patterns accumulate.
1884
+ */ function applyUnionConstraints(members, meta) {
1885
+ for (const member of members) {
1886
+ if (Array.isArray(member.anyOf)) {
1887
+ applyUnionConstraints(member.anyOf, meta);
1888
+ continue;
1889
+ }
1890
+ if (member.const !== void 0) continue;
1891
+ const type = member.type;
1892
+ const extra = { type };
1893
+ if (type === "array") applyArrayConstraints(extra, meta);
1894
+ else if (type === "string" || type === "number" || type === "integer") applyScalarConstraints(extra, meta);
1895
+ else continue;
1896
+ if (extra.type === "integer") member.type = "integer";
1897
+ for (const [key, value] of Object.entries(extra)) {
1898
+ if (key === "type") continue;
1899
+ if (key === "pattern" || key === "allOf") {
1900
+ const patterns = key === "pattern" ? [{ pattern: value }] : value;
1901
+ if (member.pattern === void 0 && key === "pattern" && !member.allOf) member.pattern = value;
1902
+ else member.allOf = (member.allOf || []).concat(patterns);
1903
+ } else if (member[key] === void 0) member[key] = value;
1904
+ else if (LOWER_BOUNDS.has(key)) member[key] = Math.max(member[key], value);
1905
+ else if (UPPER_BOUNDS.has(key)) member[key] = Math.min(member[key], value);
1906
+ }
1907
+ }
1908
+ }
1584
1909
 
1585
1910
  //#endregion
1586
1911
  //#region packages/typescript/src/codegen/js-renderer.ts
@@ -1964,7 +2289,10 @@ var JsRenderer = class extends BaseRenderer {
1964
2289
  const ref = node;
1965
2290
  const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
1966
2291
  const handle = this.toAnnotatedHandle(decl, true);
1967
- return skipAnnotations ? handle : this.applyExpectAnnotations(handle, this.doc.evalAnnotationsForNode(node));
2292
+ if (skipAnnotations) return handle;
2293
+ const own = this.doc.evalAnnotationsForNode(node);
2294
+ const referred = this.refTargetAnnotations(ref, decl);
2295
+ return this.applyExpectAnnotations(handle, referred ? this.doc.mergeNodesAnnotations(referred, own) : own);
1968
2296
  }
1969
2297
  case "primitive": {
1970
2298
  const prim = node;
@@ -2046,6 +2374,7 @@ var JsRenderer = class extends BaseRenderer {
2046
2374
  }, true);
2047
2375
  break;
2048
2376
  case "expect.int":
2377
+ case "meta.required":
2049
2378
  handle.annotate(a.name, true);
2050
2379
  break;
2051
2380
  case "expect.array.uniqueItems":
@@ -2065,12 +2394,10 @@ var JsRenderer = class extends BaseRenderer {
2065
2394
  case "ref": {
2066
2395
  const ref = node;
2067
2396
  const decl = this.doc.unwindType(ref.id, ref.chain)?.def;
2068
- if ((0, _atscript_core.isPrimitive)(decl)) {
2069
- const ownerDecl = this.resolveOwner(this.doc, ref.id);
2070
- if (!ownerDecl?.node || ownerDecl.node.entity !== "type" && ownerDecl.node.entity !== "interface") {
2071
- this.annotateType(decl, name);
2072
- return this;
2073
- }
2397
+ const primitive = this.inlinedPrimitive(ref, decl);
2398
+ if (primitive) {
2399
+ this.annotateType(primitive, name);
2400
+ return this;
2074
2401
  }
2075
2402
  if (this._adHocAnnotations && this.hasAdHocAnnotationsThroughRef()) {
2076
2403
  let resolved = decl ? this.doc.mergeIntersection(decl) : void 0;
@@ -2221,13 +2548,44 @@ var JsRenderer = class extends BaseRenderer {
2221
2548
  }
2222
2549
  defineGroup(node) {
2223
2550
  const items = node.unwrap();
2224
- for (const item of items) this.write(".item(").indent().annotateType(item).write(" .$type").writeln(`)`).unindent();
2551
+ for (const item of items) this.write(".item(").indent().annotateType(item).defineInlinePrimitiveMetadata(item).write(" .$type").writeln(`)`).unindent();
2225
2552
  return this;
2226
2553
  }
2227
2554
  defineArray(node) {
2228
- this.write(".of(").indent().annotateType(node.getDefinition()).write(" .$type").writeln(`)`).unindent();
2555
+ const of = node.getDefinition();
2556
+ this.write(".of(").indent().annotateType(of).defineInlinePrimitiveMetadata(of).write(" .$type").writeln(`)`).unindent();
2229
2557
  return this;
2230
2558
  }
2559
+ /**
2560
+ * A built-in primitive extension used directly as a union / tuple member or array
2561
+ * element (`number.int | null`, `string.email[]`) is inlined by `annotateType` without
2562
+ * its built-in annotations (`expect.int`, the email pattern, …) — a prop gets them
2563
+ * through `defineMetadata`, a member has no prop to carry them, so emit them here.
2564
+ */ defineInlinePrimitiveMetadata(node) {
2565
+ const primitive = node && this.inlinedPrimitive(node);
2566
+ if (primitive?.annotations?.length) {
2567
+ this.indent();
2568
+ for (const an of primitive.annotations) this.resolveAnnotationValue(primitive, an);
2569
+ this.unindent();
2570
+ }
2571
+ return this;
2572
+ }
2573
+ /**
2574
+ * The built-in primitive a ref is inlined as by `annotateType` — not a named alias that
2575
+ * resolves to one (`type MyString = string`), which is emitted as a `refTo`.
2576
+ */ inlinedPrimitive(node, decl = (0, _atscript_core.isRef)(node) ? this.doc.unwindType(node.id, node.chain)?.def : void 0) {
2577
+ if (!(0, _atscript_core.isRef)(node) || !(0, _atscript_core.isPrimitive)(decl)) return;
2578
+ const ownerDecl = this.resolveOwner(this.doc, node.id);
2579
+ if (ownerDecl?.node && (ownerDecl.node.entity === "type" || ownerDecl.node.entity === "interface")) return;
2580
+ return decl;
2581
+ }
2582
+ /** Annotations a ref inherits from its target (see `annotateType`'s ref case). */ refTargetAnnotations(ref, decl) {
2583
+ const primitive = this.inlinedPrimitive(ref, decl);
2584
+ if (primitive) return primitive.annotations;
2585
+ if (ref.hasChain) return;
2586
+ const ownerDecl = this.resolveOwner(this.doc, ref.id);
2587
+ return ownerDecl?.node ? ownerDecl.doc.filterPassedWhenReferred(ownerDecl.doc.evalAnnotationsForNode(ownerDecl.node)) : void 0;
2588
+ }
2231
2589
  defineMetadata(node) {
2232
2590
  let annotations;
2233
2591
  const nodeDef = node.getDefinition?.();