@atscript/typescript 0.1.106 → 0.1.108
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 +393 -27
- package/dist/index.cjs +2 -1
- package/dist/index.mjs +2 -1
- package/dist/{json-schema-BN0B-fMO.cjs → json-schema-BqpH_eYJ.cjs} +383 -17
- package/dist/{json-schema-DKgNpLpU.mjs → json-schema-BtZ_OIcu.mjs} +383 -17
- package/dist/{plugin-91-z7KE-.cjs → plugin-Cv2odCwI.cjs} +1 -1
- package/dist/{plugin-GvSLmYJb.mjs → plugin-DpfsG4O7.mjs} +1 -1
- package/dist/test-utils.cjs +2 -1
- package/dist/test-utils.mjs +2 -1
- package/dist/utils.cjs +1 -1
- package/dist/utils.mjs +1 -1
- package/package.json +7 -7
package/dist/cli.cjs
CHANGED
|
@@ -36,7 +36,7 @@ let node_readline = require("node:readline");
|
|
|
36
36
|
let node_url = require("node:url");
|
|
37
37
|
|
|
38
38
|
//#region packages/typescript/src/codegen/code-printer.ts
|
|
39
|
-
function _define_property$
|
|
39
|
+
function _define_property$8(obj, key, value) {
|
|
40
40
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
41
41
|
value,
|
|
42
42
|
enumerable: true,
|
|
@@ -129,17 +129,17 @@ var CodePrinter = class {
|
|
|
129
129
|
return " ".repeat(this.indentLevel * this.indentSize);
|
|
130
130
|
}
|
|
131
131
|
constructor() {
|
|
132
|
-
_define_property$
|
|
133
|
-
_define_property$
|
|
134
|
-
_define_property$
|
|
135
|
-
_define_property$
|
|
136
|
-
_define_property$
|
|
132
|
+
_define_property$8(this, "lines", []);
|
|
133
|
+
_define_property$8(this, "currentLine", "");
|
|
134
|
+
_define_property$8(this, "indentLevel", 0);
|
|
135
|
+
_define_property$8(this, "indentSize", 2);
|
|
136
|
+
_define_property$8(this, "blockStack", []);
|
|
137
137
|
}
|
|
138
138
|
};
|
|
139
139
|
|
|
140
140
|
//#endregion
|
|
141
141
|
//#region packages/typescript/src/codegen/base-renderer.ts
|
|
142
|
-
function _define_property$
|
|
142
|
+
function _define_property$7(obj, key, value) {
|
|
143
143
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
144
144
|
value,
|
|
145
145
|
enumerable: true,
|
|
@@ -202,7 +202,7 @@ var BaseRenderer = class extends CodePrinter {
|
|
|
202
202
|
}
|
|
203
203
|
}
|
|
204
204
|
constructor(doc) {
|
|
205
|
-
super(), _define_property$
|
|
205
|
+
super(), _define_property$7(this, "doc", void 0), _define_property$7(this, "_unused", void 0), this.doc = doc;
|
|
206
206
|
}
|
|
207
207
|
};
|
|
208
208
|
|
|
@@ -225,7 +225,7 @@ function escapeQuotes(str) {
|
|
|
225
225
|
|
|
226
226
|
//#endregion
|
|
227
227
|
//#region packages/typescript/src/codegen/type-renderer.ts
|
|
228
|
-
function _define_property$
|
|
228
|
+
function _define_property$6(obj, key, value) {
|
|
229
229
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
230
230
|
value,
|
|
231
231
|
enumerable: true,
|
|
@@ -649,7 +649,7 @@ var TypeRenderer = class TypeRenderer extends BaseRenderer {
|
|
|
649
649
|
this.writeln(` */`);
|
|
650
650
|
}
|
|
651
651
|
constructor(doc, opts) {
|
|
652
|
-
super(doc), _define_property$
|
|
652
|
+
super(doc), _define_property$6(this, "opts", void 0), this.opts = opts;
|
|
653
653
|
}
|
|
654
654
|
};
|
|
655
655
|
function renderPrimitiveTypeDef(def) {
|
|
@@ -668,13 +668,28 @@ function renderPrimitiveTypeDef(def) {
|
|
|
668
668
|
|
|
669
669
|
//#endregion
|
|
670
670
|
//#region packages/typescript/src/runtime/validator-fast-check.ts
|
|
671
|
+
function _define_property$5(obj, key, value) {
|
|
672
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
673
|
+
value,
|
|
674
|
+
enumerable: true,
|
|
675
|
+
configurable: true,
|
|
676
|
+
writable: true
|
|
677
|
+
});
|
|
678
|
+
else obj[key] = value;
|
|
679
|
+
return obj;
|
|
680
|
+
}
|
|
671
681
|
/**
|
|
672
682
|
* Allocation-free boolean pre-check used by {@link Validator.validate} before the
|
|
673
683
|
* error-collecting walk.
|
|
674
684
|
*
|
|
675
|
-
*
|
|
676
|
-
*
|
|
677
|
-
*
|
|
685
|
+
* Nodes are checked by the generic functions below, which read the type tree and metadata
|
|
686
|
+
* LIVE on every visit. Array items — the same type visited many times per call — go
|
|
687
|
+
* through a compiled {@link Plan} of the item type instead (cached per node, shared by every
|
|
688
|
+
* validator and option set). A plan snapshots what its node's check reads and re-verifies
|
|
689
|
+
* every snapshot against the live node lazily, once per call, right before first using it
|
|
690
|
+
* (rebuilding it when anything differs); constraint objects and pattern arrays are read
|
|
691
|
+
* live on every visit. Either way `annotate()` / `metadata.set()` / prop mutations made
|
|
692
|
+
* after a validator was created are always honoured — the same as the full walk.
|
|
678
693
|
*
|
|
679
694
|
* Result contract (relative to the full walk over the same node and value):
|
|
680
695
|
* - `PASS` — the walk returns `true`, collects no errors and has no side effects.
|
|
@@ -690,10 +705,12 @@ const PASS = 1;
|
|
|
690
705
|
const BAIL = 2;
|
|
691
706
|
/** Node is at walk depth 0 (only object props / array & tuple items increase depth). */ const TOP = 1;
|
|
692
707
|
/** Node is inside a union branch — failures must be exhaustive (see contract). */ const IN_UNION = 2;
|
|
708
|
+
/** Incremented per {@link fastCheckPasses} call; snapshots verified in older calls re-verify. */ let stamp = 0;
|
|
693
709
|
/**
|
|
694
710
|
* `true` only when the full walk over `value` is guaranteed to pass with no errors
|
|
695
711
|
* and no side effects. Any other outcome (including a throw) means "run the walk".
|
|
696
712
|
*/ function fastCheckPasses(def, value, opts) {
|
|
713
|
+
stamp++;
|
|
697
714
|
try {
|
|
698
715
|
return fastCheck(def, value, TOP, opts) === PASS;
|
|
699
716
|
} catch {
|
|
@@ -744,7 +761,7 @@ function fastCheck(def, value, flags, opts) {
|
|
|
744
761
|
const branchFlags = flags | IN_UNION;
|
|
745
762
|
for (const item of type.items) {
|
|
746
763
|
const result = fastCheck(item, value, branchFlags, opts);
|
|
747
|
-
if (result !== FAIL) return result
|
|
764
|
+
if (result !== FAIL) return unionMatchResult(result, item, def.metadata, value);
|
|
748
765
|
}
|
|
749
766
|
return FAIL;
|
|
750
767
|
}
|
|
@@ -784,6 +801,9 @@ function fastCheckFinal(def, value) {
|
|
|
784
801
|
default: return BAIL;
|
|
785
802
|
}
|
|
786
803
|
}
|
|
804
|
+
/** Result of a union whose branch `item` did not fail (`PASS` or `BAIL`). */ function unionMatchResult(result, item, metadata, value) {
|
|
805
|
+
return result === PASS && item.type.value === void 0 ? fastCheckUnionConstraints(metadata, value) : result;
|
|
806
|
+
}
|
|
787
807
|
/**
|
|
788
808
|
* Mirrors Validator.validateUnionConstraints: union-level constraints on the matched value
|
|
789
809
|
* (`null` / objects pass; the caller skips literal branches).
|
|
@@ -798,8 +818,7 @@ function fastCheckFinal(def, value) {
|
|
|
798
818
|
}
|
|
799
819
|
}
|
|
800
820
|
function fastCheckBoolean(metadata, value) {
|
|
801
|
-
|
|
802
|
-
return metadata.get("meta.required") && value !== true ? FAIL : PASS;
|
|
821
|
+
return metadata.size === 0 ? PASS : booleanConstraints(value, metadata.get("meta.required"));
|
|
803
822
|
}
|
|
804
823
|
function fastCheckString(metadata, value) {
|
|
805
824
|
if (metadata.size === 0) return PASS;
|
|
@@ -819,13 +838,24 @@ function fastCheckString(metadata, value) {
|
|
|
819
838
|
}
|
|
820
839
|
function fastCheckNumber(metadata, value) {
|
|
821
840
|
if (metadata.size === 0) return PASS;
|
|
822
|
-
|
|
823
|
-
|
|
841
|
+
return numberConstraints(value, metadata.get("expect.int"), metadata.get("expect.min"), metadata.get("expect.max"));
|
|
842
|
+
}
|
|
843
|
+
function booleanConstraints(value, required) {
|
|
844
|
+
return required && value !== true ? FAIL : PASS;
|
|
845
|
+
}
|
|
846
|
+
function numberConstraints(value, int, min, max) {
|
|
847
|
+
if (int && value % 1 !== 0) return FAIL;
|
|
824
848
|
if (min !== void 0 && value < (typeof min === "number" ? min : min.minValue)) return FAIL;
|
|
825
|
-
const max = metadata.get("expect.max");
|
|
826
849
|
if (max !== void 0 && value > (typeof max === "number" ? max : max.maxValue)) return FAIL;
|
|
827
850
|
return PASS;
|
|
828
851
|
}
|
|
852
|
+
/** `@expect.minLength` / `@expect.maxLength` (strings and arrays). */ function lengthFails(length, minLength, maxLength) {
|
|
853
|
+
return minLength !== void 0 && length < (typeof minLength === "number" ? minLength : minLength.length) || maxLength !== void 0 && length > (typeof maxLength === "number" ? maxLength : maxLength.length);
|
|
854
|
+
}
|
|
855
|
+
/** Array constraints; duplicate detection (`uniqueItems`) is left to the walk. */ function arrayConstraints(length, minLength, maxLength, uniqueItems) {
|
|
856
|
+
if (lengthFails(length, minLength, maxLength)) return FAIL;
|
|
857
|
+
return uniqueItems ? BAIL : PASS;
|
|
858
|
+
}
|
|
829
859
|
function fastCheckObject(def, value, flags, opts) {
|
|
830
860
|
if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
|
|
831
861
|
const type = def.type;
|
|
@@ -863,12 +893,14 @@ function fastCheckArray(def, value, flags, opts) {
|
|
|
863
893
|
const result = fastCheckArrayConstraints(metadata, value);
|
|
864
894
|
if (result !== PASS) return result;
|
|
865
895
|
}
|
|
866
|
-
|
|
896
|
+
return checkItems(planFor(def.type.of), value, flags, opts);
|
|
897
|
+
}
|
|
898
|
+
/** Checks every array item against the item type's plan. */ function checkItems(of, value, flags, opts) {
|
|
867
899
|
const inUnion = (flags & IN_UNION) !== 0;
|
|
868
900
|
const itemFlags = flags & IN_UNION;
|
|
869
901
|
let result = PASS;
|
|
870
902
|
for (const item of value) {
|
|
871
|
-
const itemResult =
|
|
903
|
+
const itemResult = checkPlan(of, item, itemFlags, opts);
|
|
872
904
|
if (itemResult !== PASS) {
|
|
873
905
|
if (itemResult === BAIL || !inUnion) return itemResult;
|
|
874
906
|
result = FAIL;
|
|
@@ -877,11 +909,338 @@ function fastCheckArray(def, value, flags, opts) {
|
|
|
877
909
|
return result;
|
|
878
910
|
}
|
|
879
911
|
function fastCheckArrayConstraints(metadata, value) {
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
912
|
+
return arrayConstraints(value.length, metadata.get("expect.minLength"), metadata.get("expect.maxLength"), metadata.get("expect.array.uniqueItems"));
|
|
913
|
+
}
|
|
914
|
+
const OP_GENERIC = 0;
|
|
915
|
+
const OP_PHANTOM = 1;
|
|
916
|
+
const OP_STRING = 2;
|
|
917
|
+
const OP_NUMBER = 3;
|
|
918
|
+
const OP_BOOLEAN = 4;
|
|
919
|
+
/** Any other final node: checked by {@link fastCheckFinal}. */ const OP_FINAL = 5;
|
|
920
|
+
const OP_OBJECT = 6;
|
|
921
|
+
const OP_ARRAY = 7;
|
|
922
|
+
const OP_UNION = 8;
|
|
923
|
+
function isFinalOp(op) {
|
|
924
|
+
return op !== OP_GENERIC && op <= OP_FINAL;
|
|
925
|
+
}
|
|
926
|
+
/** Shared placeholder for the per-kind arrays of plans that don't use them. */ const EMPTY = Object.freeze([]);
|
|
927
|
+
const plans = /* @__PURE__ */ new WeakMap();
|
|
928
|
+
/** One shape for every plan, so plan field access stays monomorphic. */ let Plan = class Plan {
|
|
929
|
+
constructor(def) {
|
|
930
|
+
_define_property$5(this, "def", void 0);
|
|
931
|
+
/** Call in which the node structure was last verified (`-1`: must rebuild). */ _define_property$5(this, "stamp", void 0);
|
|
932
|
+
/** Call in which the annotation values (`m0`–`m3`) were last verified. */ _define_property$5(this, "metaStamp", void 0);
|
|
933
|
+
_define_property$5(this, "op", void 0);
|
|
934
|
+
_define_property$5(this, "type", void 0);
|
|
935
|
+
_define_property$5(this, "optional", void 0);
|
|
936
|
+
_define_property$5(this, "metadata", void 0);
|
|
937
|
+
_define_property$5(this, "kind", void 0);
|
|
938
|
+
_define_property$5(this, "designType", void 0);
|
|
939
|
+
_define_property$5(this, "literal", void 0);
|
|
940
|
+
/** String plans: compiled `@expect.pattern` regexes by position (checked against the live entry). */ _define_property$5(this, "regexes", void 0);
|
|
941
|
+
/**
|
|
942
|
+
* Annotation keys the node's check reads, and their values (`undefined` when absent):
|
|
943
|
+
* string `meta.required, expect.minLength, maxLength, pattern`; number `expect.int, min,
|
|
944
|
+
* max`; boolean `meta.required`; array `expect.minLength, maxLength, array.uniqueItems`.
|
|
945
|
+
*/ _define_property$5(this, "k0", void 0);
|
|
946
|
+
_define_property$5(this, "k1", void 0);
|
|
947
|
+
_define_property$5(this, "k2", void 0);
|
|
948
|
+
_define_property$5(this, "k3", void 0);
|
|
949
|
+
_define_property$5(this, "m0", void 0);
|
|
950
|
+
_define_property$5(this, "m1", void 0);
|
|
951
|
+
_define_property$5(this, "m2", void 0);
|
|
952
|
+
_define_property$5(this, "m3", void 0);
|
|
953
|
+
_define_property$5(this, "props", void 0);
|
|
954
|
+
_define_property$5(this, "propsPatterns", void 0);
|
|
955
|
+
_define_property$5(this, "keys", void 0);
|
|
956
|
+
/** Call in which `props.get(keys[i]) === nodes[i]` was last verified. */ _define_property$5(this, "edges", void 0);
|
|
957
|
+
/** Prop plans by key (the entries of `props`, verified per visit via `edges`). */ _define_property$5(this, "byKey", void 0);
|
|
958
|
+
_define_property$5(this, "nodes", void 0);
|
|
959
|
+
_define_property$5(this, "children", void 0);
|
|
960
|
+
_define_property$5(this, "of", void 0);
|
|
961
|
+
_define_property$5(this, "ofPlan", void 0);
|
|
962
|
+
_define_property$5(this, "items", void 0);
|
|
963
|
+
this.def = def;
|
|
964
|
+
this.stamp = -1;
|
|
965
|
+
this.metaStamp = -1;
|
|
966
|
+
this.op = OP_GENERIC;
|
|
967
|
+
this.type = void 0;
|
|
968
|
+
this.optional = void 0;
|
|
969
|
+
this.metadata = void 0;
|
|
970
|
+
this.kind = void 0;
|
|
971
|
+
this.designType = void 0;
|
|
972
|
+
this.literal = void 0;
|
|
973
|
+
this.regexes = EMPTY;
|
|
974
|
+
this.k0 = void 0;
|
|
975
|
+
this.k1 = void 0;
|
|
976
|
+
this.k2 = void 0;
|
|
977
|
+
this.k3 = void 0;
|
|
978
|
+
this.m0 = void 0;
|
|
979
|
+
this.m1 = void 0;
|
|
980
|
+
this.m2 = void 0;
|
|
981
|
+
this.m3 = void 0;
|
|
982
|
+
this.props = void 0;
|
|
983
|
+
this.propsPatterns = void 0;
|
|
984
|
+
this.keys = EMPTY;
|
|
985
|
+
this.edges = EMPTY;
|
|
986
|
+
this.byKey = void 0;
|
|
987
|
+
this.nodes = EMPTY;
|
|
988
|
+
this.children = EMPTY;
|
|
989
|
+
this.of = void 0;
|
|
990
|
+
this.ofPlan = void 0;
|
|
991
|
+
this.items = void 0;
|
|
992
|
+
}
|
|
993
|
+
};
|
|
994
|
+
function planFor(def) {
|
|
995
|
+
let plan = plans.get(def);
|
|
996
|
+
if (plan === void 0) {
|
|
997
|
+
plan = new Plan(def);
|
|
998
|
+
plans.set(def, plan);
|
|
999
|
+
}
|
|
1000
|
+
return plan;
|
|
1001
|
+
}
|
|
1002
|
+
/** Brings the plan's structure up to date with its live node (once per call). */ function refresh(plan) {
|
|
1003
|
+
if (!(isFinalOp(plan.op) ? verifyFinal(plan) : verify(plan))) build$2(plan);
|
|
1004
|
+
plan.stamp = stamp;
|
|
1005
|
+
}
|
|
1006
|
+
/** Structure check for final ops; small enough to be inlined into the hot loops. */ function verifyFinal(plan) {
|
|
1007
|
+
const def = plan.def;
|
|
1008
|
+
const type = def.type;
|
|
1009
|
+
return plan.stamp !== -1 && type === plan.type && def.optional === plan.optional && def.metadata === plan.metadata && type.kind === "" && type.designType === plan.designType && type.value === plan.literal;
|
|
1010
|
+
}
|
|
1011
|
+
/** Structure check for objects, arrays, unions and generic nodes. */ function verify(plan) {
|
|
1012
|
+
if (plan.stamp === -1) return false;
|
|
1013
|
+
const def = plan.def;
|
|
1014
|
+
const type = def.type;
|
|
1015
|
+
if (type !== plan.type || def.optional !== plan.optional || def.metadata !== plan.metadata || type.kind !== plan.kind) return false;
|
|
1016
|
+
switch (plan.op) {
|
|
1017
|
+
case OP_OBJECT: return type.props === plan.props && plan.props.size === plan.keys.length && type.propsPatterns === plan.propsPatterns && plan.propsPatterns.length === 0;
|
|
1018
|
+
case OP_ARRAY: return type.of === plan.of;
|
|
1019
|
+
case OP_UNION: {
|
|
1020
|
+
const items = plan.items;
|
|
1021
|
+
const nodes = plan.nodes;
|
|
1022
|
+
if (type.items !== items || items.length !== nodes.length) return false;
|
|
1023
|
+
for (let i = 0; i < nodes.length; i++) if (items[i] !== nodes[i]) return false;
|
|
1024
|
+
return true;
|
|
1025
|
+
}
|
|
1026
|
+
default: return type.kind !== "object" || type.propsPatterns.length > 0;
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
/** Brings the annotation values up to date (once per call, when a constraint is read). */ function refreshMeta(plan) {
|
|
1030
|
+
const metadata = plan.metadata;
|
|
1031
|
+
if (!(metadata.size === 0 ? plan.m0 === void 0 && plan.m1 === void 0 && plan.m2 === void 0 && plan.m3 === void 0 : metadata.get(plan.k0) === plan.m0 && (plan.k1 === void 0 || metadata.get(plan.k1) === plan.m1) && (plan.k2 === void 0 || metadata.get(plan.k2) === plan.m2) && (plan.k3 === void 0 || metadata.get(plan.k3) === plan.m3))) snapshotMeta(plan);
|
|
1032
|
+
plan.metaStamp = stamp;
|
|
1033
|
+
}
|
|
1034
|
+
function snapshotMeta(plan) {
|
|
1035
|
+
const metadata = plan.metadata;
|
|
1036
|
+
plan.m0 = metadata.get(plan.k0);
|
|
1037
|
+
plan.m1 = metadata.get(plan.k1);
|
|
1038
|
+
plan.m2 = metadata.get(plan.k2);
|
|
1039
|
+
plan.m3 = metadata.get(plan.k3);
|
|
1040
|
+
}
|
|
1041
|
+
function setMetaKeys(plan, k0, k1, k2, k3) {
|
|
1042
|
+
plan.k0 = k0;
|
|
1043
|
+
plan.k1 = k1;
|
|
1044
|
+
plan.k2 = k2;
|
|
1045
|
+
plan.k3 = k3;
|
|
1046
|
+
snapshotMeta(plan);
|
|
1047
|
+
plan.metaStamp = stamp;
|
|
1048
|
+
}
|
|
1049
|
+
/** (Re)compiles `plan` from its live node. */ function build$2(plan) {
|
|
1050
|
+
plan.stamp = -1;
|
|
1051
|
+
plan.op = OP_GENERIC;
|
|
1052
|
+
const def = plan.def;
|
|
1053
|
+
const type = def.type;
|
|
1054
|
+
plan.type = type;
|
|
1055
|
+
plan.optional = def.optional;
|
|
1056
|
+
plan.metadata = def.metadata;
|
|
1057
|
+
plan.kind = type.kind;
|
|
1058
|
+
plan.designType = type.designType;
|
|
1059
|
+
plan.literal = type.value;
|
|
1060
|
+
plan.regexes = EMPTY;
|
|
1061
|
+
plan.props = void 0;
|
|
1062
|
+
plan.propsPatterns = void 0;
|
|
1063
|
+
plan.keys = EMPTY;
|
|
1064
|
+
plan.edges = EMPTY;
|
|
1065
|
+
plan.byKey = void 0;
|
|
1066
|
+
plan.nodes = EMPTY;
|
|
1067
|
+
plan.children = EMPTY;
|
|
1068
|
+
plan.of = void 0;
|
|
1069
|
+
plan.ofPlan = void 0;
|
|
1070
|
+
plan.items = void 0;
|
|
1071
|
+
setMetaKeys(plan, void 0, void 0, void 0, void 0);
|
|
1072
|
+
switch (type.kind) {
|
|
1073
|
+
case "":
|
|
1074
|
+
if (type.designType === "phantom") plan.op = OP_PHANTOM;
|
|
1075
|
+
else if (type.value !== void 0) plan.op = OP_FINAL;
|
|
1076
|
+
else if (type.designType === "string") {
|
|
1077
|
+
setMetaKeys(plan, "meta.required", "expect.minLength", "expect.maxLength", "expect.pattern");
|
|
1078
|
+
plan.regexes = [];
|
|
1079
|
+
plan.op = OP_STRING;
|
|
1080
|
+
} else if (type.designType === "number") {
|
|
1081
|
+
setMetaKeys(plan, "expect.int", "expect.min", "expect.max", void 0);
|
|
1082
|
+
plan.op = OP_NUMBER;
|
|
1083
|
+
} else if (type.designType === "boolean") {
|
|
1084
|
+
setMetaKeys(plan, "meta.required", void 0, void 0, void 0);
|
|
1085
|
+
plan.op = OP_BOOLEAN;
|
|
1086
|
+
} else plan.op = OP_FINAL;
|
|
1087
|
+
break;
|
|
1088
|
+
case "object": {
|
|
1089
|
+
if (type.propsPatterns.length > 0) break;
|
|
1090
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
1091
|
+
plan.keys = [];
|
|
1092
|
+
plan.edges = [];
|
|
1093
|
+
plan.nodes = [];
|
|
1094
|
+
plan.children = [];
|
|
1095
|
+
for (const [key, node] of type.props) {
|
|
1096
|
+
const child = planFor(node);
|
|
1097
|
+
plan.keys.push(key);
|
|
1098
|
+
plan.edges.push(stamp);
|
|
1099
|
+
plan.nodes.push(node);
|
|
1100
|
+
plan.children.push(child);
|
|
1101
|
+
byKey.set(key, child);
|
|
1102
|
+
}
|
|
1103
|
+
plan.byKey = byKey;
|
|
1104
|
+
plan.props = type.props;
|
|
1105
|
+
plan.propsPatterns = type.propsPatterns;
|
|
1106
|
+
plan.op = OP_OBJECT;
|
|
1107
|
+
break;
|
|
1108
|
+
}
|
|
1109
|
+
case "array":
|
|
1110
|
+
setMetaKeys(plan, "expect.minLength", "expect.maxLength", "expect.array.uniqueItems", void 0);
|
|
1111
|
+
plan.of = type.of;
|
|
1112
|
+
plan.ofPlan = planFor(type.of);
|
|
1113
|
+
plan.op = OP_ARRAY;
|
|
1114
|
+
break;
|
|
1115
|
+
case "union":
|
|
1116
|
+
plan.nodes = [];
|
|
1117
|
+
plan.children = [];
|
|
1118
|
+
for (const node of type.items) {
|
|
1119
|
+
plan.nodes.push(node);
|
|
1120
|
+
plan.children.push(planFor(node));
|
|
1121
|
+
}
|
|
1122
|
+
plan.items = type.items;
|
|
1123
|
+
plan.op = OP_UNION;
|
|
1124
|
+
break;
|
|
1125
|
+
default:
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
/** Plan counterpart of {@link fastCheck}. */ function checkPlan(plan, value, flags, opts) {
|
|
1129
|
+
if (plan.stamp !== stamp) refresh(plan);
|
|
1130
|
+
if (plan.optional && (value === void 0 || value === null)) return value === null && rejectsNullAsRequired(plan.def) ? FAIL : PASS;
|
|
1131
|
+
switch (plan.op) {
|
|
1132
|
+
case OP_STRING: return typeof value === "string" ? checkString(plan, value) : FAIL;
|
|
1133
|
+
case OP_NUMBER: return typeof value === "number" && Number.isFinite(value) ? checkNumber(plan, value) : FAIL;
|
|
1134
|
+
case OP_OBJECT: return checkObject(plan, value, flags, opts);
|
|
1135
|
+
case OP_ARRAY: return checkArray(plan, value, flags, opts);
|
|
1136
|
+
case OP_UNION: {
|
|
1137
|
+
const branchFlags = flags | IN_UNION;
|
|
1138
|
+
for (const child of plan.children) {
|
|
1139
|
+
const result = checkPlan(child, value, branchFlags, opts);
|
|
1140
|
+
if (result !== FAIL) return unionMatchResult(result, child.def, plan.metadata, value);
|
|
1141
|
+
}
|
|
1142
|
+
return FAIL;
|
|
1143
|
+
}
|
|
1144
|
+
case OP_BOOLEAN:
|
|
1145
|
+
if (typeof value !== "boolean") return FAIL;
|
|
1146
|
+
if (plan.metaStamp !== stamp) refreshMeta(plan);
|
|
1147
|
+
return booleanConstraints(value, plan.m0);
|
|
1148
|
+
case OP_PHANTOM:
|
|
1149
|
+
case OP_FINAL: return fastCheckFinal(plan.def, value);
|
|
1150
|
+
default: return fastCheck(plan.def, value, flags, opts);
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
/** Plan counterpart of {@link fastCheckString}; constraint objects are read live. */ function checkString(plan, value) {
|
|
1154
|
+
if (plan.metaStamp !== stamp) refreshMeta(plan);
|
|
1155
|
+
if (plan.m0 && value.trim().length === 0) return FAIL;
|
|
1156
|
+
if (lengthFails(value.length, plan.m1, plan.m2)) return FAIL;
|
|
1157
|
+
const patterns = plan.m3;
|
|
1158
|
+
if (patterns) {
|
|
1159
|
+
if (!Array.isArray(patterns)) return fastCheckString(plan.metadata, value);
|
|
1160
|
+
for (let i = 0; i < patterns.length; i++) {
|
|
1161
|
+
const { pattern, flags } = patterns[i];
|
|
1162
|
+
if (!pattern) continue;
|
|
1163
|
+
let entry = plan.regexes[i];
|
|
1164
|
+
if (entry === void 0 || entry.pattern !== pattern || entry.flags !== flags) {
|
|
1165
|
+
const re = getRegex(pattern, flags);
|
|
1166
|
+
entry = {
|
|
1167
|
+
pattern,
|
|
1168
|
+
flags,
|
|
1169
|
+
re,
|
|
1170
|
+
stateful: re.global || re.sticky
|
|
1171
|
+
};
|
|
1172
|
+
plan.regexes[i] = entry;
|
|
1173
|
+
}
|
|
1174
|
+
if (entry.stateful) return BAIL;
|
|
1175
|
+
if (!entry.re.test(value)) return FAIL;
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
return PASS;
|
|
1179
|
+
}
|
|
1180
|
+
/** Plan counterpart of {@link fastCheckNumber}; constraint objects are read live. */ function checkNumber(plan, value) {
|
|
1181
|
+
if (plan.metaStamp !== stamp) refreshMeta(plan);
|
|
1182
|
+
return numberConstraints(value, plan.m0, plan.m1, plan.m2);
|
|
1183
|
+
}
|
|
1184
|
+
/** Plan counterpart of {@link fastCheckObject}. */ function checkObject(plan, value, flags, opts) {
|
|
1185
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
|
|
1186
|
+
const partial = opts.partial;
|
|
1187
|
+
const skipUndefined = partial === "deep" || partial === true && (flags & TOP) !== 0;
|
|
1188
|
+
const inUnion = (flags & IN_UNION) !== 0;
|
|
1189
|
+
const propFlags = flags & IN_UNION;
|
|
1190
|
+
const keys = plan.keys;
|
|
1191
|
+
const children = plan.children;
|
|
1192
|
+
const edges = plan.edges;
|
|
1193
|
+
let result = PASS;
|
|
1194
|
+
for (let i = 0; i < keys.length; i++) {
|
|
1195
|
+
if (edges[i] !== stamp) {
|
|
1196
|
+
if (plan.props.get(keys[i]) !== plan.nodes[i]) {
|
|
1197
|
+
plan.stamp = -1;
|
|
1198
|
+
return BAIL;
|
|
1199
|
+
}
|
|
1200
|
+
edges[i] = stamp;
|
|
1201
|
+
}
|
|
1202
|
+
const child = children[i];
|
|
1203
|
+
if (child.stamp !== stamp) refresh(child);
|
|
1204
|
+
const op = child.op;
|
|
1205
|
+
if (op === OP_PHANTOM) continue;
|
|
1206
|
+
const propValue = value[keys[i]];
|
|
1207
|
+
if (propValue === void 0 && skipUndefined) continue;
|
|
1208
|
+
if (typeof propValue === "string" && op === OP_STRING) {
|
|
1209
|
+
const r = checkString(child, propValue);
|
|
1210
|
+
if (r === PASS) continue;
|
|
1211
|
+
if (r === BAIL || !inUnion) return r;
|
|
1212
|
+
result = FAIL;
|
|
1213
|
+
continue;
|
|
1214
|
+
}
|
|
1215
|
+
if (typeof propValue === "number" && op === OP_NUMBER) {
|
|
1216
|
+
if (Number.isFinite(propValue) && checkNumber(child, propValue) === PASS) continue;
|
|
1217
|
+
if (!inUnion) return FAIL;
|
|
1218
|
+
result = FAIL;
|
|
1219
|
+
continue;
|
|
1220
|
+
}
|
|
1221
|
+
const propResult = checkPlan(child, propValue, propFlags, opts);
|
|
1222
|
+
if (propResult !== PASS) {
|
|
1223
|
+
if (propResult === BAIL || !inUnion) return propResult;
|
|
1224
|
+
result = FAIL;
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
if (result !== PASS) return result;
|
|
1228
|
+
const unknownProps = opts.unknownProps;
|
|
1229
|
+
if (unknownProps === "error" || unknownProps === "strip") {
|
|
1230
|
+
const byKey = plan.byKey;
|
|
1231
|
+
for (const key of Object.keys(value)) {
|
|
1232
|
+
const known = byKey.get(key);
|
|
1233
|
+
if (known === void 0 || known.op === OP_PHANTOM) return unknownProps === "error" ? FAIL : BAIL;
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
return PASS;
|
|
1237
|
+
}
|
|
1238
|
+
/** Plan counterpart of {@link fastCheckArray}. */ function checkArray(plan, value, flags, opts) {
|
|
1239
|
+
if (!Array.isArray(value)) return FAIL;
|
|
1240
|
+
if (plan.metaStamp !== stamp) refreshMeta(plan);
|
|
1241
|
+
const result = arrayConstraints(value.length, plan.m0, plan.m1, plan.m2);
|
|
1242
|
+
if (result !== PASS) return result;
|
|
1243
|
+
return checkItems(plan.ofPlan, value, flags, opts);
|
|
885
1244
|
}
|
|
886
1245
|
|
|
887
1246
|
//#endregion
|
|
@@ -1232,7 +1591,7 @@ function _define_property$4(obj, key, value) {
|
|
|
1232
1591
|
validatePrimitive(def, value) {
|
|
1233
1592
|
if (def.type.value !== void 0) {
|
|
1234
1593
|
if (value !== def.type.value) {
|
|
1235
|
-
this.error(`Expected ${def.type.value}, got ${value}`);
|
|
1594
|
+
this.error(`Expected ${def.type.value}, got ${printValue(value)}`);
|
|
1236
1595
|
return false;
|
|
1237
1596
|
}
|
|
1238
1597
|
return true;
|
|
@@ -1424,6 +1783,13 @@ function _define_property$4(obj, key, value) {
|
|
|
1424
1783
|
if (of) return of;
|
|
1425
1784
|
}
|
|
1426
1785
|
}
|
|
1786
|
+
/** `${value}` throws for symbols and null-prototype objects: `String()` handles symbols, the tag the rest. */ function printValue(value) {
|
|
1787
|
+
try {
|
|
1788
|
+
return String(value);
|
|
1789
|
+
} catch {
|
|
1790
|
+
return Object.prototype.toString.call(value);
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1427
1793
|
/** Error thrown by {@link Validator.validate} when validation fails. Contains structured error details. */ var ValidatorError = class extends Error {
|
|
1428
1794
|
constructor(errors) {
|
|
1429
1795
|
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";
|
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
|
|
2
|
-
const require_plugin = require('./plugin-
|
|
2
|
+
const require_plugin = require('./plugin-Cv2odCwI.cjs');
|
|
3
|
+
require('./json-schema-BqpH_eYJ.cjs');
|
|
3
4
|
|
|
4
5
|
exports.default = require_plugin.tsPlugin;
|
|
5
6
|
exports.tsPlugin = require_plugin.tsPlugin;
|
package/dist/index.mjs
CHANGED