@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/model/merge.js
CHANGED
|
@@ -2,20 +2,19 @@
|
|
|
2
2
|
// constraints of both (the length and the bounds that satisfy both, the
|
|
3
3
|
// values both enums hold, the validate functions of both), a field in one
|
|
4
4
|
// comes as it is, and what no value can satisfy (two types, two patterns,
|
|
5
|
-
// bounds with nothing in common, two defaults) is
|
|
5
|
+
// bounds with nothing in common, two defaults) is ALX_ERR_MERGE_CONFLICT with
|
|
6
6
|
// the field. CUE composes definitions this way, instead of inheritance.
|
|
7
7
|
const { hasBrand, define } = require('../util.js');
|
|
8
8
|
const { Schema } = require('../schema.js');
|
|
9
9
|
const { isStruct } = require('../struct.js');
|
|
10
|
-
const { isThenable } = require('../issues.js');
|
|
11
|
-
const { SchemaDefinitionError, invalidTarget } = require('../errors.js');
|
|
10
|
+
const { isThenable, observe } = require('../issues.js');
|
|
11
|
+
const { SchemaDefinitionError, SchemaUsageError, invalidTarget } = require('../errors.js');
|
|
12
12
|
const {
|
|
13
13
|
same,
|
|
14
14
|
definitionOf,
|
|
15
15
|
structDefinitionOf,
|
|
16
16
|
metadataOf,
|
|
17
17
|
structDefinition,
|
|
18
|
-
atSchema,
|
|
19
18
|
namespacesOf,
|
|
20
19
|
} = require('./data.js');
|
|
21
20
|
|
|
@@ -26,24 +25,33 @@ const BOOLEANS = new Set(['unique', 'index', 'primary', 'deprecated']);
|
|
|
26
25
|
// A conflict carries the path of the field it is at; `merge` locates it in
|
|
27
26
|
// the schema on the way out, as a struct does with a definition error.
|
|
28
27
|
const conflict = (reason, path = '') => {
|
|
29
|
-
|
|
30
|
-
error.field = path;
|
|
31
|
-
return error;
|
|
28
|
+
return new SchemaDefinitionError(reason, { code: 'ALX_ERR_MERGE_CONFLICT', field: path });
|
|
32
29
|
};
|
|
33
30
|
|
|
34
31
|
// Both validate functions, as one that returns what both returned (an
|
|
35
32
|
// array of returns is one the check reads), awaited when either is async.
|
|
33
|
+
// The promise of the first is observed before the second runs: a second that
|
|
34
|
+
// throws leaves it behind, and it must not reject unhandled.
|
|
36
35
|
const compose = (first, second) =>
|
|
37
36
|
function validate(value, path) {
|
|
38
|
-
const
|
|
37
|
+
const returned = first.call(this, value, path);
|
|
38
|
+
const a = isThenable(returned) ? observe(returned) : returned;
|
|
39
39
|
const b = second.call(this, value, path);
|
|
40
40
|
if (isThenable(a) || isThenable(b)) return Promise.all([a, b]);
|
|
41
41
|
return [a, b];
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
const lower = (a, b) =>
|
|
44
|
+
const lower = (a, b) => {
|
|
45
|
+
if (a === undefined) return b;
|
|
46
|
+
if (b === undefined) return a;
|
|
47
|
+
return a > b ? a : b;
|
|
48
|
+
};
|
|
45
49
|
|
|
46
|
-
const upper = (a, b) =>
|
|
50
|
+
const upper = (a, b) => {
|
|
51
|
+
if (a === undefined) return b;
|
|
52
|
+
if (b === undefined) return a;
|
|
53
|
+
return a < b ? a : b;
|
|
54
|
+
};
|
|
47
55
|
|
|
48
56
|
// The length both rules allow: the greater of the minimums, the lesser of
|
|
49
57
|
// the maximums.
|
|
@@ -153,7 +161,9 @@ const mergeFields = (x, y, path) => {
|
|
|
153
161
|
const b = typeof y === 'string' ? { type: y } : y;
|
|
154
162
|
const type = mergeType(a.type, b.type, path);
|
|
155
163
|
// The side whose type the result has sets the keys of its type.
|
|
156
|
-
const
|
|
164
|
+
const swapped = type === b.type && type !== a.type;
|
|
165
|
+
const first = swapped ? b : a;
|
|
166
|
+
const second = swapped ? a : b;
|
|
157
167
|
const def = { type };
|
|
158
168
|
const keys = new Set(Object.keys(first));
|
|
159
169
|
for (const key of Object.keys(second)) keys.add(key);
|
|
@@ -212,7 +222,9 @@ const merge = (a, b, options = {}) => {
|
|
|
212
222
|
throw invalidTarget('Two Schemas are expected');
|
|
213
223
|
}
|
|
214
224
|
if (options === null || typeof options !== 'object') {
|
|
215
|
-
throw new
|
|
225
|
+
throw new SchemaUsageError('Merge options must be an object', {
|
|
226
|
+
code: 'ALX_ERR_INVALID_OPTION',
|
|
227
|
+
});
|
|
216
228
|
}
|
|
217
229
|
const { name = a.name, metadata = {} } = options;
|
|
218
230
|
const namespaces = namespacesOf([a, b]);
|
|
@@ -249,12 +261,11 @@ const merge = (a, b, options = {}) => {
|
|
|
249
261
|
}
|
|
250
262
|
return new Schema(name, definition, namespaces);
|
|
251
263
|
} catch (error) {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
}
|
|
264
|
+
// A conflict carries its field from where it was found: located at the
|
|
265
|
+
// schema, its message names the place.
|
|
266
|
+
const isConflict =
|
|
267
|
+
error instanceof SchemaDefinitionError && error.code === 'ALX_ERR_MERGE_CONFLICT';
|
|
268
|
+
if (isConflict) error.locate(name, '');
|
|
258
269
|
throw error;
|
|
259
270
|
}
|
|
260
271
|
};
|
package/src/model/pattern.js
CHANGED
|
@@ -3,12 +3,26 @@
|
|
|
3
3
|
// random source of the sample. The subset is what a validation pattern is
|
|
4
4
|
// made of: alternatives, groups, classes, escapes and quantifiers; what
|
|
5
5
|
// generates no string (a lookaround, a back reference, a word boundary) is
|
|
6
|
-
//
|
|
6
|
+
// ALX_ERR_SAMPLE_PATTERN. Nothing here reads a schema: `sample.js` hands over
|
|
7
7
|
// the RegExp and the random source, and the letters of the locale are read
|
|
8
8
|
// at draw time, so the nodes serve every locale.
|
|
9
9
|
const { SchemaDefinitionError } = require('../errors.js');
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
// How many times a quantifier may repeat, and how deep groups may nest: the
|
|
12
|
+
// bounds of what a draw allocates and of what the parser recurses into.
|
|
13
|
+
const MAX_REPEAT = 10_000;
|
|
14
|
+
const MAX_GROUP_DEPTH = 64;
|
|
15
|
+
|
|
16
|
+
const LETTER = /^[A-Za-z]$/;
|
|
17
|
+
|
|
18
|
+
const PATTERNS = new WeakMap();
|
|
19
|
+
|
|
20
|
+
// The pool a negated class draws from: the letters and digits that are not
|
|
21
|
+
// in it; a class that excludes the letters excludes every letter of the
|
|
22
|
+
// pool, since \P{L} matches no letter in any locale.
|
|
23
|
+
const POOL = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_- ';
|
|
24
|
+
|
|
25
|
+
const invalid = (reason) => new SchemaDefinitionError(reason, { code: 'ALX_ERR_SAMPLE_PATTERN' });
|
|
12
26
|
|
|
13
27
|
// A whole number in [0, max], and an element of a list, from the random
|
|
14
28
|
// source of a sample.
|
|
@@ -22,32 +36,100 @@ const code = (char) => char.codePointAt(0);
|
|
|
22
36
|
|
|
23
37
|
const range = (from, to) => [code(from), code(to)];
|
|
24
38
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const MAX_REPEAT = 10_000;
|
|
28
|
-
const MAX_GROUPS = 64;
|
|
29
|
-
|
|
30
|
-
const CLASSES = Object.assign(Object.create(null), {
|
|
39
|
+
const CLASSES = {
|
|
40
|
+
__proto__: null,
|
|
31
41
|
d: [range('0', '9')],
|
|
32
42
|
w: [range('0', '9'), range('A', 'Z'), range('a', 'z'), range('_', '_')],
|
|
33
43
|
s: [range(' ', ' ')],
|
|
34
|
-
}
|
|
44
|
+
};
|
|
35
45
|
|
|
36
46
|
// The Unicode property escapes a validation pattern uses; the letters are
|
|
37
47
|
// those of the locale, read when the string is drawn.
|
|
38
|
-
const PROPERTIES =
|
|
48
|
+
const PROPERTIES = {
|
|
49
|
+
__proto__: null,
|
|
39
50
|
L: 'letters',
|
|
40
51
|
Letter: 'letters',
|
|
41
52
|
Lu: [range('A', 'Z')],
|
|
42
53
|
Ll: [range('a', 'z')],
|
|
43
54
|
N: [range('0', '9')],
|
|
44
55
|
Nd: [range('0', '9')],
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const LETTER = /^[A-Za-z]$/;
|
|
56
|
+
};
|
|
48
57
|
|
|
49
58
|
const classNode = (set, negate, letters) => ({ set, negate, letters });
|
|
50
59
|
|
|
60
|
+
// The fewest code points a string of the node has: a lower bound of its
|
|
61
|
+
// length however the length rule counts (a surrogate pair in the source is
|
|
62
|
+
// two text nodes and one code point).
|
|
63
|
+
const minLengthOf = (node) => {
|
|
64
|
+
if (node.text !== undefined) return /^[\uDC00-\uDFFF]$/.test(node.text) ? 0 : 1;
|
|
65
|
+
if (node.set !== undefined) return 1;
|
|
66
|
+
if (node.alt !== undefined) {
|
|
67
|
+
let least = Infinity;
|
|
68
|
+
for (const branch of node.alt) least = Math.min(least, minLengthOf(branch));
|
|
69
|
+
return least;
|
|
70
|
+
}
|
|
71
|
+
if (node.seq !== undefined) {
|
|
72
|
+
let total = 0;
|
|
73
|
+
for (const part of node.seq) total += minLengthOf(part);
|
|
74
|
+
return total;
|
|
75
|
+
}
|
|
76
|
+
return node.min * minLengthOf(node.node);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// The nodes of a RegExp and the minimal length of its strings, once per
|
|
80
|
+
// RegExp: the nodes carry nothing of a locale or a seed.
|
|
81
|
+
const patternOf = (pattern) => {
|
|
82
|
+
let entry = PATTERNS.get(pattern);
|
|
83
|
+
if (entry === undefined) {
|
|
84
|
+
const node = new PatternParser(pattern.source).parse();
|
|
85
|
+
entry = { node, minLength: minLengthOf(node) };
|
|
86
|
+
PATTERNS.set(pattern, entry);
|
|
87
|
+
}
|
|
88
|
+
return entry;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const inSet = (set, point) => {
|
|
92
|
+
for (const pair of set) if (point >= pair[0] && point <= pair[1]) return true;
|
|
93
|
+
return false;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// A character of a class: one of its ranges, or a letter of the locale
|
|
97
|
+
// where the class holds them.
|
|
98
|
+
const fromSet = (state, node) => {
|
|
99
|
+
if (node.negate) {
|
|
100
|
+
const out = [];
|
|
101
|
+
for (const char of POOL) {
|
|
102
|
+
if (!inSet(node.set, code(char)) && !(node.letters && LETTER.test(char))) out.push(char);
|
|
103
|
+
}
|
|
104
|
+
if (out.length === 0) throw invalid('A class excludes every character');
|
|
105
|
+
return pick(state, out);
|
|
106
|
+
}
|
|
107
|
+
const choices = node.set.length + (node.letters ? 1 : 0);
|
|
108
|
+
if (choices === 0) throw invalid('A class allows no character');
|
|
109
|
+
const index = int(state, choices - 1);
|
|
110
|
+
if (index === node.set.length) return pick(state, state.letters);
|
|
111
|
+
const pair = node.set[index];
|
|
112
|
+
return String.fromCodePoint(pair[0] + int(state, pair[1] - pair[0]));
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// `extra` is how many repetitions an unbounded quantifier may add: the
|
|
116
|
+
// length the field asks for, when it does, else a few.
|
|
117
|
+
const fromPattern = (state, node, extra) => {
|
|
118
|
+
if (node.text !== undefined) return node.text;
|
|
119
|
+
if (node.set !== undefined) return fromSet(state, node);
|
|
120
|
+
if (node.alt !== undefined) return fromPattern(state, pick(state, node.alt), extra);
|
|
121
|
+
if (node.seq !== undefined) {
|
|
122
|
+
let text = '';
|
|
123
|
+
for (const part of node.seq) text += fromPattern(state, part, extra);
|
|
124
|
+
return text;
|
|
125
|
+
}
|
|
126
|
+
const max = node.max === Infinity ? node.min + extra : node.max;
|
|
127
|
+
const count = node.min + int(state, max - node.min);
|
|
128
|
+
let text = '';
|
|
129
|
+
for (let index = 0; index < count; index += 1) text += fromPattern(state, node.node, extra);
|
|
130
|
+
return text;
|
|
131
|
+
};
|
|
132
|
+
|
|
51
133
|
// The nodes of a pattern: { alt }, { seq }, { set, negate, letters } for a
|
|
52
134
|
// class (`letters`: the letters of the locale are in it), { text }, and
|
|
53
135
|
// { node, min, max } for a repetition.
|
|
@@ -110,7 +192,9 @@ class PatternParser {
|
|
|
110
192
|
|
|
111
193
|
group() {
|
|
112
194
|
this.groups += 1;
|
|
113
|
-
if (this.groups >
|
|
195
|
+
if (this.groups > MAX_GROUP_DEPTH) {
|
|
196
|
+
throw this.fail(`groups nested deeper than ${MAX_GROUP_DEPTH}`);
|
|
197
|
+
}
|
|
114
198
|
if (this.peek() === '?') {
|
|
115
199
|
this.next();
|
|
116
200
|
const kind = this.next();
|
|
@@ -128,7 +212,7 @@ class PatternParser {
|
|
|
128
212
|
|
|
129
213
|
// A class escape (\d, \w, \s, \p{..}) as ranges, the negated ones as
|
|
130
214
|
// what they are not among the letters and digits, or one character.
|
|
131
|
-
escape(
|
|
215
|
+
escape(isInClass) {
|
|
132
216
|
const char = this.next();
|
|
133
217
|
if (char === undefined) throw this.fail('a trailing backslash');
|
|
134
218
|
const lower = char.toLowerCase();
|
|
@@ -146,7 +230,7 @@ class PatternParser {
|
|
|
146
230
|
const letters = property === 'letters';
|
|
147
231
|
return classNode(letters ? [] : property, char !== lower, letters);
|
|
148
232
|
}
|
|
149
|
-
if (char === 'b' && !
|
|
233
|
+
if (char === 'b' && !isInClass) throw this.fail('a word boundary');
|
|
150
234
|
if (char === 'B' || char === 'k' || (char >= '1' && char <= '9')) {
|
|
151
235
|
throw this.fail('a back reference');
|
|
152
236
|
}
|
|
@@ -197,11 +281,8 @@ class PatternParser {
|
|
|
197
281
|
}
|
|
198
282
|
char = escaped.text;
|
|
199
283
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
this.source[this.at + 1] !== ']' &&
|
|
203
|
-
this.at + 1 < this.source.length
|
|
204
|
-
) {
|
|
284
|
+
const isRange = this.peek() === '-' && this.at + 1 < this.source.length;
|
|
285
|
+
if (isRange && this.source[this.at + 1] !== ']') {
|
|
205
286
|
this.next();
|
|
206
287
|
let to = this.next();
|
|
207
288
|
if (to === '\\') to = this.escape(true).text;
|
|
@@ -227,7 +308,9 @@ class PatternParser {
|
|
|
227
308
|
const match = /^\{(\d+)(,(\d*))?\}$/.exec(this.source.slice(this.at, close + 1));
|
|
228
309
|
if (match === null) return node;
|
|
229
310
|
min = Number(match[1]);
|
|
230
|
-
|
|
311
|
+
if (match[2] === undefined) max = min;
|
|
312
|
+
else if (match[3] === '') max = Infinity;
|
|
313
|
+
else max = Number(match[3]);
|
|
231
314
|
if (min > MAX_REPEAT || (max !== Infinity && max > MAX_REPEAT)) {
|
|
232
315
|
throw this.fail(`a repetition over ${MAX_REPEAT}`);
|
|
233
316
|
}
|
|
@@ -239,84 +322,4 @@ class PatternParser {
|
|
|
239
322
|
}
|
|
240
323
|
}
|
|
241
324
|
|
|
242
|
-
// The fewest code points a string of the node has: a lower bound of its
|
|
243
|
-
// length however the length rule counts (a surrogate pair in the source is
|
|
244
|
-
// two text nodes and one code point).
|
|
245
|
-
const minLengthOf = (node) => {
|
|
246
|
-
if (node.text !== undefined) return /^[\uDC00-\uDFFF]$/.test(node.text) ? 0 : 1;
|
|
247
|
-
if (node.set !== undefined) return 1;
|
|
248
|
-
if (node.alt !== undefined) {
|
|
249
|
-
let least = Infinity;
|
|
250
|
-
for (const branch of node.alt) least = Math.min(least, minLengthOf(branch));
|
|
251
|
-
return least;
|
|
252
|
-
}
|
|
253
|
-
if (node.seq !== undefined) {
|
|
254
|
-
let total = 0;
|
|
255
|
-
for (const part of node.seq) total += minLengthOf(part);
|
|
256
|
-
return total;
|
|
257
|
-
}
|
|
258
|
-
return node.min * minLengthOf(node.node);
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
const PATTERNS = new WeakMap();
|
|
262
|
-
|
|
263
|
-
// The nodes of a RegExp and the minimal length of its strings, once per
|
|
264
|
-
// RegExp: the nodes carry nothing of a locale or a seed.
|
|
265
|
-
const patternOf = (pattern) => {
|
|
266
|
-
let entry = PATTERNS.get(pattern);
|
|
267
|
-
if (entry === undefined) {
|
|
268
|
-
const node = new PatternParser(pattern.source).parse();
|
|
269
|
-
entry = { node, minLength: minLengthOf(node) };
|
|
270
|
-
PATTERNS.set(pattern, entry);
|
|
271
|
-
}
|
|
272
|
-
return entry;
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
// The pool a negated class draws from: the letters and digits that are not
|
|
276
|
-
// in it; a class that excludes the letters excludes every letter of the
|
|
277
|
-
// pool, since \P{L} matches no letter in any locale.
|
|
278
|
-
const POOL = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_- ';
|
|
279
|
-
|
|
280
|
-
const inSet = (set, point) => {
|
|
281
|
-
for (const pair of set) if (point >= pair[0] && point <= pair[1]) return true;
|
|
282
|
-
return false;
|
|
283
|
-
};
|
|
284
|
-
|
|
285
|
-
// A character of a class: one of its ranges, or a letter of the locale
|
|
286
|
-
// where the class holds them.
|
|
287
|
-
const fromSet = (state, node) => {
|
|
288
|
-
if (node.negate) {
|
|
289
|
-
const out = [];
|
|
290
|
-
for (const char of POOL) {
|
|
291
|
-
if (!inSet(node.set, code(char)) && !(node.letters && LETTER.test(char))) out.push(char);
|
|
292
|
-
}
|
|
293
|
-
if (out.length === 0) throw invalid('A class excludes every character');
|
|
294
|
-
return pick(state, out);
|
|
295
|
-
}
|
|
296
|
-
const choices = node.set.length + (node.letters ? 1 : 0);
|
|
297
|
-
if (choices === 0) throw invalid('A class allows no character');
|
|
298
|
-
const index = int(state, choices - 1);
|
|
299
|
-
if (index === node.set.length) return pick(state, state.letters);
|
|
300
|
-
const pair = node.set[index];
|
|
301
|
-
return String.fromCodePoint(pair[0] + int(state, pair[1] - pair[0]));
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
// `extra` is how many repetitions an unbounded quantifier may add: the
|
|
305
|
-
// length the field asks for, when it does, else a few.
|
|
306
|
-
const fromPattern = (state, node, extra) => {
|
|
307
|
-
if (node.text !== undefined) return node.text;
|
|
308
|
-
if (node.set !== undefined) return fromSet(state, node);
|
|
309
|
-
if (node.alt !== undefined) return fromPattern(state, pick(state, node.alt), extra);
|
|
310
|
-
if (node.seq !== undefined) {
|
|
311
|
-
let text = '';
|
|
312
|
-
for (const part of node.seq) text += fromPattern(state, part, extra);
|
|
313
|
-
return text;
|
|
314
|
-
}
|
|
315
|
-
const max = node.max === Infinity ? node.min + extra : node.max;
|
|
316
|
-
const count = node.min + int(state, max - node.min);
|
|
317
|
-
let text = '';
|
|
318
|
-
for (let index = 0; index < count; index += 1) text += fromPattern(state, node.node, extra);
|
|
319
|
-
return text;
|
|
320
|
-
};
|
|
321
|
-
|
|
322
325
|
module.exports = { int, pick, patternOf, fromPattern };
|
package/src/model/restore.js
CHANGED
|
@@ -7,19 +7,19 @@
|
|
|
7
7
|
// `options.types` brings the real custom types where the model should
|
|
8
8
|
// validate as the original did, and `model.placeholders` lists the paths
|
|
9
9
|
// the placeholders stand at. The snapshot is input: whatever fails while it
|
|
10
|
-
// is read (a tag that does not read, a nesting deeper than
|
|
11
|
-
// named like a field) is
|
|
10
|
+
// is read (a tag that does not read, a nesting deeper than MAX_DEPTH, an index
|
|
11
|
+
// named like a field) is ALX_ERR_INVALID_SNAPSHOT located where the walk was,
|
|
12
12
|
// and the kind metadata it declares is never loosened by its `custom` keys.
|
|
13
13
|
const { define, shorten } = require('../util.js');
|
|
14
14
|
const { isFirstLower } = require('../metautil.js');
|
|
15
15
|
const { Schema } = require('../schema.js');
|
|
16
16
|
const { Model } = require('../model.js');
|
|
17
|
-
const { SchemaDefinitionError } = require('../errors.js');
|
|
17
|
+
const { SchemaDefinitionError, SchemaUsageError } = require('../errors.js');
|
|
18
18
|
const {
|
|
19
|
-
|
|
19
|
+
MAX_DEPTH,
|
|
20
20
|
tooDeep,
|
|
21
21
|
invalidSnapshot,
|
|
22
|
-
|
|
22
|
+
unreadable,
|
|
23
23
|
isObject,
|
|
24
24
|
isSnapshot,
|
|
25
25
|
checkRoot,
|
|
@@ -27,6 +27,10 @@ const {
|
|
|
27
27
|
kindKeyOf,
|
|
28
28
|
} = require('./data.js');
|
|
29
29
|
|
|
30
|
+
// The keys whose function a placeholder gives the value back for: a codec
|
|
31
|
+
// (and the collected `format`) that returns what it was given.
|
|
32
|
+
const CODECS = new Set(['parse', 'serialize', 'format']);
|
|
33
|
+
|
|
30
34
|
const nothing = () => undefined;
|
|
31
35
|
|
|
32
36
|
// The bytes of base64 text: any padded text of the alphabet, as written.
|
|
@@ -46,17 +50,17 @@ const identity = (value) => value;
|
|
|
46
50
|
// the field's key and among the struct's fields).
|
|
47
51
|
const placeholderOf = (key, at, context) => {
|
|
48
52
|
context.placeholders.add(at);
|
|
49
|
-
return key
|
|
53
|
+
return CODECS.has(key) ? identity : nothing;
|
|
50
54
|
};
|
|
51
55
|
|
|
52
56
|
// A value of a snapshot as a value of a definition: the tags revived (a
|
|
53
57
|
// bigint that does not read throws its own error, a date is checked), a
|
|
54
58
|
// function marker as the placeholder of its key. `at` is the path of the
|
|
55
59
|
// value (`context.at` while it is read) and `depth` its nesting, which
|
|
56
|
-
// stops at
|
|
60
|
+
// stops at MAX_DEPTH.
|
|
57
61
|
const revive = (value, key, at, depth, context) => {
|
|
58
62
|
context.at = at;
|
|
59
|
-
if (depth >
|
|
63
|
+
if (depth > MAX_DEPTH) throw tooDeep();
|
|
60
64
|
if (Array.isArray(value)) {
|
|
61
65
|
const list = [];
|
|
62
66
|
for (const item of value) list.push(revive(item, key, at, depth + 1, context));
|
|
@@ -97,7 +101,7 @@ const isField = (value) => isObject(value) && typeof value.type === 'string';
|
|
|
97
101
|
// instance (the one form the schema type takes it in).
|
|
98
102
|
const fieldDef = (def, at, depth, context) => {
|
|
99
103
|
context.at = at;
|
|
100
|
-
if (depth >
|
|
104
|
+
if (depth > MAX_DEPTH) throw tooDeep();
|
|
101
105
|
if (!isField(def)) throw invalidSnapshot('A field of a snapshot needs a type');
|
|
102
106
|
const out = { type: def.type };
|
|
103
107
|
for (const key of Object.keys(def)) {
|
|
@@ -206,13 +210,13 @@ const typeDef = (name, entry, context) => {
|
|
|
206
210
|
};
|
|
207
211
|
|
|
208
212
|
// The types in an order the registry accepts: an alias after the type it
|
|
209
|
-
// aliases, down a chain no longer than
|
|
213
|
+
// aliases, down a chain no longer than MAX_DEPTH.
|
|
210
214
|
const typesDef = (given, context) => {
|
|
211
215
|
const out = {};
|
|
212
216
|
const seen = new Set();
|
|
213
217
|
const add = (name, depth) => {
|
|
214
218
|
if (seen.has(name)) return;
|
|
215
|
-
if (depth >
|
|
219
|
+
if (depth > MAX_DEPTH) throw tooDeep();
|
|
216
220
|
seen.add(name);
|
|
217
221
|
const entry = given[name];
|
|
218
222
|
if (isObject(entry) && typeof entry.js === 'string' && Object.hasOwn(given, entry.js)) {
|
|
@@ -237,13 +241,15 @@ const fromJSON = (snapshot, options = {}) => {
|
|
|
237
241
|
}
|
|
238
242
|
checkRoot(snapshot);
|
|
239
243
|
if (!isObject(options)) {
|
|
240
|
-
throw new
|
|
244
|
+
throw new SchemaUsageError('Restore options must be an object', {
|
|
245
|
+
code: 'ALX_ERR_INVALID_OPTION',
|
|
246
|
+
});
|
|
241
247
|
}
|
|
242
248
|
const { registry = 'isolated', types: given = {} } = options;
|
|
243
249
|
const context = { name: 'types', at: 'types', given, shared: registry === 'shared' };
|
|
244
250
|
context.placeholders = new Set();
|
|
245
251
|
const entities = [];
|
|
246
|
-
let types;
|
|
252
|
+
let types = null;
|
|
247
253
|
try {
|
|
248
254
|
types = typesDef(snapshot.types ?? {}, context);
|
|
249
255
|
for (const name of Object.keys(snapshot.entities)) {
|
|
@@ -255,21 +261,20 @@ const fromJSON = (snapshot, options = {}) => {
|
|
|
255
261
|
// (a bigint or a pattern that does not read throws its own), located in
|
|
256
262
|
// the entity at the path the walk was on, as a struct locates a
|
|
257
263
|
// definition error.
|
|
258
|
-
const located = error instanceof SchemaDefinitionError ? error :
|
|
264
|
+
const located = error instanceof SchemaDefinitionError ? error : unreadable(error);
|
|
259
265
|
const field = context.at.slice(context.name.length + 1);
|
|
260
|
-
throw field ? located.locate(context.name, field) :
|
|
266
|
+
throw field ? located.locate(context.name, field) : located.locate(context.name, '');
|
|
261
267
|
}
|
|
262
268
|
Object.assign(types, given);
|
|
263
|
-
let model;
|
|
269
|
+
let model = null;
|
|
264
270
|
try {
|
|
265
271
|
model = new Model(types, entities, snapshot.database ?? null, { registry });
|
|
266
272
|
} catch (error) {
|
|
267
273
|
// What the core rejects is located already; anything else is the
|
|
268
274
|
// snapshot's too, never a bare TypeError.
|
|
269
275
|
if (error instanceof SchemaDefinitionError) throw error;
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
throw wrapped;
|
|
276
|
+
const reason = `The model of the snapshot cannot be built: ${error.message}`;
|
|
277
|
+
throw invalidSnapshot(reason, { cause: error });
|
|
273
278
|
}
|
|
274
279
|
Object.defineProperty(model, 'placeholders', { value: Array.from(context.placeholders) });
|
|
275
280
|
return model;
|