@atscript/typescript 0.1.109 → 0.1.110
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 +1587 -189
- package/dist/index.cjs +5 -2
- package/dist/index.d.ts +53 -1
- package/dist/index.mjs +3 -3
- package/dist/{json-schema-CioBVi6F.mjs → json-schema-B3iAqkGQ.cjs} +919 -37
- package/dist/{json-schema-9OBvrg9D.cjs → json-schema-BG1SEcjI.mjs} +618 -120
- package/dist/{plugin-D3_BnRrI.mjs → plugin-DpzE8aJr.mjs} +955 -116
- package/dist/{plugin-6r27w6rG.cjs → plugin-mzTvme4k.cjs} +966 -115
- package/dist/test-utils.cjs +4 -4
- package/dist/test-utils.d.ts +53 -0
- package/dist/test-utils.mjs +4 -4
- package/dist/utils.cjs +14 -26
- package/dist/utils.d.ts +81 -2
- package/dist/utils.mjs +13 -27
- package/package.json +3 -3
|
@@ -1,3 +1,545 @@
|
|
|
1
|
+
|
|
2
|
+
//#region packages/typescript/src/runtime/validator-keys.ts
|
|
3
|
+
/**
|
|
4
|
+
* Annotation keys the validator pre-check reads, per kind of checked value — shared by the
|
|
5
|
+
* interpreted pre-check (validator-fast-check.ts) and the compiled checks (compiled-check/).
|
|
6
|
+
*/ /** Annotations of a string node, in check order. */ const STRING_KEYS = [
|
|
7
|
+
"meta.required",
|
|
8
|
+
"expect.minLength",
|
|
9
|
+
"expect.maxLength",
|
|
10
|
+
"expect.pattern"
|
|
11
|
+
];
|
|
12
|
+
/** Annotations of a number node, in check order. */ const NUMBER_KEYS = [
|
|
13
|
+
"expect.int",
|
|
14
|
+
"expect.min",
|
|
15
|
+
"expect.max",
|
|
16
|
+
"expect.exclusiveMin",
|
|
17
|
+
"expect.exclusiveMax"
|
|
18
|
+
];
|
|
19
|
+
/** Annotations of a boolean node. */ const BOOLEAN_KEYS = ["meta.required"];
|
|
20
|
+
/** Annotations of an array node, in check order. */ const ARRAY_KEYS = [
|
|
21
|
+
"expect.minLength",
|
|
22
|
+
"expect.maxLength",
|
|
23
|
+
"expect.array.uniqueItems"
|
|
24
|
+
];
|
|
25
|
+
/**
|
|
26
|
+
* Every annotation key the validator pre-check reads. The order is the bit order of
|
|
27
|
+
* compiled-check layouts: changing it changes the layout format.
|
|
28
|
+
*/ const VALIDATOR_KEYS = [
|
|
29
|
+
"meta.required",
|
|
30
|
+
"expect.minLength",
|
|
31
|
+
"expect.maxLength",
|
|
32
|
+
"expect.pattern",
|
|
33
|
+
"expect.int",
|
|
34
|
+
"expect.min",
|
|
35
|
+
"expect.max",
|
|
36
|
+
"expect.exclusiveMin",
|
|
37
|
+
"expect.exclusiveMax",
|
|
38
|
+
"expect.array.uniqueItems"
|
|
39
|
+
];
|
|
40
|
+
/**
|
|
41
|
+
* The number a length / bound annotation compares with: the annotation is either the number
|
|
42
|
+
* itself or an object carrying it in `field` (`{ length }`, `{ minValue }`, …).
|
|
43
|
+
*/ function boundOf(m, field) {
|
|
44
|
+
return typeof m === "number" ? m : m[field];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
//#endregion
|
|
48
|
+
//#region packages/typescript/src/runtime/compiled-check/tracking.ts
|
|
49
|
+
/**
|
|
50
|
+
* Mutation tracking for build-time compiled validator checks.
|
|
51
|
+
*
|
|
52
|
+
* A compiled check is bound to the live type tree before use (see registry.ts): the bind
|
|
53
|
+
* verifies that the tree still has the structure the check was compiled for and snapshots
|
|
54
|
+
* the constraint values the check reads. Every tracked mutation of an object a bind has
|
|
55
|
+
* read bumps one global epoch, which makes every compiled check re-bind on its next use.
|
|
56
|
+
*
|
|
57
|
+
* Tracked: `annotate()`, the metadata / props `Map` methods (`set` / `delete` / `clear`),
|
|
58
|
+
* the `defineAnnotatedType()` handle methods and `cloneRefProp()`. Raw property writes
|
|
59
|
+
* (`node.optional = …`, `type.items.push(…)`, editing a constraint object in place, …)
|
|
60
|
+
* cannot be seen: call {@link invalidateCompiledValidators} after them.
|
|
61
|
+
*/ const VALIDATOR_KEY_SET = new Set(VALIDATOR_KEYS);
|
|
62
|
+
/** Global mutation epoch: compiled checks bound in an older epoch re-bind before use. */ let epoch = 0;
|
|
63
|
+
/** Some bind has watched an object (until then no mutation can affect a compiled check). */ let tracking = false;
|
|
64
|
+
/** Objects (nodes, type defs, metadata / props maps) read by some compiled-check bind. */ const watched = /* @__PURE__ */ new WeakSet();
|
|
65
|
+
/** The current mutation epoch. */ function currentEpoch() {
|
|
66
|
+
return epoch;
|
|
67
|
+
}
|
|
68
|
+
/** Marks `obj` as read by a bind: tracked mutations of it invalidate compiled checks. */ function watch(obj) {
|
|
69
|
+
tracking = true;
|
|
70
|
+
watched.add(obj);
|
|
71
|
+
}
|
|
72
|
+
/** Records a tracked mutation of `obj` (a no-op unless some bind has read it). */ function touched(obj) {
|
|
73
|
+
if (tracking && watched.has(obj)) epoch++;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Makes every compiled validator check re-verify the type tree it was compiled for before
|
|
77
|
+
* its next use.
|
|
78
|
+
*
|
|
79
|
+
* Compiled checks (emitted with `tsPlugin({ compileValidators: true })`) see mutations made
|
|
80
|
+
* through `annotate()`, the metadata / props `Map` methods, the `defineAnnotatedType()`
|
|
81
|
+
* handle methods and `cloneRefProp()` automatically. Call this after changing a type tree
|
|
82
|
+
* any other way:
|
|
83
|
+
* - a raw property write: `node.optional = …`, `node.type = …`, `node.metadata = …`,
|
|
84
|
+
* `type.items.push(…)`, `type.of = …`, `type.propsPatterns.push(…)`;
|
|
85
|
+
* - an in-place edit of an annotation value: mutating a constraint object
|
|
86
|
+
* (`metadata.get('expect.min').minValue = 5`), editing or pushing onto an
|
|
87
|
+
* `@expect.pattern` array directly (use `annotate(metadata, key, value, true)` instead).
|
|
88
|
+
*
|
|
89
|
+
* Without it the compiled check may keep validating against the old shape or values.
|
|
90
|
+
* Types without compiled checks are always read live and never need it.
|
|
91
|
+
*
|
|
92
|
+
* Any mutation of a type a compiled check has bound to (tracked or invalidated) makes every
|
|
93
|
+
* compiled check re-bind once on its next use: cheap during startup / configuration, but
|
|
94
|
+
* avoid mutating validated types per request.
|
|
95
|
+
*/ function invalidateCompiledValidators() {
|
|
96
|
+
epoch++;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Metadata map of runtime type nodes: a `Map` whose writes to validator annotation keys
|
|
100
|
+
* are tracked (see {@link invalidateCompiledValidators}). Reads are the builtin ones.
|
|
101
|
+
*/ var AnnotationMap = class extends Map {
|
|
102
|
+
set(key, value) {
|
|
103
|
+
super.set(key, value);
|
|
104
|
+
if (tracking && VALIDATOR_KEY_SET.has(key)) touched(this);
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
delete(key) {
|
|
108
|
+
const deleted = super.delete(key);
|
|
109
|
+
if (deleted && tracking && VALIDATOR_KEY_SET.has(key)) touched(this);
|
|
110
|
+
return deleted;
|
|
111
|
+
}
|
|
112
|
+
clear() {
|
|
113
|
+
super.clear();
|
|
114
|
+
touched(this);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
/** Props map of runtime object types: a `Map` whose writes are tracked. */ var PropsMap = class extends Map {
|
|
118
|
+
set(key, value) {
|
|
119
|
+
super.set(key, value);
|
|
120
|
+
touched(this);
|
|
121
|
+
return this;
|
|
122
|
+
}
|
|
123
|
+
delete(key) {
|
|
124
|
+
const deleted = super.delete(key);
|
|
125
|
+
if (deleted) touched(this);
|
|
126
|
+
return deleted;
|
|
127
|
+
}
|
|
128
|
+
clear() {
|
|
129
|
+
super.clear();
|
|
130
|
+
touched(this);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
/** `true` when `key` is an annotation the validator pre-check reads. */ function isValidatorKey(key) {
|
|
134
|
+
return VALIDATOR_KEY_SET.has(key);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region packages/typescript/src/runtime/compiled-check/layout.ts
|
|
139
|
+
/**
|
|
140
|
+
* Layout of a compiled validator check: the structure a check was compiled for, as plain
|
|
141
|
+
* data. `buildLayout` (build-layout.ts) derives it from a type tree (the build-time model
|
|
142
|
+
* of a generated module, or a live tree in tests); the binder (registry.ts) verifies a
|
|
143
|
+
* live tree against it and collects the constraint values the compiled code reads into
|
|
144
|
+
* the K slots.
|
|
145
|
+
*
|
|
146
|
+
* ```
|
|
147
|
+
* Layout = [defs: Def[], root: Site, slotCount]
|
|
148
|
+
* Site = [optional: 0|1, keyMask, def: number | EXT | INTERP, patternShape, slot]
|
|
149
|
+
* Def = [FINAL, designType] | [FINAL, designType, literal]
|
|
150
|
+
* | [OBJECT, keys, sites] | [OBJECT_PATTERNS]
|
|
151
|
+
* | [ARRAY, of] | [UNION | INTERSECTION | TUPLE, items] | [UNKNOWN, kind]
|
|
152
|
+
* ```
|
|
153
|
+
* - A site is one annotated-type node (a class root, prop, array element, union / tuple /
|
|
154
|
+
* intersection item). A def is one type-def object: sites sharing a def index share the
|
|
155
|
+
* def object (by identity) at runtime, which is how shared and recursive types are
|
|
156
|
+
* compiled once.
|
|
157
|
+
* - `keyMask` has a bit (by {@link VALIDATOR_KEYS} index) per validator annotation that is
|
|
158
|
+
* *effective* on the node and relevant to its def (present; flags: truthy).
|
|
159
|
+
* `patternShape` has one char per `@expect.pattern` entry: `e` (empty, skipped),
|
|
160
|
+
* `n` (checked), `s` (stateful `g` / `y` regex: the check bails to the walk there).
|
|
161
|
+
* - `slot`: the site's first K slot (see {@link siteSlotCount}); `slotCount` is the total.
|
|
162
|
+
* - `EXT`: an anonymous reference into another module's type; bound to that type's own
|
|
163
|
+
* compiled check (as a template, against the referring node) when it has one, else
|
|
164
|
+
* checked by the interpreted pre-check.
|
|
165
|
+
* - `INTERP`: a node only known at runtime (e.g. a chained reference into another module's
|
|
166
|
+
* type): always checked by the interpreted pre-check.
|
|
167
|
+
*/
|
|
168
|
+
/** Layout format version; checks of another version are ignored (interpreted). */ const LAYOUT_VERSION = 2;
|
|
169
|
+
const EXT = -1;
|
|
170
|
+
const INTERP = -2;
|
|
171
|
+
const D_FINAL = 0;
|
|
172
|
+
const D_OBJECT = 1;
|
|
173
|
+
const D_ARRAY = 2;
|
|
174
|
+
const D_UNION = 3;
|
|
175
|
+
const D_INTERSECTION = 4;
|
|
176
|
+
const D_TUPLE = 5;
|
|
177
|
+
const D_UNKNOWN = 6;
|
|
178
|
+
const D_OBJECT_PATTERNS = 7;
|
|
179
|
+
/** Def codes of the kinds with `items`, by kind name. */ const ITEMS_KIND_CODES = {
|
|
180
|
+
union: D_UNION,
|
|
181
|
+
intersection: D_INTERSECTION,
|
|
182
|
+
tuple: D_TUPLE
|
|
183
|
+
};
|
|
184
|
+
/** Kind names of the def codes with `items`. */ const ITEMS_KIND_NAMES = {
|
|
185
|
+
[D_UNION]: "union",
|
|
186
|
+
[D_INTERSECTION]: "intersection",
|
|
187
|
+
[D_TUPLE]: "tuple"
|
|
188
|
+
};
|
|
189
|
+
/** Bits of a compiled check's `flags` argument: the pre-check's TOP / IN_UNION, then options. */ const F_TOP = 1;
|
|
190
|
+
const F_IN_UNION = 2;
|
|
191
|
+
const F_ERROR = 4;
|
|
192
|
+
const F_STRIP = 8;
|
|
193
|
+
const F_PARTIAL_DEEP = 16;
|
|
194
|
+
const F_PARTIAL_TOP = 32;
|
|
195
|
+
/** Key mask bit of a validator annotation. */ function bit(key) {
|
|
196
|
+
return 1 << VALIDATOR_KEYS.indexOf(key);
|
|
197
|
+
}
|
|
198
|
+
function bitsOf(keys) {
|
|
199
|
+
let bits = 0;
|
|
200
|
+
for (const key of keys) bits |= bit(key);
|
|
201
|
+
return bits;
|
|
202
|
+
}
|
|
203
|
+
const B_REQUIRED = bit("meta.required");
|
|
204
|
+
const B_MIN_LENGTH = bit("expect.minLength");
|
|
205
|
+
const B_MAX_LENGTH = bit("expect.maxLength");
|
|
206
|
+
const B_PATTERN = bit("expect.pattern");
|
|
207
|
+
const B_INT = bit("expect.int");
|
|
208
|
+
const B_MIN = bit("expect.min");
|
|
209
|
+
const B_MAX = bit("expect.max");
|
|
210
|
+
const B_EX_MIN = bit("expect.exclusiveMin");
|
|
211
|
+
const B_EX_MAX = bit("expect.exclusiveMax");
|
|
212
|
+
const B_UNIQUE = bit("expect.array.uniqueItems");
|
|
213
|
+
/** Annotations the pre-check reads per kind of value (see validator-keys.ts). */ const STRING_BITS = bitsOf(STRING_KEYS);
|
|
214
|
+
const NUMBER_BITS = bitsOf(NUMBER_KEYS);
|
|
215
|
+
const ARRAY_BITS = bitsOf(ARRAY_KEYS);
|
|
216
|
+
const BOOLEAN_BITS = bitsOf(BOOLEAN_KEYS);
|
|
217
|
+
/** Keys whose value the compiled code reads (one K slot each). */ const VALUE_BITS = B_MIN_LENGTH | B_MAX_LENGTH | B_MIN | B_MAX | B_EX_MIN | B_EX_MAX;
|
|
218
|
+
const FLAG_BITS = B_REQUIRED | B_INT | B_UNIQUE;
|
|
219
|
+
const ALL_BITS = (1 << VALIDATOR_KEYS.length) - 1;
|
|
220
|
+
/** The field holding the bound of each value key's object form. */ const BOUND_FIELDS = {
|
|
221
|
+
[B_MIN_LENGTH]: "length",
|
|
222
|
+
[B_MAX_LENGTH]: "length",
|
|
223
|
+
[B_MIN]: "minValue",
|
|
224
|
+
[B_MAX]: "maxValue",
|
|
225
|
+
[B_EX_MIN]: "exclusiveMinValue",
|
|
226
|
+
[B_EX_MAX]: "exclusiveMaxValue"
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* Validator annotations the pre-check reads for a node of this type def (the rest of the
|
|
230
|
+
* node's annotations cannot change its outcome, so they are not part of the layout).
|
|
231
|
+
*/ function relevantBits(type) {
|
|
232
|
+
switch (type.kind) {
|
|
233
|
+
case "":
|
|
234
|
+
if (type.designType === "phantom" || type.value !== void 0) return 0;
|
|
235
|
+
switch (type.designType) {
|
|
236
|
+
case "string": return STRING_BITS;
|
|
237
|
+
case "number": return NUMBER_BITS;
|
|
238
|
+
case "boolean": return BOOLEAN_BITS;
|
|
239
|
+
default: return 0;
|
|
240
|
+
}
|
|
241
|
+
case "array": return ARRAY_BITS;
|
|
242
|
+
case "union": return ALL_BITS;
|
|
243
|
+
default: return 0;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* {@link relevantBits} of a site (`undefined`: a type only known at runtime);
|
|
248
|
+
* `meta.required` also matters on any optional node.
|
|
249
|
+
*/ function siteBits(type, optional) {
|
|
250
|
+
return (type === void 0 ? 0 : relevantBits(type)) | (optional ? B_REQUIRED : 0);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Number of K slots a site takes, from its first slot on:
|
|
254
|
+
* - INTERP: its interpreted check, then whether its type is a literal;
|
|
255
|
+
* - otherwise, for an optional `@meta.required` node ({@link hasRejectSlot}): the node
|
|
256
|
+
* (its `null` is rejected unless its type accepts `null`, decided live);
|
|
257
|
+
* - then EXT: the bound check of the referenced type, and whether that type is a literal;
|
|
258
|
+
* any other site: its annotation values (see {@link keySlots}).
|
|
259
|
+
*/ function siteSlotCount(site) {
|
|
260
|
+
const [, mask, def, shape] = site;
|
|
261
|
+
if (def === INTERP) return 2;
|
|
262
|
+
let n = (hasRejectSlot(site) ? 1 : 0) + (def === EXT ? 2 : 0);
|
|
263
|
+
if (def !== EXT) {
|
|
264
|
+
for (let bits = mask & VALUE_BITS; bits !== 0; bits &= bits - 1) n++;
|
|
265
|
+
for (const c of shape) if (c === "n") n++;
|
|
266
|
+
}
|
|
267
|
+
return n;
|
|
268
|
+
}
|
|
269
|
+
/** An optional node with `@meta.required`: `null` fails unless its type accepts `null`. */ function hasRejectSlot(site) {
|
|
270
|
+
return site[0] === 1 && (site[1] & B_REQUIRED) !== 0;
|
|
271
|
+
}
|
|
272
|
+
/** The K slot of each annotation value of a site whose values start at slot `at`. */ function keySlots(mask, shape, at) {
|
|
273
|
+
let k = at;
|
|
274
|
+
const next = (b) => (mask & b) === 0 ? -1 : k++;
|
|
275
|
+
const minLength = next(B_MIN_LENGTH);
|
|
276
|
+
const maxLength = next(B_MAX_LENGTH);
|
|
277
|
+
const patterns = [];
|
|
278
|
+
for (const c of shape) if (c === "n") patterns.push(k++);
|
|
279
|
+
return {
|
|
280
|
+
minLength,
|
|
281
|
+
maxLength,
|
|
282
|
+
patterns,
|
|
283
|
+
min: next(B_MIN),
|
|
284
|
+
max: next(B_MAX),
|
|
285
|
+
exMin: next(B_EX_MIN),
|
|
286
|
+
exMax: next(B_EX_MAX)
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
/** The child sites of a def. */ function childSites(def) {
|
|
290
|
+
switch (def[0]) {
|
|
291
|
+
case D_OBJECT: return def[2];
|
|
292
|
+
case D_ARRAY: return [def[1]];
|
|
293
|
+
case D_UNION:
|
|
294
|
+
case D_INTERSECTION:
|
|
295
|
+
case D_TUPLE: return def[1];
|
|
296
|
+
default: return [];
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Computes a node's key mask and pattern shape over `relevant` bits, and — when `out` is
|
|
301
|
+
* given — writes the normalised annotation values from `out[at]` on (the K slots).
|
|
302
|
+
* Throws on annotation values the pre-check itself would throw on (the bind then fails
|
|
303
|
+
* and the interpreted pre-check runs, which reproduces that behaviour).
|
|
304
|
+
*/ function readKeys(metadata, relevant, reader, out, at = 0) {
|
|
305
|
+
let mask = 0;
|
|
306
|
+
let shape = "";
|
|
307
|
+
let k = at;
|
|
308
|
+
if (metadata.size === 0) return {
|
|
309
|
+
mask,
|
|
310
|
+
shape
|
|
311
|
+
};
|
|
312
|
+
for (let i = 0; i < VALIDATOR_KEYS.length; i++) {
|
|
313
|
+
const b = 1 << i;
|
|
314
|
+
if ((relevant & b) === 0) continue;
|
|
315
|
+
const raw = metadata.get(VALIDATOR_KEYS[i]);
|
|
316
|
+
if ((b & FLAG_BITS) !== 0) {
|
|
317
|
+
if (raw) mask |= b;
|
|
318
|
+
continue;
|
|
319
|
+
}
|
|
320
|
+
if (b === B_PATTERN) {
|
|
321
|
+
if (!raw) continue;
|
|
322
|
+
if (!Array.isArray(raw)) throw new TypeError("expect.pattern is not an array");
|
|
323
|
+
mask |= b;
|
|
324
|
+
for (const entry of raw) {
|
|
325
|
+
const { pattern, flags } = entry;
|
|
326
|
+
if (!pattern) {
|
|
327
|
+
shape += "e";
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
const re = reader.regex(pattern, flags);
|
|
331
|
+
if (re.global || re.sticky) {
|
|
332
|
+
shape += "s";
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
shape += "n";
|
|
336
|
+
if (out) out[k] = re;
|
|
337
|
+
k++;
|
|
338
|
+
}
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
if (raw === void 0) continue;
|
|
342
|
+
mask |= b;
|
|
343
|
+
if (out) out[k] = boundOf(raw, BOUND_FIELDS[b]);
|
|
344
|
+
k++;
|
|
345
|
+
}
|
|
346
|
+
return {
|
|
347
|
+
mask,
|
|
348
|
+
shape
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
//#endregion
|
|
353
|
+
//#region packages/typescript/src/runtime/compiled-check/registry.ts
|
|
354
|
+
/**
|
|
355
|
+
* Runtime registry and binder of build-time compiled validator checks.
|
|
356
|
+
*
|
|
357
|
+
* Generated `.as.js` modules (built with `tsPlugin({ compileValidators: true })`) call
|
|
358
|
+
* {@link registerCompiledCheck} once per class with the class's layout and a factory of
|
|
359
|
+
* its check. On first use, and again after each mutation epoch (see tracking.ts), the
|
|
360
|
+
* entry is bound: `bindLayout` verifies the live type tree against the layout and collects
|
|
361
|
+
* the constraint values into `K`; the factory turns `K` into the check function. A tree
|
|
362
|
+
* that no longer matches leaves the entry unbound for that epoch, so the interpreted
|
|
363
|
+
* pre-check runs instead — always correct, never stale.
|
|
364
|
+
*
|
|
365
|
+
* A registered entry is also a template: a node referencing a class of another module
|
|
366
|
+
* (an `EXT` site) binds that class's layout against itself — its own annotations feed `K`
|
|
367
|
+
* — so cross-module references, aliases of imported types and cloned imported props run
|
|
368
|
+
* compiled code whenever their tree matches the referenced type's structure.
|
|
369
|
+
*
|
|
370
|
+
* The registry is module-local: classes registered with another copy of this runtime
|
|
371
|
+
* (dual ESM / CJS loads, duplicate installs) and subclasses of registered classes are
|
|
372
|
+
* simply validated by the interpreted pre-check.
|
|
373
|
+
*/
|
|
374
|
+
/** Entries by class (own registrations only: a subclass is not registered). */ const entries = /* @__PURE__ */ new WeakMap();
|
|
375
|
+
/** Template bindings by referring node. */ const templates = /* @__PURE__ */ new WeakMap();
|
|
376
|
+
/** Some compiled check is registered (until then lookups return at once). */ let anyCompiled = false;
|
|
377
|
+
/** The option bits of `opts` for a compiled check's `flags` argument. */ function optionFlags(opts) {
|
|
378
|
+
const unknownProps = opts.unknownProps;
|
|
379
|
+
const partial = opts.partial;
|
|
380
|
+
return (unknownProps === "error" ? F_ERROR : unknownProps === "strip" ? F_STRIP : 0) | (partial === "deep" ? F_PARTIAL_DEEP : partial === true ? F_PARTIAL_TOP : 0);
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Registers the build-time compiled validator check of a generated class (emitted into
|
|
384
|
+
* `.as.js` by `tsPlugin({ compileValidators: true })`; not meant to be called by hand).
|
|
385
|
+
* A layout of another format version is ignored, so the class stays interpreted.
|
|
386
|
+
*/ function registerCompiledCheck(cls, version, layout, factory) {
|
|
387
|
+
if (version !== LAYOUT_VERSION) return;
|
|
388
|
+
entries.set(cls, {
|
|
389
|
+
layout,
|
|
390
|
+
factory,
|
|
391
|
+
epoch: -1,
|
|
392
|
+
fn: null,
|
|
393
|
+
binding: false
|
|
394
|
+
});
|
|
395
|
+
anyCompiled = true;
|
|
396
|
+
}
|
|
397
|
+
let autoCompile = null;
|
|
398
|
+
/** The compiled check to run for `def` in this call, or `null` (interpreted). */ function compiledCheckFor(def, env) {
|
|
399
|
+
if (!anyCompiled) return null;
|
|
400
|
+
let entry = entries.get(def);
|
|
401
|
+
if (entry === void 0) {
|
|
402
|
+
if (autoCompile === null) return null;
|
|
403
|
+
autoCompile(def);
|
|
404
|
+
entry = entries.get(def);
|
|
405
|
+
if (entry === void 0) return null;
|
|
406
|
+
}
|
|
407
|
+
return entry.epoch === currentEpoch() ? entry.fn : bind(entry, entry, def, env, false);
|
|
408
|
+
}
|
|
409
|
+
/** (Re)binds `entry`'s layout against `node` for the current epoch. */ function bind(binding, entry, node, env, template) {
|
|
410
|
+
binding.epoch = currentEpoch();
|
|
411
|
+
binding.fn = null;
|
|
412
|
+
binding.binding = true;
|
|
413
|
+
try {
|
|
414
|
+
const K = bindLayout(entry.layout, node, env, template);
|
|
415
|
+
if (K !== void 0) binding.fn = entry.factory(K, env);
|
|
416
|
+
} catch {} finally {
|
|
417
|
+
binding.binding = false;
|
|
418
|
+
}
|
|
419
|
+
return binding.fn;
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Verifies the live tree of `node` against `layout` and collects the K slots; `undefined`
|
|
423
|
+
* when anything differs. A `template` bind (a node referring to the layout's class) leaves
|
|
424
|
+
* the root's `optional` to the referring site.
|
|
425
|
+
*/ function bindLayout(layout, node, env, template) {
|
|
426
|
+
const [defs, root, slotCount] = layout;
|
|
427
|
+
const bound = [];
|
|
428
|
+
const K = [];
|
|
429
|
+
K.length = slotCount;
|
|
430
|
+
/** An EXT / INTERP site: its check, then whether its type is a literal. */ const bindRuntimeSite = (n, at, ext) => {
|
|
431
|
+
const type = n.type;
|
|
432
|
+
if (typeof type !== "object" || type === null || isPhantomType(n)) return false;
|
|
433
|
+
watch(n);
|
|
434
|
+
watch(type);
|
|
435
|
+
K[at] = ext ? externalCheck(n, env) : interpreted(n, env);
|
|
436
|
+
K[at + 1] = type.value !== void 0;
|
|
437
|
+
return true;
|
|
438
|
+
};
|
|
439
|
+
const bindSite = (site, n) => {
|
|
440
|
+
const [optional, mask, d, shape, at] = site;
|
|
441
|
+
if (d === INTERP) return bindRuntimeSite(n, at, false);
|
|
442
|
+
if (template && site === root ? optional !== 0 : !!n.optional !== (optional === 1)) return false;
|
|
443
|
+
watch(n);
|
|
444
|
+
const metadata = n.metadata;
|
|
445
|
+
if (!(metadata instanceof AnnotationMap)) return false;
|
|
446
|
+
watch(metadata);
|
|
447
|
+
let k = at;
|
|
448
|
+
if (hasRejectSlot(site)) K[k++] = n;
|
|
449
|
+
if (d === EXT) return readKeys(metadata, siteBits(void 0, optional === 1), env).mask === mask && bindRuntimeSite(n, k, true);
|
|
450
|
+
const type = n.type;
|
|
451
|
+
if (bound[d] === void 0) {
|
|
452
|
+
if (typeof type !== "object" || type === null) return false;
|
|
453
|
+
bound[d] = type;
|
|
454
|
+
if (!bindDef(defs[d], type)) return false;
|
|
455
|
+
} else if (bound[d] !== type) return false;
|
|
456
|
+
const read = readKeys(metadata, siteBits(type, optional === 1), env, K, k);
|
|
457
|
+
return read.mask === mask && read.shape === shape;
|
|
458
|
+
};
|
|
459
|
+
const bindSites = (sites, nodes) => {
|
|
460
|
+
if (!Array.isArray(nodes) || nodes.length !== sites.length) return false;
|
|
461
|
+
for (let i = 0; i < sites.length; i++) if (!bindSite(sites[i], nodes[i])) return false;
|
|
462
|
+
return true;
|
|
463
|
+
};
|
|
464
|
+
const bindDef = (def, type) => {
|
|
465
|
+
watch(type);
|
|
466
|
+
switch (def[0]) {
|
|
467
|
+
case D_FINAL: return type.kind === "" && type.designType === def[1] && (def.length === 2 ? type.value === void 0 : type.value === def[2]);
|
|
468
|
+
case D_OBJECT: {
|
|
469
|
+
const props = type.props;
|
|
470
|
+
if (type.kind !== "object" || !(props instanceof PropsMap) || !Array.isArray(type.propsPatterns) || type.propsPatterns.length > 0) return false;
|
|
471
|
+
watch(props);
|
|
472
|
+
const keys = def[1];
|
|
473
|
+
const sites = def[2];
|
|
474
|
+
if (props.size !== keys.length) return false;
|
|
475
|
+
let i = 0;
|
|
476
|
+
for (const [key, prop] of props) {
|
|
477
|
+
if (key !== keys[i] || !bindSite(sites[i], prop)) return false;
|
|
478
|
+
i++;
|
|
479
|
+
}
|
|
480
|
+
return true;
|
|
481
|
+
}
|
|
482
|
+
case D_OBJECT_PATTERNS: return type.kind === "object" && Array.isArray(type.propsPatterns) && type.propsPatterns.length > 0;
|
|
483
|
+
case D_ARRAY: return type.kind === "array" && bindSite(def[1], type.of);
|
|
484
|
+
case D_UNION:
|
|
485
|
+
case D_INTERSECTION:
|
|
486
|
+
case D_TUPLE: return type.kind === ITEMS_KIND_NAMES[def[0]] && bindSites(def[1], type.items);
|
|
487
|
+
case D_UNKNOWN: return type.kind === def[1];
|
|
488
|
+
default: return false;
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
return bindSite(root, node) ? K : void 0;
|
|
492
|
+
}
|
|
493
|
+
/** The interpreted pre-check of `node` (its subtree is read live on every call). */ function interpreted(node, env) {
|
|
494
|
+
const interp = env.interp;
|
|
495
|
+
return (value, flags, opts) => interp(node, value, flags, opts);
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* The check of a node referencing another module's type (called for a non-nullish value
|
|
499
|
+
* when the node is optional; the referring site handles `optional`): the referenced class's
|
|
500
|
+
* layout bound against the node when it matches, else the interpreted pre-check.
|
|
501
|
+
*/ function externalCheck(node, env) {
|
|
502
|
+
const entry = templateOf(node);
|
|
503
|
+
if (entry !== void 0) {
|
|
504
|
+
let binding = templates.get(node);
|
|
505
|
+
if (binding === void 0 || binding.entry !== entry) {
|
|
506
|
+
binding = {
|
|
507
|
+
entry,
|
|
508
|
+
epoch: -1,
|
|
509
|
+
fn: null,
|
|
510
|
+
binding: false
|
|
511
|
+
};
|
|
512
|
+
templates.set(node, binding);
|
|
513
|
+
}
|
|
514
|
+
if (binding.binding) {
|
|
515
|
+
const pending = binding;
|
|
516
|
+
const fallback = interpreted(node, env);
|
|
517
|
+
return (value, flags, opts) => (pending.fn ?? fallback)(value, flags, opts);
|
|
518
|
+
}
|
|
519
|
+
const fn = binding.epoch === currentEpoch() ? binding.fn : bind(binding, entry, node, env, true);
|
|
520
|
+
if (fn !== null) return fn;
|
|
521
|
+
}
|
|
522
|
+
return interpreted(node, env);
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* The registered class whose layout a referring node binds against: the target of its
|
|
526
|
+
* (unchained) reference, through aliases of other modules' types.
|
|
527
|
+
*/ function templateOf(node) {
|
|
528
|
+
let ref = node.ref;
|
|
529
|
+
for (let hops = 0; hops < 16 && ref !== void 0 && ref.field === ""; hops++) {
|
|
530
|
+
let target;
|
|
531
|
+
try {
|
|
532
|
+
target = ref.type();
|
|
533
|
+
} catch {
|
|
534
|
+
return;
|
|
535
|
+
}
|
|
536
|
+
const entry = entries.get(target);
|
|
537
|
+
if (entry === void 0 || entry.layout[1][2] !== EXT) return entry;
|
|
538
|
+
ref = target.ref;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
//#endregion
|
|
1
543
|
//#region packages/typescript/src/runtime/validator-fast-check.ts
|
|
2
544
|
function _define_property$1(obj, key, value) {
|
|
3
545
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
@@ -63,7 +605,7 @@ const BAIL = 2;
|
|
|
63
605
|
if (opts.unknownProps === "strip") return fastCheckStripping(def, value, opts);
|
|
64
606
|
stamp++;
|
|
65
607
|
try {
|
|
66
|
-
return
|
|
608
|
+
return check(def, value, opts) === PASS;
|
|
67
609
|
} catch {
|
|
68
610
|
return false;
|
|
69
611
|
}
|
|
@@ -74,7 +616,7 @@ const BAIL = 2;
|
|
|
74
616
|
stripping = true;
|
|
75
617
|
let passed;
|
|
76
618
|
try {
|
|
77
|
-
passed =
|
|
619
|
+
passed = check(def, value, opts) === PASS;
|
|
78
620
|
} catch {
|
|
79
621
|
passed = false;
|
|
80
622
|
} finally {
|
|
@@ -87,6 +629,14 @@ const BAIL = 2;
|
|
|
87
629
|
}
|
|
88
630
|
return passed;
|
|
89
631
|
}
|
|
632
|
+
/**
|
|
633
|
+
* The pre-check of the root: the type's build-time compiled check when it has one bound to
|
|
634
|
+
* the live tree (see compiled-check/registry.ts), else the interpreted one. Both share the
|
|
635
|
+
* contract above and the strip state.
|
|
636
|
+
*/ function check(def, value, opts) {
|
|
637
|
+
const compiled = compiledCheckFor(def, COMPILED_ENV);
|
|
638
|
+
return compiled === null ? fastCheck(def, value, TOP, opts) : compiled(value, TOP | optionFlags(opts), opts);
|
|
639
|
+
}
|
|
90
640
|
const DECIMAL_RE = /^[+-]?\d+(\.\d+)?$/;
|
|
91
641
|
const regexCache = /* @__PURE__ */ new Map();
|
|
92
642
|
/** Compiled `@expect.pattern` regex, cached per (pattern, flags). */ function getRegex(pattern, flags) {
|
|
@@ -120,6 +670,14 @@ function acceptsNull(def) {
|
|
|
120
670
|
}
|
|
121
671
|
return false;
|
|
122
672
|
}
|
|
673
|
+
/** What compiled checks share with the interpreted pre-check. */ const COMPILED_ENV = {
|
|
674
|
+
pk: pendingKeys,
|
|
675
|
+
ho: Object.prototype.hasOwnProperty,
|
|
676
|
+
dec: DECIMAL_RE,
|
|
677
|
+
regex: getRegex,
|
|
678
|
+
interp: fastCheck,
|
|
679
|
+
rn: rejectsNullAsRequired
|
|
680
|
+
};
|
|
123
681
|
function fastCheck(def, value, flags, opts) {
|
|
124
682
|
if (def.optional && (value === void 0 || value === null)) return value === null && rejectsNullAsRequired(def) ? FAIL : PASS;
|
|
125
683
|
const type = def.type;
|
|
@@ -468,14 +1026,14 @@ function setMetaKeys(plan, k0, k1, k2, k3, k4) {
|
|
|
468
1026
|
if (type.designType === "phantom") plan.op = OP_PHANTOM;
|
|
469
1027
|
else if (type.value !== void 0) plan.op = OP_FINAL;
|
|
470
1028
|
else if (type.designType === "string") {
|
|
471
|
-
setMetaKeys(plan,
|
|
1029
|
+
setMetaKeys(plan, ...STRING_KEYS);
|
|
472
1030
|
plan.regexes = [];
|
|
473
1031
|
plan.op = OP_STRING;
|
|
474
1032
|
} else if (type.designType === "number") {
|
|
475
|
-
setMetaKeys(plan,
|
|
1033
|
+
setMetaKeys(plan, ...NUMBER_KEYS);
|
|
476
1034
|
plan.op = OP_NUMBER;
|
|
477
1035
|
} else if (type.designType === "boolean") {
|
|
478
|
-
setMetaKeys(plan,
|
|
1036
|
+
setMetaKeys(plan, ...BOOLEAN_KEYS);
|
|
479
1037
|
plan.op = OP_BOOLEAN;
|
|
480
1038
|
} else plan.op = OP_FINAL;
|
|
481
1039
|
break;
|
|
@@ -501,7 +1059,7 @@ function setMetaKeys(plan, k0, k1, k2, k3, k4) {
|
|
|
501
1059
|
break;
|
|
502
1060
|
}
|
|
503
1061
|
case "array":
|
|
504
|
-
setMetaKeys(plan,
|
|
1062
|
+
setMetaKeys(plan, ...ARRAY_KEYS);
|
|
505
1063
|
plan.of = type.of;
|
|
506
1064
|
plan.ofPlan = planFor(type.of);
|
|
507
1065
|
plan.op = OP_ARRAY;
|
|
@@ -1211,7 +1769,12 @@ const NON_PRIMITIVE_KINDS = new Set(["array", "object"]);
|
|
|
1211
1769
|
/**
|
|
1212
1770
|
* Creates a minimal annotated type node from a type def and metadata map.
|
|
1213
1771
|
* Centralises the shape so callers never duplicate the boilerplate.
|
|
1214
|
-
|
|
1772
|
+
*
|
|
1773
|
+
* `metadata` is used as-is (the node keeps the caller's map, so later writes through it
|
|
1774
|
+
* apply). When omitted, the node gets a new, tracked metadata map. Compiled validator
|
|
1775
|
+
* checks only bind to nodes whose metadata is tracked; nodes built over a caller-supplied
|
|
1776
|
+
* `Map` are always validated by the interpreted validator.
|
|
1777
|
+
*/ function createAnnotatedTypeNode(type, metadata = new AnnotationMap(), opts) {
|
|
1215
1778
|
return {
|
|
1216
1779
|
__is_atscript_annotated_type: true,
|
|
1217
1780
|
type,
|
|
@@ -1222,6 +1785,26 @@ const NON_PRIMITIVE_KINDS = new Set(["array", "object"]);
|
|
|
1222
1785
|
ref: opts?.ref
|
|
1223
1786
|
};
|
|
1224
1787
|
}
|
|
1788
|
+
/** A new tracked metadata map holding `entries` (see {@link createAnnotatedTypeNode}). */ function createMetadataMap(entries) {
|
|
1789
|
+
return new AnnotationMap(entries);
|
|
1790
|
+
}
|
|
1791
|
+
/**
|
|
1792
|
+
* The metadata map `defineAnnotatedType()` installs on `base`: `base.metadata` itself, except
|
|
1793
|
+
* that an absent map or an empty plain `Map` (what a generated class starts with:
|
|
1794
|
+
* `static metadata = new Map()`) is replaced by a new tracked map, so the node can bind to a
|
|
1795
|
+
* compiled validator check. A non-empty map or a `Map` subclass is always kept as-is (its
|
|
1796
|
+
* identity and overrides preserved; such a node stays interpreted).
|
|
1797
|
+
*/ function baseMetadata(metadata) {
|
|
1798
|
+
return !metadata || Object.getPrototypeOf(metadata) === Map.prototype && metadata.size === 0 ? createMetadataMap() : metadata;
|
|
1799
|
+
}
|
|
1800
|
+
/** A new object type def (its props map is tracked, see {@link createAnnotatedTypeNode}). */ function createObjectTypeDef(tags = /* @__PURE__ */ new Set(), propsPatterns = []) {
|
|
1801
|
+
return {
|
|
1802
|
+
kind: "object",
|
|
1803
|
+
props: new PropsMap(),
|
|
1804
|
+
propsPatterns,
|
|
1805
|
+
tags
|
|
1806
|
+
};
|
|
1807
|
+
}
|
|
1225
1808
|
/**
|
|
1226
1809
|
* Type Guard to check if a type is atscript-annotated
|
|
1227
1810
|
*/ function isAnnotatedType(type) {
|
|
@@ -1234,8 +1817,10 @@ const NON_PRIMITIVE_KINDS = new Set(["array", "object"]);
|
|
|
1234
1817
|
if (!metadata) return;
|
|
1235
1818
|
if (asArray) if (metadata.has(key)) {
|
|
1236
1819
|
const a = metadata.get(key);
|
|
1237
|
-
if (Array.isArray(a))
|
|
1238
|
-
|
|
1820
|
+
if (Array.isArray(a)) {
|
|
1821
|
+
a.push(value);
|
|
1822
|
+
if (isValidatorKey(key)) touched(metadata);
|
|
1823
|
+
} else metadata.set(key, [a, value]);
|
|
1239
1824
|
} else metadata.set(key, [value]);
|
|
1240
1825
|
else metadata.set(key, value);
|
|
1241
1826
|
}
|
|
@@ -1247,28 +1832,21 @@ const NON_PRIMITIVE_KINDS = new Set(["array", "object"]);
|
|
|
1247
1832
|
const objType = parentType;
|
|
1248
1833
|
const existing = objType.props.get(propName);
|
|
1249
1834
|
if (!existing) return;
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1835
|
+
objType.props.set(propName, cloneNode(existing, cloneTypeDef(existing.type)));
|
|
1836
|
+
}
|
|
1837
|
+
/** A copy of `node` (own metadata map) over `type`. */ function cloneNode(node, type) {
|
|
1838
|
+
return createAnnotatedTypeNode(type, createMetadataMap(node.metadata), {
|
|
1839
|
+
id: node.id,
|
|
1840
|
+
optional: node.optional,
|
|
1841
|
+
ref: node.ref
|
|
1842
|
+
});
|
|
1256
1843
|
}
|
|
1257
1844
|
function cloneTypeDef(type) {
|
|
1258
1845
|
if (type.kind === "object") {
|
|
1259
1846
|
const obj = type;
|
|
1260
|
-
const
|
|
1261
|
-
for (const [k, v] of obj.props) props.set(k,
|
|
1262
|
-
|
|
1263
|
-
optional: v.optional,
|
|
1264
|
-
ref: v.ref
|
|
1265
|
-
}));
|
|
1266
|
-
return {
|
|
1267
|
-
kind: "object",
|
|
1268
|
-
props,
|
|
1269
|
-
propsPatterns: [...obj.propsPatterns],
|
|
1270
|
-
tags: new Set(obj.tags)
|
|
1271
|
-
};
|
|
1847
|
+
const clone = createObjectTypeDef(new Set(obj.tags), [...obj.propsPatterns]);
|
|
1848
|
+
for (const [k, v] of obj.props) clone.props.set(k, cloneNode(v, v.type));
|
|
1849
|
+
return clone;
|
|
1272
1850
|
}
|
|
1273
1851
|
if (type.kind === "array") {
|
|
1274
1852
|
const arr = type;
|
|
@@ -1313,14 +1891,15 @@ function cloneTypeDef(type) {
|
|
|
1313
1891
|
const kind = _kind || "";
|
|
1314
1892
|
const baseProvided = base !== void 0;
|
|
1315
1893
|
const type = base?.type || {};
|
|
1894
|
+
touched(type);
|
|
1316
1895
|
type.kind = kind;
|
|
1317
1896
|
if (COMPLEX_KINDS.has(kind)) type.items = [];
|
|
1318
1897
|
if (kind === "object") {
|
|
1319
|
-
type.props =
|
|
1898
|
+
type.props = new PropsMap();
|
|
1320
1899
|
type.propsPatterns = [];
|
|
1321
1900
|
}
|
|
1322
1901
|
type.tags = /* @__PURE__ */ new Set();
|
|
1323
|
-
const metadata = base?.metadata
|
|
1902
|
+
const metadata = baseMetadata(base?.metadata);
|
|
1324
1903
|
const payload = {
|
|
1325
1904
|
__is_atscript_annotated_type: true,
|
|
1326
1905
|
metadata,
|
|
@@ -1338,18 +1917,22 @@ function cloneTypeDef(type) {
|
|
|
1338
1917
|
},
|
|
1339
1918
|
designType(value) {
|
|
1340
1919
|
this.$def.designType = value;
|
|
1920
|
+
touched(this.$def);
|
|
1341
1921
|
return this;
|
|
1342
1922
|
},
|
|
1343
1923
|
value(value) {
|
|
1344
1924
|
this.$def.value = value;
|
|
1925
|
+
touched(this.$def);
|
|
1345
1926
|
return this;
|
|
1346
1927
|
},
|
|
1347
1928
|
of(value) {
|
|
1348
1929
|
this.$def.of = value;
|
|
1930
|
+
touched(this.$def);
|
|
1349
1931
|
return this;
|
|
1350
1932
|
},
|
|
1351
1933
|
item(value) {
|
|
1352
1934
|
this.$def.items.push(value);
|
|
1935
|
+
touched(this.$def);
|
|
1353
1936
|
return this;
|
|
1354
1937
|
},
|
|
1355
1938
|
prop(name, value) {
|
|
@@ -1361,10 +1944,12 @@ function cloneTypeDef(type) {
|
|
|
1361
1944
|
pattern,
|
|
1362
1945
|
def
|
|
1363
1946
|
});
|
|
1947
|
+
touched(this.$def);
|
|
1364
1948
|
return this;
|
|
1365
1949
|
},
|
|
1366
1950
|
optional(value = true) {
|
|
1367
1951
|
this.$type.optional = value;
|
|
1952
|
+
touched(this.$type);
|
|
1368
1953
|
return this;
|
|
1369
1954
|
},
|
|
1370
1955
|
copyMetadata(fromMetadata, ignore) {
|
|
@@ -1373,6 +1958,7 @@ function cloneTypeDef(type) {
|
|
|
1373
1958
|
},
|
|
1374
1959
|
refTo(type, chain) {
|
|
1375
1960
|
const node = this.$type;
|
|
1961
|
+
touched(node);
|
|
1376
1962
|
const field = chain ? chain.join(".") : "";
|
|
1377
1963
|
if (isAnnotatedType(type)) {
|
|
1378
1964
|
let newBase = type;
|
|
@@ -1644,18 +2230,18 @@ function cloneTypeDef(type) {
|
|
|
1644
2230
|
}
|
|
1645
2231
|
/** `@expect.minLength` / `maxLength` → `minItems` / `maxItems` on an array schema. */ function applyArrayConstraints(schema, meta) {
|
|
1646
2232
|
const minLength = meta.get("expect.minLength");
|
|
1647
|
-
if (minLength !== void 0) schema.minItems =
|
|
2233
|
+
if (minLength !== void 0) schema.minItems = boundOf(minLength, "length");
|
|
1648
2234
|
const maxLength = meta.get("expect.maxLength");
|
|
1649
|
-
if (maxLength !== void 0) schema.maxItems =
|
|
2235
|
+
if (maxLength !== void 0) schema.maxItems = boundOf(maxLength, "length");
|
|
1650
2236
|
}
|
|
1651
2237
|
/** String / number constraints on a scalar schema whose `type` is already set. */ function applyScalarConstraints(schema, meta) {
|
|
1652
2238
|
if (schema.type === "number" && meta.get("expect.int")) schema.type = "integer";
|
|
1653
2239
|
if (schema.type === "string") {
|
|
1654
2240
|
if (meta.get("meta.required")) schema.minLength = 1;
|
|
1655
2241
|
const minLength = meta.get("expect.minLength");
|
|
1656
|
-
if (minLength !== void 0) schema.minLength =
|
|
2242
|
+
if (minLength !== void 0) schema.minLength = boundOf(minLength, "length");
|
|
1657
2243
|
const maxLength = meta.get("expect.maxLength");
|
|
1658
|
-
if (maxLength !== void 0) schema.maxLength =
|
|
2244
|
+
if (maxLength !== void 0) schema.maxLength = boundOf(maxLength, "length");
|
|
1659
2245
|
const patterns = meta.get("expect.pattern");
|
|
1660
2246
|
if (patterns?.length) if (patterns.length === 1) schema.pattern = patterns[0].pattern;
|
|
1661
2247
|
else schema.allOf = (schema.allOf || []).concat(patterns.map((p) => ({ pattern: p.pattern })));
|
|
@@ -1671,9 +2257,6 @@ function cloneTypeDef(type) {
|
|
|
1671
2257
|
if (exMax !== void 0) schema.exclusiveMaximum = boundOf(exMax, "exclusiveMaxValue");
|
|
1672
2258
|
}
|
|
1673
2259
|
}
|
|
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
|
-
}
|
|
1677
2260
|
const LOWER_BOUNDS = new Set([
|
|
1678
2261
|
"minimum",
|
|
1679
2262
|
"exclusiveMinimum",
|
|
@@ -1877,4 +2460,303 @@ const UPPER_BOUNDS = new Set([
|
|
|
1877
2460
|
}
|
|
1878
2461
|
|
|
1879
2462
|
//#endregion
|
|
1880
|
-
|
|
2463
|
+
Object.defineProperty(exports, 'ARRAY_BITS', {
|
|
2464
|
+
enumerable: true,
|
|
2465
|
+
get: function () {
|
|
2466
|
+
return ARRAY_BITS;
|
|
2467
|
+
}
|
|
2468
|
+
});
|
|
2469
|
+
Object.defineProperty(exports, 'B_INT', {
|
|
2470
|
+
enumerable: true,
|
|
2471
|
+
get: function () {
|
|
2472
|
+
return B_INT;
|
|
2473
|
+
}
|
|
2474
|
+
});
|
|
2475
|
+
Object.defineProperty(exports, 'B_REQUIRED', {
|
|
2476
|
+
enumerable: true,
|
|
2477
|
+
get: function () {
|
|
2478
|
+
return B_REQUIRED;
|
|
2479
|
+
}
|
|
2480
|
+
});
|
|
2481
|
+
Object.defineProperty(exports, 'B_UNIQUE', {
|
|
2482
|
+
enumerable: true,
|
|
2483
|
+
get: function () {
|
|
2484
|
+
return B_UNIQUE;
|
|
2485
|
+
}
|
|
2486
|
+
});
|
|
2487
|
+
Object.defineProperty(exports, 'D_ARRAY', {
|
|
2488
|
+
enumerable: true,
|
|
2489
|
+
get: function () {
|
|
2490
|
+
return D_ARRAY;
|
|
2491
|
+
}
|
|
2492
|
+
});
|
|
2493
|
+
Object.defineProperty(exports, 'D_FINAL', {
|
|
2494
|
+
enumerable: true,
|
|
2495
|
+
get: function () {
|
|
2496
|
+
return D_FINAL;
|
|
2497
|
+
}
|
|
2498
|
+
});
|
|
2499
|
+
Object.defineProperty(exports, 'D_INTERSECTION', {
|
|
2500
|
+
enumerable: true,
|
|
2501
|
+
get: function () {
|
|
2502
|
+
return D_INTERSECTION;
|
|
2503
|
+
}
|
|
2504
|
+
});
|
|
2505
|
+
Object.defineProperty(exports, 'D_OBJECT', {
|
|
2506
|
+
enumerable: true,
|
|
2507
|
+
get: function () {
|
|
2508
|
+
return D_OBJECT;
|
|
2509
|
+
}
|
|
2510
|
+
});
|
|
2511
|
+
Object.defineProperty(exports, 'D_OBJECT_PATTERNS', {
|
|
2512
|
+
enumerable: true,
|
|
2513
|
+
get: function () {
|
|
2514
|
+
return D_OBJECT_PATTERNS;
|
|
2515
|
+
}
|
|
2516
|
+
});
|
|
2517
|
+
Object.defineProperty(exports, 'D_TUPLE', {
|
|
2518
|
+
enumerable: true,
|
|
2519
|
+
get: function () {
|
|
2520
|
+
return D_TUPLE;
|
|
2521
|
+
}
|
|
2522
|
+
});
|
|
2523
|
+
Object.defineProperty(exports, 'D_UNION', {
|
|
2524
|
+
enumerable: true,
|
|
2525
|
+
get: function () {
|
|
2526
|
+
return D_UNION;
|
|
2527
|
+
}
|
|
2528
|
+
});
|
|
2529
|
+
Object.defineProperty(exports, 'D_UNKNOWN', {
|
|
2530
|
+
enumerable: true,
|
|
2531
|
+
get: function () {
|
|
2532
|
+
return D_UNKNOWN;
|
|
2533
|
+
}
|
|
2534
|
+
});
|
|
2535
|
+
Object.defineProperty(exports, 'EXT', {
|
|
2536
|
+
enumerable: true,
|
|
2537
|
+
get: function () {
|
|
2538
|
+
return EXT;
|
|
2539
|
+
}
|
|
2540
|
+
});
|
|
2541
|
+
Object.defineProperty(exports, 'F_ERROR', {
|
|
2542
|
+
enumerable: true,
|
|
2543
|
+
get: function () {
|
|
2544
|
+
return F_ERROR;
|
|
2545
|
+
}
|
|
2546
|
+
});
|
|
2547
|
+
Object.defineProperty(exports, 'F_IN_UNION', {
|
|
2548
|
+
enumerable: true,
|
|
2549
|
+
get: function () {
|
|
2550
|
+
return F_IN_UNION;
|
|
2551
|
+
}
|
|
2552
|
+
});
|
|
2553
|
+
Object.defineProperty(exports, 'F_PARTIAL_DEEP', {
|
|
2554
|
+
enumerable: true,
|
|
2555
|
+
get: function () {
|
|
2556
|
+
return F_PARTIAL_DEEP;
|
|
2557
|
+
}
|
|
2558
|
+
});
|
|
2559
|
+
Object.defineProperty(exports, 'F_PARTIAL_TOP', {
|
|
2560
|
+
enumerable: true,
|
|
2561
|
+
get: function () {
|
|
2562
|
+
return F_PARTIAL_TOP;
|
|
2563
|
+
}
|
|
2564
|
+
});
|
|
2565
|
+
Object.defineProperty(exports, 'F_STRIP', {
|
|
2566
|
+
enumerable: true,
|
|
2567
|
+
get: function () {
|
|
2568
|
+
return F_STRIP;
|
|
2569
|
+
}
|
|
2570
|
+
});
|
|
2571
|
+
Object.defineProperty(exports, 'F_TOP', {
|
|
2572
|
+
enumerable: true,
|
|
2573
|
+
get: function () {
|
|
2574
|
+
return F_TOP;
|
|
2575
|
+
}
|
|
2576
|
+
});
|
|
2577
|
+
Object.defineProperty(exports, 'INTERP', {
|
|
2578
|
+
enumerable: true,
|
|
2579
|
+
get: function () {
|
|
2580
|
+
return INTERP;
|
|
2581
|
+
}
|
|
2582
|
+
});
|
|
2583
|
+
Object.defineProperty(exports, 'ITEMS_KIND_CODES', {
|
|
2584
|
+
enumerable: true,
|
|
2585
|
+
get: function () {
|
|
2586
|
+
return ITEMS_KIND_CODES;
|
|
2587
|
+
}
|
|
2588
|
+
});
|
|
2589
|
+
Object.defineProperty(exports, 'LAYOUT_VERSION', {
|
|
2590
|
+
enumerable: true,
|
|
2591
|
+
get: function () {
|
|
2592
|
+
return LAYOUT_VERSION;
|
|
2593
|
+
}
|
|
2594
|
+
});
|
|
2595
|
+
Object.defineProperty(exports, 'NUMBER_BITS', {
|
|
2596
|
+
enumerable: true,
|
|
2597
|
+
get: function () {
|
|
2598
|
+
return NUMBER_BITS;
|
|
2599
|
+
}
|
|
2600
|
+
});
|
|
2601
|
+
Object.defineProperty(exports, 'STRING_BITS', {
|
|
2602
|
+
enumerable: true,
|
|
2603
|
+
get: function () {
|
|
2604
|
+
return STRING_BITS;
|
|
2605
|
+
}
|
|
2606
|
+
});
|
|
2607
|
+
Object.defineProperty(exports, 'Validator', {
|
|
2608
|
+
enumerable: true,
|
|
2609
|
+
get: function () {
|
|
2610
|
+
return Validator;
|
|
2611
|
+
}
|
|
2612
|
+
});
|
|
2613
|
+
Object.defineProperty(exports, 'ValidatorError', {
|
|
2614
|
+
enumerable: true,
|
|
2615
|
+
get: function () {
|
|
2616
|
+
return ValidatorError;
|
|
2617
|
+
}
|
|
2618
|
+
});
|
|
2619
|
+
Object.defineProperty(exports, 'annotate', {
|
|
2620
|
+
enumerable: true,
|
|
2621
|
+
get: function () {
|
|
2622
|
+
return annotate;
|
|
2623
|
+
}
|
|
2624
|
+
});
|
|
2625
|
+
Object.defineProperty(exports, 'buildJsonSchema', {
|
|
2626
|
+
enumerable: true,
|
|
2627
|
+
get: function () {
|
|
2628
|
+
return buildJsonSchema;
|
|
2629
|
+
}
|
|
2630
|
+
});
|
|
2631
|
+
Object.defineProperty(exports, 'childSites', {
|
|
2632
|
+
enumerable: true,
|
|
2633
|
+
get: function () {
|
|
2634
|
+
return childSites;
|
|
2635
|
+
}
|
|
2636
|
+
});
|
|
2637
|
+
Object.defineProperty(exports, 'cloneRefProp', {
|
|
2638
|
+
enumerable: true,
|
|
2639
|
+
get: function () {
|
|
2640
|
+
return cloneRefProp;
|
|
2641
|
+
}
|
|
2642
|
+
});
|
|
2643
|
+
Object.defineProperty(exports, 'createAnnotatedTypeNode', {
|
|
2644
|
+
enumerable: true,
|
|
2645
|
+
get: function () {
|
|
2646
|
+
return createAnnotatedTypeNode;
|
|
2647
|
+
}
|
|
2648
|
+
});
|
|
2649
|
+
Object.defineProperty(exports, 'createMetadataMap', {
|
|
2650
|
+
enumerable: true,
|
|
2651
|
+
get: function () {
|
|
2652
|
+
return createMetadataMap;
|
|
2653
|
+
}
|
|
2654
|
+
});
|
|
2655
|
+
Object.defineProperty(exports, 'createObjectTypeDef', {
|
|
2656
|
+
enumerable: true,
|
|
2657
|
+
get: function () {
|
|
2658
|
+
return createObjectTypeDef;
|
|
2659
|
+
}
|
|
2660
|
+
});
|
|
2661
|
+
Object.defineProperty(exports, 'defineAnnotatedType', {
|
|
2662
|
+
enumerable: true,
|
|
2663
|
+
get: function () {
|
|
2664
|
+
return defineAnnotatedType;
|
|
2665
|
+
}
|
|
2666
|
+
});
|
|
2667
|
+
Object.defineProperty(exports, 'detectDiscriminator', {
|
|
2668
|
+
enumerable: true,
|
|
2669
|
+
get: function () {
|
|
2670
|
+
return detectDiscriminator;
|
|
2671
|
+
}
|
|
2672
|
+
});
|
|
2673
|
+
Object.defineProperty(exports, 'forAnnotatedType', {
|
|
2674
|
+
enumerable: true,
|
|
2675
|
+
get: function () {
|
|
2676
|
+
return forAnnotatedType;
|
|
2677
|
+
}
|
|
2678
|
+
});
|
|
2679
|
+
Object.defineProperty(exports, 'fromJsonSchema', {
|
|
2680
|
+
enumerable: true,
|
|
2681
|
+
get: function () {
|
|
2682
|
+
return fromJsonSchema;
|
|
2683
|
+
}
|
|
2684
|
+
});
|
|
2685
|
+
Object.defineProperty(exports, 'getRegex', {
|
|
2686
|
+
enumerable: true,
|
|
2687
|
+
get: function () {
|
|
2688
|
+
return getRegex;
|
|
2689
|
+
}
|
|
2690
|
+
});
|
|
2691
|
+
Object.defineProperty(exports, 'hasRejectSlot', {
|
|
2692
|
+
enumerable: true,
|
|
2693
|
+
get: function () {
|
|
2694
|
+
return hasRejectSlot;
|
|
2695
|
+
}
|
|
2696
|
+
});
|
|
2697
|
+
Object.defineProperty(exports, 'invalidateCompiledValidators', {
|
|
2698
|
+
enumerable: true,
|
|
2699
|
+
get: function () {
|
|
2700
|
+
return invalidateCompiledValidators;
|
|
2701
|
+
}
|
|
2702
|
+
});
|
|
2703
|
+
Object.defineProperty(exports, 'isAnnotatedType', {
|
|
2704
|
+
enumerable: true,
|
|
2705
|
+
get: function () {
|
|
2706
|
+
return isAnnotatedType;
|
|
2707
|
+
}
|
|
2708
|
+
});
|
|
2709
|
+
Object.defineProperty(exports, 'isAnnotatedTypeOfPrimitive', {
|
|
2710
|
+
enumerable: true,
|
|
2711
|
+
get: function () {
|
|
2712
|
+
return isAnnotatedTypeOfPrimitive;
|
|
2713
|
+
}
|
|
2714
|
+
});
|
|
2715
|
+
Object.defineProperty(exports, 'isPhantomType', {
|
|
2716
|
+
enumerable: true,
|
|
2717
|
+
get: function () {
|
|
2718
|
+
return isPhantomType;
|
|
2719
|
+
}
|
|
2720
|
+
});
|
|
2721
|
+
Object.defineProperty(exports, 'isValidatorKey', {
|
|
2722
|
+
enumerable: true,
|
|
2723
|
+
get: function () {
|
|
2724
|
+
return isValidatorKey;
|
|
2725
|
+
}
|
|
2726
|
+
});
|
|
2727
|
+
Object.defineProperty(exports, 'keySlots', {
|
|
2728
|
+
enumerable: true,
|
|
2729
|
+
get: function () {
|
|
2730
|
+
return keySlots;
|
|
2731
|
+
}
|
|
2732
|
+
});
|
|
2733
|
+
Object.defineProperty(exports, 'mergeJsonSchemas', {
|
|
2734
|
+
enumerable: true,
|
|
2735
|
+
get: function () {
|
|
2736
|
+
return mergeJsonSchemas;
|
|
2737
|
+
}
|
|
2738
|
+
});
|
|
2739
|
+
Object.defineProperty(exports, 'readKeys', {
|
|
2740
|
+
enumerable: true,
|
|
2741
|
+
get: function () {
|
|
2742
|
+
return readKeys;
|
|
2743
|
+
}
|
|
2744
|
+
});
|
|
2745
|
+
Object.defineProperty(exports, 'registerCompiledCheck', {
|
|
2746
|
+
enumerable: true,
|
|
2747
|
+
get: function () {
|
|
2748
|
+
return registerCompiledCheck;
|
|
2749
|
+
}
|
|
2750
|
+
});
|
|
2751
|
+
Object.defineProperty(exports, 'siteBits', {
|
|
2752
|
+
enumerable: true,
|
|
2753
|
+
get: function () {
|
|
2754
|
+
return siteBits;
|
|
2755
|
+
}
|
|
2756
|
+
});
|
|
2757
|
+
Object.defineProperty(exports, 'siteSlotCount', {
|
|
2758
|
+
enumerable: true,
|
|
2759
|
+
get: function () {
|
|
2760
|
+
return siteSlotCount;
|
|
2761
|
+
}
|
|
2762
|
+
});
|