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