@alexify/metaschema 3.3.0 → 3.4.1
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/CHANGELOG.md +251 -1
- package/README.md +31 -20
- package/SECURITY.md +24 -1
- package/index.d.ts +280 -48
- package/package.json +13 -3
- package/src/checks.js +789 -0
- package/src/codec.js +37 -13
- package/src/coerce/index.js +34 -1
- package/src/compile/emit.js +87 -28
- package/src/compile/index.js +9 -4
- package/src/definition/index.d.ts +19 -0
- package/src/definition/index.js +27 -0
- package/src/derive/index.js +23 -13
- package/src/errors.js +23 -0
- package/src/generators/fromjsonschema.js +14 -0
- package/src/generators/index.js +1 -1
- package/src/generators/jsonschema.js +44 -7
- package/src/generators/typescript.js +11 -0
- package/src/index.js +3 -2
- package/src/issues.js +9 -7
- package/src/kinds.js +24 -2
- package/src/lint.js +64 -2
- package/src/locales/en.js +6 -0
- package/src/locales/uk.js +6 -0
- package/src/metadata.js +2 -24
- package/src/model/data.js +68 -11
- package/src/model/diff.js +7 -4
- package/src/model/index.js +1 -1
- package/src/model/infer.js +13 -6
- package/src/model/merge.js +7 -3
- package/src/model/restore.js +60 -8
- package/src/model/sample.js +26 -1
- package/src/model.js +25 -53
- package/src/mongodb/bson.js +15 -3
- package/src/mongodb/document.js +10 -2
- package/src/preprocessor.js +14 -5
- package/src/prototypes/abstract.js +32 -64
- package/src/prototypes/collections.js +3 -90
- package/src/prototypes/flags.js +34 -0
- package/src/prototypes/json.js +0 -11
- package/src/prototypes/reference.js +0 -76
- package/src/prototypes/scalars.js +17 -96
- package/src/prototypes/schema.js +1 -10
- package/src/prototypes/tuple.js +5 -31
- package/src/prototypes/union.js +12 -82
- package/src/rules.js +33 -157
- package/src/schema.js +108 -233
- package/src/snapshot.js +216 -41
- package/src/stream/index.js +1 -1
- package/src/struct.js +6 -91
- package/src/types.js +31 -10
- package/src/util.js +48 -0
- package/src/validation.js +313 -0
package/src/checks.js
ADDED
|
@@ -0,0 +1,789 @@
|
|
|
1
|
+
// The checks of the types: the validation half of what a field is (its
|
|
2
|
+
// prototype in prototypes/ is the definition half). The type check of each
|
|
3
|
+
// built-in (`COMPILE`), the check of a field around it (its rules, its
|
|
4
|
+
// validate, null), of a custom type through its checkType, of a struct over
|
|
5
|
+
// its plan, and of the rules. validation.js installs them into the classes;
|
|
6
|
+
// the closures are built from a field on the first use of its `check`.
|
|
7
|
+
const { RUN, ENUM_SET_SIZE, isDate, isBytes, isMap, isSet } = require('./util.js');
|
|
8
|
+
const { issues, release, runValidate, runCheckType } = require('./issues.js');
|
|
9
|
+
const { formatOf } = require('./rules.js');
|
|
10
|
+
const { STRUCT, isStruct } = require('./struct.js');
|
|
11
|
+
const { embeds } = require('./prototypes/reference.js');
|
|
12
|
+
const { SchemaDefinitionError } = require('./errors.js');
|
|
13
|
+
|
|
14
|
+
// The rules of a field (`rules.js` reads and normalizes them when the field
|
|
15
|
+
// is built): one closure per rule over the field's bounds, run after the type
|
|
16
|
+
// check in the order the type lists them.
|
|
17
|
+
|
|
18
|
+
// Code points of a string: a surrogate pair counts once, a lone surrogate
|
|
19
|
+
// once too.
|
|
20
|
+
const codePoints = (text) => {
|
|
21
|
+
const { length } = text;
|
|
22
|
+
let count = 0;
|
|
23
|
+
for (let index = 0; index < length; index += 1) {
|
|
24
|
+
const unit = text.charCodeAt(index);
|
|
25
|
+
if (unit >= 0xd800 && unit <= 0xdbff && index + 1 < length) {
|
|
26
|
+
const next = text.charCodeAt(index + 1);
|
|
27
|
+
if (next >= 0xdc00 && next <= 0xdfff) index += 1;
|
|
28
|
+
}
|
|
29
|
+
count += 1;
|
|
30
|
+
}
|
|
31
|
+
return count;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// The own keys of an object, counted without a list of them.
|
|
35
|
+
const keyCount = (value) => {
|
|
36
|
+
let count = 0;
|
|
37
|
+
for (const name in value) if (Object.hasOwn(value, name)) count += 1;
|
|
38
|
+
return count;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// The length of a value, by what the value is rather than by what it carries
|
|
42
|
+
// (an object with a `size` key is an object): the units of a string, the
|
|
43
|
+
// length of an array, the size of a Set or a Map, the own keys of an object.
|
|
44
|
+
const lengthOf = (value) => {
|
|
45
|
+
if (typeof value === 'string' || Array.isArray(value)) return value.length;
|
|
46
|
+
return isMap(value) || isSet(value) ? value.size : keyCount(value);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// A string field measures its units, or its code points when it asks for
|
|
50
|
+
// them, a bytes field its bytes; anything else is measured by what it is.
|
|
51
|
+
// One closure per case, each calling its measure directly: a measure held in
|
|
52
|
+
// a variable is a call site shared by every length rule of the process,
|
|
53
|
+
// which V8 then cannot inline.
|
|
54
|
+
const compileLength = (type) => {
|
|
55
|
+
const { min, max } = type.length;
|
|
56
|
+
const { scalar } = type;
|
|
57
|
+
if (scalar === 'string' && type.unicode === true) {
|
|
58
|
+
return (value, context, key) => {
|
|
59
|
+
const actual = codePoints(value);
|
|
60
|
+
const short = min !== undefined && actual < min;
|
|
61
|
+
const long = max !== undefined && actual > max;
|
|
62
|
+
if (short || long) issues.length(context, min, max, actual, key);
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
if (scalar === 'string' || scalar === 'bytes') {
|
|
66
|
+
return (value, context, key) => {
|
|
67
|
+
const actual = value.length;
|
|
68
|
+
const short = min !== undefined && actual < min;
|
|
69
|
+
const long = max !== undefined && actual > max;
|
|
70
|
+
if (short || long) issues.length(context, min, max, actual, key);
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
return (value, context, key) => {
|
|
74
|
+
const actual = lengthOf(value);
|
|
75
|
+
const short = min !== undefined && actual < min;
|
|
76
|
+
const long = max !== undefined && actual > max;
|
|
77
|
+
if (short || long) issues.length(context, min, max, actual, key);
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// One closure for both bounds when a field has both.
|
|
82
|
+
const compileRange = (type) => {
|
|
83
|
+
const { min, max } = type;
|
|
84
|
+
if (min === undefined) {
|
|
85
|
+
return (value, context, key) => {
|
|
86
|
+
if (value > max) issues.range(context, min, max, value, key);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
if (max === undefined) {
|
|
90
|
+
return (value, context, key) => {
|
|
91
|
+
if (value < min) issues.range(context, min, max, value, key);
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
return (value, context, key) => {
|
|
95
|
+
if (value < min || value > max) issues.range(context, min, max, value, key);
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// The units a pattern may be run over: `length.max` (twice it when the
|
|
100
|
+
// length counts code points, each at most two units), so that a string past
|
|
101
|
+
// the bound, reported for its length already, never reaches a pattern that
|
|
102
|
+
// backtracks over it; no limit without the bound.
|
|
103
|
+
const patternLimit = (type) => {
|
|
104
|
+
const max = type.length?.max;
|
|
105
|
+
if (max === undefined) return Infinity;
|
|
106
|
+
return type.unicode === true ? 2 * max : max;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const compilePattern = (type) => {
|
|
110
|
+
const { pattern } = type;
|
|
111
|
+
const { source } = pattern;
|
|
112
|
+
const limit = patternLimit(type);
|
|
113
|
+
return (value, context, key) => {
|
|
114
|
+
if (value.length <= limit && !pattern.test(value)) issues.pattern(context, source, key);
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// A format is the test the subpath registered under its name, run after the
|
|
119
|
+
// pattern and under the same bound.
|
|
120
|
+
const compileFormat = (type) => {
|
|
121
|
+
const { format } = type;
|
|
122
|
+
const { test } = formatOf(type);
|
|
123
|
+
const limit = patternLimit(type);
|
|
124
|
+
return (value, context, key) => {
|
|
125
|
+
if (value.length <= limit && !test(value)) issues.format(context, format, key);
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// A number of `n` decimals is a whole number times 10ⁿ, to within the error
|
|
130
|
+
// the arithmetic of doubles leaves on the way (0.1 + 0.2 has two decimals):
|
|
131
|
+
// ULPS units in the last place of the scaled value. A few units is what a
|
|
132
|
+
// sum or a product of decimals leaves; 256 is room for a chain of them, and
|
|
133
|
+
// still tells a decimal too many apart below 2⁴³ units of the last decimal
|
|
134
|
+
// (ten billion at two), where a bound of 1e-9 of the value would let
|
|
135
|
+
// 12345678.123 pass as two decimals. Above it every double passes. NaN and
|
|
136
|
+
// the infinities have no decimals to count.
|
|
137
|
+
const ULPS = 256;
|
|
138
|
+
const NEAR = ULPS * Number.EPSILON;
|
|
139
|
+
|
|
140
|
+
// Whether a number has more decimals than `scale` (10ⁿ) allows: the one test
|
|
141
|
+
// of both backends (the JIT calls it through the scope).
|
|
142
|
+
const decimalsOff = (value, scale) => {
|
|
143
|
+
const scaled = value * scale;
|
|
144
|
+
const off = Math.abs(Math.round(scaled) - scaled) > NEAR * Math.max(1, Math.abs(scaled));
|
|
145
|
+
return off || !Number.isFinite(value);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const compileDecimals = (type) => {
|
|
149
|
+
const { decimals } = type;
|
|
150
|
+
const scale = 10 ** decimals;
|
|
151
|
+
return (value, context, key) => {
|
|
152
|
+
if (decimalsOff(value, scale)) issues.decimals(context, decimals, value, key);
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// The rules a field carries, by name, in the order its type lists them, min
|
|
157
|
+
// and max as the one `range`: the order the closures run them in and the JIT
|
|
158
|
+
// emits them in.
|
|
159
|
+
const ruleNames = (type) => {
|
|
160
|
+
const names = [];
|
|
161
|
+
let range = false;
|
|
162
|
+
for (const name of type.constructor.rules) {
|
|
163
|
+
if (type[name] === undefined) continue;
|
|
164
|
+
if (name === 'min' || name === 'max') range = true;
|
|
165
|
+
else names.push(name);
|
|
166
|
+
}
|
|
167
|
+
if (range) names.push('range');
|
|
168
|
+
return names;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const COMPILERS = Object.assign(Object.create(null), {
|
|
172
|
+
length: compileLength,
|
|
173
|
+
pattern: compilePattern,
|
|
174
|
+
format: compileFormat,
|
|
175
|
+
decimals: compileDecimals,
|
|
176
|
+
range: compileRange,
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
// The checks of the rules a field carries.
|
|
180
|
+
const compileRules = (type) => {
|
|
181
|
+
const names = ruleNames(type);
|
|
182
|
+
const checks = [];
|
|
183
|
+
for (let index = 0; index < names.length; index += 1) checks.push(COMPILERS[names[index]](type));
|
|
184
|
+
return checks;
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// A nullable field accepts null in place of a value of its type, whatever
|
|
188
|
+
// its rules; `required` still says whether the key must be there.
|
|
189
|
+
const nullable = (inner) => (value, context, key) => {
|
|
190
|
+
if (value === null) return;
|
|
191
|
+
inner(value, context, key);
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// The check of a field with rules or a validate function: the type check,
|
|
195
|
+
// then the rules, then the validate function. A type failure cancels the
|
|
196
|
+
// rest (a length or a validator makes no sense for a value of another type),
|
|
197
|
+
// and validate runs only on a value that passed everything before it, so it
|
|
198
|
+
// can rely on the type and the rules. A field with neither is its type check
|
|
199
|
+
// alone.
|
|
200
|
+
const withRules = (type, inner, rules) => {
|
|
201
|
+
const { required, validate } = type;
|
|
202
|
+
return (value, context, key) => {
|
|
203
|
+
if (value == null && !required) return;
|
|
204
|
+
const before = context.count;
|
|
205
|
+
inner(value, context, key);
|
|
206
|
+
if (context.count !== before) return;
|
|
207
|
+
for (let index = 0; index < rules.length; index += 1) {
|
|
208
|
+
if (context.count >= context.limit) break;
|
|
209
|
+
rules[index](value, context, key);
|
|
210
|
+
}
|
|
211
|
+
if (validate && context.count === before) {
|
|
212
|
+
const nested = key !== undefined;
|
|
213
|
+
if (nested) context.path.push(key);
|
|
214
|
+
runValidate(type, validate, value, context);
|
|
215
|
+
if (nested) context.path.pop();
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// The check of a field: the type check, wrapped in the rules and the
|
|
221
|
+
// validate function when it has any, and in the null test when nullable. A
|
|
222
|
+
// field without rules is its type check alone, and check is the hot path.
|
|
223
|
+
// The type check of a field. A custom type with a `compile` of its own runs
|
|
224
|
+
// it here, on the first check, not when the schema is built: what it throws
|
|
225
|
+
// is the type's (ERR_INVALID_CUSTOM_TYPE, located at the schema, with the
|
|
226
|
+
// error as the cause), never a bare error out of a check.
|
|
227
|
+
const compiledOf = (type) => {
|
|
228
|
+
try {
|
|
229
|
+
return type.compile();
|
|
230
|
+
} catch (error) {
|
|
231
|
+
if (error instanceof SchemaDefinitionError) throw error;
|
|
232
|
+
const { root } = type;
|
|
233
|
+
const reason = `Custom type "${type.type}" could not compile its check: ${error?.message}`;
|
|
234
|
+
const failed = new SchemaDefinitionError('ERR_INVALID_CUSTOM_TYPE', reason);
|
|
235
|
+
failed.schema = root.name;
|
|
236
|
+
failed.message = `${reason} in "${root.name}"`;
|
|
237
|
+
failed.cause = error;
|
|
238
|
+
throw failed;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const compileField = (type) => {
|
|
243
|
+
const checks = compileRules(type);
|
|
244
|
+
const inner = compiledOf(type);
|
|
245
|
+
const plain = checks.length === 0 && !type.validate;
|
|
246
|
+
const check = plain ? inner : withRules(type, inner, checks);
|
|
247
|
+
return type.nullable === true ? nullable(check) : check;
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
// The type check of a custom type: a `checkType(value, path)` that returns
|
|
251
|
+
// messages, run through the validator contract (the `compile` of
|
|
252
|
+
// AbstractType). A built-in prototype compiles its own.
|
|
253
|
+
const custom = {
|
|
254
|
+
compile() {
|
|
255
|
+
const type = this;
|
|
256
|
+
const { required } = this;
|
|
257
|
+
return (value, context, key) => {
|
|
258
|
+
if (value == null && !required) return;
|
|
259
|
+
const nested = key !== undefined;
|
|
260
|
+
if (nested) context.path.push(key);
|
|
261
|
+
runCheckType(type, value, context);
|
|
262
|
+
if (nested) context.path.pop();
|
|
263
|
+
};
|
|
264
|
+
},
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
// The key a value carries for a field that has nothing under its own key: a
|
|
268
|
+
// field renamed with `aliases: ['old']` is still read from its old key, and
|
|
269
|
+
// the field's own key when the value has none of them.
|
|
270
|
+
const aliasOf = (value, aliases, name) => {
|
|
271
|
+
for (const alias of aliases) if (Object.hasOwn(value, alias)) return alias;
|
|
272
|
+
return name;
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// A field is read with a keyed load, and `Object.hasOwn` is consulted only
|
|
276
|
+
// when the value is undefined, to tell a missing key from one set to
|
|
277
|
+
// undefined. A field named after a member of Object.prototype (`toString`,
|
|
278
|
+
// `constructor`, ...) would read the inherited function from a plain object,
|
|
279
|
+
// so such a field (`own`) always asks `Object.hasOwn` first.
|
|
280
|
+
const scanUnknown = (value, known, context) => {
|
|
281
|
+
let unexpected = null;
|
|
282
|
+
for (const name in value) {
|
|
283
|
+
if (name in known || !Object.hasOwn(value, name)) continue;
|
|
284
|
+
if (unexpected === null) unexpected = [];
|
|
285
|
+
unexpected.push(name);
|
|
286
|
+
}
|
|
287
|
+
if (unexpected !== null) issues.unexpected(context, unexpected);
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
// The check of a struct, as a closure over its plan: an index loop with no
|
|
291
|
+
// lookups in the raw definition, no brand checks and no path strings. Every
|
|
292
|
+
// plan entry has the same shape and every field check is a function chosen
|
|
293
|
+
// when the field was built, called with the key of the field so that a leaf
|
|
294
|
+
// never touches the path. Unknown keys are looked for only when the value has
|
|
295
|
+
// more keys than the plan found, so a value with exactly the expected keys is
|
|
296
|
+
// never scanned twice.
|
|
297
|
+
// A field with nothing under its own key (absent, inherited, or own and set
|
|
298
|
+
// to undefined): an alias may hold the value, and the check reports it under
|
|
299
|
+
// the key it found it under. Then the same for an optional field, a required
|
|
300
|
+
// one asks which (own and undefined is a type error). Kept out of the loop
|
|
301
|
+
// of the struct check, whose body is the fast path of a present field; the
|
|
302
|
+
// return is what the field adds to the keys found.
|
|
303
|
+
const checkAbsent = (entry, value, context, name) => {
|
|
304
|
+
const under = aliasOf(value, entry.aliases, name);
|
|
305
|
+
let item;
|
|
306
|
+
if (under !== name) {
|
|
307
|
+
item = value[under];
|
|
308
|
+
context.renamed = true;
|
|
309
|
+
}
|
|
310
|
+
if (item === undefined) {
|
|
311
|
+
if (!entry.required) return 0;
|
|
312
|
+
if (!Object.hasOwn(value, under)) {
|
|
313
|
+
issues.required(context, name);
|
|
314
|
+
return 0;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
entry.check(item, context, under);
|
|
318
|
+
return 1;
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
// The checks of the fields are taken from them when the struct's check is
|
|
322
|
+
// compiled, on its first use, into entries of the shape the loop reads (the
|
|
323
|
+
// entry of the plan and the check of its field in one object: a load from
|
|
324
|
+
// it is what the loop did when the plan held the checks).
|
|
325
|
+
const compileStruct = (entries, known) => {
|
|
326
|
+
const plan = [];
|
|
327
|
+
for (const entry of entries) {
|
|
328
|
+
const { key, type, required, own, aliases } = entry;
|
|
329
|
+
plan.push({ key, type, required, own, aliases, check: type.check });
|
|
330
|
+
}
|
|
331
|
+
Object.freeze(plan);
|
|
332
|
+
return (value, context, key) => {
|
|
333
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
334
|
+
issues.type(context, 'object', value, key);
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
const nested = key !== undefined;
|
|
338
|
+
if (nested) context.path.push(key);
|
|
339
|
+
let found = 0;
|
|
340
|
+
for (let index = 0; index < plan.length; index += 1) {
|
|
341
|
+
if (context.count >= context.limit) break;
|
|
342
|
+
const entry = plan[index];
|
|
343
|
+
const name = entry.key;
|
|
344
|
+
const item = value[name];
|
|
345
|
+
if (item !== undefined && (!entry.own || Object.hasOwn(value, name))) {
|
|
346
|
+
found += 1;
|
|
347
|
+
entry.check(item, context, name);
|
|
348
|
+
} else found += checkAbsent(entry, value, context, name);
|
|
349
|
+
}
|
|
350
|
+
if (context.unknown === 'reject' && context.count < context.limit) {
|
|
351
|
+
let total = 0;
|
|
352
|
+
// oxlint-disable-next-line no-unused-vars
|
|
353
|
+
for (const name in value) total += 1;
|
|
354
|
+
if (total !== found) scanUnknown(value, known, context);
|
|
355
|
+
}
|
|
356
|
+
if (nested) context.path.pop();
|
|
357
|
+
};
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
// The check of a struct, compiled on its first use: kept apart from the
|
|
361
|
+
// frozen plan, whose creation is the cost of building a struct.
|
|
362
|
+
const CHECKS = new WeakMap();
|
|
363
|
+
|
|
364
|
+
const checkOf = (fields) => {
|
|
365
|
+
let check = CHECKS.get(fields);
|
|
366
|
+
if (check === undefined) {
|
|
367
|
+
const { plan, known } = fields[STRUCT];
|
|
368
|
+
check = compileStruct(plan, known);
|
|
369
|
+
CHECKS.set(fields, check);
|
|
370
|
+
}
|
|
371
|
+
return check;
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
// A single comparison on the way through for a valid value; the optional
|
|
375
|
+
// case is looked at only when it fails.
|
|
376
|
+
const scalarCheck = (scalar, required) => (value, context, key) => {
|
|
377
|
+
// oxlint-disable-next-line valid-typeof
|
|
378
|
+
if (typeof value === scalar) return;
|
|
379
|
+
if (!required && value == null) return;
|
|
380
|
+
issues.type(context, scalar, value, key);
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
// Any value at all, null and undefined included; `required` still says
|
|
384
|
+
// whether the key must be there.
|
|
385
|
+
const accept = () => {};
|
|
386
|
+
|
|
387
|
+
// An array and a Set are walked the same way, by position; only the type
|
|
388
|
+
// test differs (`isInstance` of their prototypes).
|
|
389
|
+
const compileList = (type, isList) => {
|
|
390
|
+
const { required } = type;
|
|
391
|
+
const { check } = type.value;
|
|
392
|
+
const name = type.type;
|
|
393
|
+
return (value, context, key) => {
|
|
394
|
+
if (!isList(value)) {
|
|
395
|
+
if (!required && value == null) return;
|
|
396
|
+
issues.type(context, name, value, key);
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
const nested = key !== undefined;
|
|
400
|
+
if (nested) context.path.push(key);
|
|
401
|
+
let index = 0;
|
|
402
|
+
for (const item of value) {
|
|
403
|
+
if (context.count >= context.limit) break;
|
|
404
|
+
check(item, context, index);
|
|
405
|
+
index += 1;
|
|
406
|
+
}
|
|
407
|
+
if (nested) context.path.pop();
|
|
408
|
+
};
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
// A reference is the one place a schema can recurse (Category.parent is a
|
|
412
|
+
// Category), so it is where a value met again while it is still being
|
|
413
|
+
// checked is a cycle, reported instead of recursed into, and where a graph
|
|
414
|
+
// nested deeper than the check allows is reported instead of overflowing the
|
|
415
|
+
// stack.
|
|
416
|
+
const runReferenced = (schema, value, context, key) => {
|
|
417
|
+
if (value === null || typeof value !== 'object') {
|
|
418
|
+
schema[RUN](value, context, key);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
if (context.depth >= context.maxDepth) {
|
|
422
|
+
issues.depth(context, context.maxDepth, key);
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
let { seen } = context;
|
|
426
|
+
if (seen === null) {
|
|
427
|
+
// The first embedded record creates the set, and the value the check
|
|
428
|
+
// started from joins it then, so a check that embeds nothing pays nothing.
|
|
429
|
+
seen = new Set();
|
|
430
|
+
if (context.source !== null) seen.add(context.source);
|
|
431
|
+
context.seen = seen;
|
|
432
|
+
}
|
|
433
|
+
if (seen.has(value)) {
|
|
434
|
+
issues.circular(context, key);
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
seen.add(value);
|
|
438
|
+
context.depth += 1;
|
|
439
|
+
schema[RUN](value, context, key);
|
|
440
|
+
context.depth -= 1;
|
|
441
|
+
seen.delete(value);
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
// What a branch is called in the message of a value that matched none.
|
|
445
|
+
const nameOf = (branch) => (branch.type === 'schema' ? 'object' : branch.type);
|
|
446
|
+
|
|
447
|
+
// The branch of a discriminated union by the value of its discriminator, as
|
|
448
|
+
// a Map of value to branch index: built once per field, on first use, and
|
|
449
|
+
// shared by the check, the passes of parse and the JIT.
|
|
450
|
+
const TABLES = new WeakMap();
|
|
451
|
+
|
|
452
|
+
const tableOf = (field) => {
|
|
453
|
+
let table = TABLES.get(field);
|
|
454
|
+
if (table === undefined) {
|
|
455
|
+
table = new Map();
|
|
456
|
+
const { union: branches, discriminator } = field;
|
|
457
|
+
for (let index = 0; index < branches.length; index += 1) {
|
|
458
|
+
const values = branches[index].schema[discriminator].enum;
|
|
459
|
+
for (let at = 0; at < values.length; at += 1) table.set(values[at], index);
|
|
460
|
+
}
|
|
461
|
+
TABLES.set(field, table);
|
|
462
|
+
}
|
|
463
|
+
return table;
|
|
464
|
+
};
|
|
465
|
+
|
|
466
|
+
// A plain union: the branches in order, the first that records nothing
|
|
467
|
+
// wins, and what a failed branch recorded is dropped.
|
|
468
|
+
const compilePlain = (type, required) => {
|
|
469
|
+
const checks = [];
|
|
470
|
+
const expected = [];
|
|
471
|
+
for (let index = 0; index < type.union.length; index += 1) {
|
|
472
|
+
const branch = type.union[index];
|
|
473
|
+
checks.push(branch.check);
|
|
474
|
+
expected.push(nameOf(branch));
|
|
475
|
+
}
|
|
476
|
+
const { length } = checks;
|
|
477
|
+
return (value, context, key) => {
|
|
478
|
+
if (!required && value == null) return;
|
|
479
|
+
const { count } = context;
|
|
480
|
+
// The validators a failed branch set aside in an async check go with
|
|
481
|
+
// its issues: they belong to a branch the value does not match.
|
|
482
|
+
const pending = context.async === null ? 0 : context.async.length;
|
|
483
|
+
for (let index = 0; index < length; index += 1) {
|
|
484
|
+
checks[index](value, context, key);
|
|
485
|
+
if (context.count === count) return;
|
|
486
|
+
context.issues.length = count;
|
|
487
|
+
context.count = count;
|
|
488
|
+
if (context.async !== null) release(context.async, pending);
|
|
489
|
+
}
|
|
490
|
+
issues.union(context, expected, undefined, key);
|
|
491
|
+
};
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
// The type check of each built-in prototype by the name of its type, a
|
|
495
|
+
// closure built once from the field: the `compile` validation.js installs on
|
|
496
|
+
// the prototype objects, and the identity codec.js and the JIT of
|
|
497
|
+
// `./compile` tell the shape of a node by. A string, a number, a bigint and
|
|
498
|
+
// a boolean share one (by `typeof`), so do any and unknown, an array and a
|
|
499
|
+
// Set (by `isInstance`).
|
|
500
|
+
const COMPILE = {
|
|
501
|
+
// A string, a number, a bigint or a boolean: one `typeof`.
|
|
502
|
+
string() {
|
|
503
|
+
return scalarCheck(this.scalar, this.required);
|
|
504
|
+
},
|
|
505
|
+
// A number without a fraction.
|
|
506
|
+
integer() {
|
|
507
|
+
const { required } = this;
|
|
508
|
+
return (value, context, key) => {
|
|
509
|
+
if (Number.isInteger(value)) return;
|
|
510
|
+
if (!required && value == null) return;
|
|
511
|
+
issues.type(context, 'integer', value, key);
|
|
512
|
+
};
|
|
513
|
+
},
|
|
514
|
+
// A Date instance with a time: an invalid Date is a type error.
|
|
515
|
+
date() {
|
|
516
|
+
const { required } = this;
|
|
517
|
+
return (value, context, key) => {
|
|
518
|
+
if (isDate(value) && !Number.isNaN(value.getTime())) return;
|
|
519
|
+
if (!required && value == null) return;
|
|
520
|
+
issues.type(context, 'date', value, key);
|
|
521
|
+
};
|
|
522
|
+
},
|
|
523
|
+
// A Uint8Array of any realm, a Buffer included.
|
|
524
|
+
bytes() {
|
|
525
|
+
const { required } = this;
|
|
526
|
+
return (value, context, key) => {
|
|
527
|
+
if (isBytes(value)) return;
|
|
528
|
+
if (!required && value == null) return;
|
|
529
|
+
issues.type(context, 'bytes', value, key);
|
|
530
|
+
};
|
|
531
|
+
},
|
|
532
|
+
null() {
|
|
533
|
+
const { required } = this;
|
|
534
|
+
return (value, context, key) => {
|
|
535
|
+
if (value === null) return;
|
|
536
|
+
if (value === undefined && !required) return;
|
|
537
|
+
issues.type(context, 'null', value, key);
|
|
538
|
+
};
|
|
539
|
+
},
|
|
540
|
+
any() {
|
|
541
|
+
return accept;
|
|
542
|
+
},
|
|
543
|
+
// One of the values: a Set past ENUM_SET_SIZE of them.
|
|
544
|
+
enum() {
|
|
545
|
+
const values = this.enum;
|
|
546
|
+
const { required } = this;
|
|
547
|
+
if (values.length > ENUM_SET_SIZE) {
|
|
548
|
+
const set = new Set(values);
|
|
549
|
+
return (value, context, key) => {
|
|
550
|
+
if (set.has(value)) return;
|
|
551
|
+
if (!required && value == null) return;
|
|
552
|
+
issues.enum(context, values, key);
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
return (value, context, key) => {
|
|
556
|
+
if (values.includes(value)) return;
|
|
557
|
+
if (!required && value == null) return;
|
|
558
|
+
issues.enum(context, values, key);
|
|
559
|
+
};
|
|
560
|
+
},
|
|
561
|
+
// Collections iterate their source directly: an object with for...in
|
|
562
|
+
// over its own keys, a Map and a Set through their iterators, an array by
|
|
563
|
+
// index. A collection pushes its own key on the path before its elements
|
|
564
|
+
// and pops it after them; an element is checked with its key or index.
|
|
565
|
+
object() {
|
|
566
|
+
const { required, type, key: keyType } = this;
|
|
567
|
+
const { check } = this.value;
|
|
568
|
+
return (value, context, key) => {
|
|
569
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
570
|
+
if (!required && value == null) return;
|
|
571
|
+
issues.type(context, type, value, key);
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
const nested = key !== undefined;
|
|
575
|
+
if (nested) context.path.push(key);
|
|
576
|
+
let count = 0;
|
|
577
|
+
for (const name in value) {
|
|
578
|
+
if (!Object.hasOwn(value, name)) continue;
|
|
579
|
+
if (context.count >= context.limit) break;
|
|
580
|
+
count += 1;
|
|
581
|
+
// oxlint-disable-next-line valid-typeof
|
|
582
|
+
if (typeof name !== keyType) {
|
|
583
|
+
issues.key(context, keyType, name);
|
|
584
|
+
break;
|
|
585
|
+
}
|
|
586
|
+
check(value[name], context, name);
|
|
587
|
+
}
|
|
588
|
+
if (count === 0 && required) issues.required(context);
|
|
589
|
+
if (nested) context.path.pop();
|
|
590
|
+
};
|
|
591
|
+
},
|
|
592
|
+
map() {
|
|
593
|
+
const { required, type, key: keyType } = this;
|
|
594
|
+
const { check } = this.value;
|
|
595
|
+
return (value, context, key) => {
|
|
596
|
+
if (!isMap(value)) {
|
|
597
|
+
if (!required && value == null) return;
|
|
598
|
+
issues.type(context, type, value, key);
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
if (value.size === 0) {
|
|
602
|
+
if (required) issues.required(context, key);
|
|
603
|
+
return;
|
|
604
|
+
}
|
|
605
|
+
const nested = key !== undefined;
|
|
606
|
+
if (nested) context.path.push(key);
|
|
607
|
+
for (const pair of value) {
|
|
608
|
+
if (context.count >= context.limit) break;
|
|
609
|
+
const name = pair[0];
|
|
610
|
+
// oxlint-disable-next-line valid-typeof
|
|
611
|
+
if (typeof name !== keyType) {
|
|
612
|
+
issues.key(context, keyType, name);
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
check(pair[1], context, name);
|
|
616
|
+
}
|
|
617
|
+
if (nested) context.path.pop();
|
|
618
|
+
};
|
|
619
|
+
},
|
|
620
|
+
array() {
|
|
621
|
+
return compileList(this, this.isInstance);
|
|
622
|
+
},
|
|
623
|
+
// The target is looked up at check time: entities of a model are built
|
|
624
|
+
// in any order and a namespace may be attached later.
|
|
625
|
+
reference() {
|
|
626
|
+
const { one, many, root, required, embed } = this;
|
|
627
|
+
if (one) {
|
|
628
|
+
return (value, context, key) => {
|
|
629
|
+
if (!required && value == null) return;
|
|
630
|
+
const schema = root.findReference(one);
|
|
631
|
+
if (!schema) {
|
|
632
|
+
issues.reference(context, one, key);
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
if (embeds(schema, embed, context.references)) runReferenced(schema, value, context, key);
|
|
636
|
+
else if (typeof value !== 'string') issues.type(context, 'string', value, key);
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
const expected = `array of ${many}`;
|
|
640
|
+
return (value, context, key) => {
|
|
641
|
+
if (!required && value == null) return;
|
|
642
|
+
const schema = root.findReference(many);
|
|
643
|
+
if (!schema) {
|
|
644
|
+
issues.reference(context, many, key);
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
if (!Array.isArray(value)) {
|
|
648
|
+
issues.type(context, expected, value, key);
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
const nested = key !== undefined;
|
|
652
|
+
if (nested) context.path.push(key);
|
|
653
|
+
const embedded = embeds(schema, embed, context.references);
|
|
654
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
655
|
+
if (context.count >= context.limit) break;
|
|
656
|
+
const item = value[index];
|
|
657
|
+
if (embedded) runReferenced(schema, item, context, index);
|
|
658
|
+
else if (typeof item !== 'string') issues.type(context, 'string', item, index);
|
|
659
|
+
}
|
|
660
|
+
if (nested) context.path.pop();
|
|
661
|
+
};
|
|
662
|
+
},
|
|
663
|
+
// A nested struct: the check of its fields, or of the one type of a
|
|
664
|
+
// Schema held as a field.
|
|
665
|
+
schema() {
|
|
666
|
+
const { required } = this;
|
|
667
|
+
const check = isStruct(this.schema) ? checkOf(this.schema) : this.schema.check;
|
|
668
|
+
return (value, context, key) => {
|
|
669
|
+
if (!required && value == null) return;
|
|
670
|
+
check(value, context, key);
|
|
671
|
+
};
|
|
672
|
+
},
|
|
673
|
+
// The elements by index, no more of them than the tuple has.
|
|
674
|
+
tuple() {
|
|
675
|
+
const { required, type } = this;
|
|
676
|
+
const checks = [];
|
|
677
|
+
for (let index = 0; index < this.value.length; index += 1) checks.push(this.value[index].check);
|
|
678
|
+
const { length } = checks;
|
|
679
|
+
return (value, context, key) => {
|
|
680
|
+
if (!Array.isArray(value)) {
|
|
681
|
+
if (!required && value == null) return;
|
|
682
|
+
issues.type(context, type, value, key);
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
if (value.length > length) {
|
|
686
|
+
issues.length(context, undefined, length, value.length, key);
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
const nested = key !== undefined;
|
|
690
|
+
if (nested) context.path.push(key);
|
|
691
|
+
for (let index = 0; index < length; index += 1) {
|
|
692
|
+
if (context.count >= context.limit) break;
|
|
693
|
+
checks[index](value[index], context, index);
|
|
694
|
+
}
|
|
695
|
+
if (nested) context.path.pop();
|
|
696
|
+
};
|
|
697
|
+
},
|
|
698
|
+
// Without a discriminator the branches are tried in order; with one, the
|
|
699
|
+
// branch is picked by the value of that field in one Map lookup.
|
|
700
|
+
union() {
|
|
701
|
+
const { required, discriminator } = this;
|
|
702
|
+
if (discriminator === undefined) return compilePlain(this, required);
|
|
703
|
+
const checks = [];
|
|
704
|
+
for (let index = 0; index < this.union.length; index += 1) checks.push(this.union[index].check);
|
|
705
|
+
const table = tableOf(this);
|
|
706
|
+
const expected = [];
|
|
707
|
+
for (const value of table.keys()) expected.push(value);
|
|
708
|
+
return (value, context, key) => {
|
|
709
|
+
if (value === null || typeof value !== 'object') {
|
|
710
|
+
if (!required && value == null) return;
|
|
711
|
+
issues.type(context, 'object', value, key);
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
const index = table.get(value[discriminator]);
|
|
715
|
+
if (index !== undefined) {
|
|
716
|
+
checks[index](value, context, key);
|
|
717
|
+
return;
|
|
718
|
+
}
|
|
719
|
+
const nested = key !== undefined;
|
|
720
|
+
if (nested) context.path.push(key);
|
|
721
|
+
issues.union(context, expected, discriminator, discriminator);
|
|
722
|
+
if (nested) context.path.pop();
|
|
723
|
+
};
|
|
724
|
+
},
|
|
725
|
+
// An object or an array.
|
|
726
|
+
json() {
|
|
727
|
+
const { required } = this;
|
|
728
|
+
return (value, context, key) => {
|
|
729
|
+
if (value !== null && typeof value === 'object') return;
|
|
730
|
+
if (!required && value == null) return;
|
|
731
|
+
issues.type(context, 'object', value, key);
|
|
732
|
+
};
|
|
733
|
+
},
|
|
734
|
+
// The own keys of an object, each a name of the list holding a boolean;
|
|
735
|
+
// another key is unexpected under the reject policy, as a struct's is.
|
|
736
|
+
flags() {
|
|
737
|
+
const { required, type } = this;
|
|
738
|
+
// A null-prototype dictionary: a key such as `constructor` is not a name.
|
|
739
|
+
const known = Object.create(null);
|
|
740
|
+
for (const name of this.flags) known[name] = true;
|
|
741
|
+
return (value, context, key) => {
|
|
742
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
743
|
+
if (!required && value == null) return;
|
|
744
|
+
issues.type(context, type, value, key);
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
const nested = key !== undefined;
|
|
748
|
+
if (nested) context.path.push(key);
|
|
749
|
+
let unexpected = null;
|
|
750
|
+
for (const name in value) {
|
|
751
|
+
if (!Object.hasOwn(value, name)) continue;
|
|
752
|
+
if (context.count >= context.limit) break;
|
|
753
|
+
if (known[name] === true) {
|
|
754
|
+
const item = value[name];
|
|
755
|
+
if (typeof item !== 'boolean') issues.type(context, 'boolean', item, name);
|
|
756
|
+
} else if (context.unknown === 'reject') {
|
|
757
|
+
if (unexpected === null) unexpected = [];
|
|
758
|
+
unexpected.push(name);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
if (unexpected !== null && context.count < context.limit) {
|
|
762
|
+
issues.unexpected(context, unexpected);
|
|
763
|
+
}
|
|
764
|
+
if (nested) context.path.pop();
|
|
765
|
+
};
|
|
766
|
+
},
|
|
767
|
+
};
|
|
768
|
+
|
|
769
|
+
COMPILE.number = COMPILE.string;
|
|
770
|
+
COMPILE.bigint = COMPILE.string;
|
|
771
|
+
COMPILE.boolean = COMPILE.string;
|
|
772
|
+
COMPILE.unknown = COMPILE.any;
|
|
773
|
+
COMPILE.set = COMPILE.array;
|
|
774
|
+
|
|
775
|
+
module.exports = {
|
|
776
|
+
COMPILE,
|
|
777
|
+
custom,
|
|
778
|
+
compileField,
|
|
779
|
+
checkOf,
|
|
780
|
+
aliasOf,
|
|
781
|
+
ruleNames,
|
|
782
|
+
compileRules,
|
|
783
|
+
patternLimit,
|
|
784
|
+
decimalsOff,
|
|
785
|
+
codePoints,
|
|
786
|
+
lengthOf,
|
|
787
|
+
nameOf,
|
|
788
|
+
tableOf,
|
|
789
|
+
};
|