@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/src/codec.js
CHANGED
|
@@ -81,15 +81,6 @@ const SHAPES = new Map([
|
|
|
81
81
|
[COMPILE.flags, FLAGS],
|
|
82
82
|
]);
|
|
83
83
|
|
|
84
|
-
const shapeOf = (node) => {
|
|
85
|
-
if (hasBrand(node, 'Schema')) return SCHEMA;
|
|
86
|
-
if (isStruct(node)) return FIELDS;
|
|
87
|
-
const shape = SHAPES.get(node.compile);
|
|
88
|
-
return shape === undefined ? LEAF : shape;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
const isAsyncFunction = (fn) => typeof fn === 'function' && fn.constructor.name === 'AsyncFunction';
|
|
92
|
-
|
|
93
84
|
// What the passes know about a node, computed on first use and kept apart
|
|
94
85
|
// from the node: its shape, the bits of its subtree, its own decoder and
|
|
95
86
|
// serializer, its own coercion policy (a field's `coerce` key, which turns
|
|
@@ -100,19 +91,65 @@ const isAsyncFunction = (fn) => typeof fn === 'function' && fn.constructor.name
|
|
|
100
91
|
// reference counts as carrying everything).
|
|
101
92
|
const INFO = new WeakMap();
|
|
102
93
|
|
|
94
|
+
// Before the check (the defaults, a serialization) a graph may loop through
|
|
95
|
+
// its references; the records on the way down are kept in `seen`, which the
|
|
96
|
+
// caller clears before the check. A loop is marked there: the copies the
|
|
97
|
+
// defaults make cannot close it, so parse checks such a value as it was
|
|
98
|
+
// given, and the check reports the loop. After the check a loop cannot be
|
|
99
|
+
// there.
|
|
100
|
+
const LOOP = Symbol('loop');
|
|
101
|
+
|
|
102
|
+
const shapeOf = (node) => {
|
|
103
|
+
if (hasBrand(node, 'Schema')) return SCHEMA;
|
|
104
|
+
if (isStruct(node)) return FIELDS;
|
|
105
|
+
const shape = SHAPES.get(node.compile);
|
|
106
|
+
return shape === undefined ? LEAF : shape;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const isAsyncFunction = (fn) => typeof fn === 'function' && fn.constructor.name === 'AsyncFunction';
|
|
110
|
+
|
|
111
|
+
// One pass over the plan of a struct: the child and its info of every
|
|
112
|
+
// field into the lists given, and the fields each phase has to visit
|
|
113
|
+
// (every field under strip) into `parts`; the bits of the fields.
|
|
114
|
+
const fillParts = (plan, parts, children, infos, refs) => {
|
|
115
|
+
let bits = 0;
|
|
116
|
+
for (let index = 0; index < plan.length; index += 1) {
|
|
117
|
+
const { type } = plan[index];
|
|
118
|
+
const childInfo = infoOf(type);
|
|
119
|
+
children.push(type);
|
|
120
|
+
infos.push(childInfo);
|
|
121
|
+
bits |= childInfo.bits;
|
|
122
|
+
for (let at = 0; at < childInfo.refs.length; at += 1) refs.push(childInfo.refs[at]);
|
|
123
|
+
const defaulted = type.default !== undefined;
|
|
124
|
+
if (defaulted) bits |= DEFAULTS;
|
|
125
|
+
if (defaulted || (childInfo.bits & DEFAULTS) !== 0) parts[DEFAULTS].push(index);
|
|
126
|
+
// A field with aliases, or with one below, is visited by the pass
|
|
127
|
+
// that decodes, which renames; whether that pass enters the subtree
|
|
128
|
+
// is decided by the bits and the context at the field.
|
|
129
|
+
const aliased = plan[index].aliases.length > 0;
|
|
130
|
+
if (aliased) bits |= ALIASES;
|
|
131
|
+
if (aliased || (childInfo.bits & (DECODERS | ALIASES)) !== 0) parts[DECODERS].push(index);
|
|
132
|
+
if ((childInfo.bits & ENCODERS) !== 0) parts[ENCODERS].push(index);
|
|
133
|
+
parts[STRUCTS].push(index);
|
|
134
|
+
}
|
|
135
|
+
return bits;
|
|
136
|
+
};
|
|
137
|
+
|
|
103
138
|
const infoOf = (node) => {
|
|
104
139
|
let info = INFO.get(node);
|
|
105
140
|
if (info !== undefined) return info;
|
|
106
141
|
const shape = shapeOf(node);
|
|
107
142
|
// A struct's own functions belong to the node that holds it.
|
|
108
|
-
|
|
143
|
+
let own = node;
|
|
144
|
+
if (shape === SCHEMA) own = node.options;
|
|
145
|
+
else if (shape === FIELDS) own = EMPTY;
|
|
109
146
|
const decode = own.parse || undefined;
|
|
110
147
|
const encode = own.serialize || undefined;
|
|
111
148
|
// A field that coerces makes the pass before the check enter its subtree
|
|
112
149
|
// whatever the policy of the call. An own key only: a `coerce` set on
|
|
113
150
|
// Object.prototype elsewhere must not turn every schema's on.
|
|
114
151
|
const policy = Object.hasOwn(own, 'coerce') ? own.coerce : undefined;
|
|
115
|
-
const coerce = policy ===
|
|
152
|
+
const coerce = typeof policy === 'boolean' ? policy : undefined;
|
|
116
153
|
let bits = decode === undefined ? COERCE : COERCE | DECODERS;
|
|
117
154
|
if (encode !== undefined) bits |= ENCODERS;
|
|
118
155
|
if (coerce === true) bits |= DEFAULTS | POLICY;
|
|
@@ -149,34 +186,14 @@ const infoOf = (node) => {
|
|
|
149
186
|
}
|
|
150
187
|
if (plan === null) {
|
|
151
188
|
for (let index = 0; index < children.length; index += 1) {
|
|
152
|
-
const
|
|
153
|
-
infos.push(
|
|
154
|
-
bits |=
|
|
155
|
-
for (let at = 0; at <
|
|
189
|
+
const childInfo = infoOf(children[index]);
|
|
190
|
+
infos.push(childInfo);
|
|
191
|
+
bits |= childInfo.bits;
|
|
192
|
+
for (let at = 0; at < childInfo.refs.length; at += 1) refs.push(childInfo.refs[at]);
|
|
156
193
|
}
|
|
157
194
|
} else {
|
|
158
|
-
// One pass over the plan: the child and its info, and the fields each
|
|
159
|
-
// phase has to visit (every field under strip).
|
|
160
195
|
parts = { [DEFAULTS]: [], [DECODERS]: [], [ENCODERS]: [], [STRUCTS]: [] };
|
|
161
|
-
|
|
162
|
-
const { type } = plan[index];
|
|
163
|
-
const sub = infoOf(type);
|
|
164
|
-
children.push(type);
|
|
165
|
-
infos.push(sub);
|
|
166
|
-
bits |= sub.bits;
|
|
167
|
-
for (let at = 0; at < sub.refs.length; at += 1) refs.push(sub.refs[at]);
|
|
168
|
-
const defaulted = type.default !== undefined;
|
|
169
|
-
if (defaulted) bits |= DEFAULTS;
|
|
170
|
-
if (defaulted || (sub.bits & DEFAULTS) !== 0) parts[DEFAULTS].push(index);
|
|
171
|
-
// A field with aliases, or with one below, is visited by the pass
|
|
172
|
-
// that decodes, which renames; whether that pass enters the subtree
|
|
173
|
-
// is decided by the bits and the context at the field.
|
|
174
|
-
const aliased = plan[index].aliases.length > 0;
|
|
175
|
-
if (aliased) bits |= ALIASES;
|
|
176
|
-
if (aliased || (sub.bits & (DECODERS | ALIASES)) !== 0) parts[DECODERS].push(index);
|
|
177
|
-
if ((sub.bits & ENCODERS) !== 0) parts[ENCODERS].push(index);
|
|
178
|
-
parts[STRUCTS].push(index);
|
|
179
|
-
}
|
|
196
|
+
bits |= fillParts(plan, parts, children, infos, refs);
|
|
180
197
|
}
|
|
181
198
|
// The walker of the node: the one of its shape, or for a field with a
|
|
182
199
|
// coercion policy of its own the one that applies the policy first.
|
|
@@ -304,7 +321,7 @@ const decode = (info, node, value, context, key) => {
|
|
|
304
321
|
return out instanceof Promise ? out : boxed(out);
|
|
305
322
|
});
|
|
306
323
|
}
|
|
307
|
-
let out;
|
|
324
|
+
let out = undefined;
|
|
308
325
|
try {
|
|
309
326
|
out = fn.call(node, value);
|
|
310
327
|
} catch (error) {
|
|
@@ -345,14 +362,6 @@ const walkLeaf = (info, node, value, context, key, phase, want) => convert(want,
|
|
|
345
362
|
const walkInner = (info, node, value, context, key, phase, want) =>
|
|
346
363
|
apply(info.infos[0], info.children[0], value, context, key, phase, want);
|
|
347
364
|
|
|
348
|
-
// Before the check (the defaults, a serialization) a graph may loop through
|
|
349
|
-
// its references; the records on the way down are kept in `seen`, which the
|
|
350
|
-
// caller clears before the check. A loop is marked there: the copies the
|
|
351
|
-
// defaults make cannot close it, so parse checks such a value as it was
|
|
352
|
-
// given, and the check reports the loop. After the check a loop cannot be
|
|
353
|
-
// there.
|
|
354
|
-
const LOOP = Symbol('loop');
|
|
355
|
-
|
|
356
365
|
const looped = (context) => context.seen !== null && context.seen.has(LOOP);
|
|
357
366
|
|
|
358
367
|
const walkSchema = (info, node, value, context, key, phase, want) => {
|
|
@@ -481,7 +490,7 @@ const walkItems = (children, infos, fixed, value, context, key, phase, want) =>
|
|
|
481
490
|
|
|
482
491
|
const walkList = (info, node, value, context, key, phase, want) => {
|
|
483
492
|
const input = convert(want, value, node, info);
|
|
484
|
-
if (input
|
|
493
|
+
if (input === undefined || input === null) return input;
|
|
485
494
|
return walkItems(
|
|
486
495
|
info.children,
|
|
487
496
|
info.infos,
|
|
@@ -497,11 +506,11 @@ const walkList = (info, node, value, context, key, phase, want) => {
|
|
|
497
506
|
// The values of an object or a Map, by key.
|
|
498
507
|
const walkKeyed = (info, node, given, context, key, phase, want) => {
|
|
499
508
|
const value = convert(want, given, node, info);
|
|
500
|
-
if (value
|
|
509
|
+
if (value === undefined || value === null) return value;
|
|
501
510
|
const map = isMap(value);
|
|
502
511
|
if (!map && (typeof value !== 'object' || Array.isArray(value))) return value;
|
|
503
512
|
const child = info.children[0];
|
|
504
|
-
const
|
|
513
|
+
const childInfo = info.infos[0];
|
|
505
514
|
const clone = map ? toMap : copy;
|
|
506
515
|
const put = map ? putEntry : putKey;
|
|
507
516
|
const nested = key !== undefined;
|
|
@@ -512,7 +521,7 @@ const walkKeyed = (info, node, given, context, key, phase, want) => {
|
|
|
512
521
|
for (const pair of value) {
|
|
513
522
|
const name = pair[0];
|
|
514
523
|
const item = pair[1];
|
|
515
|
-
const result = apply(
|
|
524
|
+
const result = apply(childInfo, child, item, context, name, phase, want);
|
|
516
525
|
if (result !== item) {
|
|
517
526
|
out = written(out, value, clone, put, name, result);
|
|
518
527
|
if (phase === DECODERS && isPending(context, result)) {
|
|
@@ -525,7 +534,7 @@ const walkKeyed = (info, node, given, context, key, phase, want) => {
|
|
|
525
534
|
for (const name in value) {
|
|
526
535
|
if (!Object.hasOwn(value, name)) continue;
|
|
527
536
|
const item = value[name];
|
|
528
|
-
const result = apply(
|
|
537
|
+
const result = apply(childInfo, child, item, context, name, phase, want);
|
|
529
538
|
if (result !== item) {
|
|
530
539
|
out = written(out, value, clone, put, name, result);
|
|
531
540
|
if (phase === DECODERS && isPending(context, result)) {
|
|
@@ -630,16 +639,16 @@ const walkReference = (info, node, value, context, key, phase, want) => {
|
|
|
630
639
|
const target = root.findReference(one || many);
|
|
631
640
|
if (target === null || !embeds(target, embed, context.references)) return value;
|
|
632
641
|
if (context.depth >= context.maxDepth) return value;
|
|
633
|
-
const
|
|
642
|
+
const childInfo = infoOf(target);
|
|
634
643
|
context.depth += 1;
|
|
635
644
|
let out = value;
|
|
636
|
-
if (one) out = apply(
|
|
645
|
+
if (one) out = apply(childInfo, target, value, context, key, phase, want);
|
|
637
646
|
else if (Array.isArray(value)) {
|
|
638
647
|
// The target as the one child of the list, kept on the info from one
|
|
639
648
|
// visit to the next (it changes only when the namespaces do).
|
|
640
649
|
if (info.children[0] !== target) {
|
|
641
650
|
info.children[0] = target;
|
|
642
|
-
info.infos[0] =
|
|
651
|
+
info.infos[0] = childInfo;
|
|
643
652
|
}
|
|
644
653
|
out = walkItems(info.children, info.infos, false, value, context, key, phase, want);
|
|
645
654
|
}
|
|
@@ -708,12 +717,12 @@ const wantOf = (phase, context) => {
|
|
|
708
717
|
// the bits the pass wants is returned as it is, which is the one test a
|
|
709
718
|
// value nothing applies to costs.
|
|
710
719
|
const apply = (info, node, value, context, key, phase, want) => {
|
|
711
|
-
if (value
|
|
720
|
+
if (value === undefined || value === null) return value;
|
|
712
721
|
if ((info.bits & want) === 0) return value;
|
|
713
722
|
let input = value;
|
|
714
723
|
if (phase === ENCODERS && info.encode !== undefined) {
|
|
715
724
|
input = info.encode.call(node, value);
|
|
716
|
-
if (input
|
|
725
|
+
if (input === undefined || input === null) return input;
|
|
717
726
|
}
|
|
718
727
|
const out = info.walk(info, node, input, context, key, phase, want);
|
|
719
728
|
return phase === DECODERS ? decode(info, node, out, context, key) : out;
|
|
@@ -726,8 +735,8 @@ module.exports = {
|
|
|
726
735
|
COERCE,
|
|
727
736
|
COERCION,
|
|
728
737
|
infoOf,
|
|
729
|
-
apply,
|
|
730
|
-
wantOf,
|
|
731
|
-
looped,
|
|
732
738
|
hasAsync,
|
|
739
|
+
looped,
|
|
740
|
+
wantOf,
|
|
741
|
+
apply,
|
|
733
742
|
};
|
package/src/coerce/index.d.ts
CHANGED
|
@@ -1,34 +1,44 @@
|
|
|
1
1
|
import type { CoerceOptions, Coercion, FieldType, Schema } from '../../index.js';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
/**
|
|
4
|
+
* A converter: the value of a field of its type read from what the input
|
|
5
|
+
* gave, or the value itself when it cannot be read (the check then reports
|
|
6
|
+
* it as given). It never throws on purpose: a throw is taken as "cannot".
|
|
7
|
+
* Declared with the core (`ModelOptions.coercions` takes them too).
|
|
8
|
+
*/
|
|
7
9
|
export type { Coercion };
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* The converters by type name: the built-in ones (`string`, `number`,
|
|
13
|
+
* `integer`, `bigint`, `boolean`, `date`, `enum`, `array`, `set`, `map`) and
|
|
14
|
+
* the program's own, once `register()` ran.
|
|
15
|
+
*/
|
|
12
16
|
export const COERCIONS: { readonly [name: string]: Coercion | undefined };
|
|
13
17
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
/**
|
|
19
|
+
* The value with the conversions of the schema applied, and its defaults:
|
|
20
|
+
* the pass parse runs before its check with coercion on, nothing checked.
|
|
21
|
+
* The value given is never written to, and comes back itself when nothing
|
|
22
|
+
* changed. Needs `register()`: without it a conversion is a SchemaUsageError with
|
|
23
|
+
* `code: 'ALX_ERR_NOT_REGISTERED'`.
|
|
24
|
+
*/
|
|
19
25
|
export function coerce(schema: Schema, value: unknown, options?: CoerceOptions): unknown;
|
|
20
26
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
27
|
+
/**
|
|
28
|
+
* The options of `register`: `onError` is told of a converter that threw
|
|
29
|
+
* (the value is left to the check as if the converter could not read it, so
|
|
30
|
+
* a bug in a converter of the program would look the same without it).
|
|
31
|
+
*/
|
|
24
32
|
export interface RegisterOptions {
|
|
25
33
|
onError?: (error: unknown, field: FieldType) => void;
|
|
26
34
|
}
|
|
27
35
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
36
|
+
/**
|
|
37
|
+
* Fills the table with the built-in converters and the program's own, by
|
|
38
|
+
* type name, once per name and process-wide (another function under a
|
|
39
|
+
* registered name is `ALX_ERR_COERCION_REGISTERED`, a name that is not one of a
|
|
40
|
+
* type or an entry that is not a function `ALX_ERR_INVALID_COERCION`; every
|
|
41
|
+
* entry is checked before any is written), and adds
|
|
42
|
+
* `schema.coerce(value, options)`. Importing the subpath registers nothing.
|
|
43
|
+
*/
|
|
34
44
|
export function register(custom?: Record<string, Coercion>, options?: RegisterOptions): void;
|
package/src/coerce/index.js
CHANGED
|
@@ -11,6 +11,7 @@ const { isFirstLower } = require('../metautil.js');
|
|
|
11
11
|
const { Schema } = require('../validation.js');
|
|
12
12
|
const { createContext } = require('../context.js');
|
|
13
13
|
const { COERCE, COERCION, PREPARE, infoOf, apply, wantOf, looped } = require('../codec.js');
|
|
14
|
+
const { isThenable, abandon } = require('../issues.js');
|
|
14
15
|
const { SchemaDefinitionError, invalidTarget, invalidOption } = require('../errors.js');
|
|
15
16
|
|
|
16
17
|
// The integer digits and the fraction are told apart by the dot alone:
|
|
@@ -22,6 +23,22 @@ const ISO =
|
|
|
22
23
|
/^(\d{4})-(\d{2})-(\d{2})(?:[Tt ]([01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?(?:[Zz]|[+-](?:[01]\d|2[0-3]):[0-5]\d))?$/;
|
|
23
24
|
const SAFE = BigInt(Number.MAX_SAFE_INTEGER);
|
|
24
25
|
|
|
26
|
+
// Canonical base64 (RFC 4648 §4): padded, and with the bits the padding
|
|
27
|
+
// leaves unused at zero, so that one text is one value; what a decoder
|
|
28
|
+
// would forgive (whitespace, a missing `=`, the URL alphabet) is left to
|
|
29
|
+
// the check. A charset, a length and the last quantum rather than one
|
|
30
|
+
// expression of quanta, whose repetition grows the stack of the engine by a
|
|
31
|
+
// frame each and overflowed it on a valid text of a few megabytes (the
|
|
32
|
+
// formats subpath reads base64 the same way). `''` is the text of no bytes.
|
|
33
|
+
const ALPHABET = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
34
|
+
|
|
35
|
+
// The types whose values are written as text as they read back: an enum
|
|
36
|
+
// member of one of them matches the text `${member}`.
|
|
37
|
+
const PRIMITIVES = new Set(['number', 'boolean', 'bigint']);
|
|
38
|
+
|
|
39
|
+
// The table: a converter by type name, `(value, field) => value`.
|
|
40
|
+
const COERCIONS = Object.create(null);
|
|
41
|
+
|
|
25
42
|
const toNumber = (value) => {
|
|
26
43
|
if (typeof value === 'string') {
|
|
27
44
|
if (!DECIMAL.test(value)) return value;
|
|
@@ -76,19 +93,10 @@ const toDate = (value) => {
|
|
|
76
93
|
|
|
77
94
|
const toText = (value) => {
|
|
78
95
|
const type = typeof value;
|
|
79
|
-
if (type === 'number') return Number.isFinite(value) ?
|
|
80
|
-
return type === 'bigint' || type === 'boolean' ?
|
|
96
|
+
if (type === 'number') return Number.isFinite(value) ? `${value}` : value;
|
|
97
|
+
return type === 'bigint' || type === 'boolean' ? `${value}` : value;
|
|
81
98
|
};
|
|
82
99
|
|
|
83
|
-
// Canonical base64 (RFC 4648 §4): padded, and with the bits the padding
|
|
84
|
-
// leaves unused at zero, so that one text is one value; what a decoder
|
|
85
|
-
// would forgive (whitespace, a missing `=`, the URL alphabet) is left to
|
|
86
|
-
// the check. A charset, a length and the last quantum rather than one
|
|
87
|
-
// expression of quanta, whose repetition grows the stack of the engine by a
|
|
88
|
-
// frame each and overflowed it on a valid text of a few megabytes (the
|
|
89
|
-
// formats subpath reads base64 the same way). `''` is the text of no bytes.
|
|
90
|
-
const ALPHABET = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
91
|
-
|
|
92
100
|
const isBase64 = (text) => {
|
|
93
101
|
const { length } = text;
|
|
94
102
|
if (length % 4 !== 0 || !ALPHABET.test(text)) return false;
|
|
@@ -103,7 +111,7 @@ const isBase64 = (text) => {
|
|
|
103
111
|
const toBytes = (value, field) => {
|
|
104
112
|
if (typeof value !== 'string' || !isBase64(value)) return value;
|
|
105
113
|
const max = field.length?.max;
|
|
106
|
-
const pad = value.endsWith('
|
|
114
|
+
const pad = (value.endsWith('=') ? 1 : 0) + (value.endsWith('==') ? 1 : 0);
|
|
107
115
|
if (max !== undefined && (value.length / 4) * 3 - pad > max) return value;
|
|
108
116
|
if (Uint8Array.fromBase64 !== undefined) return Uint8Array.fromBase64(value);
|
|
109
117
|
const text = atob(value);
|
|
@@ -112,10 +120,7 @@ const toBytes = (value, field) => {
|
|
|
112
120
|
return bytes;
|
|
113
121
|
};
|
|
114
122
|
|
|
115
|
-
const isPrimitive = (value) =>
|
|
116
|
-
const type = typeof value;
|
|
117
|
-
return type === 'number' || type === 'boolean' || type === 'bigint';
|
|
118
|
-
};
|
|
123
|
+
const isPrimitive = (value) => PRIMITIVES.has(typeof value);
|
|
119
124
|
|
|
120
125
|
// The member a value is written as: a string for a number, a boolean or a
|
|
121
126
|
// bigint member, or one of those for a string member.
|
|
@@ -125,7 +130,7 @@ const toEnum = (value, field) => {
|
|
|
125
130
|
const text = typeof value === 'string';
|
|
126
131
|
if (!text && !isPrimitive(value)) return value;
|
|
127
132
|
for (const member of members) {
|
|
128
|
-
if (text ? isPrimitive(member) &&
|
|
133
|
+
if (text ? isPrimitive(member) && `${member}` === value : member === `${value}`) {
|
|
129
134
|
return member;
|
|
130
135
|
}
|
|
131
136
|
}
|
|
@@ -141,7 +146,7 @@ const enumConverterOf = (field) => {
|
|
|
141
146
|
for (const member of field.enum) {
|
|
142
147
|
if (typeof member === 'string') strings.add(member);
|
|
143
148
|
else if (isPrimitive(member)) {
|
|
144
|
-
const text =
|
|
149
|
+
const text = `${member}`;
|
|
145
150
|
if (!written.has(text)) written.set(text, member);
|
|
146
151
|
}
|
|
147
152
|
}
|
|
@@ -149,7 +154,7 @@ const enumConverterOf = (field) => {
|
|
|
149
154
|
if (members.has(value)) return value;
|
|
150
155
|
if (typeof value === 'string') return written.has(value) ? written.get(value) : value;
|
|
151
156
|
if (!isPrimitive(value)) return value;
|
|
152
|
-
const text =
|
|
157
|
+
const text = `${value}`;
|
|
153
158
|
return strings.has(text) ? text : value;
|
|
154
159
|
};
|
|
155
160
|
};
|
|
@@ -179,16 +184,27 @@ const isPlain = (value) => {
|
|
|
179
184
|
return prototype === null || Object.getPrototypeOf(prototype) === null;
|
|
180
185
|
};
|
|
181
186
|
|
|
182
|
-
//
|
|
183
|
-
|
|
187
|
+
// The keys of a map of numbers that are written as numbers become numbers.
|
|
188
|
+
const mapKey = (key, field) =>
|
|
189
|
+
field.key === 'number' && typeof key === 'string' ? toNumber(key) : key;
|
|
190
|
+
|
|
191
|
+
// A plain object or a list of pairs.
|
|
184
192
|
const toMap = (value, field) => {
|
|
185
193
|
if (value === null || typeof value !== 'object') return value;
|
|
186
194
|
const list = Array.isArray(value);
|
|
187
195
|
if (list ? !isPairs(value) : !isPlain(value)) return value;
|
|
188
196
|
const map = new Map();
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
197
|
+
if (list) {
|
|
198
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
199
|
+
const pair = value[index];
|
|
200
|
+
map.set(mapKey(pair[0], field), pair[1]);
|
|
201
|
+
}
|
|
202
|
+
return map;
|
|
203
|
+
}
|
|
204
|
+
const keys = Object.keys(value);
|
|
205
|
+
for (let index = 0; index < keys.length; index += 1) {
|
|
206
|
+
const key = keys[index];
|
|
207
|
+
map.set(mapKey(key, field), value[key]);
|
|
192
208
|
}
|
|
193
209
|
return map;
|
|
194
210
|
};
|
|
@@ -207,10 +223,8 @@ const BUILT_IN = {
|
|
|
207
223
|
map: toMap,
|
|
208
224
|
};
|
|
209
225
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
const invalidCoercion = (reason) => new SchemaDefinitionError('ERR_INVALID_COERCION', reason);
|
|
226
|
+
const invalidCoercion = (reason) =>
|
|
227
|
+
new SchemaDefinitionError(reason, { code: 'ALX_ERR_INVALID_COERCION' });
|
|
214
228
|
|
|
215
229
|
// The converter of a field: its type's, or the one of the type it is an
|
|
216
230
|
// alias of (`{ money: { js: 'number' } }` converts as a number); an alias
|
|
@@ -221,8 +235,8 @@ const invalidCoercion = (reason) => new SchemaDefinitionError('ERR_INVALID_COERC
|
|
|
221
235
|
// process-wide one: the models among the namespaces of the schema.
|
|
222
236
|
const ownConverterOf = (root, name) => {
|
|
223
237
|
for (const namespace of root.namespaces) {
|
|
224
|
-
const own = namespace.coercions;
|
|
225
|
-
if (own
|
|
238
|
+
const own = namespace.coercions?.[name];
|
|
239
|
+
if (own !== undefined) return own;
|
|
226
240
|
}
|
|
227
241
|
return undefined;
|
|
228
242
|
};
|
|
@@ -240,6 +254,18 @@ const converterOf = (field) => {
|
|
|
240
254
|
}
|
|
241
255
|
};
|
|
242
256
|
|
|
257
|
+
// An observer never fails the operation it watches (alexiskills,
|
|
258
|
+
// library-errors): what the hook throws, or a promise of it that rejects, is
|
|
259
|
+
// dropped. A copy per subpath, as `install`, so the core carries none.
|
|
260
|
+
const notify = (observer, first, second) => {
|
|
261
|
+
try {
|
|
262
|
+
const returned = observer(first, second);
|
|
263
|
+
if (isThenable(returned)) abandon(returned);
|
|
264
|
+
} catch {
|
|
265
|
+
// ignore: an observer never fails the operation it watches
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
243
269
|
// The hook `register({ onError })` gives: told of a converter that threw,
|
|
244
270
|
// with the field, since the value is left to the check as if the converter
|
|
245
271
|
// could not read it, and a bug in a converter of the program would look the
|
|
@@ -258,7 +284,7 @@ const resolve = (node) => {
|
|
|
258
284
|
try {
|
|
259
285
|
return run(value, node);
|
|
260
286
|
} catch (error) {
|
|
261
|
-
if (onError !== null) onError
|
|
287
|
+
if (onError !== null) notify(onError, error, node);
|
|
262
288
|
return value;
|
|
263
289
|
}
|
|
264
290
|
};
|
|
@@ -274,7 +300,7 @@ const check = (name, converter) => {
|
|
|
274
300
|
const known = COERCIONS[name] ?? BUILT_IN[name];
|
|
275
301
|
if (known !== undefined && known !== converter) {
|
|
276
302
|
const reason = `Converter "${name}" is registered already with another function`;
|
|
277
|
-
throw new SchemaDefinitionError('
|
|
303
|
+
throw new SchemaDefinitionError(reason, { code: 'ALX_ERR_COERCION_REGISTERED' });
|
|
278
304
|
}
|
|
279
305
|
};
|
|
280
306
|
|
|
@@ -287,23 +313,14 @@ const report = (input, output, path, paths) => {
|
|
|
287
313
|
if (output === input) return;
|
|
288
314
|
const container = output !== null && typeof output === 'object';
|
|
289
315
|
const given = input !== null && typeof input === 'object';
|
|
290
|
-
if (!container || !given)
|
|
291
|
-
paths.push(path);
|
|
292
|
-
return;
|
|
293
|
-
}
|
|
316
|
+
if (!container || !given) return void paths.push(path);
|
|
294
317
|
if (Array.isArray(output)) {
|
|
295
|
-
if (!Array.isArray(input))
|
|
296
|
-
paths.push(path);
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
318
|
+
if (!Array.isArray(input)) return void paths.push(path);
|
|
299
319
|
for (let index = 0; index < output.length; index += 1) {
|
|
300
320
|
report(input[index], output[index], [...path, index], paths);
|
|
301
321
|
}
|
|
302
322
|
} else if (isMap(output)) {
|
|
303
|
-
if (!isMap(input))
|
|
304
|
-
paths.push(path);
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
323
|
+
if (!isMap(input)) return void paths.push(path);
|
|
307
324
|
for (const pair of output) report(input.get(pair[0]), pair[1], [...path, pair[0]], paths);
|
|
308
325
|
} else if (!isPlain(output) || !isPlain(input)) {
|
|
309
326
|
paths.push(path);
|
|
@@ -350,7 +367,7 @@ let installed = false;
|
|
|
350
367
|
|
|
351
368
|
// Fills the table with the built-in converters and the program's own, once
|
|
352
369
|
// per name, process-wide (another function under a registered name is
|
|
353
|
-
//
|
|
370
|
+
// ALX_ERR_COERCION_REGISTERED; a converter of an alias type is the way to read a
|
|
354
371
|
// type differently), and adds `schema.coerce(value, options)`. Every entry
|
|
355
372
|
// is checked before any is written, so a call that throws leaves the table
|
|
356
373
|
// as it was. `options.onError` is told of a converter that threw.
|
|
@@ -359,11 +376,11 @@ const register = (custom = {}, settings = {}) => {
|
|
|
359
376
|
throw invalidCoercion('The converters must be an object of functions by type name');
|
|
360
377
|
}
|
|
361
378
|
if (settings === null || typeof settings !== 'object') {
|
|
362
|
-
throw
|
|
379
|
+
throw invalidOption('The options of register', 'an object', settings);
|
|
363
380
|
}
|
|
364
381
|
const hook = settings.onError;
|
|
365
382
|
if (hook !== undefined && typeof hook !== 'function') {
|
|
366
|
-
throw
|
|
383
|
+
throw invalidOption('Option "onError"', 'a function', typeof hook);
|
|
367
384
|
}
|
|
368
385
|
const names = Object.keys(custom);
|
|
369
386
|
for (const name of names) check(name, custom[name]);
|
package/src/compile/code.js
CHANGED
|
@@ -29,13 +29,13 @@ const literal = (value) => {
|
|
|
29
29
|
case 'string':
|
|
30
30
|
return quote(value);
|
|
31
31
|
case 'boolean':
|
|
32
|
-
return
|
|
32
|
+
return `${value}`;
|
|
33
33
|
case 'bigint':
|
|
34
34
|
return `${value}n`;
|
|
35
35
|
case 'undefined':
|
|
36
36
|
return 'void 0';
|
|
37
37
|
case 'number':
|
|
38
|
-
return Number.isFinite(value) && !Object.is(value, -0) ?
|
|
38
|
+
return Number.isFinite(value) && !Object.is(value, -0) ? `${value}` : undefined;
|
|
39
39
|
default:
|
|
40
40
|
return value === null ? 'null' : undefined;
|
|
41
41
|
}
|