@alexify/metaschema 3.2.0 → 3.4.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 +307 -1
- package/README.md +173 -89
- package/SECURITY.md +24 -1
- package/index.d.ts +601 -29
- package/package.json +28 -3
- package/src/checks.js +789 -0
- package/src/codec.js +201 -46
- package/src/coerce/index.d.ts +34 -0
- package/src/coerce/index.js +385 -0
- package/src/compile/emit.js +87 -28
- package/src/compile/index.js +9 -4
- package/src/context.js +6 -1
- package/src/definition/index.d.ts +19 -0
- package/src/definition/index.js +27 -0
- package/src/derive/index.d.ts +86 -0
- package/src/derive/index.js +273 -0
- package/src/errors.js +46 -4
- package/src/formats/index.d.ts +20 -8
- package/src/formats/index.js +450 -39
- package/src/generators/fromjsonschema.js +21 -2
- package/src/generators/index.js +1 -1
- package/src/generators/jsonschema.js +90 -21
- package/src/generators/typescript.js +11 -0
- package/src/index.js +3 -2
- package/src/issues.js +9 -7
- package/src/kinds.js +15 -3
- package/src/lint.js +38 -0
- package/src/locales/en.js +6 -0
- package/src/locales/uk.js +6 -0
- package/src/metadata.js +6 -24
- package/src/model/data.js +67 -10
- package/src/model/diff.js +7 -4
- package/src/model/index.js +1 -1
- package/src/model/infer.js +10 -3
- package/src/model/merge.js +16 -9
- package/src/model/restore.js +52 -7
- package/src/model/sample.js +28 -4
- package/src/model.js +54 -56
- package/src/mongodb/bson.js +130 -0
- package/src/mongodb/document.js +217 -0
- package/src/mongodb/index.d.ts +133 -0
- package/src/mongodb/index.js +24 -0
- package/src/mongodb/indexes.js +79 -0
- package/src/mongodb/update.js +258 -0
- package/src/preprocessor.js +1 -2
- package/src/prototypes/abstract.js +41 -67
- 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 +0 -26
- package/src/prototypes/union.js +12 -82
- package/src/rules.js +33 -157
- package/src/schema.js +105 -220
- package/src/snapshot.js +216 -41
- package/src/stream/index.js +1 -1
- package/src/struct.js +6 -91
- package/src/types.js +43 -6
- package/src/util.js +43 -0
- package/src/validation.js +313 -0
package/src/codec.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
// What parse and serialize do beyond the check: the
|
|
2
|
-
// the serializers and the stripping of unknown
|
|
3
|
-
// types. `parse` is the check of the schema
|
|
4
|
-
// between two passes,
|
|
1
|
+
// What parse and serialize do beyond the check: the coercion of the input,
|
|
2
|
+
// the defaults, the decoders, the serializers and the stripping of unknown
|
|
3
|
+
// keys, as passes over the built types. `parse` is the check of the schema
|
|
4
|
+
// (whichever backend installed it) between two passes, coercion and defaults
|
|
5
|
+
// before and decoders after; `serialize` is one
|
|
5
6
|
// pass the other way. The passes read the same plan the closures walk and
|
|
6
7
|
// validate nothing themselves, so the JIT needs no second generator. A node
|
|
7
8
|
// whose subtree carries nothing for a phase is returned as it is, and a
|
|
@@ -19,13 +20,10 @@ const {
|
|
|
19
20
|
toDotPath,
|
|
20
21
|
} = require('./issues.js');
|
|
21
22
|
const { createContext } = require('./context.js');
|
|
22
|
-
const { STRUCT, isStruct
|
|
23
|
-
const {
|
|
24
|
-
const
|
|
25
|
-
const {
|
|
26
|
-
const { tuple } = require('./prototypes/tuple.js');
|
|
27
|
-
const { union, tableOf } = require('./prototypes/union.js');
|
|
28
|
-
const { reference, embeds } = require('./prototypes/reference.js');
|
|
23
|
+
const { STRUCT, isStruct } = require('./struct.js');
|
|
24
|
+
const { COMPILE, aliasOf, tableOf } = require('./checks.js');
|
|
25
|
+
const { asyncReturn, notRegistered } = require('./errors.js');
|
|
26
|
+
const { embeds } = require('./prototypes/reference.js');
|
|
29
27
|
|
|
30
28
|
// What a subtree carries, as bits; a phase is the bit it applies.
|
|
31
29
|
const DEFAULTS = 1;
|
|
@@ -36,6 +34,27 @@ const ASYNC = 16;
|
|
|
36
34
|
// A field with aliases below: the pass after the check visits it only when
|
|
37
35
|
// the check read a field from an alias (`context.renamed`).
|
|
38
36
|
const ALIASES = 32;
|
|
37
|
+
// On every info: a pass before the check that coerces enters every subtree,
|
|
38
|
+
// since any value of the input may need a conversion.
|
|
39
|
+
const COERCE = 64;
|
|
40
|
+
// A field with `coerce: true` of its own below: the pass before the check
|
|
41
|
+
// converts there whatever the policy of the call.
|
|
42
|
+
const POLICY = 128;
|
|
43
|
+
|
|
44
|
+
// The converters of coercion, by type name, are the `./coerce` subpath's:
|
|
45
|
+
// its register() installs `resolve` here (the converter of a node, or null)
|
|
46
|
+
// and says so. The converter resolved is kept on the info of the node with
|
|
47
|
+
// the `version` it was resolved under, which every register() bumps, so a
|
|
48
|
+
// leaf pays no lookup per visit. Until the subpath is registered a pass
|
|
49
|
+
// that coerces is a call that cannot work, and says so as it enters the
|
|
50
|
+
// schema (`walkSchema`), whatever the value holds.
|
|
51
|
+
const COERCION = {
|
|
52
|
+
installed: false,
|
|
53
|
+
version: 0,
|
|
54
|
+
resolve() {
|
|
55
|
+
throw notRegistered('coerce', 'coerce');
|
|
56
|
+
},
|
|
57
|
+
};
|
|
39
58
|
|
|
40
59
|
// The shape of a node: a Schema, a struct (the dictionary of its fields), or
|
|
41
60
|
// a field by the identity of its prototype's compile, as the JIT tells them
|
|
@@ -49,15 +68,17 @@ const KEYED = 5;
|
|
|
49
68
|
const TUPLE = 6;
|
|
50
69
|
const UNION = 7;
|
|
51
70
|
const REFERENCE = 8;
|
|
71
|
+
const FLAGS = 9;
|
|
52
72
|
|
|
53
73
|
const SHAPES = new Map([
|
|
54
|
-
[
|
|
55
|
-
[
|
|
56
|
-
[
|
|
57
|
-
[
|
|
58
|
-
[tuple
|
|
59
|
-
[union
|
|
60
|
-
[reference
|
|
74
|
+
[COMPILE.schema, NESTED],
|
|
75
|
+
[COMPILE.array, LIST],
|
|
76
|
+
[COMPILE.object, KEYED],
|
|
77
|
+
[COMPILE.map, KEYED],
|
|
78
|
+
[COMPILE.tuple, TUPLE],
|
|
79
|
+
[COMPILE.union, UNION],
|
|
80
|
+
[COMPILE.reference, REFERENCE],
|
|
81
|
+
[COMPILE.flags, FLAGS],
|
|
61
82
|
]);
|
|
62
83
|
|
|
63
84
|
const shapeOf = (node) => {
|
|
@@ -71,7 +92,9 @@ const isAsyncFunction = (fn) => typeof fn === 'function' && fn.constructor.name
|
|
|
71
92
|
|
|
72
93
|
// What the passes know about a node, computed on first use and kept apart
|
|
73
94
|
// from the node: its shape, the bits of its subtree, its own decoder and
|
|
74
|
-
// serializer, its
|
|
95
|
+
// serializer, its own coercion policy (a field's `coerce` key, which turns
|
|
96
|
+
// coercion on or off for its subtree), its children with their infos, the
|
|
97
|
+
// fields of a struct a phase
|
|
75
98
|
// has to visit (`parts`, by phase bit; every field under `strip`), and the
|
|
76
99
|
// reference fields beneath it (their targets are resolved at parse time, so a
|
|
77
100
|
// reference counts as carrying everything).
|
|
@@ -85,8 +108,14 @@ const infoOf = (node) => {
|
|
|
85
108
|
const own = shape === SCHEMA ? node.options : shape === FIELDS ? EMPTY : node;
|
|
86
109
|
const decode = own.parse || undefined;
|
|
87
110
|
const encode = own.serialize || undefined;
|
|
88
|
-
|
|
111
|
+
// A field that coerces makes the pass before the check enter its subtree
|
|
112
|
+
// whatever the policy of the call. An own key only: a `coerce` set on
|
|
113
|
+
// Object.prototype elsewhere must not turn every schema's on.
|
|
114
|
+
const policy = Object.hasOwn(own, 'coerce') ? own.coerce : undefined;
|
|
115
|
+
const coerce = policy === true ? true : policy === false ? false : undefined;
|
|
116
|
+
let bits = decode === undefined ? COERCE : COERCE | DECODERS;
|
|
89
117
|
if (encode !== undefined) bits |= ENCODERS;
|
|
118
|
+
if (coerce === true) bits |= DEFAULTS | POLICY;
|
|
90
119
|
if (isAsyncFunction(own.validate) || isAsyncFunction(decode) || isAsyncFunction(own.checkType)) {
|
|
91
120
|
bits |= ASYNC;
|
|
92
121
|
}
|
|
@@ -114,6 +143,9 @@ const infoOf = (node) => {
|
|
|
114
143
|
} else if (shape === REFERENCE) {
|
|
115
144
|
bits |= DEFAULTS | DECODERS | ENCODERS | STRUCTS;
|
|
116
145
|
refs.push(node);
|
|
146
|
+
} else if (shape === FLAGS) {
|
|
147
|
+
bits |= STRUCTS;
|
|
148
|
+
table = new Set(node.flags);
|
|
117
149
|
}
|
|
118
150
|
if (plan === null) {
|
|
119
151
|
for (let index = 0; index < children.length; index += 1) {
|
|
@@ -146,7 +178,26 @@ const infoOf = (node) => {
|
|
|
146
178
|
parts[STRUCTS].push(index);
|
|
147
179
|
}
|
|
148
180
|
}
|
|
149
|
-
|
|
181
|
+
// The walker of the node: the one of its shape, or for a field with a
|
|
182
|
+
// coercion policy of its own the one that applies the policy first.
|
|
183
|
+
const walk = coerce === undefined ? WALKERS[shape] : walkPolicy;
|
|
184
|
+
info = {
|
|
185
|
+
shape,
|
|
186
|
+
walk,
|
|
187
|
+
bits,
|
|
188
|
+
decode,
|
|
189
|
+
encode,
|
|
190
|
+
coerce,
|
|
191
|
+
convert: null,
|
|
192
|
+
version: -1,
|
|
193
|
+
children,
|
|
194
|
+
infos,
|
|
195
|
+
plan,
|
|
196
|
+
parts,
|
|
197
|
+
extra,
|
|
198
|
+
table,
|
|
199
|
+
refs,
|
|
200
|
+
};
|
|
150
201
|
INFO.set(node, info);
|
|
151
202
|
return info;
|
|
152
203
|
};
|
|
@@ -274,7 +325,22 @@ const decode = (info, node, value, context, key) => {
|
|
|
274
325
|
});
|
|
275
326
|
};
|
|
276
327
|
|
|
277
|
-
|
|
328
|
+
// Under coercion a leaf, a list, a tuple or a dictionary asks for a
|
|
329
|
+
// conversion of its value before anything else looks at it (a struct, a
|
|
330
|
+
// union or a reference has none of its own: their parts convert; a tuple
|
|
331
|
+
// and an `object` have no built-in converter, and get the value back). Done
|
|
332
|
+
// by the walkers so that apply stays as small as parse needs it to be
|
|
333
|
+
// inlined.
|
|
334
|
+
const convert = (want, value, node, info) => {
|
|
335
|
+
if ((want & COERCE) === 0) return value;
|
|
336
|
+
if (info.version !== COERCION.version) {
|
|
337
|
+
info.convert = COERCION.resolve(node);
|
|
338
|
+
info.version = COERCION.version;
|
|
339
|
+
}
|
|
340
|
+
return info.convert === null ? value : info.convert(value);
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const walkLeaf = (info, node, value, context, key, phase, want) => convert(want, value, node, info);
|
|
278
344
|
|
|
279
345
|
const walkInner = (info, node, value, context, key, phase, want) =>
|
|
280
346
|
apply(info.infos[0], info.children[0], value, context, key, phase, want);
|
|
@@ -290,6 +356,11 @@ const LOOP = Symbol('loop');
|
|
|
290
356
|
const looped = (context) => context.seen !== null && context.seen.has(LOOP);
|
|
291
357
|
|
|
292
358
|
const walkSchema = (info, node, value, context, key, phase, want) => {
|
|
359
|
+
// A pass that converts somewhere below (the policy of the call, or a
|
|
360
|
+
// field's own) needs the converters, whatever the value holds.
|
|
361
|
+
if (phase === DEFAULTS && !COERCION.installed && ((want & COERCE) | (info.bits & POLICY)) !== 0) {
|
|
362
|
+
throw notRegistered('coerce', 'coerce');
|
|
363
|
+
}
|
|
293
364
|
if (phase === DECODERS || info.refs.length === 0 || typeof value !== 'object') {
|
|
294
365
|
return walkInner(info, node, value, context, key, phase, want);
|
|
295
366
|
}
|
|
@@ -318,8 +389,9 @@ const renameKeys = (out, renames) => {
|
|
|
318
389
|
return result;
|
|
319
390
|
};
|
|
320
391
|
|
|
321
|
-
// The fields of a struct:
|
|
322
|
-
//
|
|
392
|
+
// The fields of a struct: before the check a present value is coerced and a
|
|
393
|
+
// default fills a missing key (coerced too: it is validated like any
|
|
394
|
+
// value), a decoder replaces the value after it. Stripping rebuilds the object from
|
|
323
395
|
// the known keys; otherwise the object is copied when a field changed. A
|
|
324
396
|
// value under an alias of a field is read from there (no default fills the
|
|
325
397
|
// field, and the check sees it there), and the pass after the check writes
|
|
@@ -329,7 +401,8 @@ const walkFields = (info, fields, value, context, key, phase, want) => {
|
|
|
329
401
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) return value;
|
|
330
402
|
const { plan, infos, extra } = info;
|
|
331
403
|
const strip = (want & STRUCTS) !== 0;
|
|
332
|
-
|
|
404
|
+
// Every field under strip, and under coercion: any of them may convert.
|
|
405
|
+
const parts = info.parts[(want & (STRUCTS | COERCE)) !== 0 ? STRUCTS : phase];
|
|
333
406
|
const nested = key !== undefined;
|
|
334
407
|
if (nested) context.path.push(key);
|
|
335
408
|
let out = strip ? {} : null;
|
|
@@ -346,14 +419,17 @@ const walkFields = (info, fields, value, context, key, phase, want) => {
|
|
|
346
419
|
if (from !== name) item = value[from];
|
|
347
420
|
}
|
|
348
421
|
if (phase === DEFAULTS) {
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
item = typeof fallback === 'function' ? fallback() : fallback;
|
|
353
|
-
out = written(out, value, copy, putKey, from, item);
|
|
422
|
+
let result = item;
|
|
423
|
+
if (item !== undefined) {
|
|
424
|
+
result = apply(infos[index], entry.type, item, context, from, phase, want);
|
|
354
425
|
}
|
|
355
|
-
const
|
|
356
|
-
if (result
|
|
426
|
+
const fallback = entry.type.default;
|
|
427
|
+
if (result === undefined && fallback !== undefined) {
|
|
428
|
+
const filled = typeof fallback === 'function' ? fallback() : fallback;
|
|
429
|
+
out = written(out, value, copy, putKey, from, filled);
|
|
430
|
+
result = apply(infos[index], entry.type, filled, context, from, phase, want);
|
|
431
|
+
if (result !== filled) out = written(out, value, copy, putKey, from, result);
|
|
432
|
+
} else if (result !== item) out = written(out, value, copy, putKey, from, result);
|
|
357
433
|
continue;
|
|
358
434
|
}
|
|
359
435
|
const result = apply(infos[index], entry.type, item, context, from, phase, want);
|
|
@@ -403,11 +479,25 @@ const walkItems = (children, infos, fixed, value, context, key, phase, want) =>
|
|
|
403
479
|
return settled(value, out, pending, putIndex, set ? toSet : undefined);
|
|
404
480
|
};
|
|
405
481
|
|
|
406
|
-
const walkList = (info, node, value, context, key, phase, want) =>
|
|
407
|
-
|
|
482
|
+
const walkList = (info, node, value, context, key, phase, want) => {
|
|
483
|
+
const input = convert(want, value, node, info);
|
|
484
|
+
if (input == null) return input;
|
|
485
|
+
return walkItems(
|
|
486
|
+
info.children,
|
|
487
|
+
info.infos,
|
|
488
|
+
info.shape === TUPLE,
|
|
489
|
+
input,
|
|
490
|
+
context,
|
|
491
|
+
key,
|
|
492
|
+
phase,
|
|
493
|
+
want,
|
|
494
|
+
);
|
|
495
|
+
};
|
|
408
496
|
|
|
409
497
|
// The values of an object or a Map, by key.
|
|
410
|
-
const walkKeyed = (info, node,
|
|
498
|
+
const walkKeyed = (info, node, given, context, key, phase, want) => {
|
|
499
|
+
const value = convert(want, given, node, info);
|
|
500
|
+
if (value == null) return value;
|
|
411
501
|
const map = isMap(value);
|
|
412
502
|
if (!map && (typeof value !== 'object' || Array.isArray(value))) return value;
|
|
413
503
|
const child = info.children[0];
|
|
@@ -480,7 +570,9 @@ const accepts = (branch, value, scratch, path, key) => {
|
|
|
480
570
|
// The branch of a union a value belongs to: by the discriminator, or the
|
|
481
571
|
// first branch whose check records nothing, as the check picks it. Before
|
|
482
572
|
// the check a branch is tried with its own defaults in place, and the value
|
|
483
|
-
// with those defaults is what parse goes on with.
|
|
573
|
+
// with those defaults is what parse goes on with. Under coercion a value
|
|
574
|
+
// some branch accepts as given is not converted: the branches are tried as
|
|
575
|
+
// given first, then converted.
|
|
484
576
|
const walkUnion = (info, node, value, context, key, phase, want) => {
|
|
485
577
|
const { children, infos, table } = info;
|
|
486
578
|
if (table !== null) {
|
|
@@ -489,14 +581,43 @@ const walkUnion = (info, node, value, context, key, phase, want) => {
|
|
|
489
581
|
if (index === undefined) return value;
|
|
490
582
|
return apply(infos[index], children[index], value, context, key, phase, want);
|
|
491
583
|
}
|
|
584
|
+
if (phase !== DEFAULTS) {
|
|
585
|
+
const scratch = scratchOf(context);
|
|
586
|
+
for (let index = 0; index < children.length; index += 1) {
|
|
587
|
+
if (!accepts(children[index], value, scratch, context.path, key)) continue;
|
|
588
|
+
return apply(infos[index], children[index], value, context, key, phase, want);
|
|
589
|
+
}
|
|
590
|
+
return value;
|
|
591
|
+
}
|
|
592
|
+
// Before the check, every branch is prepared first, with all the pass
|
|
593
|
+
// wants: when none of them changes the value (no default to fill, nothing
|
|
594
|
+
// to convert below) there is nothing to pick between, and the check picks
|
|
595
|
+
// the branch. The trial is a check of the subtree, so over a union of
|
|
596
|
+
// records that nest unions this is the difference between a linear and an
|
|
597
|
+
// exponential parse. What was prepared is what the last round picks from.
|
|
598
|
+
const prepared = [];
|
|
599
|
+
let same = true;
|
|
600
|
+
for (let index = 0; index < children.length; index += 1) {
|
|
601
|
+
const out = apply(infos[index], children[index], value, context, key, phase, want);
|
|
602
|
+
prepared.push(out);
|
|
603
|
+
if (out !== value) same = false;
|
|
604
|
+
}
|
|
605
|
+
if (same) return value;
|
|
492
606
|
const scratch = scratchOf(context);
|
|
607
|
+
if ((want & COERCE) !== 0) {
|
|
608
|
+
// As given first (with the defaults): a branch that accepts the value as
|
|
609
|
+
// it is wins, and nothing is converted.
|
|
610
|
+
const given = want & ~COERCE;
|
|
611
|
+
for (let index = 0; index < children.length; index += 1) {
|
|
612
|
+
const branch = children[index];
|
|
613
|
+
const out = apply(infos[index], branch, value, context, key, phase, given);
|
|
614
|
+
if (accepts(branch, out, scratch, context.path, key)) return out;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
493
617
|
for (let index = 0; index < children.length; index += 1) {
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
phase === DEFAULTS ? apply(sub, branch, value, context, key, phase, want) : value;
|
|
498
|
-
if (!accepts(branch, prepared, scratch, context.path, key)) continue;
|
|
499
|
-
return phase === DEFAULTS ? prepared : apply(sub, branch, value, context, key, phase, want);
|
|
618
|
+
if (accepts(children[index], prepared[index], scratch, context.path, key)) {
|
|
619
|
+
return prepared[index];
|
|
620
|
+
}
|
|
500
621
|
}
|
|
501
622
|
return value;
|
|
502
623
|
};
|
|
@@ -526,6 +647,35 @@ const walkReference = (info, node, value, context, key, phase, want) => {
|
|
|
526
647
|
return out;
|
|
527
648
|
};
|
|
528
649
|
|
|
650
|
+
// The names of a flags value: under strip the ones the field does not have
|
|
651
|
+
// are left out, the value copied only when it has one (a value of names of
|
|
652
|
+
// the list comes back as it is). The names are in a Set on the info; no
|
|
653
|
+
// name is `__proto__` (the type refuses it), so a plain object holds them.
|
|
654
|
+
const walkFlags = (info, node, given, context, key, phase, want) => {
|
|
655
|
+
const value = convert(want, given, node, info);
|
|
656
|
+
if ((want & STRUCTS) === 0 || value === null || typeof value !== 'object') return value;
|
|
657
|
+
const known = info.table;
|
|
658
|
+
let foreign = false;
|
|
659
|
+
for (const name in value) {
|
|
660
|
+
if (Object.hasOwn(value, name) && !known.has(name)) {
|
|
661
|
+
foreign = true;
|
|
662
|
+
break;
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
if (!foreign) return value;
|
|
666
|
+
const out = {};
|
|
667
|
+
for (const name of node.flags) if (Object.hasOwn(value, name)) out[name] = value[name];
|
|
668
|
+
return out;
|
|
669
|
+
};
|
|
670
|
+
|
|
671
|
+
// A field's own `coerce` turns coercion on or off for its subtree, in the
|
|
672
|
+
// pass before the check.
|
|
673
|
+
const walkPolicy = (info, node, value, context, key, phase, want) => {
|
|
674
|
+
let mode = want;
|
|
675
|
+
if (phase === DEFAULTS) mode = info.coerce ? want | COERCE : want & ~COERCE;
|
|
676
|
+
return WALKERS[info.shape](info, node, value, context, key, phase, mode);
|
|
677
|
+
};
|
|
678
|
+
|
|
529
679
|
const WALKERS = [
|
|
530
680
|
walkLeaf,
|
|
531
681
|
walkSchema,
|
|
@@ -536,11 +686,13 @@ const WALKERS = [
|
|
|
536
686
|
walkList,
|
|
537
687
|
walkUnion,
|
|
538
688
|
walkReference,
|
|
689
|
+
walkFlags,
|
|
539
690
|
];
|
|
540
691
|
|
|
541
692
|
// The bits a pass wants of a subtree, decided once per pass: its own, and
|
|
542
693
|
// for the pass after the check the structs (under `strip`) and the fields
|
|
543
|
-
// with aliases (after a check that read one)
|
|
694
|
+
// with aliases (after a check that read one); a parse that coerces adds
|
|
695
|
+
// COERCE to the pass before it.
|
|
544
696
|
const wantOf = (phase, context) => {
|
|
545
697
|
let want = phase;
|
|
546
698
|
if (phase === DECODERS) {
|
|
@@ -552,8 +704,9 @@ const wantOf = (phase, context) => {
|
|
|
552
704
|
|
|
553
705
|
// One pass over a value from a node whose info is known, by the shape of
|
|
554
706
|
// each node. A serializer runs before the parts of its value, a decoder
|
|
555
|
-
// after them
|
|
556
|
-
// is, which is the one test a
|
|
707
|
+
// after them, a conversion before them (in the walkers); a subtree without
|
|
708
|
+
// the bits the pass wants is returned as it is, which is the one test a
|
|
709
|
+
// value nothing applies to costs.
|
|
557
710
|
const apply = (info, node, value, context, key, phase, want) => {
|
|
558
711
|
if (value == null) return value;
|
|
559
712
|
if ((info.bits & want) === 0) return value;
|
|
@@ -562,7 +715,7 @@ const apply = (info, node, value, context, key, phase, want) => {
|
|
|
562
715
|
input = info.encode.call(node, value);
|
|
563
716
|
if (input == null) return input;
|
|
564
717
|
}
|
|
565
|
-
const out =
|
|
718
|
+
const out = info.walk(info, node, input, context, key, phase, want);
|
|
566
719
|
return phase === DECODERS ? decode(info, node, out, context, key) : out;
|
|
567
720
|
};
|
|
568
721
|
|
|
@@ -570,6 +723,8 @@ module.exports = {
|
|
|
570
723
|
PREPARE: DEFAULTS,
|
|
571
724
|
FINISH: DECODERS,
|
|
572
725
|
SERIALIZE: ENCODERS,
|
|
726
|
+
COERCE,
|
|
727
|
+
COERCION,
|
|
573
728
|
infoOf,
|
|
574
729
|
apply,
|
|
575
730
|
wantOf,
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { CoerceOptions, Coercion, FieldType, Schema } from '../../index.js';
|
|
2
|
+
|
|
3
|
+
// A converter: the value of a field of its type read from what the input
|
|
4
|
+
// gave, or the value itself when it cannot be read (the check then reports
|
|
5
|
+
// it as given). It never throws on purpose: a throw is taken as "cannot".
|
|
6
|
+
// Declared with the core (`ModelOptions.coercions` takes them too).
|
|
7
|
+
export type { Coercion };
|
|
8
|
+
|
|
9
|
+
// The converters by type name: the built-in ones (`string`, `number`,
|
|
10
|
+
// `integer`, `bigint`, `boolean`, `date`, `enum`, `array`, `set`, `map`) and
|
|
11
|
+
// the program's own, once `register()` ran.
|
|
12
|
+
export const COERCIONS: { readonly [name: string]: Coercion | undefined };
|
|
13
|
+
|
|
14
|
+
// The value with the conversions of the schema applied, and its defaults:
|
|
15
|
+
// the pass parse runs before its check with coercion on, nothing checked.
|
|
16
|
+
// The value given is never written to, and comes back itself when nothing
|
|
17
|
+
// changed. Needs `register()`: without it a conversion is a TypeError with
|
|
18
|
+
// `code: 'ERR_NOT_REGISTERED'`.
|
|
19
|
+
export function coerce(schema: Schema, value: unknown, options?: CoerceOptions): unknown;
|
|
20
|
+
|
|
21
|
+
// The options of `register`: `onError` is told of a converter that threw
|
|
22
|
+
// (the value is left to the check as if the converter could not read it, so
|
|
23
|
+
// a bug in a converter of the program would look the same without it).
|
|
24
|
+
export interface RegisterOptions {
|
|
25
|
+
onError?: (error: unknown, field: FieldType) => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Fills the table with the built-in converters and the program's own, by
|
|
29
|
+
// type name, once per name and process-wide (another function under a
|
|
30
|
+
// registered name is `ERR_COERCION_REGISTERED`, a name that is not one of a
|
|
31
|
+
// type or an entry that is not a function `ERR_INVALID_COERCION`; every
|
|
32
|
+
// entry is checked before any is written), and adds
|
|
33
|
+
// `schema.coerce(value, options)`. Importing the subpath registers nothing.
|
|
34
|
+
export function register(custom?: Record<string, Coercion>, options?: RegisterOptions): void;
|