@atscript/typescript 0.1.100 → 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 +34 -22
- package/dist/utils.d.ts +18 -0
- package/dist/utils.mjs +34 -22
- package/package.json +2 -2
|
@@ -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
|
}
|