@alexify/metaschema 3.4.1 → 4.0.0
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 +162 -1
- package/README.md +22 -7
- package/SECURITY.md +5 -4
- package/index.d.ts +856 -460
- package/package.json +8 -6
- package/src/checks.js +72 -107
- package/src/codec.js +67 -58
- package/src/coerce/index.d.ts +31 -21
- package/src/coerce/index.js +64 -47
- package/src/compile/code.js +2 -2
- package/src/compile/emit.js +20 -31
- package/src/compile/index.d.ts +41 -25
- package/src/compile/index.js +45 -32
- package/src/context.js +10 -10
- package/src/definition/index.d.ts +10 -7
- package/src/definition/index.js +2 -1
- package/src/derive/index.d.ts +25 -17
- package/src/derive/index.js +14 -11
- package/src/errors.js +84 -59
- package/src/formats/index.d.ts +27 -21
- package/src/formats/index.js +15 -13
- package/src/generators/{fromjsonschema.js → from-json-schema.js} +170 -127
- package/src/generators/index.d.ts +23 -15
- package/src/generators/index.js +2 -2
- package/src/generators/{jsonschema.js → json-schema.js} +37 -43
- package/src/generators/typescript.js +9 -6
- package/src/index.js +2 -1
- package/src/issues.js +41 -42
- package/src/kinds.js +18 -16
- package/src/lint.js +15 -18
- package/src/locales/en.js +7 -7
- package/src/locales/uk.js +6 -6
- package/src/metadata.js +2 -2
- package/src/model/data.js +17 -20
- package/src/model/diff.js +29 -24
- package/src/model/index.d.ts +51 -35
- package/src/model/infer.js +69 -60
- package/src/model/merge.js +29 -18
- package/src/model/pattern.js +105 -102
- package/src/model/restore.js +24 -19
- package/src/model/sample.js +90 -100
- package/src/model.js +4 -2
- package/src/mongodb/bson.js +12 -12
- package/src/mongodb/document.js +7 -6
- package/src/mongodb/index.d.ts +54 -31
- package/src/mongodb/update.js +12 -9
- package/src/preprocessor.js +13 -13
- package/src/prototypes/abstract.js +8 -8
- package/src/prototypes/collections.js +7 -7
- package/src/prototypes/flags.js +1 -1
- package/src/prototypes/reference.js +3 -4
- package/src/prototypes/scalars.js +1 -1
- package/src/prototypes/schema.js +5 -6
- package/src/prototypes/tuple.js +6 -5
- package/src/prototypes/union.js +5 -4
- package/src/rules.js +23 -20
- package/src/runtime/browser.js +6 -1
- package/src/runtime/node.js +19 -2
- package/src/schema.js +15 -13
- package/src/snapshot.js +14 -12
- package/src/stream/index.d.ts +33 -21
- package/src/stream/index.js +37 -20
- package/src/struct.js +6 -6
- package/src/types.js +28 -22
- package/src/util.js +65 -64
- package/src/validation.js +10 -13
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexify/metaschema",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Fast, zero-dependency metadata schema and interface definition language (IDL) for Node.js and browsers — validation and parsing with a JIT backend, domain models, TypeScript and JSON Schema",
|
|
5
5
|
"license": "MIT",
|
|
6
|
+
"type": "commonjs",
|
|
6
7
|
"author": "Alex Dolid <dolid.sasha@gmail.com>",
|
|
7
8
|
"repository": {
|
|
8
9
|
"type": "git",
|
|
@@ -83,7 +84,8 @@
|
|
|
83
84
|
"README.md",
|
|
84
85
|
"CHANGELOG.md",
|
|
85
86
|
"LICENSE",
|
|
86
|
-
"SECURITY.md"
|
|
87
|
+
"SECURITY.md",
|
|
88
|
+
"!**/CLAUDE.md"
|
|
87
89
|
],
|
|
88
90
|
"sideEffects": [
|
|
89
91
|
"./index.js",
|
|
@@ -122,6 +124,7 @@
|
|
|
122
124
|
"metarhia"
|
|
123
125
|
],
|
|
124
126
|
"devDependencies": {
|
|
127
|
+
"@alexify/metaskills": "^1.2.0",
|
|
125
128
|
"@braintree/sanitize-url": "^7.1.2",
|
|
126
129
|
"@standard-schema/spec": "^1.1.0",
|
|
127
130
|
"@vercel/analytics": "^2.0.1",
|
|
@@ -160,11 +163,11 @@
|
|
|
160
163
|
"test:coverage": "c8 --all --include 'src/**' --check-coverage --lines 98 --statements 98 --branches 90 --functions 100 --reporter text --reporter lcov node --test tests/unit/*.test.js",
|
|
161
164
|
"test:types": "tsd",
|
|
162
165
|
"check:dts": "tsc --noEmit --strict --target es2022 --skipLibCheck false index.d.ts src/locales/en.d.ts src/locales/uk.d.ts src/generators/index.d.ts src/compile/index.d.ts src/model/index.d.ts src/stream/index.d.ts src/formats/index.d.ts src/derive/index.d.ts src/coerce/index.d.ts src/mongodb/index.d.ts src/definition/index.d.ts",
|
|
163
|
-
"lint": "oxlint index.js browser.js src tests scripts bench",
|
|
166
|
+
"lint": "oxlint --report-unused-disable-directives-severity=error index.js browser.js src tests scripts bench",
|
|
164
167
|
"format": "oxfmt index.js browser.js src tests scripts bench",
|
|
165
168
|
"format:check": "oxfmt --check index.js browser.js src tests scripts bench",
|
|
166
169
|
"size": "node scripts/size.js",
|
|
167
|
-
"size:gate": "node scripts/size.js --max-gzip
|
|
170
|
+
"size:gate": "node scripts/size.js --max-gzip 23 --max-gzip-generators 8 --max-gzip-compile 5 --max-gzip-model 13 --max-gzip-stream 1.5 --max-gzip-formats 5 --max-gzip-derive 2.5 --max-gzip-coerce 3 --max-gzip-mongodb 4 --max-gzip-definition 11",
|
|
168
171
|
"bench": "node bench/bench.js",
|
|
169
172
|
"bench:compare": "node bench/compare.js",
|
|
170
173
|
"bench:releases": "node bench/releases.js",
|
|
@@ -173,7 +176,6 @@
|
|
|
173
176
|
"docs:dev": "vitepress dev docs",
|
|
174
177
|
"docs:build": "vitepress build docs",
|
|
175
178
|
"docs:preview": "vitepress preview docs",
|
|
176
|
-
"docs:comparison": "node scripts/comparison.js"
|
|
177
|
-
"release": "pnpm publish"
|
|
179
|
+
"docs:comparison": "node scripts/comparison.js"
|
|
178
180
|
}
|
|
179
181
|
}
|
package/src/checks.js
CHANGED
|
@@ -11,6 +11,26 @@ const { STRUCT, isStruct } = require('./struct.js');
|
|
|
11
11
|
const { embeds } = require('./prototypes/reference.js');
|
|
12
12
|
const { SchemaDefinitionError } = require('./errors.js');
|
|
13
13
|
|
|
14
|
+
// A number of `n` decimals is a whole number times 10ⁿ, to within the error
|
|
15
|
+
// the arithmetic of doubles leaves on the way (0.1 + 0.2 has two decimals):
|
|
16
|
+
// ULPS units in the last place of the scaled value. A few units is what a
|
|
17
|
+
// sum or a product of decimals leaves; 256 is room for a chain of them, and
|
|
18
|
+
// still tells a decimal too many apart below 2⁴³ units of the last decimal
|
|
19
|
+
// (ten billion at two), where a bound of 1e-9 of the value would let
|
|
20
|
+
// 12345678.123 pass as two decimals. Above it every double passes. NaN and
|
|
21
|
+
// the infinities have no decimals to count.
|
|
22
|
+
const ULPS = 256;
|
|
23
|
+
const NEAR = ULPS * Number.EPSILON;
|
|
24
|
+
|
|
25
|
+
// The check of a struct, compiled on its first use: kept apart from the
|
|
26
|
+
// frozen plan, whose creation is the cost of building a struct.
|
|
27
|
+
const CHECKS = new WeakMap();
|
|
28
|
+
|
|
29
|
+
// The branch of a discriminated union by the value of its discriminator, as
|
|
30
|
+
// a Map of value to branch index: built once per field, on first use, and
|
|
31
|
+
// shared by the check, the passes of parse and the JIT.
|
|
32
|
+
const TABLES = new WeakMap();
|
|
33
|
+
|
|
14
34
|
// The rules of a field (`rules.js` reads and normalizes them when the field
|
|
15
35
|
// is built): one closure per rule over the field's bounds, run after the type
|
|
16
36
|
// check in the order the type lists them.
|
|
@@ -126,17 +146,6 @@ const compileFormat = (type) => {
|
|
|
126
146
|
};
|
|
127
147
|
};
|
|
128
148
|
|
|
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
149
|
// Whether a number has more decimals than `scale` (10ⁿ) allows: the one test
|
|
141
150
|
// of both backends (the JIT calls it through the scope).
|
|
142
151
|
const decimalsOff = (value, scale) => {
|
|
@@ -168,15 +177,15 @@ const ruleNames = (type) => {
|
|
|
168
177
|
return names;
|
|
169
178
|
};
|
|
170
179
|
|
|
171
|
-
const COMPILERS =
|
|
180
|
+
const COMPILERS = {
|
|
181
|
+
__proto__: null,
|
|
172
182
|
length: compileLength,
|
|
173
183
|
pattern: compilePattern,
|
|
174
184
|
format: compileFormat,
|
|
175
185
|
decimals: compileDecimals,
|
|
176
186
|
range: compileRange,
|
|
177
|
-
}
|
|
187
|
+
};
|
|
178
188
|
|
|
179
|
-
// The checks of the rules a field carries.
|
|
180
189
|
const compileRules = (type) => {
|
|
181
190
|
const names = ruleNames(type);
|
|
182
191
|
const checks = [];
|
|
@@ -200,7 +209,7 @@ const nullable = (inner) => (value, context, key) => {
|
|
|
200
209
|
const withRules = (type, inner, rules) => {
|
|
201
210
|
const { required, validate } = type;
|
|
202
211
|
return (value, context, key) => {
|
|
203
|
-
if (value
|
|
212
|
+
if ((value === undefined || value === null) && !required) return;
|
|
204
213
|
const before = context.count;
|
|
205
214
|
inner(value, context, key);
|
|
206
215
|
if (context.count !== before) return;
|
|
@@ -222,7 +231,7 @@ const withRules = (type, inner, rules) => {
|
|
|
222
231
|
// field without rules is its type check alone, and check is the hot path.
|
|
223
232
|
// The type check of a field. A custom type with a `compile` of its own runs
|
|
224
233
|
// it here, on the first check, not when the schema is built: what it throws
|
|
225
|
-
// is the type's (
|
|
234
|
+
// is the type's (ALX_ERR_INVALID_CUSTOM_TYPE, located at the schema, with the
|
|
226
235
|
// error as the cause), never a bare error out of a check.
|
|
227
236
|
const compiledOf = (type) => {
|
|
228
237
|
try {
|
|
@@ -231,11 +240,8 @@ const compiledOf = (type) => {
|
|
|
231
240
|
if (error instanceof SchemaDefinitionError) throw error;
|
|
232
241
|
const { root } = type;
|
|
233
242
|
const reason = `Custom type "${type.type}" could not compile its check: ${error?.message}`;
|
|
234
|
-
const
|
|
235
|
-
|
|
236
|
-
failed.message = `${reason} in "${root.name}"`;
|
|
237
|
-
failed.cause = error;
|
|
238
|
-
throw failed;
|
|
243
|
+
const code = 'ALX_ERR_INVALID_CUSTOM_TYPE';
|
|
244
|
+
throw new SchemaDefinitionError(reason, { code, cause: error }).locate(root.name, '');
|
|
239
245
|
}
|
|
240
246
|
};
|
|
241
247
|
|
|
@@ -255,7 +261,7 @@ const custom = {
|
|
|
255
261
|
const type = this;
|
|
256
262
|
const { required } = this;
|
|
257
263
|
return (value, context, key) => {
|
|
258
|
-
if (value
|
|
264
|
+
if ((value === undefined || value === null) && !required) return;
|
|
259
265
|
const nested = key !== undefined;
|
|
260
266
|
if (nested) context.path.push(key);
|
|
261
267
|
runCheckType(type, value, context);
|
|
@@ -302,11 +308,9 @@ const scanUnknown = (value, known, context) => {
|
|
|
302
308
|
// return is what the field adds to the keys found.
|
|
303
309
|
const checkAbsent = (entry, value, context, name) => {
|
|
304
310
|
const under = aliasOf(value, entry.aliases, name);
|
|
305
|
-
|
|
306
|
-
if (
|
|
307
|
-
|
|
308
|
-
context.renamed = true;
|
|
309
|
-
}
|
|
311
|
+
const renamed = under !== name;
|
|
312
|
+
if (renamed) context.renamed = true;
|
|
313
|
+
const item = renamed ? value[under] : undefined;
|
|
310
314
|
if (item === undefined) {
|
|
311
315
|
if (!entry.required) return 0;
|
|
312
316
|
if (!Object.hasOwn(value, under)) {
|
|
@@ -331,8 +335,7 @@ const compileStruct = (entries, known) => {
|
|
|
331
335
|
Object.freeze(plan);
|
|
332
336
|
return (value, context, key) => {
|
|
333
337
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
334
|
-
issues.type(context, 'object', value, key);
|
|
335
|
-
return;
|
|
338
|
+
return void issues.type(context, 'object', value, key);
|
|
336
339
|
}
|
|
337
340
|
const nested = key !== undefined;
|
|
338
341
|
if (nested) context.path.push(key);
|
|
@@ -357,10 +360,6 @@ const compileStruct = (entries, known) => {
|
|
|
357
360
|
};
|
|
358
361
|
};
|
|
359
362
|
|
|
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
363
|
const checkOf = (fields) => {
|
|
365
364
|
let check = CHECKS.get(fields);
|
|
366
365
|
if (check === undefined) {
|
|
@@ -376,7 +375,7 @@ const checkOf = (fields) => {
|
|
|
376
375
|
const scalarCheck = (scalar, required) => (value, context, key) => {
|
|
377
376
|
// oxlint-disable-next-line valid-typeof
|
|
378
377
|
if (typeof value === scalar) return;
|
|
379
|
-
if (!required && value
|
|
378
|
+
if (!required && (value === undefined || value === null)) return;
|
|
380
379
|
issues.type(context, scalar, value, key);
|
|
381
380
|
};
|
|
382
381
|
|
|
@@ -392,9 +391,8 @@ const compileList = (type, isList) => {
|
|
|
392
391
|
const name = type.type;
|
|
393
392
|
return (value, context, key) => {
|
|
394
393
|
if (!isList(value)) {
|
|
395
|
-
if (!required && value
|
|
396
|
-
issues.type(context, name, value, key);
|
|
397
|
-
return;
|
|
394
|
+
if (!required && (value === undefined || value === null)) return;
|
|
395
|
+
return void issues.type(context, name, value, key);
|
|
398
396
|
}
|
|
399
397
|
const nested = key !== undefined;
|
|
400
398
|
if (nested) context.path.push(key);
|
|
@@ -414,14 +412,8 @@ const compileList = (type, isList) => {
|
|
|
414
412
|
// nested deeper than the check allows is reported instead of overflowing the
|
|
415
413
|
// stack.
|
|
416
414
|
const runReferenced = (schema, value, context, key) => {
|
|
417
|
-
if (value === null || typeof value !== 'object')
|
|
418
|
-
|
|
419
|
-
return;
|
|
420
|
-
}
|
|
421
|
-
if (context.depth >= context.maxDepth) {
|
|
422
|
-
issues.depth(context, context.maxDepth, key);
|
|
423
|
-
return;
|
|
424
|
-
}
|
|
415
|
+
if (value === null || typeof value !== 'object') return void schema[RUN](value, context, key);
|
|
416
|
+
if (context.depth >= context.maxDepth) return void issues.depth(context, context.maxDepth, key);
|
|
425
417
|
let { seen } = context;
|
|
426
418
|
if (seen === null) {
|
|
427
419
|
// The first embedded record creates the set, and the value the check
|
|
@@ -430,10 +422,7 @@ const runReferenced = (schema, value, context, key) => {
|
|
|
430
422
|
if (context.source !== null) seen.add(context.source);
|
|
431
423
|
context.seen = seen;
|
|
432
424
|
}
|
|
433
|
-
if (seen.has(value))
|
|
434
|
-
issues.circular(context, key);
|
|
435
|
-
return;
|
|
436
|
-
}
|
|
425
|
+
if (seen.has(value)) return void issues.circular(context, key);
|
|
437
426
|
seen.add(value);
|
|
438
427
|
context.depth += 1;
|
|
439
428
|
schema[RUN](value, context, key);
|
|
@@ -444,11 +433,6 @@ const runReferenced = (schema, value, context, key) => {
|
|
|
444
433
|
// What a branch is called in the message of a value that matched none.
|
|
445
434
|
const nameOf = (branch) => (branch.type === 'schema' ? 'object' : branch.type);
|
|
446
435
|
|
|
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
436
|
const tableOf = (field) => {
|
|
453
437
|
let table = TABLES.get(field);
|
|
454
438
|
if (table === undefined) {
|
|
@@ -475,7 +459,7 @@ const compilePlain = (type, required) => {
|
|
|
475
459
|
}
|
|
476
460
|
const { length } = checks;
|
|
477
461
|
return (value, context, key) => {
|
|
478
|
-
if (!required && value
|
|
462
|
+
if (!required && (value === undefined || value === null)) return;
|
|
479
463
|
const { count } = context;
|
|
480
464
|
// The validators a failed branch set aside in an async check go with
|
|
481
465
|
// its issues: they belong to a branch the value does not match.
|
|
@@ -502,12 +486,11 @@ const COMPILE = {
|
|
|
502
486
|
string() {
|
|
503
487
|
return scalarCheck(this.scalar, this.required);
|
|
504
488
|
},
|
|
505
|
-
// A number without a fraction.
|
|
506
489
|
integer() {
|
|
507
490
|
const { required } = this;
|
|
508
491
|
return (value, context, key) => {
|
|
509
492
|
if (Number.isInteger(value)) return;
|
|
510
|
-
if (!required && value
|
|
493
|
+
if (!required && (value === undefined || value === null)) return;
|
|
511
494
|
issues.type(context, 'integer', value, key);
|
|
512
495
|
};
|
|
513
496
|
},
|
|
@@ -516,7 +499,7 @@ const COMPILE = {
|
|
|
516
499
|
const { required } = this;
|
|
517
500
|
return (value, context, key) => {
|
|
518
501
|
if (isDate(value) && !Number.isNaN(value.getTime())) return;
|
|
519
|
-
if (!required && value
|
|
502
|
+
if (!required && (value === undefined || value === null)) return;
|
|
520
503
|
issues.type(context, 'date', value, key);
|
|
521
504
|
};
|
|
522
505
|
},
|
|
@@ -525,7 +508,7 @@ const COMPILE = {
|
|
|
525
508
|
const { required } = this;
|
|
526
509
|
return (value, context, key) => {
|
|
527
510
|
if (isBytes(value)) return;
|
|
528
|
-
if (!required && value
|
|
511
|
+
if (!required && (value === undefined || value === null)) return;
|
|
529
512
|
issues.type(context, 'bytes', value, key);
|
|
530
513
|
};
|
|
531
514
|
},
|
|
@@ -548,13 +531,13 @@ const COMPILE = {
|
|
|
548
531
|
const set = new Set(values);
|
|
549
532
|
return (value, context, key) => {
|
|
550
533
|
if (set.has(value)) return;
|
|
551
|
-
if (!required && value
|
|
534
|
+
if (!required && (value === undefined || value === null)) return;
|
|
552
535
|
issues.enum(context, values, key);
|
|
553
536
|
};
|
|
554
537
|
}
|
|
555
538
|
return (value, context, key) => {
|
|
556
539
|
if (values.includes(value)) return;
|
|
557
|
-
if (!required && value
|
|
540
|
+
if (!required && (value === undefined || value === null)) return;
|
|
558
541
|
issues.enum(context, values, key);
|
|
559
542
|
};
|
|
560
543
|
},
|
|
@@ -567,9 +550,8 @@ const COMPILE = {
|
|
|
567
550
|
const { check } = this.value;
|
|
568
551
|
return (value, context, key) => {
|
|
569
552
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
570
|
-
if (!required && value
|
|
571
|
-
issues.type(context, type, value, key);
|
|
572
|
-
return;
|
|
553
|
+
if (!required && (value === undefined || value === null)) return;
|
|
554
|
+
return void issues.type(context, type, value, key);
|
|
573
555
|
}
|
|
574
556
|
const nested = key !== undefined;
|
|
575
557
|
if (nested) context.path.push(key);
|
|
@@ -594,9 +576,8 @@ const COMPILE = {
|
|
|
594
576
|
const { check } = this.value;
|
|
595
577
|
return (value, context, key) => {
|
|
596
578
|
if (!isMap(value)) {
|
|
597
|
-
if (!required && value
|
|
598
|
-
issues.type(context, type, value, key);
|
|
599
|
-
return;
|
|
579
|
+
if (!required && (value === undefined || value === null)) return;
|
|
580
|
+
return void issues.type(context, type, value, key);
|
|
600
581
|
}
|
|
601
582
|
if (value.size === 0) {
|
|
602
583
|
if (required) issues.required(context, key);
|
|
@@ -626,28 +607,19 @@ const COMPILE = {
|
|
|
626
607
|
const { one, many, root, required, embed } = this;
|
|
627
608
|
if (one) {
|
|
628
609
|
return (value, context, key) => {
|
|
629
|
-
if (!required && value
|
|
610
|
+
if (!required && (value === undefined || value === null)) return;
|
|
630
611
|
const schema = root.findReference(one);
|
|
631
|
-
if (!schema)
|
|
632
|
-
issues.reference(context, one, key);
|
|
633
|
-
return;
|
|
634
|
-
}
|
|
612
|
+
if (!schema) return void issues.reference(context, one, key);
|
|
635
613
|
if (embeds(schema, embed, context.references)) runReferenced(schema, value, context, key);
|
|
636
614
|
else if (typeof value !== 'string') issues.type(context, 'string', value, key);
|
|
637
615
|
};
|
|
638
616
|
}
|
|
639
617
|
const expected = `array of ${many}`;
|
|
640
618
|
return (value, context, key) => {
|
|
641
|
-
if (!required && value
|
|
619
|
+
if (!required && (value === undefined || value === null)) return;
|
|
642
620
|
const schema = root.findReference(many);
|
|
643
|
-
if (!schema)
|
|
644
|
-
|
|
645
|
-
return;
|
|
646
|
-
}
|
|
647
|
-
if (!Array.isArray(value)) {
|
|
648
|
-
issues.type(context, expected, value, key);
|
|
649
|
-
return;
|
|
650
|
-
}
|
|
621
|
+
if (!schema) return void issues.reference(context, many, key);
|
|
622
|
+
if (!Array.isArray(value)) return void issues.type(context, expected, value, key);
|
|
651
623
|
const nested = key !== undefined;
|
|
652
624
|
if (nested) context.path.push(key);
|
|
653
625
|
const embedded = embeds(schema, embed, context.references);
|
|
@@ -666,7 +638,7 @@ const COMPILE = {
|
|
|
666
638
|
const { required } = this;
|
|
667
639
|
const check = isStruct(this.schema) ? checkOf(this.schema) : this.schema.check;
|
|
668
640
|
return (value, context, key) => {
|
|
669
|
-
if (!required && value
|
|
641
|
+
if (!required && (value === undefined || value === null)) return;
|
|
670
642
|
check(value, context, key);
|
|
671
643
|
};
|
|
672
644
|
},
|
|
@@ -678,13 +650,11 @@ const COMPILE = {
|
|
|
678
650
|
const { length } = checks;
|
|
679
651
|
return (value, context, key) => {
|
|
680
652
|
if (!Array.isArray(value)) {
|
|
681
|
-
if (!required && value
|
|
682
|
-
issues.type(context, type, value, key);
|
|
683
|
-
return;
|
|
653
|
+
if (!required && (value === undefined || value === null)) return;
|
|
654
|
+
return void issues.type(context, type, value, key);
|
|
684
655
|
}
|
|
685
656
|
if (value.length > length) {
|
|
686
|
-
issues.length(context, undefined, length, value.length, key);
|
|
687
|
-
return;
|
|
657
|
+
return void issues.length(context, undefined, length, value.length, key);
|
|
688
658
|
}
|
|
689
659
|
const nested = key !== undefined;
|
|
690
660
|
if (nested) context.path.push(key);
|
|
@@ -707,15 +677,11 @@ const COMPILE = {
|
|
|
707
677
|
for (const value of table.keys()) expected.push(value);
|
|
708
678
|
return (value, context, key) => {
|
|
709
679
|
if (value === null || typeof value !== 'object') {
|
|
710
|
-
if (!required && value
|
|
711
|
-
issues.type(context, 'object', value, key);
|
|
712
|
-
return;
|
|
680
|
+
if (!required && (value === undefined || value === null)) return;
|
|
681
|
+
return void issues.type(context, 'object', value, key);
|
|
713
682
|
}
|
|
714
683
|
const index = table.get(value[discriminator]);
|
|
715
|
-
if (index !== undefined)
|
|
716
|
-
checks[index](value, context, key);
|
|
717
|
-
return;
|
|
718
|
-
}
|
|
684
|
+
if (index !== undefined) return void checks[index](value, context, key);
|
|
719
685
|
const nested = key !== undefined;
|
|
720
686
|
if (nested) context.path.push(key);
|
|
721
687
|
issues.union(context, expected, discriminator, discriminator);
|
|
@@ -727,7 +693,7 @@ const COMPILE = {
|
|
|
727
693
|
const { required } = this;
|
|
728
694
|
return (value, context, key) => {
|
|
729
695
|
if (value !== null && typeof value === 'object') return;
|
|
730
|
-
if (!required && value
|
|
696
|
+
if (!required && (value === undefined || value === null)) return;
|
|
731
697
|
issues.type(context, 'object', value, key);
|
|
732
698
|
};
|
|
733
699
|
},
|
|
@@ -740,9 +706,8 @@ const COMPILE = {
|
|
|
740
706
|
for (const name of this.flags) known[name] = true;
|
|
741
707
|
return (value, context, key) => {
|
|
742
708
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
743
|
-
if (!required && value
|
|
744
|
-
issues.type(context, type, value, key);
|
|
745
|
-
return;
|
|
709
|
+
if (!required && (value === undefined || value === null)) return;
|
|
710
|
+
return void issues.type(context, type, value, key);
|
|
746
711
|
}
|
|
747
712
|
const nested = key !== undefined;
|
|
748
713
|
if (nested) context.path.push(key);
|
|
@@ -773,17 +738,17 @@ COMPILE.unknown = COMPILE.any;
|
|
|
773
738
|
COMPILE.set = COMPILE.array;
|
|
774
739
|
|
|
775
740
|
module.exports = {
|
|
776
|
-
COMPILE,
|
|
777
|
-
custom,
|
|
778
|
-
compileField,
|
|
779
|
-
checkOf,
|
|
780
|
-
aliasOf,
|
|
781
|
-
ruleNames,
|
|
782
|
-
compileRules,
|
|
783
|
-
patternLimit,
|
|
784
|
-
decimalsOff,
|
|
785
741
|
codePoints,
|
|
786
742
|
lengthOf,
|
|
743
|
+
patternLimit,
|
|
744
|
+
decimalsOff,
|
|
745
|
+
ruleNames,
|
|
746
|
+
compileRules,
|
|
747
|
+
compileField,
|
|
748
|
+
custom,
|
|
749
|
+
aliasOf,
|
|
750
|
+
checkOf,
|
|
787
751
|
nameOf,
|
|
788
752
|
tableOf,
|
|
753
|
+
COMPILE,
|
|
789
754
|
};
|