@atscript/typescript 0.1.107 → 0.1.109
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 +508 -60
- package/dist/index.cjs +2 -1
- package/dist/index.mjs +2 -1
- package/dist/{json-schema-BN0B-fMO.cjs → json-schema-9OBvrg9D.cjs} +503 -48
- package/dist/{json-schema-DKgNpLpU.mjs → json-schema-CioBVi6F.mjs} +503 -48
- package/dist/{plugin-91-z7KE-.cjs → plugin-6r27w6rG.cjs} +10 -7
- package/dist/{plugin-GvSLmYJb.mjs → plugin-D3_BnRrI.mjs} +10 -7
- package/dist/test-utils.cjs +2 -1
- package/dist/test-utils.mjs +2 -1
- package/dist/utils.cjs +1 -1
- package/dist/utils.d.ts +3 -1
- package/dist/utils.mjs +1 -1
- package/package.json +6 -6
|
@@ -1,36 +1,92 @@
|
|
|
1
1
|
//#region packages/typescript/src/runtime/validator-fast-check.ts
|
|
2
|
+
function _define_property$1(obj, key, value) {
|
|
3
|
+
if (key in obj) Object.defineProperty(obj, key, {
|
|
4
|
+
value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
else obj[key] = value;
|
|
10
|
+
return obj;
|
|
11
|
+
}
|
|
2
12
|
/**
|
|
3
13
|
* Allocation-free boolean pre-check used by {@link Validator.validate} before the
|
|
4
14
|
* error-collecting walk.
|
|
5
15
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
16
|
+
* Nodes are checked by the generic functions below, which read the type tree and metadata
|
|
17
|
+
* LIVE on every visit. Array items — the same type visited many times per call — go
|
|
18
|
+
* through a compiled {@link Plan} of the item type instead (cached per node, shared by every
|
|
19
|
+
* validator and option set). A plan snapshots what its node's check reads and re-verifies
|
|
20
|
+
* every snapshot against the live node lazily, once per call, right before first using it
|
|
21
|
+
* (rebuilding it when anything differs); constraint objects and pattern arrays are read
|
|
22
|
+
* live on every visit. Either way `annotate()` / `metadata.set()` / prop mutations made
|
|
23
|
+
* after a validator was created are always honoured — the same as the full walk.
|
|
9
24
|
*
|
|
10
25
|
* 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
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
26
|
+
* - `PASS` — the walk returns `true`, collects no errors and has no side effects other
|
|
27
|
+
* than the strips recorded here (see below).
|
|
28
|
+
* - `FAIL` — the walk returns `false`. Inside a union, every node the walk would visit
|
|
29
|
+
* was visited here too (so a walk that would throw makes this throw first) and the
|
|
30
|
+
* walk's only side effects for this node are the strips recorded here. Outside unions
|
|
31
|
+
* a `FAIL` always ends in the walk, so it may be returned early.
|
|
15
32
|
* - `BAIL` — not decidable here; the caller must run the full walk.
|
|
16
33
|
*
|
|
17
34
|
* Union branches are tried in declared order, continuing past `FAIL` and stopping
|
|
18
35
|
* at `BAIL`, which keeps union results identical to the walk.
|
|
36
|
+
*
|
|
37
|
+
* `unknownProps: 'strip'`: the walk deletes an object's unknown keys right after the object
|
|
38
|
+
* passes (even when an ancestor fails later). This check records those deletions only for
|
|
39
|
+
* the top-level object (`pendingKeys`), whose strip is the walk's last read of the input:
|
|
40
|
+
* deferring an earlier strip past later reads could be observed by them (a prototype, a
|
|
41
|
+
* proxy or an accessor over the stripped object), so a nested object with unknown keys
|
|
42
|
+
* bails, as does any object visit once a strip is pending (a later intersection member).
|
|
43
|
+
* The recorded keys are deleted, in the walk's order, only when the whole value passes —
|
|
44
|
+
* so a `PASS` result still has exactly the walk's side effects, and `FAIL` / `BAIL` leave
|
|
45
|
+
* the value untouched for the walk.
|
|
19
46
|
*/ const FAIL = 0;
|
|
20
47
|
const PASS = 1;
|
|
21
48
|
const BAIL = 2;
|
|
22
49
|
/** Node is at walk depth 0 (only object props / array & tuple items increase depth). */ const TOP = 1;
|
|
23
50
|
/** Node is inside a union branch — failures must be exhaustive (see contract). */ const IN_UNION = 2;
|
|
51
|
+
/** Incremented per {@link fastCheckPasses} call; snapshots verified in older calls re-verify. */ let stamp = 0;
|
|
52
|
+
/** A 'strip' {@link fastCheckPasses} call is in progress (the pending strips below are its own). */ let stripping = false;
|
|
24
53
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
54
|
+
* Unknown keys of the top-level object the walk would delete (`unknownProps: 'strip'`), in
|
|
55
|
+
* the walk's deletion order. Deleted when the call passes, dropped otherwise.
|
|
56
|
+
*/ const pendingKeys = [];
|
|
57
|
+
/**
|
|
58
|
+
* `true` only when the full walk over `value` is guaranteed to pass with no errors and
|
|
59
|
+
* no side effects other than its 'strip' deletions, which are then performed here, as the
|
|
60
|
+
* walk would. Any other outcome (including a throw) leaves `value` untouched and means
|
|
61
|
+
* "run the walk".
|
|
27
62
|
*/ function fastCheckPasses(def, value, opts) {
|
|
63
|
+
if (opts.unknownProps === "strip") return fastCheckStripping(def, value, opts);
|
|
64
|
+
stamp++;
|
|
28
65
|
try {
|
|
29
66
|
return fastCheck(def, value, TOP, opts) === PASS;
|
|
30
67
|
} catch {
|
|
31
68
|
return false;
|
|
32
69
|
}
|
|
33
70
|
}
|
|
71
|
+
/** {@link fastCheckPasses} for `unknownProps: 'strip'`: performs the recorded strips on a pass. */ function fastCheckStripping(def, value, opts) {
|
|
72
|
+
if (stripping) return false;
|
|
73
|
+
stamp++;
|
|
74
|
+
stripping = true;
|
|
75
|
+
let passed;
|
|
76
|
+
try {
|
|
77
|
+
passed = fastCheck(def, value, TOP, opts) === PASS;
|
|
78
|
+
} catch {
|
|
79
|
+
passed = false;
|
|
80
|
+
} finally {
|
|
81
|
+
stripping = false;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
if (passed) for (const key of pendingKeys) delete value[key];
|
|
85
|
+
} finally {
|
|
86
|
+
pendingKeys.length = 0;
|
|
87
|
+
}
|
|
88
|
+
return passed;
|
|
89
|
+
}
|
|
34
90
|
const DECIMAL_RE = /^[+-]?\d+(\.\d+)?$/;
|
|
35
91
|
const regexCache = /* @__PURE__ */ new Map();
|
|
36
92
|
/** Compiled `@expect.pattern` regex, cached per (pattern, flags). */ function getRegex(pattern, flags) {
|
|
@@ -75,7 +131,8 @@ function fastCheck(def, value, flags, opts) {
|
|
|
75
131
|
const branchFlags = flags | IN_UNION;
|
|
76
132
|
for (const item of type.items) {
|
|
77
133
|
const result = fastCheck(item, value, branchFlags, opts);
|
|
78
|
-
if (result !== FAIL) return result
|
|
134
|
+
if (result !== FAIL) return unionMatchResult(result, item, def.metadata, value);
|
|
135
|
+
if (pendingKeys.length > 0) return BAIL;
|
|
79
136
|
}
|
|
80
137
|
return FAIL;
|
|
81
138
|
}
|
|
@@ -115,6 +172,9 @@ function fastCheckFinal(def, value) {
|
|
|
115
172
|
default: return BAIL;
|
|
116
173
|
}
|
|
117
174
|
}
|
|
175
|
+
/** Result of a union whose branch `item` did not fail (`PASS` or `BAIL`). */ function unionMatchResult(result, item, metadata, value) {
|
|
176
|
+
return result === PASS && item.type.value === void 0 ? fastCheckUnionConstraints(metadata, value) : result;
|
|
177
|
+
}
|
|
118
178
|
/**
|
|
119
179
|
* Mirrors Validator.validateUnionConstraints: union-level constraints on the matched value
|
|
120
180
|
* (`null` / objects pass; the caller skips literal branches).
|
|
@@ -129,8 +189,7 @@ function fastCheckFinal(def, value) {
|
|
|
129
189
|
}
|
|
130
190
|
}
|
|
131
191
|
function fastCheckBoolean(metadata, value) {
|
|
132
|
-
|
|
133
|
-
return metadata.get("meta.required") && value !== true ? FAIL : PASS;
|
|
192
|
+
return metadata.size === 0 ? PASS : booleanConstraints(value, metadata.get("meta.required"));
|
|
134
193
|
}
|
|
135
194
|
function fastCheckString(metadata, value) {
|
|
136
195
|
if (metadata.size === 0) return PASS;
|
|
@@ -150,20 +209,36 @@ function fastCheckString(metadata, value) {
|
|
|
150
209
|
}
|
|
151
210
|
function fastCheckNumber(metadata, value) {
|
|
152
211
|
if (metadata.size === 0) return PASS;
|
|
153
|
-
|
|
154
|
-
|
|
212
|
+
return numberConstraints(value, metadata.get("expect.int"), metadata.get("expect.min"), metadata.get("expect.max"), metadata.get("expect.exclusiveMin"), metadata.get("expect.exclusiveMax"));
|
|
213
|
+
}
|
|
214
|
+
function booleanConstraints(value, required) {
|
|
215
|
+
return required && value !== true ? FAIL : PASS;
|
|
216
|
+
}
|
|
217
|
+
function numberConstraints(value, int, min, max, exMin, exMax) {
|
|
218
|
+
if (int && value % 1 !== 0) return FAIL;
|
|
155
219
|
if (min !== void 0 && value < (typeof min === "number" ? min : min.minValue)) return FAIL;
|
|
156
|
-
const max = metadata.get("expect.max");
|
|
157
220
|
if (max !== void 0 && value > (typeof max === "number" ? max : max.maxValue)) return FAIL;
|
|
221
|
+
if (exMin !== void 0 && value <= (typeof exMin === "number" ? exMin : exMin.exclusiveMinValue)) return FAIL;
|
|
222
|
+
if (exMax !== void 0 && value >= (typeof exMax === "number" ? exMax : exMax.exclusiveMaxValue)) return FAIL;
|
|
158
223
|
return PASS;
|
|
159
224
|
}
|
|
225
|
+
/** `@expect.minLength` / `@expect.maxLength` (strings and arrays). */ function lengthFails(length, minLength, maxLength) {
|
|
226
|
+
return minLength !== void 0 && length < (typeof minLength === "number" ? minLength : minLength.length) || maxLength !== void 0 && length > (typeof maxLength === "number" ? maxLength : maxLength.length);
|
|
227
|
+
}
|
|
228
|
+
/** Array constraints; duplicate detection (`uniqueItems`) is left to the walk. */ function arrayConstraints(length, minLength, maxLength, uniqueItems) {
|
|
229
|
+
if (lengthFails(length, minLength, maxLength)) return FAIL;
|
|
230
|
+
return uniqueItems ? BAIL : PASS;
|
|
231
|
+
}
|
|
160
232
|
function fastCheckObject(def, value, flags, opts) {
|
|
161
233
|
if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
|
|
162
234
|
const type = def.type;
|
|
163
235
|
if (type.propsPatterns.length > 0) return BAIL;
|
|
236
|
+
if (pendingKeys.length > 0) return BAIL;
|
|
237
|
+
const unknownProps = opts.unknownProps;
|
|
238
|
+
const inUnion = (flags & IN_UNION) !== 0;
|
|
239
|
+
if (unknownProps === "error" && !inUnion && hasUnknownKey(type.props, value)) return FAIL;
|
|
164
240
|
const partial = opts.partial;
|
|
165
241
|
const skipUndefined = partial === "deep" || partial === true && (flags & TOP) !== 0;
|
|
166
|
-
const inUnion = (flags & IN_UNION) !== 0;
|
|
167
242
|
const propFlags = flags & IN_UNION;
|
|
168
243
|
let result = PASS;
|
|
169
244
|
for (const [key, item] of type.props) {
|
|
@@ -177,16 +252,28 @@ function fastCheckObject(def, value, flags, opts) {
|
|
|
177
252
|
}
|
|
178
253
|
}
|
|
179
254
|
if (result !== PASS) return result;
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
255
|
+
if (unknownProps === "strip") return pendStrips(type.props, value, flags);
|
|
256
|
+
return unknownProps === "error" && inUnion && hasUnknownKey(type.props, value) ? FAIL : PASS;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Records the unknown keys of a passing top-level object: the walk deletes them at this
|
|
260
|
+
* point, after its last read of the input. A nested object's strip would be followed by
|
|
261
|
+
* more reads, so it is left to the walk.
|
|
262
|
+
*/ function pendStrips(props, value, flags) {
|
|
263
|
+
for (const key of Object.keys(value)) if (isUnknownKey(props, key)) {
|
|
264
|
+
if ((flags & TOP) === 0) return BAIL;
|
|
265
|
+
pendingKeys.push(key);
|
|
187
266
|
}
|
|
188
267
|
return PASS;
|
|
189
268
|
}
|
|
269
|
+
/** `true` when `value` has a key the walk treats as unknown. */ function hasUnknownKey(props, value) {
|
|
270
|
+
for (const key of Object.keys(value)) if (isUnknownKey(props, key)) return true;
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
/** `true` when the walk treats `key` as unknown (no prop, or a phantom one). */ function isUnknownKey(props, key) {
|
|
274
|
+
const known = props.get(key);
|
|
275
|
+
return known === void 0 || isPhantomType(known);
|
|
276
|
+
}
|
|
190
277
|
function fastCheckArray(def, value, flags, opts) {
|
|
191
278
|
if (!Array.isArray(value)) return FAIL;
|
|
192
279
|
const metadata = def.metadata;
|
|
@@ -194,12 +281,14 @@ function fastCheckArray(def, value, flags, opts) {
|
|
|
194
281
|
const result = fastCheckArrayConstraints(metadata, value);
|
|
195
282
|
if (result !== PASS) return result;
|
|
196
283
|
}
|
|
197
|
-
|
|
284
|
+
return checkItems(planFor(def.type.of), value, flags, opts);
|
|
285
|
+
}
|
|
286
|
+
/** Checks every array item against the item type's plan. */ function checkItems(of, value, flags, opts) {
|
|
198
287
|
const inUnion = (flags & IN_UNION) !== 0;
|
|
199
288
|
const itemFlags = flags & IN_UNION;
|
|
200
289
|
let result = PASS;
|
|
201
290
|
for (const item of value) {
|
|
202
|
-
const itemResult =
|
|
291
|
+
const itemResult = checkPlan(of, item, itemFlags, opts);
|
|
203
292
|
if (itemResult !== PASS) {
|
|
204
293
|
if (itemResult === BAIL || !inUnion) return itemResult;
|
|
205
294
|
result = FAIL;
|
|
@@ -208,11 +297,344 @@ function fastCheckArray(def, value, flags, opts) {
|
|
|
208
297
|
return result;
|
|
209
298
|
}
|
|
210
299
|
function fastCheckArrayConstraints(metadata, value) {
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
300
|
+
return arrayConstraints(value.length, metadata.get("expect.minLength"), metadata.get("expect.maxLength"), metadata.get("expect.array.uniqueItems"));
|
|
301
|
+
}
|
|
302
|
+
const OP_GENERIC = 0;
|
|
303
|
+
const OP_PHANTOM = 1;
|
|
304
|
+
const OP_STRING = 2;
|
|
305
|
+
const OP_NUMBER = 3;
|
|
306
|
+
const OP_BOOLEAN = 4;
|
|
307
|
+
/** Any other final node: checked by {@link fastCheckFinal}. */ const OP_FINAL = 5;
|
|
308
|
+
const OP_OBJECT = 6;
|
|
309
|
+
const OP_ARRAY = 7;
|
|
310
|
+
const OP_UNION = 8;
|
|
311
|
+
function isFinalOp(op) {
|
|
312
|
+
return op !== OP_GENERIC && op <= OP_FINAL;
|
|
313
|
+
}
|
|
314
|
+
/** Shared placeholder for the per-kind arrays of plans that don't use them. */ const EMPTY = Object.freeze([]);
|
|
315
|
+
const plans = /* @__PURE__ */ new WeakMap();
|
|
316
|
+
/** One shape for every plan, so plan field access stays monomorphic. */ let Plan = class Plan {
|
|
317
|
+
constructor(def) {
|
|
318
|
+
_define_property$1(this, "def", void 0);
|
|
319
|
+
/** Call in which the node structure was last verified (`-1`: must rebuild). */ _define_property$1(this, "stamp", void 0);
|
|
320
|
+
/** Call in which the annotation values (`m0`–`m4`) were last verified. */ _define_property$1(this, "metaStamp", void 0);
|
|
321
|
+
_define_property$1(this, "op", void 0);
|
|
322
|
+
_define_property$1(this, "type", void 0);
|
|
323
|
+
_define_property$1(this, "optional", void 0);
|
|
324
|
+
_define_property$1(this, "metadata", void 0);
|
|
325
|
+
_define_property$1(this, "kind", void 0);
|
|
326
|
+
_define_property$1(this, "designType", void 0);
|
|
327
|
+
_define_property$1(this, "literal", void 0);
|
|
328
|
+
/** String plans: compiled `@expect.pattern` regexes by position (checked against the live entry). */ _define_property$1(this, "regexes", void 0);
|
|
329
|
+
/**
|
|
330
|
+
* Annotation keys the node's check reads, and their values (`undefined` when absent):
|
|
331
|
+
* string `meta.required, expect.minLength, maxLength, pattern`; number `expect.int, min,
|
|
332
|
+
* max, exclusiveMin, exclusiveMax`; boolean `meta.required`; array `expect.minLength, maxLength, array.uniqueItems`.
|
|
333
|
+
*/ _define_property$1(this, "k0", void 0);
|
|
334
|
+
_define_property$1(this, "k1", void 0);
|
|
335
|
+
_define_property$1(this, "k2", void 0);
|
|
336
|
+
_define_property$1(this, "k3", void 0);
|
|
337
|
+
_define_property$1(this, "k4", void 0);
|
|
338
|
+
_define_property$1(this, "m0", void 0);
|
|
339
|
+
_define_property$1(this, "m1", void 0);
|
|
340
|
+
_define_property$1(this, "m2", void 0);
|
|
341
|
+
_define_property$1(this, "m3", void 0);
|
|
342
|
+
_define_property$1(this, "m4", void 0);
|
|
343
|
+
_define_property$1(this, "props", void 0);
|
|
344
|
+
_define_property$1(this, "propsPatterns", void 0);
|
|
345
|
+
_define_property$1(this, "keys", void 0);
|
|
346
|
+
/** Call in which `props.get(keys[i]) === nodes[i]` was last verified. */ _define_property$1(this, "edges", void 0);
|
|
347
|
+
/** Prop plans by key (the entries of `props`, verified per visit via `edges`). */ _define_property$1(this, "byKey", void 0);
|
|
348
|
+
_define_property$1(this, "nodes", void 0);
|
|
349
|
+
_define_property$1(this, "children", void 0);
|
|
350
|
+
_define_property$1(this, "of", void 0);
|
|
351
|
+
_define_property$1(this, "ofPlan", void 0);
|
|
352
|
+
_define_property$1(this, "items", void 0);
|
|
353
|
+
this.def = def;
|
|
354
|
+
this.stamp = -1;
|
|
355
|
+
this.metaStamp = -1;
|
|
356
|
+
this.op = OP_GENERIC;
|
|
357
|
+
this.type = void 0;
|
|
358
|
+
this.optional = void 0;
|
|
359
|
+
this.metadata = void 0;
|
|
360
|
+
this.kind = void 0;
|
|
361
|
+
this.designType = void 0;
|
|
362
|
+
this.literal = void 0;
|
|
363
|
+
this.regexes = EMPTY;
|
|
364
|
+
this.k0 = void 0;
|
|
365
|
+
this.k1 = void 0;
|
|
366
|
+
this.k2 = void 0;
|
|
367
|
+
this.k3 = void 0;
|
|
368
|
+
this.k4 = void 0;
|
|
369
|
+
this.m0 = void 0;
|
|
370
|
+
this.m1 = void 0;
|
|
371
|
+
this.m2 = void 0;
|
|
372
|
+
this.m3 = void 0;
|
|
373
|
+
this.m4 = void 0;
|
|
374
|
+
this.props = void 0;
|
|
375
|
+
this.propsPatterns = void 0;
|
|
376
|
+
this.keys = EMPTY;
|
|
377
|
+
this.edges = EMPTY;
|
|
378
|
+
this.byKey = void 0;
|
|
379
|
+
this.nodes = EMPTY;
|
|
380
|
+
this.children = EMPTY;
|
|
381
|
+
this.of = void 0;
|
|
382
|
+
this.ofPlan = void 0;
|
|
383
|
+
this.items = void 0;
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
function planFor(def) {
|
|
387
|
+
let plan = plans.get(def);
|
|
388
|
+
if (plan === void 0) {
|
|
389
|
+
plan = new Plan(def);
|
|
390
|
+
plans.set(def, plan);
|
|
391
|
+
}
|
|
392
|
+
return plan;
|
|
393
|
+
}
|
|
394
|
+
/** Brings the plan's structure up to date with its live node (once per call). */ function refresh(plan) {
|
|
395
|
+
if (!(isFinalOp(plan.op) ? verifyFinal(plan) : verify(plan))) build(plan);
|
|
396
|
+
plan.stamp = stamp;
|
|
397
|
+
}
|
|
398
|
+
/** Structure check for final ops; small enough to be inlined into the hot loops. */ function verifyFinal(plan) {
|
|
399
|
+
const def = plan.def;
|
|
400
|
+
const type = def.type;
|
|
401
|
+
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;
|
|
402
|
+
}
|
|
403
|
+
/** Structure check for objects, arrays, unions and generic nodes. */ function verify(plan) {
|
|
404
|
+
if (plan.stamp === -1) return false;
|
|
405
|
+
const def = plan.def;
|
|
406
|
+
const type = def.type;
|
|
407
|
+
if (type !== plan.type || def.optional !== plan.optional || def.metadata !== plan.metadata || type.kind !== plan.kind) return false;
|
|
408
|
+
switch (plan.op) {
|
|
409
|
+
case OP_OBJECT: return type.props === plan.props && plan.props.size === plan.keys.length && type.propsPatterns === plan.propsPatterns && plan.propsPatterns.length === 0;
|
|
410
|
+
case OP_ARRAY: return type.of === plan.of;
|
|
411
|
+
case OP_UNION: {
|
|
412
|
+
const items = plan.items;
|
|
413
|
+
const nodes = plan.nodes;
|
|
414
|
+
if (type.items !== items || items.length !== nodes.length) return false;
|
|
415
|
+
for (let i = 0; i < nodes.length; i++) if (items[i] !== nodes[i]) return false;
|
|
416
|
+
return true;
|
|
417
|
+
}
|
|
418
|
+
default: return type.kind !== "object" || type.propsPatterns.length > 0;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
/** Brings the annotation values up to date (once per call, when a constraint is read). */ function refreshMeta(plan) {
|
|
422
|
+
const metadata = plan.metadata;
|
|
423
|
+
if (!(metadata.size === 0 ? plan.m0 === void 0 && plan.m1 === void 0 && plan.m2 === void 0 && plan.m3 === void 0 && plan.m4 === 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) && (plan.k4 === void 0 || metadata.get(plan.k4) === plan.m4))) snapshotMeta(plan);
|
|
424
|
+
plan.metaStamp = stamp;
|
|
425
|
+
}
|
|
426
|
+
function snapshotMeta(plan) {
|
|
427
|
+
const metadata = plan.metadata;
|
|
428
|
+
plan.m0 = metadata.get(plan.k0);
|
|
429
|
+
plan.m1 = metadata.get(plan.k1);
|
|
430
|
+
plan.m2 = metadata.get(plan.k2);
|
|
431
|
+
plan.m3 = metadata.get(plan.k3);
|
|
432
|
+
plan.m4 = metadata.get(plan.k4);
|
|
433
|
+
}
|
|
434
|
+
function setMetaKeys(plan, k0, k1, k2, k3, k4) {
|
|
435
|
+
plan.k0 = k0;
|
|
436
|
+
plan.k1 = k1;
|
|
437
|
+
plan.k2 = k2;
|
|
438
|
+
plan.k3 = k3;
|
|
439
|
+
plan.k4 = k4;
|
|
440
|
+
snapshotMeta(plan);
|
|
441
|
+
plan.metaStamp = stamp;
|
|
442
|
+
}
|
|
443
|
+
/** (Re)compiles `plan` from its live node. */ function build(plan) {
|
|
444
|
+
plan.stamp = -1;
|
|
445
|
+
plan.op = OP_GENERIC;
|
|
446
|
+
const def = plan.def;
|
|
447
|
+
const type = def.type;
|
|
448
|
+
plan.type = type;
|
|
449
|
+
plan.optional = def.optional;
|
|
450
|
+
plan.metadata = def.metadata;
|
|
451
|
+
plan.kind = type.kind;
|
|
452
|
+
plan.designType = type.designType;
|
|
453
|
+
plan.literal = type.value;
|
|
454
|
+
plan.regexes = EMPTY;
|
|
455
|
+
plan.props = void 0;
|
|
456
|
+
plan.propsPatterns = void 0;
|
|
457
|
+
plan.keys = EMPTY;
|
|
458
|
+
plan.edges = EMPTY;
|
|
459
|
+
plan.byKey = void 0;
|
|
460
|
+
plan.nodes = EMPTY;
|
|
461
|
+
plan.children = EMPTY;
|
|
462
|
+
plan.of = void 0;
|
|
463
|
+
plan.ofPlan = void 0;
|
|
464
|
+
plan.items = void 0;
|
|
465
|
+
setMetaKeys(plan);
|
|
466
|
+
switch (type.kind) {
|
|
467
|
+
case "":
|
|
468
|
+
if (type.designType === "phantom") plan.op = OP_PHANTOM;
|
|
469
|
+
else if (type.value !== void 0) plan.op = OP_FINAL;
|
|
470
|
+
else if (type.designType === "string") {
|
|
471
|
+
setMetaKeys(plan, "meta.required", "expect.minLength", "expect.maxLength", "expect.pattern");
|
|
472
|
+
plan.regexes = [];
|
|
473
|
+
plan.op = OP_STRING;
|
|
474
|
+
} else if (type.designType === "number") {
|
|
475
|
+
setMetaKeys(plan, "expect.int", "expect.min", "expect.max", "expect.exclusiveMin", "expect.exclusiveMax");
|
|
476
|
+
plan.op = OP_NUMBER;
|
|
477
|
+
} else if (type.designType === "boolean") {
|
|
478
|
+
setMetaKeys(plan, "meta.required");
|
|
479
|
+
plan.op = OP_BOOLEAN;
|
|
480
|
+
} else plan.op = OP_FINAL;
|
|
481
|
+
break;
|
|
482
|
+
case "object": {
|
|
483
|
+
if (type.propsPatterns.length > 0) break;
|
|
484
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
485
|
+
plan.keys = [];
|
|
486
|
+
plan.edges = [];
|
|
487
|
+
plan.nodes = [];
|
|
488
|
+
plan.children = [];
|
|
489
|
+
for (const [key, node] of type.props) {
|
|
490
|
+
const child = planFor(node);
|
|
491
|
+
plan.keys.push(key);
|
|
492
|
+
plan.edges.push(stamp);
|
|
493
|
+
plan.nodes.push(node);
|
|
494
|
+
plan.children.push(child);
|
|
495
|
+
byKey.set(key, child);
|
|
496
|
+
}
|
|
497
|
+
plan.byKey = byKey;
|
|
498
|
+
plan.props = type.props;
|
|
499
|
+
plan.propsPatterns = type.propsPatterns;
|
|
500
|
+
plan.op = OP_OBJECT;
|
|
501
|
+
break;
|
|
502
|
+
}
|
|
503
|
+
case "array":
|
|
504
|
+
setMetaKeys(plan, "expect.minLength", "expect.maxLength", "expect.array.uniqueItems");
|
|
505
|
+
plan.of = type.of;
|
|
506
|
+
plan.ofPlan = planFor(type.of);
|
|
507
|
+
plan.op = OP_ARRAY;
|
|
508
|
+
break;
|
|
509
|
+
case "union":
|
|
510
|
+
plan.nodes = [];
|
|
511
|
+
plan.children = [];
|
|
512
|
+
for (const node of type.items) {
|
|
513
|
+
plan.nodes.push(node);
|
|
514
|
+
plan.children.push(planFor(node));
|
|
515
|
+
}
|
|
516
|
+
plan.items = type.items;
|
|
517
|
+
plan.op = OP_UNION;
|
|
518
|
+
break;
|
|
519
|
+
default:
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
/** Plan counterpart of {@link fastCheck}. */ function checkPlan(plan, value, flags, opts) {
|
|
523
|
+
if (plan.stamp !== stamp) refresh(plan);
|
|
524
|
+
if (plan.optional && (value === void 0 || value === null)) return value === null && rejectsNullAsRequired(plan.def) ? FAIL : PASS;
|
|
525
|
+
switch (plan.op) {
|
|
526
|
+
case OP_STRING: return typeof value === "string" ? checkString(plan, value) : FAIL;
|
|
527
|
+
case OP_NUMBER: return typeof value === "number" && Number.isFinite(value) ? checkNumber(plan, value) : FAIL;
|
|
528
|
+
case OP_OBJECT: return checkObject(plan, value, flags, opts);
|
|
529
|
+
case OP_ARRAY: return checkArray(plan, value, flags, opts);
|
|
530
|
+
case OP_UNION: {
|
|
531
|
+
const branchFlags = flags | IN_UNION;
|
|
532
|
+
for (const child of plan.children) {
|
|
533
|
+
const result = checkPlan(child, value, branchFlags, opts);
|
|
534
|
+
if (result !== FAIL) return unionMatchResult(result, child.def, plan.metadata, value);
|
|
535
|
+
}
|
|
536
|
+
return FAIL;
|
|
537
|
+
}
|
|
538
|
+
case OP_BOOLEAN:
|
|
539
|
+
if (typeof value !== "boolean") return FAIL;
|
|
540
|
+
if (plan.metaStamp !== stamp) refreshMeta(plan);
|
|
541
|
+
return booleanConstraints(value, plan.m0);
|
|
542
|
+
case OP_PHANTOM:
|
|
543
|
+
case OP_FINAL: return fastCheckFinal(plan.def, value);
|
|
544
|
+
default: return fastCheck(plan.def, value, flags, opts);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
/** Plan counterpart of {@link fastCheckString}; constraint objects are read live. */ function checkString(plan, value) {
|
|
548
|
+
if (plan.metaStamp !== stamp) refreshMeta(plan);
|
|
549
|
+
if (plan.m0 && value.trim().length === 0) return FAIL;
|
|
550
|
+
if (lengthFails(value.length, plan.m1, plan.m2)) return FAIL;
|
|
551
|
+
const patterns = plan.m3;
|
|
552
|
+
if (patterns) {
|
|
553
|
+
if (!Array.isArray(patterns)) return fastCheckString(plan.metadata, value);
|
|
554
|
+
for (let i = 0; i < patterns.length; i++) {
|
|
555
|
+
const { pattern, flags } = patterns[i];
|
|
556
|
+
if (!pattern) continue;
|
|
557
|
+
let entry = plan.regexes[i];
|
|
558
|
+
if (entry === void 0 || entry.pattern !== pattern || entry.flags !== flags) {
|
|
559
|
+
const re = getRegex(pattern, flags);
|
|
560
|
+
entry = {
|
|
561
|
+
pattern,
|
|
562
|
+
flags,
|
|
563
|
+
re,
|
|
564
|
+
stateful: re.global || re.sticky
|
|
565
|
+
};
|
|
566
|
+
plan.regexes[i] = entry;
|
|
567
|
+
}
|
|
568
|
+
if (entry.stateful) return BAIL;
|
|
569
|
+
if (!entry.re.test(value)) return FAIL;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
return PASS;
|
|
573
|
+
}
|
|
574
|
+
/** Plan counterpart of {@link fastCheckNumber}; constraint objects are read live. */ function checkNumber(plan, value) {
|
|
575
|
+
if (plan.metaStamp !== stamp) refreshMeta(plan);
|
|
576
|
+
return numberConstraints(value, plan.m0, plan.m1, plan.m2, plan.m3, plan.m4);
|
|
577
|
+
}
|
|
578
|
+
/** Plan counterpart of {@link fastCheckObject}. */ function checkObject(plan, value, flags, opts) {
|
|
579
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return FAIL;
|
|
580
|
+
const partial = opts.partial;
|
|
581
|
+
const skipUndefined = partial === "deep" || partial === true && (flags & TOP) !== 0;
|
|
582
|
+
const inUnion = (flags & IN_UNION) !== 0;
|
|
583
|
+
const propFlags = flags & IN_UNION;
|
|
584
|
+
const keys = plan.keys;
|
|
585
|
+
const children = plan.children;
|
|
586
|
+
const edges = plan.edges;
|
|
587
|
+
let result = PASS;
|
|
588
|
+
for (let i = 0; i < keys.length; i++) {
|
|
589
|
+
if (edges[i] !== stamp) {
|
|
590
|
+
if (plan.props.get(keys[i]) !== plan.nodes[i]) {
|
|
591
|
+
plan.stamp = -1;
|
|
592
|
+
return BAIL;
|
|
593
|
+
}
|
|
594
|
+
edges[i] = stamp;
|
|
595
|
+
}
|
|
596
|
+
const child = children[i];
|
|
597
|
+
if (child.stamp !== stamp) refresh(child);
|
|
598
|
+
const op = child.op;
|
|
599
|
+
if (op === OP_PHANTOM) continue;
|
|
600
|
+
const propValue = value[keys[i]];
|
|
601
|
+
if (propValue === void 0 && skipUndefined) continue;
|
|
602
|
+
if (typeof propValue === "string" && op === OP_STRING) {
|
|
603
|
+
const r = checkString(child, propValue);
|
|
604
|
+
if (r === PASS) continue;
|
|
605
|
+
if (r === BAIL || !inUnion) return r;
|
|
606
|
+
result = FAIL;
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
if (typeof propValue === "number" && op === OP_NUMBER) {
|
|
610
|
+
if (Number.isFinite(propValue) && checkNumber(child, propValue) === PASS) continue;
|
|
611
|
+
if (!inUnion) return FAIL;
|
|
612
|
+
result = FAIL;
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
const propResult = checkPlan(child, propValue, propFlags, opts);
|
|
616
|
+
if (propResult !== PASS) {
|
|
617
|
+
if (propResult === BAIL || !inUnion) return propResult;
|
|
618
|
+
result = FAIL;
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
if (result !== PASS) return result;
|
|
622
|
+
const unknownProps = opts.unknownProps;
|
|
623
|
+
if (unknownProps === "error" || unknownProps === "strip") {
|
|
624
|
+
const byKey = plan.byKey;
|
|
625
|
+
for (const key of Object.keys(value)) {
|
|
626
|
+
const known = byKey.get(key);
|
|
627
|
+
if (known === void 0 || known.op === OP_PHANTOM) return unknownProps === "error" ? FAIL : BAIL;
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
return PASS;
|
|
631
|
+
}
|
|
632
|
+
/** Plan counterpart of {@link fastCheckArray}. */ function checkArray(plan, value, flags, opts) {
|
|
633
|
+
if (!Array.isArray(value)) return FAIL;
|
|
634
|
+
if (plan.metaStamp !== stamp) refreshMeta(plan);
|
|
635
|
+
const result = arrayConstraints(value.length, plan.m0, plan.m1, plan.m2);
|
|
636
|
+
if (result !== PASS) return result;
|
|
637
|
+
return checkItems(plan.ofPlan, value, flags, opts);
|
|
216
638
|
}
|
|
217
639
|
|
|
218
640
|
//#endregion
|
|
@@ -563,7 +985,7 @@ function _define_property(obj, key, value) {
|
|
|
563
985
|
validatePrimitive(def, value) {
|
|
564
986
|
if (def.type.value !== void 0) {
|
|
565
987
|
if (value !== def.type.value) {
|
|
566
|
-
this.error(`Expected ${def.type.value}, got ${value}`);
|
|
988
|
+
this.error(`Expected ${def.type.value}, got ${printValue(value)}`);
|
|
567
989
|
return false;
|
|
568
990
|
}
|
|
569
991
|
return true;
|
|
@@ -665,7 +1087,7 @@ function _define_property(obj, key, value) {
|
|
|
665
1087
|
validateNumber(def, value) {
|
|
666
1088
|
return this.validateNumberConstraints(def.metadata, value);
|
|
667
1089
|
}
|
|
668
|
-
/** `@expect.int` / `min` / `max` checks for a number value. */ validateNumberConstraints(metadata, value) {
|
|
1090
|
+
/** `@expect.int` / `min` / `max` / `exclusiveMin` / `exclusiveMax` checks for a number value. */ validateNumberConstraints(metadata, value) {
|
|
669
1091
|
if (metadata.size === 0) return true;
|
|
670
1092
|
const int = metadata.get("expect.int");
|
|
671
1093
|
if (int && value % 1 !== 0) {
|
|
@@ -676,23 +1098,30 @@ function _define_property(obj, key, value) {
|
|
|
676
1098
|
const min = metadata.get("expect.min");
|
|
677
1099
|
if (min !== void 0) {
|
|
678
1100
|
const minValue = typeof min === "number" ? min : min.minValue;
|
|
679
|
-
if (value < minValue)
|
|
680
|
-
const message = typeof min === "object" && min.message ? min.message : `Expected minimum ${minValue}, got ${value}`;
|
|
681
|
-
this.error(message);
|
|
682
|
-
return false;
|
|
683
|
-
}
|
|
1101
|
+
if (value < minValue) return this.numberBoundFailed(min, "minimum", minValue, value);
|
|
684
1102
|
}
|
|
685
1103
|
const max = metadata.get("expect.max");
|
|
686
1104
|
if (max !== void 0) {
|
|
687
1105
|
const maxValue = typeof max === "number" ? max : max.maxValue;
|
|
688
|
-
if (value > maxValue)
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
1106
|
+
if (value > maxValue) return this.numberBoundFailed(max, "maximum", maxValue, value);
|
|
1107
|
+
}
|
|
1108
|
+
const exMin = metadata.get("expect.exclusiveMin");
|
|
1109
|
+
if (exMin !== void 0) {
|
|
1110
|
+
const bound = typeof exMin === "number" ? exMin : exMin.exclusiveMinValue;
|
|
1111
|
+
if (value <= bound) return this.numberBoundFailed(exMin, "greater than", bound, value);
|
|
1112
|
+
}
|
|
1113
|
+
const exMax = metadata.get("expect.exclusiveMax");
|
|
1114
|
+
if (exMax !== void 0) {
|
|
1115
|
+
const bound = typeof exMax === "number" ? exMax : exMax.exclusiveMaxValue;
|
|
1116
|
+
if (value >= bound) return this.numberBoundFailed(exMax, "less than", bound, value);
|
|
693
1117
|
}
|
|
694
1118
|
return true;
|
|
695
1119
|
}
|
|
1120
|
+
/** Reports a failed number bound: its custom message, else `Expected <label> <bound>, got <value>`. */ numberBoundFailed(annotation, label, bound, value) {
|
|
1121
|
+
const message = typeof annotation === "object" && annotation.message ? annotation.message : `Expected ${label} ${bound}, got ${value}`;
|
|
1122
|
+
this.error(message);
|
|
1123
|
+
return false;
|
|
1124
|
+
}
|
|
696
1125
|
validateBoolean(def, value) {
|
|
697
1126
|
return this.validateBooleanConstraints(def.metadata, value);
|
|
698
1127
|
}
|
|
@@ -755,6 +1184,13 @@ function _define_property(obj, key, value) {
|
|
|
755
1184
|
if (of) return of;
|
|
756
1185
|
}
|
|
757
1186
|
}
|
|
1187
|
+
/** `${value}` throws for symbols and null-prototype objects: `String()` handles symbols, the tag the rest. */ function printValue(value) {
|
|
1188
|
+
try {
|
|
1189
|
+
return String(value);
|
|
1190
|
+
} catch {
|
|
1191
|
+
return Object.prototype.toString.call(value);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
758
1194
|
/** Error thrown by {@link Validator.validate} when validation fails. Contains structured error details. */ var ValidatorError = class extends Error {
|
|
759
1195
|
constructor(errors) {
|
|
760
1196
|
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";
|
|
@@ -1226,18 +1662,27 @@ function cloneTypeDef(type) {
|
|
|
1226
1662
|
}
|
|
1227
1663
|
if (schema.type === "number" || schema.type === "integer") {
|
|
1228
1664
|
const min = meta.get("expect.min");
|
|
1229
|
-
if (min !== void 0) schema.minimum =
|
|
1665
|
+
if (min !== void 0) schema.minimum = boundOf(min, "minValue");
|
|
1230
1666
|
const max = meta.get("expect.max");
|
|
1231
|
-
if (max !== void 0) schema.maximum =
|
|
1667
|
+
if (max !== void 0) schema.maximum = boundOf(max, "maxValue");
|
|
1668
|
+
const exMin = meta.get("expect.exclusiveMin");
|
|
1669
|
+
if (exMin !== void 0) schema.exclusiveMinimum = boundOf(exMin, "exclusiveMinValue");
|
|
1670
|
+
const exMax = meta.get("expect.exclusiveMax");
|
|
1671
|
+
if (exMax !== void 0) schema.exclusiveMaximum = boundOf(exMax, "exclusiveMaxValue");
|
|
1232
1672
|
}
|
|
1233
1673
|
}
|
|
1674
|
+
/** A number-bound annotation's value: the bare number, or its `field` in the object form. */ function boundOf(m, field) {
|
|
1675
|
+
return typeof m === "number" ? m : m[field];
|
|
1676
|
+
}
|
|
1234
1677
|
const LOWER_BOUNDS = new Set([
|
|
1235
1678
|
"minimum",
|
|
1679
|
+
"exclusiveMinimum",
|
|
1236
1680
|
"minLength",
|
|
1237
1681
|
"minItems"
|
|
1238
1682
|
]);
|
|
1239
1683
|
const UPPER_BOUNDS = new Set([
|
|
1240
1684
|
"maximum",
|
|
1685
|
+
"exclusiveMaximum",
|
|
1241
1686
|
"maxLength",
|
|
1242
1687
|
"maxItems"
|
|
1243
1688
|
]);
|
|
@@ -1377,14 +1822,12 @@ const UPPER_BOUNDS = new Set([
|
|
|
1377
1822
|
if (s.type === "integer") {
|
|
1378
1823
|
const handle = defineAnnotatedType().designType("number").tags("number");
|
|
1379
1824
|
handle.annotate("expect.int", true);
|
|
1380
|
-
|
|
1381
|
-
if (typeof s.maximum === "number") handle.annotate("expect.max", { maxValue: s.maximum });
|
|
1825
|
+
annotateNumberBounds(handle, s);
|
|
1382
1826
|
return handle.$type;
|
|
1383
1827
|
}
|
|
1384
1828
|
if (s.type === "number") {
|
|
1385
1829
|
const handle = defineAnnotatedType().designType("number").tags("number");
|
|
1386
|
-
|
|
1387
|
-
if (typeof s.maximum === "number") handle.annotate("expect.max", { maxValue: s.maximum });
|
|
1830
|
+
annotateNumberBounds(handle, s);
|
|
1388
1831
|
return handle.$type;
|
|
1389
1832
|
}
|
|
1390
1833
|
if (s.type === "boolean") return defineAnnotatedType().designType("boolean").tags("boolean").$type;
|
|
@@ -1394,6 +1837,18 @@ const UPPER_BOUNDS = new Set([
|
|
|
1394
1837
|
return convert(schema);
|
|
1395
1838
|
}
|
|
1396
1839
|
/**
|
|
1840
|
+
* Maps `minimum` / `maximum` / `exclusiveMinimum` / `exclusiveMaximum` onto
|
|
1841
|
+
* `@expect.min` / `max` / `exclusiveMin` / `exclusiveMax`. Also reads the draft-4 form,
|
|
1842
|
+
* where `exclusiveMinimum: true` makes `minimum` exclusive.
|
|
1843
|
+
*/ function annotateNumberBounds(handle, s) {
|
|
1844
|
+
if (typeof s.minimum === "number") if (s.exclusiveMinimum === true) handle.annotate("expect.exclusiveMin", { exclusiveMinValue: s.minimum });
|
|
1845
|
+
else handle.annotate("expect.min", { minValue: s.minimum });
|
|
1846
|
+
if (typeof s.maximum === "number") if (s.exclusiveMaximum === true) handle.annotate("expect.exclusiveMax", { exclusiveMaxValue: s.maximum });
|
|
1847
|
+
else handle.annotate("expect.max", { maxValue: s.maximum });
|
|
1848
|
+
if (typeof s.exclusiveMinimum === "number") handle.annotate("expect.exclusiveMin", { exclusiveMinValue: s.exclusiveMinimum });
|
|
1849
|
+
if (typeof s.exclusiveMaximum === "number") handle.annotate("expect.exclusiveMax", { exclusiveMaxValue: s.exclusiveMaximum });
|
|
1850
|
+
}
|
|
1851
|
+
/**
|
|
1397
1852
|
* Merges multiple annotated types into a combined schema map with shared `$defs`.
|
|
1398
1853
|
*
|
|
1399
1854
|
* Each type must have an `id`. The returned `schemas` object contains individual
|