@atscript/typescript 0.1.101 → 0.1.102
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 +214 -22
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/{json-schema-CtQ_rzJn.cjs → json-schema-B9xrfeoQ.cjs} +214 -22
- package/dist/{json-schema-BogNt-9w.mjs → json-schema-CW_50s1W.mjs} +214 -22
- package/dist/{plugin-DG3jlbSN.mjs → plugin-CECXHL6M.mjs} +1 -1
- package/dist/{plugin-Cu6Nez-H.cjs → plugin-DORafTJq.cjs} +1 -1
- package/dist/test-utils.cjs +1 -1
- package/dist/test-utils.mjs +1 -1
- package/dist/utils.cjs +22 -18
- package/dist/utils.d.ts +7 -0
- package/dist/utils.mjs +22 -18
- package/package.json +2 -2
package/dist/cli.cjs
CHANGED
|
@@ -666,6 +666,191 @@ 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
|
+
function fastCheck(def, value, flags, opts) {
|
|
720
|
+
if (def.optional && (value === void 0 || value === null)) return PASS;
|
|
721
|
+
const type = def.type;
|
|
722
|
+
switch (type.kind) {
|
|
723
|
+
case "": return fastCheckFinal(def, value);
|
|
724
|
+
case "object": return fastCheckObject(def, value, flags, opts);
|
|
725
|
+
case "array": return fastCheckArray(def, value, flags, opts);
|
|
726
|
+
case "union": {
|
|
727
|
+
const branchFlags = flags | IN_UNION;
|
|
728
|
+
for (const item of type.items) {
|
|
729
|
+
const result = fastCheck(item, value, branchFlags, opts);
|
|
730
|
+
if (result !== FAIL) return result;
|
|
731
|
+
}
|
|
732
|
+
return FAIL;
|
|
733
|
+
}
|
|
734
|
+
case "intersection":
|
|
735
|
+
for (const item of type.items) {
|
|
736
|
+
const result = fastCheck(item, value, flags, opts);
|
|
737
|
+
if (result !== PASS) return result;
|
|
738
|
+
}
|
|
739
|
+
return PASS;
|
|
740
|
+
case "tuple": {
|
|
741
|
+
const items = type.items;
|
|
742
|
+
if (!Array.isArray(value) || value.length !== items.length) return FAIL;
|
|
743
|
+
const itemFlags = flags & IN_UNION;
|
|
744
|
+
for (let i = 0; i < items.length; i++) {
|
|
745
|
+
const result = fastCheck(items[i], value[i], itemFlags, opts);
|
|
746
|
+
if (result !== PASS) return result;
|
|
747
|
+
}
|
|
748
|
+
return PASS;
|
|
749
|
+
}
|
|
750
|
+
default: return BAIL;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
function fastCheckFinal(def, value) {
|
|
754
|
+
const type = def.type;
|
|
755
|
+
const designType = type.designType;
|
|
756
|
+
if (designType === "phantom") return PASS;
|
|
757
|
+
if (type.value !== void 0) return value === type.value ? PASS : FAIL;
|
|
758
|
+
switch (designType) {
|
|
759
|
+
case "string": return typeof value === "string" ? fastCheckString(def, value) : FAIL;
|
|
760
|
+
case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def, value) : FAIL;
|
|
761
|
+
case "boolean":
|
|
762
|
+
if (typeof value !== "boolean") return FAIL;
|
|
763
|
+
if (def.metadata.size === 0) return PASS;
|
|
764
|
+
return def.metadata.get("meta.required") && value !== true ? FAIL : PASS;
|
|
765
|
+
case "any": return PASS;
|
|
766
|
+
case "never": return FAIL;
|
|
767
|
+
case "undefined": return value === void 0 ? PASS : FAIL;
|
|
768
|
+
case "null": return value === null ? PASS : FAIL;
|
|
769
|
+
case "decimal": return typeof value === "string" && DECIMAL_RE.test(value) ? PASS : FAIL;
|
|
770
|
+
default: return BAIL;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
function fastCheckString(def, value) {
|
|
774
|
+
const metadata = def.metadata;
|
|
775
|
+
if (metadata.size === 0) return PASS;
|
|
776
|
+
if (metadata.get("meta.required") && value.trim().length === 0) return FAIL;
|
|
777
|
+
const minLength = metadata.get("expect.minLength");
|
|
778
|
+
if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
|
|
779
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
780
|
+
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
781
|
+
const patterns = metadata.get("expect.pattern");
|
|
782
|
+
if (patterns) for (const { pattern, flags } of patterns) {
|
|
783
|
+
if (!pattern) continue;
|
|
784
|
+
const regex = getRegex(pattern, flags);
|
|
785
|
+
if (regex.global || regex.sticky) return BAIL;
|
|
786
|
+
if (!regex.test(value)) return FAIL;
|
|
787
|
+
}
|
|
788
|
+
return PASS;
|
|
789
|
+
}
|
|
790
|
+
function fastCheckNumber(def, value) {
|
|
791
|
+
const metadata = def.metadata;
|
|
792
|
+
if (metadata.size === 0) return PASS;
|
|
793
|
+
if (metadata.get("expect.int") && value % 1 !== 0) return FAIL;
|
|
794
|
+
const min = metadata.get("expect.min");
|
|
795
|
+
if (min !== void 0 && value < (typeof min === "number" ? min : min.minValue)) return FAIL;
|
|
796
|
+
const max = metadata.get("expect.max");
|
|
797
|
+
if (max !== void 0 && value > (typeof max === "number" ? max : max.maxValue)) return FAIL;
|
|
798
|
+
return PASS;
|
|
799
|
+
}
|
|
800
|
+
function fastCheckObject(def, value, flags, opts) {
|
|
801
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
|
|
802
|
+
const type = def.type;
|
|
803
|
+
if (type.propsPatterns.length > 0) return BAIL;
|
|
804
|
+
const partial = opts.partial;
|
|
805
|
+
const skipUndefined = partial === "deep" || partial === true && (flags & TOP) !== 0;
|
|
806
|
+
const inUnion = (flags & IN_UNION) !== 0;
|
|
807
|
+
const propFlags = flags & IN_UNION;
|
|
808
|
+
let result = PASS;
|
|
809
|
+
for (const [key, item] of type.props) {
|
|
810
|
+
if (isPhantomType(item)) continue;
|
|
811
|
+
const propValue = value[key];
|
|
812
|
+
if (propValue === void 0 && skipUndefined) continue;
|
|
813
|
+
const propResult = fastCheck(item, propValue, propFlags, opts);
|
|
814
|
+
if (propResult !== PASS) {
|
|
815
|
+
if (propResult === BAIL || !inUnion) return propResult;
|
|
816
|
+
result = FAIL;
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
if (result !== PASS) return result;
|
|
820
|
+
const unknownProps = opts.unknownProps;
|
|
821
|
+
if (unknownProps === "error" || unknownProps === "strip") {
|
|
822
|
+
const props = type.props;
|
|
823
|
+
for (const key of Object.keys(value)) {
|
|
824
|
+
const known = props.get(key);
|
|
825
|
+
if (known === void 0 || isPhantomType(known)) return unknownProps === "error" ? FAIL : BAIL;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
return PASS;
|
|
829
|
+
}
|
|
830
|
+
function fastCheckArray(def, value, flags, opts) {
|
|
831
|
+
if (!Array.isArray(value)) return FAIL;
|
|
832
|
+
const metadata = def.metadata;
|
|
833
|
+
if (metadata.size > 0) {
|
|
834
|
+
const minLength = metadata.get("expect.minLength");
|
|
835
|
+
if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
|
|
836
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
837
|
+
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
838
|
+
if (metadata.get("expect.array.uniqueItems")) return BAIL;
|
|
839
|
+
}
|
|
840
|
+
const of = def.type.of;
|
|
841
|
+
const inUnion = (flags & IN_UNION) !== 0;
|
|
842
|
+
const itemFlags = flags & IN_UNION;
|
|
843
|
+
let result = PASS;
|
|
844
|
+
for (const item of value) {
|
|
845
|
+
const itemResult = fastCheck(of, item, itemFlags, opts);
|
|
846
|
+
if (itemResult !== PASS) {
|
|
847
|
+
if (itemResult === BAIL || !inUnion) return itemResult;
|
|
848
|
+
result = FAIL;
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
return result;
|
|
852
|
+
}
|
|
853
|
+
|
|
669
854
|
//#endregion
|
|
670
855
|
//#region packages/typescript/src/runtime/validator.ts
|
|
671
856
|
function _define_property$4(obj, key, value) {
|
|
@@ -678,7 +863,6 @@ function _define_property$4(obj, key, value) {
|
|
|
678
863
|
else obj[key] = value;
|
|
679
864
|
return obj;
|
|
680
865
|
}
|
|
681
|
-
const regexCache = /* @__PURE__ */ new Map();
|
|
682
866
|
/**
|
|
683
867
|
* Validates values against an {@link TAtscriptAnnotatedType} definition.
|
|
684
868
|
*
|
|
@@ -699,7 +883,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
699
883
|
*
|
|
700
884
|
* @typeParam T - The annotated type definition.
|
|
701
885
|
* @typeParam DataType - The TypeScript type that `validate` narrows to (auto-inferred).
|
|
702
|
-
*/ var Validator = class {
|
|
886
|
+
*/ var Validator = class Validator {
|
|
703
887
|
buildPath() {
|
|
704
888
|
if (this.depth <= 0) return "";
|
|
705
889
|
let path = this.pathSegments[0];
|
|
@@ -753,6 +937,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
753
937
|
this.stackErrors.length = 0;
|
|
754
938
|
this.depth = 0;
|
|
755
939
|
this.limitExceeded = false;
|
|
940
|
+
if (this.shortcuts && !this.opts.skipList && typeof this.opts.partial !== "function" && fastCheckPasses(this.def, value, this.opts)) return true;
|
|
756
941
|
this.context = context;
|
|
757
942
|
const passed = this.validateSafe(this.def, value);
|
|
758
943
|
this.context = void 0;
|
|
@@ -762,6 +947,14 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
762
947
|
}
|
|
763
948
|
return true;
|
|
764
949
|
}
|
|
950
|
+
/**
|
|
951
|
+
* Plain `Validator` without plugins / `replace`: eligible for the allocation-free
|
|
952
|
+
* shortcuts. Subclasses may override the protected `validate*` hooks, so they
|
|
953
|
+
* always take the full walk. (A getter, not a constructor-set field: validators are
|
|
954
|
+
* often built per call, so construction stays as cheap as before.)
|
|
955
|
+
*/ get shortcuts() {
|
|
956
|
+
return this.constructor === Validator && !this.hasPlugins && !this.hasReplace;
|
|
957
|
+
}
|
|
765
958
|
validateSafe(def, value) {
|
|
766
959
|
if (this.limitExceeded) return false;
|
|
767
960
|
if (this.hasReplace) {
|
|
@@ -797,6 +990,11 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
797
990
|
}
|
|
798
991
|
validateUnion(def, value) {
|
|
799
992
|
const items = def.type.items;
|
|
993
|
+
if (this.shortcuts) for (const item of items) {
|
|
994
|
+
const type = item.type;
|
|
995
|
+
if (item.optional || type.kind !== "" || type.value === void 0 || type.designType === "phantom") break;
|
|
996
|
+
if (type.value === value) return true;
|
|
997
|
+
}
|
|
800
998
|
let details;
|
|
801
999
|
for (const item of items) {
|
|
802
1000
|
this.stackErrors.push(null);
|
|
@@ -840,7 +1038,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
840
1038
|
return false;
|
|
841
1039
|
}
|
|
842
1040
|
const minLength = def.metadata.get("expect.minLength");
|
|
843
|
-
if (minLength) {
|
|
1041
|
+
if (minLength !== void 0) {
|
|
844
1042
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
845
1043
|
if (value.length < length) {
|
|
846
1044
|
const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
|
|
@@ -849,7 +1047,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
849
1047
|
}
|
|
850
1048
|
}
|
|
851
1049
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
852
|
-
if (maxLength) {
|
|
1050
|
+
if (maxLength !== void 0) {
|
|
853
1051
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
854
1052
|
if (value.length > length) {
|
|
855
1053
|
const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
|
|
@@ -1021,7 +1219,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
1021
1219
|
this.error(`Expected string (decimal), got ${typeOfValue}`);
|
|
1022
1220
|
return false;
|
|
1023
1221
|
}
|
|
1024
|
-
if (
|
|
1222
|
+
if (!DECIMAL_RE.test(value)) {
|
|
1025
1223
|
this.error(`Invalid decimal format: ${JSON.stringify(value)}`);
|
|
1026
1224
|
return false;
|
|
1027
1225
|
}
|
|
@@ -1040,7 +1238,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
1040
1238
|
}
|
|
1041
1239
|
}
|
|
1042
1240
|
const minLength = def.metadata.get("expect.minLength");
|
|
1043
|
-
if (minLength) {
|
|
1241
|
+
if (minLength !== void 0) {
|
|
1044
1242
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
1045
1243
|
if (value.length < length) {
|
|
1046
1244
|
const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
|
|
@@ -1049,7 +1247,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
1049
1247
|
}
|
|
1050
1248
|
}
|
|
1051
1249
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
1052
|
-
if (maxLength) {
|
|
1250
|
+
if (maxLength !== void 0) {
|
|
1053
1251
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1054
1252
|
if (value.length > length) {
|
|
1055
1253
|
const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
|
|
@@ -1060,13 +1258,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
1060
1258
|
const patterns = def.metadata.get("expect.pattern");
|
|
1061
1259
|
for (const { pattern, flags, message } of patterns || []) {
|
|
1062
1260
|
if (!pattern) continue;
|
|
1063
|
-
|
|
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)) {
|
|
1261
|
+
if (!getRegex(pattern, flags).test(value)) {
|
|
1070
1262
|
this.error(message || `Value is expected to match pattern "${pattern}"`);
|
|
1071
1263
|
return false;
|
|
1072
1264
|
}
|
|
@@ -1082,7 +1274,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
1082
1274
|
return false;
|
|
1083
1275
|
}
|
|
1084
1276
|
const min = def.metadata.get("expect.min");
|
|
1085
|
-
if (min) {
|
|
1277
|
+
if (min !== void 0) {
|
|
1086
1278
|
const minValue = typeof min === "number" ? min : min.minValue;
|
|
1087
1279
|
if (value < minValue) {
|
|
1088
1280
|
const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
|
|
@@ -1091,7 +1283,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
1091
1283
|
}
|
|
1092
1284
|
}
|
|
1093
1285
|
const max = def.metadata.get("expect.max");
|
|
1094
|
-
if (max) {
|
|
1286
|
+
if (max !== void 0) {
|
|
1095
1287
|
const maxValue = typeof max === "number" ? max : max.maxValue;
|
|
1096
1288
|
if (value > maxValue) {
|
|
1097
1289
|
const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
|
|
@@ -1512,9 +1704,9 @@ function cloneTypeDef(type) {
|
|
|
1512
1704
|
items: build(d.type.of)
|
|
1513
1705
|
};
|
|
1514
1706
|
const minLength = meta.get("expect.minLength");
|
|
1515
|
-
if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
1707
|
+
if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
1516
1708
|
const maxLength = meta.get("expect.maxLength");
|
|
1517
|
-
if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1709
|
+
if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1518
1710
|
return schema;
|
|
1519
1711
|
},
|
|
1520
1712
|
union(d) {
|
|
@@ -1557,18 +1749,18 @@ function cloneTypeDef(type) {
|
|
|
1557
1749
|
if (schema.type === "string") {
|
|
1558
1750
|
if (meta.get("meta.required")) schema.minLength = 1;
|
|
1559
1751
|
const minLength = meta.get("expect.minLength");
|
|
1560
|
-
if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1752
|
+
if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1561
1753
|
const maxLength = meta.get("expect.maxLength");
|
|
1562
|
-
if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1754
|
+
if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1563
1755
|
const patterns = meta.get("expect.pattern");
|
|
1564
1756
|
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
1565
1757
|
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
1566
1758
|
}
|
|
1567
1759
|
if (schema.type === "number" || schema.type === "integer") {
|
|
1568
1760
|
const min = meta.get("expect.min");
|
|
1569
|
-
if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1761
|
+
if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1570
1762
|
const max = meta.get("expect.max");
|
|
1571
|
-
if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1763
|
+
if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1572
1764
|
}
|
|
1573
1765
|
return schema;
|
|
1574
1766
|
}
|
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-
|
|
2
|
+
const require_plugin = require('./plugin-DORafTJq.cjs');
|
|
3
3
|
|
|
4
4
|
exports.default = require_plugin.tsPlugin;
|
|
5
5
|
exports.tsPlugin = require_plugin.tsPlugin;
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,189 @@
|
|
|
1
1
|
|
|
2
|
+
//#region packages/typescript/src/runtime/validator-fast-check.ts
|
|
3
|
+
/**
|
|
4
|
+
* Allocation-free boolean pre-check used by {@link Validator.validate} before the
|
|
5
|
+
* error-collecting walk.
|
|
6
|
+
*
|
|
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.
|
|
10
|
+
*
|
|
11
|
+
* 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.
|
|
16
|
+
* - `BAIL` — not decidable here; the caller must run the full walk.
|
|
17
|
+
*
|
|
18
|
+
* Union branches are tried in declared order, continuing past `FAIL` and stopping
|
|
19
|
+
* at `BAIL`, which keeps union results identical to the walk.
|
|
20
|
+
*/ const FAIL = 0;
|
|
21
|
+
const PASS = 1;
|
|
22
|
+
const BAIL = 2;
|
|
23
|
+
/** Node is at walk depth 0 (only object props / array & tuple items increase depth). */ const TOP = 1;
|
|
24
|
+
/** Node is inside a union branch — failures must be exhaustive (see contract). */ const IN_UNION = 2;
|
|
25
|
+
/**
|
|
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".
|
|
28
|
+
*/ function fastCheckPasses(def, value, opts) {
|
|
29
|
+
try {
|
|
30
|
+
return fastCheck(def, value, TOP, opts) === PASS;
|
|
31
|
+
} catch {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const DECIMAL_RE = /^[+-]?\d+(\.\d+)?$/;
|
|
36
|
+
const regexCache = /* @__PURE__ */ new Map();
|
|
37
|
+
/** Compiled `@expect.pattern` regex, cached per (pattern, flags). */ function getRegex(pattern, flags) {
|
|
38
|
+
let byFlags = regexCache.get(pattern);
|
|
39
|
+
if (!byFlags) {
|
|
40
|
+
byFlags = /* @__PURE__ */ new Map();
|
|
41
|
+
regexCache.set(pattern, byFlags);
|
|
42
|
+
}
|
|
43
|
+
const flagsKey = flags || "";
|
|
44
|
+
let regex = byFlags.get(flagsKey);
|
|
45
|
+
if (!regex) {
|
|
46
|
+
regex = new RegExp(pattern, flags);
|
|
47
|
+
byFlags.set(flagsKey, regex);
|
|
48
|
+
}
|
|
49
|
+
return regex;
|
|
50
|
+
}
|
|
51
|
+
function fastCheck(def, value, flags, opts) {
|
|
52
|
+
if (def.optional && (value === void 0 || value === null)) return PASS;
|
|
53
|
+
const type = def.type;
|
|
54
|
+
switch (type.kind) {
|
|
55
|
+
case "": return fastCheckFinal(def, value);
|
|
56
|
+
case "object": return fastCheckObject(def, value, flags, opts);
|
|
57
|
+
case "array": return fastCheckArray(def, value, flags, opts);
|
|
58
|
+
case "union": {
|
|
59
|
+
const branchFlags = flags | IN_UNION;
|
|
60
|
+
for (const item of type.items) {
|
|
61
|
+
const result = fastCheck(item, value, branchFlags, opts);
|
|
62
|
+
if (result !== FAIL) return result;
|
|
63
|
+
}
|
|
64
|
+
return FAIL;
|
|
65
|
+
}
|
|
66
|
+
case "intersection":
|
|
67
|
+
for (const item of type.items) {
|
|
68
|
+
const result = fastCheck(item, value, flags, opts);
|
|
69
|
+
if (result !== PASS) return result;
|
|
70
|
+
}
|
|
71
|
+
return PASS;
|
|
72
|
+
case "tuple": {
|
|
73
|
+
const items = type.items;
|
|
74
|
+
if (!Array.isArray(value) || value.length !== items.length) return FAIL;
|
|
75
|
+
const itemFlags = flags & IN_UNION;
|
|
76
|
+
for (let i = 0; i < items.length; i++) {
|
|
77
|
+
const result = fastCheck(items[i], value[i], itemFlags, opts);
|
|
78
|
+
if (result !== PASS) return result;
|
|
79
|
+
}
|
|
80
|
+
return PASS;
|
|
81
|
+
}
|
|
82
|
+
default: return BAIL;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function fastCheckFinal(def, value) {
|
|
86
|
+
const type = def.type;
|
|
87
|
+
const designType = type.designType;
|
|
88
|
+
if (designType === "phantom") return PASS;
|
|
89
|
+
if (type.value !== void 0) return value === type.value ? PASS : FAIL;
|
|
90
|
+
switch (designType) {
|
|
91
|
+
case "string": return typeof value === "string" ? fastCheckString(def, value) : FAIL;
|
|
92
|
+
case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def, value) : FAIL;
|
|
93
|
+
case "boolean":
|
|
94
|
+
if (typeof value !== "boolean") return FAIL;
|
|
95
|
+
if (def.metadata.size === 0) return PASS;
|
|
96
|
+
return def.metadata.get("meta.required") && value !== true ? FAIL : PASS;
|
|
97
|
+
case "any": return PASS;
|
|
98
|
+
case "never": return FAIL;
|
|
99
|
+
case "undefined": return value === void 0 ? PASS : FAIL;
|
|
100
|
+
case "null": return value === null ? PASS : FAIL;
|
|
101
|
+
case "decimal": return typeof value === "string" && DECIMAL_RE.test(value) ? PASS : FAIL;
|
|
102
|
+
default: return BAIL;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function fastCheckString(def, value) {
|
|
106
|
+
const metadata = def.metadata;
|
|
107
|
+
if (metadata.size === 0) return PASS;
|
|
108
|
+
if (metadata.get("meta.required") && value.trim().length === 0) return FAIL;
|
|
109
|
+
const minLength = metadata.get("expect.minLength");
|
|
110
|
+
if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
|
|
111
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
112
|
+
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
113
|
+
const patterns = metadata.get("expect.pattern");
|
|
114
|
+
if (patterns) for (const { pattern, flags } of patterns) {
|
|
115
|
+
if (!pattern) continue;
|
|
116
|
+
const regex = getRegex(pattern, flags);
|
|
117
|
+
if (regex.global || regex.sticky) return BAIL;
|
|
118
|
+
if (!regex.test(value)) return FAIL;
|
|
119
|
+
}
|
|
120
|
+
return PASS;
|
|
121
|
+
}
|
|
122
|
+
function fastCheckNumber(def, value) {
|
|
123
|
+
const metadata = def.metadata;
|
|
124
|
+
if (metadata.size === 0) return PASS;
|
|
125
|
+
if (metadata.get("expect.int") && value % 1 !== 0) return FAIL;
|
|
126
|
+
const min = metadata.get("expect.min");
|
|
127
|
+
if (min !== void 0 && value < (typeof min === "number" ? min : min.minValue)) return FAIL;
|
|
128
|
+
const max = metadata.get("expect.max");
|
|
129
|
+
if (max !== void 0 && value > (typeof max === "number" ? max : max.maxValue)) return FAIL;
|
|
130
|
+
return PASS;
|
|
131
|
+
}
|
|
132
|
+
function fastCheckObject(def, value, flags, opts) {
|
|
133
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
|
|
134
|
+
const type = def.type;
|
|
135
|
+
if (type.propsPatterns.length > 0) return BAIL;
|
|
136
|
+
const partial = opts.partial;
|
|
137
|
+
const skipUndefined = partial === "deep" || partial === true && (flags & TOP) !== 0;
|
|
138
|
+
const inUnion = (flags & IN_UNION) !== 0;
|
|
139
|
+
const propFlags = flags & IN_UNION;
|
|
140
|
+
let result = PASS;
|
|
141
|
+
for (const [key, item] of type.props) {
|
|
142
|
+
if (isPhantomType(item)) continue;
|
|
143
|
+
const propValue = value[key];
|
|
144
|
+
if (propValue === void 0 && skipUndefined) continue;
|
|
145
|
+
const propResult = fastCheck(item, propValue, propFlags, opts);
|
|
146
|
+
if (propResult !== PASS) {
|
|
147
|
+
if (propResult === BAIL || !inUnion) return propResult;
|
|
148
|
+
result = FAIL;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (result !== PASS) return result;
|
|
152
|
+
const unknownProps = opts.unknownProps;
|
|
153
|
+
if (unknownProps === "error" || unknownProps === "strip") {
|
|
154
|
+
const props = type.props;
|
|
155
|
+
for (const key of Object.keys(value)) {
|
|
156
|
+
const known = props.get(key);
|
|
157
|
+
if (known === void 0 || isPhantomType(known)) return unknownProps === "error" ? FAIL : BAIL;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return PASS;
|
|
161
|
+
}
|
|
162
|
+
function fastCheckArray(def, value, flags, opts) {
|
|
163
|
+
if (!Array.isArray(value)) return FAIL;
|
|
164
|
+
const metadata = def.metadata;
|
|
165
|
+
if (metadata.size > 0) {
|
|
166
|
+
const minLength = metadata.get("expect.minLength");
|
|
167
|
+
if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
|
|
168
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
169
|
+
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
170
|
+
if (metadata.get("expect.array.uniqueItems")) return BAIL;
|
|
171
|
+
}
|
|
172
|
+
const of = def.type.of;
|
|
173
|
+
const inUnion = (flags & IN_UNION) !== 0;
|
|
174
|
+
const itemFlags = flags & IN_UNION;
|
|
175
|
+
let result = PASS;
|
|
176
|
+
for (const item of value) {
|
|
177
|
+
const itemResult = fastCheck(of, item, itemFlags, opts);
|
|
178
|
+
if (itemResult !== PASS) {
|
|
179
|
+
if (itemResult === BAIL || !inUnion) return itemResult;
|
|
180
|
+
result = FAIL;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return result;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
//#endregion
|
|
2
187
|
//#region packages/typescript/src/runtime/validator.ts
|
|
3
188
|
function _define_property(obj, key, value) {
|
|
4
189
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
@@ -10,7 +195,6 @@ function _define_property(obj, key, value) {
|
|
|
10
195
|
else obj[key] = value;
|
|
11
196
|
return obj;
|
|
12
197
|
}
|
|
13
|
-
const regexCache = /* @__PURE__ */ new Map();
|
|
14
198
|
/**
|
|
15
199
|
* Validates values against an {@link TAtscriptAnnotatedType} definition.
|
|
16
200
|
*
|
|
@@ -31,7 +215,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
31
215
|
*
|
|
32
216
|
* @typeParam T - The annotated type definition.
|
|
33
217
|
* @typeParam DataType - The TypeScript type that `validate` narrows to (auto-inferred).
|
|
34
|
-
*/ var Validator = class {
|
|
218
|
+
*/ var Validator = class Validator {
|
|
35
219
|
buildPath() {
|
|
36
220
|
if (this.depth <= 0) return "";
|
|
37
221
|
let path = this.pathSegments[0];
|
|
@@ -85,6 +269,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
85
269
|
this.stackErrors.length = 0;
|
|
86
270
|
this.depth = 0;
|
|
87
271
|
this.limitExceeded = false;
|
|
272
|
+
if (this.shortcuts && !this.opts.skipList && typeof this.opts.partial !== "function" && fastCheckPasses(this.def, value, this.opts)) return true;
|
|
88
273
|
this.context = context;
|
|
89
274
|
const passed = this.validateSafe(this.def, value);
|
|
90
275
|
this.context = void 0;
|
|
@@ -94,6 +279,14 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
94
279
|
}
|
|
95
280
|
return true;
|
|
96
281
|
}
|
|
282
|
+
/**
|
|
283
|
+
* Plain `Validator` without plugins / `replace`: eligible for the allocation-free
|
|
284
|
+
* shortcuts. Subclasses may override the protected `validate*` hooks, so they
|
|
285
|
+
* always take the full walk. (A getter, not a constructor-set field: validators are
|
|
286
|
+
* often built per call, so construction stays as cheap as before.)
|
|
287
|
+
*/ get shortcuts() {
|
|
288
|
+
return this.constructor === Validator && !this.hasPlugins && !this.hasReplace;
|
|
289
|
+
}
|
|
97
290
|
validateSafe(def, value) {
|
|
98
291
|
if (this.limitExceeded) return false;
|
|
99
292
|
if (this.hasReplace) {
|
|
@@ -129,6 +322,11 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
129
322
|
}
|
|
130
323
|
validateUnion(def, value) {
|
|
131
324
|
const items = def.type.items;
|
|
325
|
+
if (this.shortcuts) for (const item of items) {
|
|
326
|
+
const type = item.type;
|
|
327
|
+
if (item.optional || type.kind !== "" || type.value === void 0 || type.designType === "phantom") break;
|
|
328
|
+
if (type.value === value) return true;
|
|
329
|
+
}
|
|
132
330
|
let details;
|
|
133
331
|
for (const item of items) {
|
|
134
332
|
this.stackErrors.push(null);
|
|
@@ -172,7 +370,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
172
370
|
return false;
|
|
173
371
|
}
|
|
174
372
|
const minLength = def.metadata.get("expect.minLength");
|
|
175
|
-
if (minLength) {
|
|
373
|
+
if (minLength !== void 0) {
|
|
176
374
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
177
375
|
if (value.length < length) {
|
|
178
376
|
const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
|
|
@@ -181,7 +379,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
181
379
|
}
|
|
182
380
|
}
|
|
183
381
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
184
|
-
if (maxLength) {
|
|
382
|
+
if (maxLength !== void 0) {
|
|
185
383
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
186
384
|
if (value.length > length) {
|
|
187
385
|
const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
|
|
@@ -353,7 +551,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
353
551
|
this.error(`Expected string (decimal), got ${typeOfValue}`);
|
|
354
552
|
return false;
|
|
355
553
|
}
|
|
356
|
-
if (
|
|
554
|
+
if (!DECIMAL_RE.test(value)) {
|
|
357
555
|
this.error(`Invalid decimal format: ${JSON.stringify(value)}`);
|
|
358
556
|
return false;
|
|
359
557
|
}
|
|
@@ -372,7 +570,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
372
570
|
}
|
|
373
571
|
}
|
|
374
572
|
const minLength = def.metadata.get("expect.minLength");
|
|
375
|
-
if (minLength) {
|
|
573
|
+
if (minLength !== void 0) {
|
|
376
574
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
377
575
|
if (value.length < length) {
|
|
378
576
|
const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
|
|
@@ -381,7 +579,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
381
579
|
}
|
|
382
580
|
}
|
|
383
581
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
384
|
-
if (maxLength) {
|
|
582
|
+
if (maxLength !== void 0) {
|
|
385
583
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
386
584
|
if (value.length > length) {
|
|
387
585
|
const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
|
|
@@ -392,13 +590,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
392
590
|
const patterns = def.metadata.get("expect.pattern");
|
|
393
591
|
for (const { pattern, flags, message } of patterns || []) {
|
|
394
592
|
if (!pattern) continue;
|
|
395
|
-
|
|
396
|
-
let regex = regexCache.get(cacheKey);
|
|
397
|
-
if (!regex) {
|
|
398
|
-
regex = new RegExp(pattern, flags);
|
|
399
|
-
regexCache.set(cacheKey, regex);
|
|
400
|
-
}
|
|
401
|
-
if (!regex.test(value)) {
|
|
593
|
+
if (!getRegex(pattern, flags).test(value)) {
|
|
402
594
|
this.error(message || `Value is expected to match pattern "${pattern}"`);
|
|
403
595
|
return false;
|
|
404
596
|
}
|
|
@@ -414,7 +606,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
414
606
|
return false;
|
|
415
607
|
}
|
|
416
608
|
const min = def.metadata.get("expect.min");
|
|
417
|
-
if (min) {
|
|
609
|
+
if (min !== void 0) {
|
|
418
610
|
const minValue = typeof min === "number" ? min : min.minValue;
|
|
419
611
|
if (value < minValue) {
|
|
420
612
|
const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
|
|
@@ -423,7 +615,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
423
615
|
}
|
|
424
616
|
}
|
|
425
617
|
const max = def.metadata.get("expect.max");
|
|
426
|
-
if (max) {
|
|
618
|
+
if (max !== void 0) {
|
|
427
619
|
const maxValue = typeof max === "number" ? max : max.maxValue;
|
|
428
620
|
if (value > maxValue) {
|
|
429
621
|
const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
|
|
@@ -874,9 +1066,9 @@ function cloneTypeDef(type) {
|
|
|
874
1066
|
items: build(d.type.of)
|
|
875
1067
|
};
|
|
876
1068
|
const minLength = meta.get("expect.minLength");
|
|
877
|
-
if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
1069
|
+
if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
878
1070
|
const maxLength = meta.get("expect.maxLength");
|
|
879
|
-
if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1071
|
+
if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
880
1072
|
return schema;
|
|
881
1073
|
},
|
|
882
1074
|
union(d) {
|
|
@@ -919,18 +1111,18 @@ function cloneTypeDef(type) {
|
|
|
919
1111
|
if (schema.type === "string") {
|
|
920
1112
|
if (meta.get("meta.required")) schema.minLength = 1;
|
|
921
1113
|
const minLength = meta.get("expect.minLength");
|
|
922
|
-
if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1114
|
+
if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
923
1115
|
const maxLength = meta.get("expect.maxLength");
|
|
924
|
-
if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1116
|
+
if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
925
1117
|
const patterns = meta.get("expect.pattern");
|
|
926
1118
|
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
927
1119
|
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
928
1120
|
}
|
|
929
1121
|
if (schema.type === "number" || schema.type === "integer") {
|
|
930
1122
|
const min = meta.get("expect.min");
|
|
931
|
-
if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1123
|
+
if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
932
1124
|
const max = meta.get("expect.max");
|
|
933
|
-
if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1125
|
+
if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
934
1126
|
}
|
|
935
1127
|
return schema;
|
|
936
1128
|
}
|
|
@@ -1,3 +1,188 @@
|
|
|
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
|
+
function fastCheck(def, value, flags, opts) {
|
|
51
|
+
if (def.optional && (value === void 0 || value === null)) return PASS;
|
|
52
|
+
const type = def.type;
|
|
53
|
+
switch (type.kind) {
|
|
54
|
+
case "": return fastCheckFinal(def, value);
|
|
55
|
+
case "object": return fastCheckObject(def, value, flags, opts);
|
|
56
|
+
case "array": return fastCheckArray(def, value, flags, opts);
|
|
57
|
+
case "union": {
|
|
58
|
+
const branchFlags = flags | IN_UNION;
|
|
59
|
+
for (const item of type.items) {
|
|
60
|
+
const result = fastCheck(item, value, branchFlags, opts);
|
|
61
|
+
if (result !== FAIL) return result;
|
|
62
|
+
}
|
|
63
|
+
return FAIL;
|
|
64
|
+
}
|
|
65
|
+
case "intersection":
|
|
66
|
+
for (const item of type.items) {
|
|
67
|
+
const result = fastCheck(item, value, flags, opts);
|
|
68
|
+
if (result !== PASS) return result;
|
|
69
|
+
}
|
|
70
|
+
return PASS;
|
|
71
|
+
case "tuple": {
|
|
72
|
+
const items = type.items;
|
|
73
|
+
if (!Array.isArray(value) || value.length !== items.length) return FAIL;
|
|
74
|
+
const itemFlags = flags & IN_UNION;
|
|
75
|
+
for (let i = 0; i < items.length; i++) {
|
|
76
|
+
const result = fastCheck(items[i], value[i], itemFlags, opts);
|
|
77
|
+
if (result !== PASS) return result;
|
|
78
|
+
}
|
|
79
|
+
return PASS;
|
|
80
|
+
}
|
|
81
|
+
default: return BAIL;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function fastCheckFinal(def, value) {
|
|
85
|
+
const type = def.type;
|
|
86
|
+
const designType = type.designType;
|
|
87
|
+
if (designType === "phantom") return PASS;
|
|
88
|
+
if (type.value !== void 0) return value === type.value ? PASS : FAIL;
|
|
89
|
+
switch (designType) {
|
|
90
|
+
case "string": return typeof value === "string" ? fastCheckString(def, value) : FAIL;
|
|
91
|
+
case "number": return typeof value === "number" && Number.isFinite(value) ? fastCheckNumber(def, value) : FAIL;
|
|
92
|
+
case "boolean":
|
|
93
|
+
if (typeof value !== "boolean") return FAIL;
|
|
94
|
+
if (def.metadata.size === 0) return PASS;
|
|
95
|
+
return def.metadata.get("meta.required") && value !== true ? FAIL : PASS;
|
|
96
|
+
case "any": return PASS;
|
|
97
|
+
case "never": return FAIL;
|
|
98
|
+
case "undefined": return value === void 0 ? PASS : FAIL;
|
|
99
|
+
case "null": return value === null ? PASS : FAIL;
|
|
100
|
+
case "decimal": return typeof value === "string" && DECIMAL_RE.test(value) ? PASS : FAIL;
|
|
101
|
+
default: return BAIL;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function fastCheckString(def, value) {
|
|
105
|
+
const metadata = def.metadata;
|
|
106
|
+
if (metadata.size === 0) return PASS;
|
|
107
|
+
if (metadata.get("meta.required") && value.trim().length === 0) return FAIL;
|
|
108
|
+
const minLength = metadata.get("expect.minLength");
|
|
109
|
+
if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
|
|
110
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
111
|
+
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
112
|
+
const patterns = metadata.get("expect.pattern");
|
|
113
|
+
if (patterns) for (const { pattern, flags } of patterns) {
|
|
114
|
+
if (!pattern) continue;
|
|
115
|
+
const regex = getRegex(pattern, flags);
|
|
116
|
+
if (regex.global || regex.sticky) return BAIL;
|
|
117
|
+
if (!regex.test(value)) return FAIL;
|
|
118
|
+
}
|
|
119
|
+
return PASS;
|
|
120
|
+
}
|
|
121
|
+
function fastCheckNumber(def, value) {
|
|
122
|
+
const metadata = def.metadata;
|
|
123
|
+
if (metadata.size === 0) return PASS;
|
|
124
|
+
if (metadata.get("expect.int") && value % 1 !== 0) return FAIL;
|
|
125
|
+
const min = metadata.get("expect.min");
|
|
126
|
+
if (min !== void 0 && value < (typeof min === "number" ? min : min.minValue)) return FAIL;
|
|
127
|
+
const max = metadata.get("expect.max");
|
|
128
|
+
if (max !== void 0 && value > (typeof max === "number" ? max : max.maxValue)) return FAIL;
|
|
129
|
+
return PASS;
|
|
130
|
+
}
|
|
131
|
+
function fastCheckObject(def, value, flags, opts) {
|
|
132
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
|
|
133
|
+
const type = def.type;
|
|
134
|
+
if (type.propsPatterns.length > 0) return BAIL;
|
|
135
|
+
const partial = opts.partial;
|
|
136
|
+
const skipUndefined = partial === "deep" || partial === true && (flags & TOP) !== 0;
|
|
137
|
+
const inUnion = (flags & IN_UNION) !== 0;
|
|
138
|
+
const propFlags = flags & IN_UNION;
|
|
139
|
+
let result = PASS;
|
|
140
|
+
for (const [key, item] of type.props) {
|
|
141
|
+
if (isPhantomType(item)) continue;
|
|
142
|
+
const propValue = value[key];
|
|
143
|
+
if (propValue === void 0 && skipUndefined) continue;
|
|
144
|
+
const propResult = fastCheck(item, propValue, propFlags, opts);
|
|
145
|
+
if (propResult !== PASS) {
|
|
146
|
+
if (propResult === BAIL || !inUnion) return propResult;
|
|
147
|
+
result = FAIL;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (result !== PASS) return result;
|
|
151
|
+
const unknownProps = opts.unknownProps;
|
|
152
|
+
if (unknownProps === "error" || unknownProps === "strip") {
|
|
153
|
+
const props = type.props;
|
|
154
|
+
for (const key of Object.keys(value)) {
|
|
155
|
+
const known = props.get(key);
|
|
156
|
+
if (known === void 0 || isPhantomType(known)) return unknownProps === "error" ? FAIL : BAIL;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return PASS;
|
|
160
|
+
}
|
|
161
|
+
function fastCheckArray(def, value, flags, opts) {
|
|
162
|
+
if (!Array.isArray(value)) return FAIL;
|
|
163
|
+
const metadata = def.metadata;
|
|
164
|
+
if (metadata.size > 0) {
|
|
165
|
+
const minLength = metadata.get("expect.minLength");
|
|
166
|
+
if (minLength !== void 0 && value.length < (typeof minLength === "number" ? minLength : minLength.length)) return FAIL;
|
|
167
|
+
const maxLength = metadata.get("expect.maxLength");
|
|
168
|
+
if (maxLength !== void 0 && value.length > (typeof maxLength === "number" ? maxLength : maxLength.length)) return FAIL;
|
|
169
|
+
if (metadata.get("expect.array.uniqueItems")) return BAIL;
|
|
170
|
+
}
|
|
171
|
+
const of = def.type.of;
|
|
172
|
+
const inUnion = (flags & IN_UNION) !== 0;
|
|
173
|
+
const itemFlags = flags & IN_UNION;
|
|
174
|
+
let result = PASS;
|
|
175
|
+
for (const item of value) {
|
|
176
|
+
const itemResult = fastCheck(of, item, itemFlags, opts);
|
|
177
|
+
if (itemResult !== PASS) {
|
|
178
|
+
if (itemResult === BAIL || !inUnion) return itemResult;
|
|
179
|
+
result = FAIL;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return result;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
//#endregion
|
|
1
186
|
//#region packages/typescript/src/runtime/validator.ts
|
|
2
187
|
function _define_property(obj, key, value) {
|
|
3
188
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
@@ -9,7 +194,6 @@ function _define_property(obj, key, value) {
|
|
|
9
194
|
else obj[key] = value;
|
|
10
195
|
return obj;
|
|
11
196
|
}
|
|
12
|
-
const regexCache = /* @__PURE__ */ new Map();
|
|
13
197
|
/**
|
|
14
198
|
* Validates values against an {@link TAtscriptAnnotatedType} definition.
|
|
15
199
|
*
|
|
@@ -30,7 +214,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
30
214
|
*
|
|
31
215
|
* @typeParam T - The annotated type definition.
|
|
32
216
|
* @typeParam DataType - The TypeScript type that `validate` narrows to (auto-inferred).
|
|
33
|
-
*/ var Validator = class {
|
|
217
|
+
*/ var Validator = class Validator {
|
|
34
218
|
buildPath() {
|
|
35
219
|
if (this.depth <= 0) return "";
|
|
36
220
|
let path = this.pathSegments[0];
|
|
@@ -84,6 +268,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
84
268
|
this.stackErrors.length = 0;
|
|
85
269
|
this.depth = 0;
|
|
86
270
|
this.limitExceeded = false;
|
|
271
|
+
if (this.shortcuts && !this.opts.skipList && typeof this.opts.partial !== "function" && fastCheckPasses(this.def, value, this.opts)) return true;
|
|
87
272
|
this.context = context;
|
|
88
273
|
const passed = this.validateSafe(this.def, value);
|
|
89
274
|
this.context = void 0;
|
|
@@ -93,6 +278,14 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
93
278
|
}
|
|
94
279
|
return true;
|
|
95
280
|
}
|
|
281
|
+
/**
|
|
282
|
+
* Plain `Validator` without plugins / `replace`: eligible for the allocation-free
|
|
283
|
+
* shortcuts. Subclasses may override the protected `validate*` hooks, so they
|
|
284
|
+
* always take the full walk. (A getter, not a constructor-set field: validators are
|
|
285
|
+
* often built per call, so construction stays as cheap as before.)
|
|
286
|
+
*/ get shortcuts() {
|
|
287
|
+
return this.constructor === Validator && !this.hasPlugins && !this.hasReplace;
|
|
288
|
+
}
|
|
96
289
|
validateSafe(def, value) {
|
|
97
290
|
if (this.limitExceeded) return false;
|
|
98
291
|
if (this.hasReplace) {
|
|
@@ -128,6 +321,11 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
128
321
|
}
|
|
129
322
|
validateUnion(def, value) {
|
|
130
323
|
const items = def.type.items;
|
|
324
|
+
if (this.shortcuts) for (const item of items) {
|
|
325
|
+
const type = item.type;
|
|
326
|
+
if (item.optional || type.kind !== "" || type.value === void 0 || type.designType === "phantom") break;
|
|
327
|
+
if (type.value === value) return true;
|
|
328
|
+
}
|
|
131
329
|
let details;
|
|
132
330
|
for (const item of items) {
|
|
133
331
|
this.stackErrors.push(null);
|
|
@@ -171,7 +369,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
171
369
|
return false;
|
|
172
370
|
}
|
|
173
371
|
const minLength = def.metadata.get("expect.minLength");
|
|
174
|
-
if (minLength) {
|
|
372
|
+
if (minLength !== void 0) {
|
|
175
373
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
176
374
|
if (value.length < length) {
|
|
177
375
|
const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} items, got ${value.length} items`;
|
|
@@ -180,7 +378,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
180
378
|
}
|
|
181
379
|
}
|
|
182
380
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
183
|
-
if (maxLength) {
|
|
381
|
+
if (maxLength !== void 0) {
|
|
184
382
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
185
383
|
if (value.length > length) {
|
|
186
384
|
const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} items, got ${value.length} items`;
|
|
@@ -352,7 +550,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
352
550
|
this.error(`Expected string (decimal), got ${typeOfValue}`);
|
|
353
551
|
return false;
|
|
354
552
|
}
|
|
355
|
-
if (
|
|
553
|
+
if (!DECIMAL_RE.test(value)) {
|
|
356
554
|
this.error(`Invalid decimal format: ${JSON.stringify(value)}`);
|
|
357
555
|
return false;
|
|
358
556
|
}
|
|
@@ -371,7 +569,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
371
569
|
}
|
|
372
570
|
}
|
|
373
571
|
const minLength = def.metadata.get("expect.minLength");
|
|
374
|
-
if (minLength) {
|
|
572
|
+
if (minLength !== void 0) {
|
|
375
573
|
const length = typeof minLength === "number" ? minLength : minLength.length;
|
|
376
574
|
if (value.length < length) {
|
|
377
575
|
const message = typeof minLength === "object" && minLength.message ? minLength.message : `Expected minimum length of ${length} characters, got ${value.length} characters`;
|
|
@@ -380,7 +578,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
380
578
|
}
|
|
381
579
|
}
|
|
382
580
|
const maxLength = def.metadata.get("expect.maxLength");
|
|
383
|
-
if (maxLength) {
|
|
581
|
+
if (maxLength !== void 0) {
|
|
384
582
|
const length = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
385
583
|
if (value.length > length) {
|
|
386
584
|
const message = typeof maxLength === "object" && maxLength.message ? maxLength.message : `Expected maximum length of ${length} characters, got ${value.length} characters`;
|
|
@@ -391,13 +589,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
391
589
|
const patterns = def.metadata.get("expect.pattern");
|
|
392
590
|
for (const { pattern, flags, message } of patterns || []) {
|
|
393
591
|
if (!pattern) continue;
|
|
394
|
-
|
|
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)) {
|
|
592
|
+
if (!getRegex(pattern, flags).test(value)) {
|
|
401
593
|
this.error(message || `Value is expected to match pattern "${pattern}"`);
|
|
402
594
|
return false;
|
|
403
595
|
}
|
|
@@ -413,7 +605,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
413
605
|
return false;
|
|
414
606
|
}
|
|
415
607
|
const min = def.metadata.get("expect.min");
|
|
416
|
-
if (min) {
|
|
608
|
+
if (min !== void 0) {
|
|
417
609
|
const minValue = typeof min === "number" ? min : min.minValue;
|
|
418
610
|
if (value < minValue) {
|
|
419
611
|
const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
|
|
@@ -422,7 +614,7 @@ const regexCache = /* @__PURE__ */ new Map();
|
|
|
422
614
|
}
|
|
423
615
|
}
|
|
424
616
|
const max = def.metadata.get("expect.max");
|
|
425
|
-
if (max) {
|
|
617
|
+
if (max !== void 0) {
|
|
426
618
|
const maxValue = typeof max === "number" ? max : max.maxValue;
|
|
427
619
|
if (value > maxValue) {
|
|
428
620
|
const message = typeof max === "object" && max.message ? max.message : `Expected maximum ${maxValue}, got ${value}`;
|
|
@@ -873,9 +1065,9 @@ function cloneTypeDef(type) {
|
|
|
873
1065
|
items: build(d.type.of)
|
|
874
1066
|
};
|
|
875
1067
|
const minLength = meta.get("expect.minLength");
|
|
876
|
-
if (minLength) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
1068
|
+
if (minLength !== void 0) schema.minItems = typeof minLength === "number" ? minLength : minLength.length;
|
|
877
1069
|
const maxLength = meta.get("expect.maxLength");
|
|
878
|
-
if (maxLength) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1070
|
+
if (maxLength !== void 0) schema.maxItems = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
879
1071
|
return schema;
|
|
880
1072
|
},
|
|
881
1073
|
union(d) {
|
|
@@ -918,18 +1110,18 @@ function cloneTypeDef(type) {
|
|
|
918
1110
|
if (schema.type === "string") {
|
|
919
1111
|
if (meta.get("meta.required")) schema.minLength = 1;
|
|
920
1112
|
const minLength = meta.get("expect.minLength");
|
|
921
|
-
if (minLength) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
1113
|
+
if (minLength !== void 0) schema.minLength = typeof minLength === "number" ? minLength : minLength.length;
|
|
922
1114
|
const maxLength = meta.get("expect.maxLength");
|
|
923
|
-
if (maxLength) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
1115
|
+
if (maxLength !== void 0) schema.maxLength = typeof maxLength === "number" ? maxLength : maxLength.length;
|
|
924
1116
|
const patterns = meta.get("expect.pattern");
|
|
925
1117
|
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
926
1118
|
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
927
1119
|
}
|
|
928
1120
|
if (schema.type === "number" || schema.type === "integer") {
|
|
929
1121
|
const min = meta.get("expect.min");
|
|
930
|
-
if (min) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
1122
|
+
if (min !== void 0) schema.minimum = typeof min === "number" ? min : min.minValue;
|
|
931
1123
|
const max = meta.get("expect.max");
|
|
932
|
-
if (max) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
1124
|
+
if (max !== void 0) schema.maximum = typeof max === "number" ? max : max.maxValue;
|
|
933
1125
|
}
|
|
934
1126
|
return schema;
|
|
935
1127
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { l as defineAnnotatedType, t as buildJsonSchema } from "./json-schema-
|
|
1
|
+
import { l as defineAnnotatedType, t as buildJsonSchema } from "./json-schema-CW_50s1W.mjs";
|
|
2
2
|
import path from "path";
|
|
3
3
|
import { DEFAULT_FORMAT, flattenInterfaceNode, getRelPath, hasNavPropAnnotation, isArray, isBareId, isBareSpecifier, isConst, isDbEntityNode, isGroup, isInterface, isPrimitive, isProp, isQueryLogical, isRef, isStructure } from "@atscript/core";
|
|
4
4
|
|
|
@@ -25,7 +25,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
25
25
|
}) : target, mod));
|
|
26
26
|
|
|
27
27
|
//#endregion
|
|
28
|
-
const require_json_schema = require('./json-schema-
|
|
28
|
+
const require_json_schema = require('./json-schema-B9xrfeoQ.cjs');
|
|
29
29
|
let path = require("path");
|
|
30
30
|
path = __toESM(path);
|
|
31
31
|
let _atscript_core = require("@atscript/core");
|
package/dist/test-utils.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_plugin = require('./plugin-
|
|
2
|
+
const require_plugin = require('./plugin-DORafTJq.cjs');
|
|
3
3
|
let path = require("path");
|
|
4
4
|
path = require_plugin.__toESM(path);
|
|
5
5
|
let _atscript_core = require("@atscript/core");
|
package/dist/test-utils.mjs
CHANGED
package/dist/utils.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_json_schema = require('./json-schema-
|
|
2
|
+
const require_json_schema = require('./json-schema-B9xrfeoQ.cjs');
|
|
3
3
|
|
|
4
4
|
//#region packages/typescript/src/runtime/coerce.ts
|
|
5
5
|
/**
|
|
@@ -44,36 +44,40 @@ const require_json_schema = require('./json-schema-CtQ_rzJn.cjs');
|
|
|
44
44
|
}
|
|
45
45
|
/** Returns the coerced value, or NO_MATCH when the value can't fit this node. */ function coerce(def, value) {
|
|
46
46
|
if (value === void 0 || value === null) return def.optional === true ? value : NO_MATCH;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
const type = def.type;
|
|
48
|
+
switch (type.kind) {
|
|
49
|
+
case "": {
|
|
50
|
+
const final = type;
|
|
51
|
+
if (final.designType === "phantom") return value;
|
|
52
|
+
const result = scalar(final.designType, value);
|
|
53
|
+
if (result !== NO_MATCH && final.value !== void 0 && result !== final.value) return NO_MATCH;
|
|
51
54
|
return result;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
object: (d) => {
|
|
55
|
+
}
|
|
56
|
+
case "object":
|
|
55
57
|
if (!isPlainObject(value)) return NO_MATCH;
|
|
56
|
-
return coerceProps(
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
58
|
+
return coerceProps(def, value);
|
|
59
|
+
case "array": return Array.isArray(value) ? coerceItems(value, type.of) : NO_MATCH;
|
|
60
|
+
case "tuple": {
|
|
61
|
+
const items = type.items;
|
|
62
|
+
return Array.isArray(value) && value.length === items.length ? coerceItems(value, void 0, items) : NO_MATCH;
|
|
63
|
+
}
|
|
64
|
+
case "union":
|
|
65
|
+
for (const item of type.items) {
|
|
62
66
|
const result = coerce(item, value);
|
|
63
67
|
if (result !== NO_MATCH) return result;
|
|
64
68
|
}
|
|
65
69
|
return NO_MATCH;
|
|
66
|
-
|
|
67
|
-
intersection: (d) => {
|
|
70
|
+
case "intersection": {
|
|
68
71
|
let current = value;
|
|
69
|
-
for (const item of
|
|
72
|
+
for (const item of type.items) {
|
|
70
73
|
const result = coerce(item, current);
|
|
71
74
|
if (result === NO_MATCH) return NO_MATCH;
|
|
72
75
|
current = result;
|
|
73
76
|
}
|
|
74
77
|
return current;
|
|
75
78
|
}
|
|
76
|
-
|
|
79
|
+
default: throw new Error(`Unknown type kind "${type.kind}"`);
|
|
80
|
+
}
|
|
77
81
|
}
|
|
78
82
|
function scalar(designType, value) {
|
|
79
83
|
switch (designType) {
|
package/dist/utils.d.ts
CHANGED
|
@@ -80,6 +80,13 @@ declare class Validator<T extends TAtscriptAnnotatedType = TAtscriptAnnotatedTyp
|
|
|
80
80
|
* @throws {ValidatorError} When validation fails and `safe` is not `true`.
|
|
81
81
|
*/
|
|
82
82
|
validate<TT = DataType>(value: any, safe?: boolean, context?: unknown): value is TT;
|
|
83
|
+
/**
|
|
84
|
+
* Plain `Validator` without plugins / `replace`: eligible for the allocation-free
|
|
85
|
+
* shortcuts. Subclasses may override the protected `validate*` hooks, so they
|
|
86
|
+
* always take the full walk. (A getter, not a constructor-set field: validators are
|
|
87
|
+
* often built per call, so construction stays as cheap as before.)
|
|
88
|
+
*/
|
|
89
|
+
private get shortcuts();
|
|
83
90
|
protected validateSafe(def: TAtscriptAnnotatedType, value: any): boolean;
|
|
84
91
|
protected get path(): string;
|
|
85
92
|
protected validateAnnotatedType(def: TAtscriptAnnotatedType, value: any): boolean;
|
package/dist/utils.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as forAnnotatedType, c as createAnnotatedTypeNode, d as isAnnotatedTypeOfPrimitive, f as isPhantomType, i as mergeJsonSchemas, l as defineAnnotatedType, m as ValidatorError, n as detectDiscriminator, o as annotate, p as Validator, r as fromJsonSchema, s as cloneRefProp, t as buildJsonSchema, u as isAnnotatedType } from "./json-schema-
|
|
1
|
+
import { a as forAnnotatedType, c as createAnnotatedTypeNode, d as isAnnotatedTypeOfPrimitive, f as isPhantomType, i as mergeJsonSchemas, l as defineAnnotatedType, m as ValidatorError, n as detectDiscriminator, o as annotate, p as Validator, r as fromJsonSchema, s as cloneRefProp, t as buildJsonSchema, u as isAnnotatedType } from "./json-schema-CW_50s1W.mjs";
|
|
2
2
|
|
|
3
3
|
//#region packages/typescript/src/runtime/coerce.ts
|
|
4
4
|
/**
|
|
@@ -43,36 +43,40 @@ import { a as forAnnotatedType, c as createAnnotatedTypeNode, d as isAnnotatedTy
|
|
|
43
43
|
}
|
|
44
44
|
/** Returns the coerced value, or NO_MATCH when the value can't fit this node. */ function coerce(def, value) {
|
|
45
45
|
if (value === void 0 || value === null) return def.optional === true ? value : NO_MATCH;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
const type = def.type;
|
|
47
|
+
switch (type.kind) {
|
|
48
|
+
case "": {
|
|
49
|
+
const final = type;
|
|
50
|
+
if (final.designType === "phantom") return value;
|
|
51
|
+
const result = scalar(final.designType, value);
|
|
52
|
+
if (result !== NO_MATCH && final.value !== void 0 && result !== final.value) return NO_MATCH;
|
|
50
53
|
return result;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
object: (d) => {
|
|
54
|
+
}
|
|
55
|
+
case "object":
|
|
54
56
|
if (!isPlainObject(value)) return NO_MATCH;
|
|
55
|
-
return coerceProps(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
57
|
+
return coerceProps(def, value);
|
|
58
|
+
case "array": return Array.isArray(value) ? coerceItems(value, type.of) : NO_MATCH;
|
|
59
|
+
case "tuple": {
|
|
60
|
+
const items = type.items;
|
|
61
|
+
return Array.isArray(value) && value.length === items.length ? coerceItems(value, void 0, items) : NO_MATCH;
|
|
62
|
+
}
|
|
63
|
+
case "union":
|
|
64
|
+
for (const item of type.items) {
|
|
61
65
|
const result = coerce(item, value);
|
|
62
66
|
if (result !== NO_MATCH) return result;
|
|
63
67
|
}
|
|
64
68
|
return NO_MATCH;
|
|
65
|
-
|
|
66
|
-
intersection: (d) => {
|
|
69
|
+
case "intersection": {
|
|
67
70
|
let current = value;
|
|
68
|
-
for (const item of
|
|
71
|
+
for (const item of type.items) {
|
|
69
72
|
const result = coerce(item, current);
|
|
70
73
|
if (result === NO_MATCH) return NO_MATCH;
|
|
71
74
|
current = result;
|
|
72
75
|
}
|
|
73
76
|
return current;
|
|
74
77
|
}
|
|
75
|
-
|
|
78
|
+
default: throw new Error(`Unknown type kind "${type.kind}"`);
|
|
79
|
+
}
|
|
76
80
|
}
|
|
77
81
|
function scalar(designType, value) {
|
|
78
82
|
switch (designType) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/typescript",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.102",
|
|
4
4
|
"description": "Atscript: typescript-gen support.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"annotations",
|
|
@@ -73,7 +73,7 @@
|
|
|
73
73
|
},
|
|
74
74
|
"peerDependencies": {
|
|
75
75
|
"@atscript/db": "^0.1.97",
|
|
76
|
-
"@atscript/core": "^0.1.
|
|
76
|
+
"@atscript/core": "^0.1.102"
|
|
77
77
|
},
|
|
78
78
|
"peerDependenciesMeta": {
|
|
79
79
|
"@atscript/db": {
|