@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
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// An update of MongoDB against a schema: the fields its dotted paths name
|
|
2
|
+
// (`'address.city'`, `'items.$.qty'`), the values its operators set, push or
|
|
3
|
+
// add to. What depends on the document stored (a length after a `$push`, a
|
|
4
|
+
// bound after an `$inc`, a parent a dotted `$set` creates) is the
|
|
5
|
+
// collection validator's to check; a union, `any` and `json` are not
|
|
6
|
+
// entered. A deep partial schema is not the shape of an update: `$set`
|
|
7
|
+
// replaces what a path names whole, and names nested fields by their paths.
|
|
8
|
+
const { hasBrand, define, RUN } = require('../util.js');
|
|
9
|
+
const { isStruct } = require('../struct.js');
|
|
10
|
+
const { embeds } = require('../prototypes/reference.js');
|
|
11
|
+
const { infoOf, apply, SERIALIZE } = require('../codec.js');
|
|
12
|
+
const { createContext } = require('../context.js');
|
|
13
|
+
const { issues, finalize } = require('../issues.js');
|
|
14
|
+
const { ValidationResult } = require('../result.js');
|
|
15
|
+
const { baseTypeOf } = require('../types.js');
|
|
16
|
+
const { invalidTarget } = require('../errors.js');
|
|
17
|
+
const { ID: KEY, idOf } = require('./bson.js');
|
|
18
|
+
const { SHAPES, walk, idValue, stateOf, listOf } = require('./document.js');
|
|
19
|
+
|
|
20
|
+
const { NESTED, LIST, KEYED, TUPLE, REFERENCE } = SHAPES;
|
|
21
|
+
|
|
22
|
+
// A place an update is not checked at: a union, any, json.
|
|
23
|
+
const LOOSE = Symbol('loose');
|
|
24
|
+
// An element of a list of ids: a string.
|
|
25
|
+
const ID = Symbol('id');
|
|
26
|
+
// A part a node does not have (an element of a string, a field of an id):
|
|
27
|
+
// the path is malformed whatever the policy for unknown keys says, which
|
|
28
|
+
// decides `null`, a key a struct does not have.
|
|
29
|
+
const NONE = Symbol('none');
|
|
30
|
+
// A path through more embedded records than `maxDepth`.
|
|
31
|
+
const DEEP = Symbol('deep');
|
|
32
|
+
|
|
33
|
+
const ELEMENT = /^(?:\d+|\$|\$\[\w*\])$/;
|
|
34
|
+
const INDEX = /^\d+$/;
|
|
35
|
+
|
|
36
|
+
// The operators of the values a path is set to, of the lists it adds to or
|
|
37
|
+
// takes from, and every operator of an update the subpath reads.
|
|
38
|
+
const VALUES = ['$set', '$setOnInsert', '$min', '$max'];
|
|
39
|
+
const LISTS = ['$push', '$addToSet', '$pull', '$pullAll'];
|
|
40
|
+
const OPERATORS = [...VALUES, ...LISTS, '$unset', '$inc', '$mul', '$currentDate', '$pop'];
|
|
41
|
+
|
|
42
|
+
// The type a field is of, through the aliases of custom types.
|
|
43
|
+
const baseOf = (field) => baseTypeOf(field).type;
|
|
44
|
+
|
|
45
|
+
// The record of a reference where it is embedded, else null.
|
|
46
|
+
const targetOf = (node, references) => {
|
|
47
|
+
const target = node.root.findReference(node.one || node.many);
|
|
48
|
+
return target !== null && embeds(target, node.embed, references) ? target : null;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// What one segment of a path names from a node: the field of a struct, the
|
|
52
|
+
// element of a list (an index, `$`, `$[]`, `$[name]`) or of a tuple, the
|
|
53
|
+
// value of a dictionary, a field of the record a reference embeds; null for
|
|
54
|
+
// a key a struct does not have, NONE for a part a node cannot have.
|
|
55
|
+
const step = (node, segment, references) => {
|
|
56
|
+
if (hasBrand(node, 'Schema')) return step(node.fields, segment, references);
|
|
57
|
+
if (isStruct(node)) return hasBrand(node[segment], 'Type') ? node[segment] : null;
|
|
58
|
+
const { shape } = infoOf(node);
|
|
59
|
+
if (shape === NESTED) return step(node.schema, segment, references);
|
|
60
|
+
if (shape === LIST) return ELEMENT.test(segment) ? node.value : NONE;
|
|
61
|
+
if (shape === KEYED) return node.value;
|
|
62
|
+
if (shape === TUPLE) {
|
|
63
|
+
if (INDEX.test(segment)) return node.value[Number(segment)] ?? NONE;
|
|
64
|
+
return ELEMENT.test(segment) ? LOOSE : NONE;
|
|
65
|
+
}
|
|
66
|
+
if (shape === REFERENCE) {
|
|
67
|
+
const target = targetOf(node, references);
|
|
68
|
+
// An id has no parts; an element of a list of ids is an id.
|
|
69
|
+
if (node.one) return target === null ? NONE : step(target, segment, references);
|
|
70
|
+
if (!ELEMENT.test(segment)) return NONE;
|
|
71
|
+
return target === null ? ID : target;
|
|
72
|
+
}
|
|
73
|
+
const base = baseOf(node);
|
|
74
|
+
return base === 'union' || base === 'any' || base === 'unknown' || base === 'json' ? LOOSE : NONE;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// The node a path names, or null, NONE, LOOSE or DEEP: the records a path
|
|
78
|
+
// goes through are counted as the check counts the records it enters.
|
|
79
|
+
const resolve = (target, segments, references, maxDepth) => {
|
|
80
|
+
let node = target;
|
|
81
|
+
let depth = 0;
|
|
82
|
+
for (let index = 0; index < segments.length && node !== LOOSE; index += 1) {
|
|
83
|
+
if (node === ID) return NONE;
|
|
84
|
+
if (hasBrand(node, 'Type') && infoOf(node).shape === REFERENCE) {
|
|
85
|
+
depth += 1;
|
|
86
|
+
if (depth > maxDepth) return DEEP;
|
|
87
|
+
}
|
|
88
|
+
node = step(node, segments[index], references);
|
|
89
|
+
if (node === null || node === NONE) return node;
|
|
90
|
+
}
|
|
91
|
+
return node;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// The element a `$push` or a `$addToSet` adds to the field, or null when it
|
|
95
|
+
// is no list.
|
|
96
|
+
const elementOf = (node, references) => {
|
|
97
|
+
if (typeof node === 'symbol' || !hasBrand(node, 'Type')) return null;
|
|
98
|
+
const { shape } = infoOf(node);
|
|
99
|
+
if (shape === LIST) return node.value;
|
|
100
|
+
if (shape !== REFERENCE || !node.many) return null;
|
|
101
|
+
return targetOf(node, references) ?? ID;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// The values a list operator carries: one, or the list of `$each`.
|
|
105
|
+
const itemsOf = (value) =>
|
|
106
|
+
value !== null && typeof value === 'object' && Array.isArray(value.$each) ? value.$each : [value];
|
|
107
|
+
|
|
108
|
+
const checkAt = (node, value, context, key) => {
|
|
109
|
+
if (node === ID) {
|
|
110
|
+
if (typeof value !== 'string') issues.type(context, 'string', value, key);
|
|
111
|
+
} else if (hasBrand(node, 'Schema')) node[RUN](value, context, key);
|
|
112
|
+
else node.check(value, context, key);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// An operand of `$inc` or `$mul`: a number for a field of numbers (a whole
|
|
116
|
+
// one for an integer, a bigint for a bigint), never a field of another type;
|
|
117
|
+
// the issue names what the operator takes, not the type of the field.
|
|
118
|
+
const checkStep = (node, value, context, key) => {
|
|
119
|
+
const base = hasBrand(node, 'Type') ? baseOf(node) : '';
|
|
120
|
+
const fits =
|
|
121
|
+
base === 'bigint'
|
|
122
|
+
? typeof value === 'bigint'
|
|
123
|
+
: typeof value === 'number' &&
|
|
124
|
+
(base === 'number' || (base === 'integer' && Number.isInteger(value)));
|
|
125
|
+
if (!fits) {
|
|
126
|
+
const expected = base === 'bigint' || base === 'integer' ? base : 'number';
|
|
127
|
+
issues.type(context, expected, value, key);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const checkOperation = (operator, node, value, context, key) => {
|
|
132
|
+
if (VALUES.includes(operator)) checkAt(node, value, context, key);
|
|
133
|
+
else if (operator === '$unset') {
|
|
134
|
+
if (hasBrand(node, 'Type') && node.required) issues.required(context, key);
|
|
135
|
+
} else if (operator === '$inc' || operator === '$mul') checkStep(node, value, context, key);
|
|
136
|
+
else if (operator === '$currentDate') checkAt(node, new Date(), context, key);
|
|
137
|
+
else if (operator === '$push' || operator === '$addToSet') {
|
|
138
|
+
const element = elementOf(node, context.references);
|
|
139
|
+
if (element === null) issues.type(context, 'array', value, key);
|
|
140
|
+
else for (const item of itemsOf(value)) checkAt(element, item, context, key);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const segmentOf = (segment) => (INDEX.test(segment) ? Number(segment) : segment);
|
|
145
|
+
|
|
146
|
+
// One path of an operator: the parents on the path of the context, the
|
|
147
|
+
// field it names checked by what the operator does. The id of a stored
|
|
148
|
+
// record (its field, or the `_id` of the document it is written to) is set
|
|
149
|
+
// by an insert only; a path with an empty segment, or into a part a node
|
|
150
|
+
// cannot have, names nothing whatever the policy for unknown keys says.
|
|
151
|
+
const checkPath = (target, id, operator, path, value, context) => {
|
|
152
|
+
const segments = path.split('.');
|
|
153
|
+
const last = segments.length - 1;
|
|
154
|
+
for (let index = 0; index < last; index += 1) context.path.push(segmentOf(segments[index]));
|
|
155
|
+
const key = segmentOf(segments[last]);
|
|
156
|
+
const first = segments[0];
|
|
157
|
+
const immutable =
|
|
158
|
+
(first === id || (first === KEY && !(KEY in target.fields))) && operator !== '$setOnInsert';
|
|
159
|
+
const malformed = immutable || segments.includes('');
|
|
160
|
+
const node = malformed ? NONE : resolve(target, segments, context.references, context.maxDepth);
|
|
161
|
+
if (node === DEEP) issues.depth(context, context.maxDepth, key);
|
|
162
|
+
else if (node === NONE || (node === null && context.unknown === 'reject')) {
|
|
163
|
+
issues.unexpected(context, [segments[last]]);
|
|
164
|
+
} else if (node !== null && node !== LOOSE) checkOperation(operator, node, value, context, key);
|
|
165
|
+
context.path.length -= last;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
169
|
+
|
|
170
|
+
const sourceOf = (target, update) => {
|
|
171
|
+
if (!hasBrand(target, 'Schema') || !isStruct(target.fields)) {
|
|
172
|
+
throw invalidTarget('A Schema of a struct is expected');
|
|
173
|
+
}
|
|
174
|
+
if (!isObject(update)) throw invalidTarget('An update is an object of operators');
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// The issues of an update against the schema, as a ValidationResult: the
|
|
178
|
+
// options of check (`unknown` decides what a path the schema does not have
|
|
179
|
+
// is, `references` which records are entered, `maxErrors`, `messages`,
|
|
180
|
+
// `root`). An operator the subpath does not know, or that has no object of
|
|
181
|
+
// paths, is one `unexpected` issue at the root.
|
|
182
|
+
const checkUpdate = (target, update, options) => {
|
|
183
|
+
sourceOf(target, update);
|
|
184
|
+
const context = createContext(options, target.name, target.unknown);
|
|
185
|
+
const id = idOf(target);
|
|
186
|
+
const unknown = [];
|
|
187
|
+
for (const operator of Object.keys(update)) {
|
|
188
|
+
const operand = update[operator];
|
|
189
|
+
if (!OPERATORS.includes(operator) || !isObject(operand)) {
|
|
190
|
+
unknown.push(operator);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
for (const path of Object.keys(operand)) {
|
|
194
|
+
if (context.count >= context.limit) break;
|
|
195
|
+
checkPath(target, id, operator, path, operand[path], context);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (unknown.length > 0 && context.count < context.limit) issues.unexpected(context, unknown);
|
|
199
|
+
finalize(context.issues, context.messages);
|
|
200
|
+
return new ValidationResult(context);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
// A value of an update as the document holds it: its serializers run, its
|
|
204
|
+
// ids made ObjectIds.
|
|
205
|
+
const valueOf = (state, context, node, value) => {
|
|
206
|
+
if (node === ID) return idValue(state, value);
|
|
207
|
+
const serialized = apply(infoOf(node), node, value, context, undefined, SERIALIZE, SERIALIZE);
|
|
208
|
+
return walk(state, node, serialized);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const operandOf = (state, context, operator, node, value) => {
|
|
212
|
+
if (VALUES.includes(operator)) return valueOf(state, context, node, value);
|
|
213
|
+
if (!LISTS.includes(operator)) return value;
|
|
214
|
+
const element = elementOf(node, state.references);
|
|
215
|
+
if (element === null) return value;
|
|
216
|
+
const each = (item) => valueOf(state, context, element, item);
|
|
217
|
+
if (operator === '$pullAll') return Array.isArray(value) ? listOf(value, each) : value;
|
|
218
|
+
if (isObject(value)) {
|
|
219
|
+
if (!Array.isArray(value.$each)) return value;
|
|
220
|
+
return { ...value, $each: listOf(value.$each, each) };
|
|
221
|
+
}
|
|
222
|
+
return each(value);
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
// The update as the database takes it: the values the schema knows
|
|
226
|
+
// serialized and their ids made ObjectIds, the id of the record as `_id`.
|
|
227
|
+
// What `checkUpdate` would reject is not checked here: check first.
|
|
228
|
+
const toUpdate = (target, update, options) => {
|
|
229
|
+
sourceOf(target, update);
|
|
230
|
+
const state = stateOf(target, options, true);
|
|
231
|
+
const context = createContext({ references: state.references }, target.name, target.unknown);
|
|
232
|
+
const id = idOf(target);
|
|
233
|
+
const out = {};
|
|
234
|
+
for (const operator of Object.keys(update)) {
|
|
235
|
+
const operand = update[operator];
|
|
236
|
+
if (!isObject(operand)) {
|
|
237
|
+
define(out, operator, operand);
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
const next = {};
|
|
241
|
+
for (const path of Object.keys(operand)) {
|
|
242
|
+
const segments = path.split('.');
|
|
243
|
+
const node = resolve(target, segments, state.references, state.maxDepth);
|
|
244
|
+
let value = operand[path];
|
|
245
|
+
if (segments[0] === id) {
|
|
246
|
+
segments[0] = KEY;
|
|
247
|
+
if (segments.length === 1) value = idValue(state, value);
|
|
248
|
+
} else if (node !== null && (node === ID || typeof node !== 'symbol')) {
|
|
249
|
+
value = operandOf(state, context, operator, node, value);
|
|
250
|
+
}
|
|
251
|
+
define(next, segments.join('.'), value);
|
|
252
|
+
}
|
|
253
|
+
define(out, operator, next);
|
|
254
|
+
}
|
|
255
|
+
return out;
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
module.exports = { checkUpdate, toUpdate };
|
package/src/preprocessor.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
const { isFirstUpper, toLowerCamel, firstKey } = require('./metautil.js');
|
|
2
2
|
|
|
3
|
-
const { hasBrand, formatters } = require('./util.js');
|
|
3
|
+
const { hasBrand, formatters, typeOf } = require('./util.js');
|
|
4
4
|
const { SchemaDefinitionError } = require('./errors.js');
|
|
5
|
-
const { typeOf } = require('./issues.js');
|
|
6
5
|
|
|
7
6
|
// The first key of an object decides what it is: a Schema or a built field is
|
|
8
7
|
// used as it is, a capitalized key is a kind, `type` is the long form, a type
|
|
@@ -1,18 +1,30 @@
|
|
|
1
1
|
const { BRAND, INSPECT } = require('../util.js');
|
|
2
|
-
const { FORMAT,
|
|
3
|
-
const {
|
|
4
|
-
const { SchemaDefinitionError } = require('../errors.js');
|
|
2
|
+
const { FORMAT, checkBounds, notApplicable } = require('../rules.js');
|
|
3
|
+
const { SchemaDefinitionError, noValidation } = require('../errors.js');
|
|
5
4
|
|
|
6
5
|
// Keys of a field definition become properties of the field, so a key that
|
|
7
6
|
// names one of its methods (or the prototype itself) would replace it. The
|
|
8
|
-
// methods of the type contract are reserved whether the type has them or
|
|
9
|
-
|
|
7
|
+
// methods of the type contract are reserved whether the type has them or
|
|
8
|
+
// not, and so is `root`, the schema the field belongs to.
|
|
9
|
+
const RESERVED = new Set([
|
|
10
|
+
'__proto__',
|
|
11
|
+
'prototype',
|
|
12
|
+
'check',
|
|
13
|
+
'checkType',
|
|
14
|
+
'compile',
|
|
15
|
+
'construct',
|
|
16
|
+
'root',
|
|
17
|
+
]);
|
|
10
18
|
|
|
11
19
|
const reservedKey = (key) =>
|
|
12
20
|
new SchemaDefinitionError('ERR_RESERVED_KEY', `Key "${key}" is reserved in a field definition`);
|
|
13
21
|
|
|
14
|
-
|
|
15
|
-
|
|
22
|
+
// The options of a field that are flags: `nullable` (null in place of a
|
|
23
|
+
// value) and `coerce` (whether parse converts the input of its subtree).
|
|
24
|
+
const FLAGS = ['nullable', 'coerce'];
|
|
25
|
+
|
|
26
|
+
const invalidFlag = (key) =>
|
|
27
|
+
new SchemaDefinitionError('ERR_INVALID_DEFINITION', `Option "${key}" needs a boolean`);
|
|
16
28
|
|
|
17
29
|
const invalidCodec = (key) =>
|
|
18
30
|
new SchemaDefinitionError('ERR_INVALID_DEFINITION', `Option "${key}" needs a function`);
|
|
@@ -26,37 +38,16 @@ const checkCodecs = (type) => {
|
|
|
26
38
|
}
|
|
27
39
|
};
|
|
28
40
|
|
|
29
|
-
//
|
|
30
|
-
//
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// and validate runs only on a value that passed everything before it, so it
|
|
40
|
-
// can rely on the type and the rules. A field with neither is its type check
|
|
41
|
-
// alone.
|
|
42
|
-
const withRules = (type, inner, rules) => {
|
|
43
|
-
const { required, validate } = type;
|
|
44
|
-
return (value, context, key) => {
|
|
45
|
-
if (value == null && !required) return;
|
|
46
|
-
const before = context.count;
|
|
47
|
-
inner(value, context, key);
|
|
48
|
-
if (context.count !== before) return;
|
|
49
|
-
for (let index = 0; index < rules.length; index += 1) {
|
|
50
|
-
if (context.count >= context.limit) break;
|
|
51
|
-
rules[index](value, context, key);
|
|
52
|
-
}
|
|
53
|
-
if (validate && context.count === before) {
|
|
54
|
-
const nested = key !== undefined;
|
|
55
|
-
if (nested) context.path.push(key);
|
|
56
|
-
runValidate(type, validate, value, context);
|
|
57
|
-
if (nested) context.path.pop();
|
|
58
|
-
}
|
|
59
|
-
};
|
|
41
|
+
// What builds the checks of a schema and of its fields on their first use:
|
|
42
|
+
// the validation half of the core installs them (`validation.js`), and a
|
|
43
|
+
// schema built through `./definition` alone says that it cannot validate.
|
|
44
|
+
const BUILD = {
|
|
45
|
+
field() {
|
|
46
|
+
throw noValidation('check');
|
|
47
|
+
},
|
|
48
|
+
schema() {
|
|
49
|
+
throw noValidation('check');
|
|
50
|
+
},
|
|
60
51
|
};
|
|
61
52
|
|
|
62
53
|
class AbstractType {
|
|
@@ -65,9 +56,11 @@ class AbstractType {
|
|
|
65
56
|
static rules = new Set();
|
|
66
57
|
static options = new Set();
|
|
67
58
|
|
|
68
|
-
// The compiled check of the field: a closure built
|
|
69
|
-
// kept out of its definition (toJSON, util.inspect)
|
|
70
|
-
|
|
59
|
+
// The compiled check of the field: a closure built from the field on the
|
|
60
|
+
// first use of `check`, kept out of its definition (toJSON, util.inspect)
|
|
61
|
+
// as a private field. A schema that is only read (rendered, written as a
|
|
62
|
+
// snapshot) never compiles one.
|
|
63
|
+
#check = undefined;
|
|
71
64
|
|
|
72
65
|
constructor(def, preprocessor) {
|
|
73
66
|
this.root = preprocessor.root;
|
|
@@ -82,43 +75,24 @@ class AbstractType {
|
|
|
82
75
|
continue;
|
|
83
76
|
}
|
|
84
77
|
if (!rules.has(key)) throw notApplicable(key, this.type, rules);
|
|
85
|
-
this[key] = format(value);
|
|
78
|
+
this[key] = format(value, this);
|
|
86
79
|
}
|
|
80
|
+
checkBounds(this);
|
|
87
81
|
this.construct(def, preprocessor);
|
|
88
82
|
if (this.type) this.root.references.add(this.type);
|
|
89
|
-
|
|
83
|
+
for (const key of FLAGS) {
|
|
84
|
+
if (this[key] !== undefined && typeof this[key] !== 'boolean') throw invalidFlag(key);
|
|
85
|
+
}
|
|
90
86
|
checkCodecs(this);
|
|
91
|
-
// The rule checks that apply to this field, compiled once: a field
|
|
92
|
-
// without rules is its type check alone, and check is the hot path.
|
|
93
|
-
const checks = compileRules(this);
|
|
94
|
-
const inner = this.compile();
|
|
95
|
-
const plain = checks.length === 0 && !this.validate;
|
|
96
|
-
const check = plain ? inner : withRules(this, inner, checks);
|
|
97
|
-
this.#check = this.nullable === true ? nullable(check) : check;
|
|
98
87
|
}
|
|
99
88
|
|
|
100
89
|
// Records the problems of a value into the context of the current check,
|
|
101
90
|
// under `key` when the field is reached through one.
|
|
102
91
|
get check() {
|
|
92
|
+
if (this.#check === undefined) this.#check = BUILD.field(this);
|
|
103
93
|
return this.#check;
|
|
104
94
|
}
|
|
105
95
|
|
|
106
|
-
// The type check of a field, as a closure built once from the field. A
|
|
107
|
-
// built-in prototype compiles its own; a custom type has a
|
|
108
|
-
// `checkType(value, path)` that returns messages, run through the
|
|
109
|
-
// validator contract.
|
|
110
|
-
compile() {
|
|
111
|
-
const type = this;
|
|
112
|
-
const { required } = this;
|
|
113
|
-
return (value, context, key) => {
|
|
114
|
-
if (value == null && !required) return;
|
|
115
|
-
const nested = key !== undefined;
|
|
116
|
-
if (nested) context.path.push(key);
|
|
117
|
-
runCheckType(type, value, context);
|
|
118
|
-
if (nested) context.path.pop();
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
|
|
122
96
|
toJSON() {
|
|
123
97
|
const { root, ...rest } = this;
|
|
124
98
|
return rest;
|
|
@@ -131,4 +105,4 @@ class AbstractType {
|
|
|
131
105
|
|
|
132
106
|
AbstractType.prototype[BRAND] = 'Type';
|
|
133
107
|
|
|
134
|
-
module.exports = { AbstractType };
|
|
108
|
+
module.exports = { AbstractType, BUILD };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const { issues } = require('../issues.js');
|
|
2
1
|
const { SchemaDefinitionError } = require('../errors.js');
|
|
3
2
|
const { isMap, isSet } = require('../util.js');
|
|
4
3
|
|
|
@@ -9,10 +8,9 @@ const invalidKey = (type) => {
|
|
|
9
8
|
return new SchemaDefinitionError('ERR_INVALID_DEFINITION', reason);
|
|
10
9
|
};
|
|
11
10
|
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
// it after them; an element is checked with its key or index.
|
|
11
|
+
// The collections: a dictionary (`object`, `map`) of a key type and a value
|
|
12
|
+
// definition, a list (`array`, `set`) of an element definition. Their checks
|
|
13
|
+
// are `COMPILE` of checks.js.
|
|
16
14
|
const object = {
|
|
17
15
|
rules: ['length'],
|
|
18
16
|
options: ['key', 'value', 'length'],
|
|
@@ -38,34 +36,6 @@ const object = {
|
|
|
38
36
|
this.value = new Type(defs, prep);
|
|
39
37
|
},
|
|
40
38
|
|
|
41
|
-
compile() {
|
|
42
|
-
const { required, type, key: keyType } = this;
|
|
43
|
-
const { check } = this.value;
|
|
44
|
-
return (value, context, key) => {
|
|
45
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
46
|
-
if (!required && value == null) return;
|
|
47
|
-
issues.type(context, type, value, key);
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
const nested = key !== undefined;
|
|
51
|
-
if (nested) context.path.push(key);
|
|
52
|
-
let count = 0;
|
|
53
|
-
for (const name in value) {
|
|
54
|
-
if (!Object.hasOwn(value, name)) continue;
|
|
55
|
-
if (context.count >= context.limit) break;
|
|
56
|
-
count += 1;
|
|
57
|
-
// oxlint-disable-next-line valid-typeof
|
|
58
|
-
if (typeof name !== keyType) {
|
|
59
|
-
issues.key(context, keyType, name);
|
|
60
|
-
break;
|
|
61
|
-
}
|
|
62
|
-
check(value[name], context, name);
|
|
63
|
-
}
|
|
64
|
-
if (count === 0 && required) issues.required(context);
|
|
65
|
-
if (nested) context.path.pop();
|
|
66
|
-
};
|
|
67
|
-
},
|
|
68
|
-
|
|
69
39
|
isInstance(value) {
|
|
70
40
|
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
71
41
|
},
|
|
@@ -74,64 +44,11 @@ const object = {
|
|
|
74
44
|
const map = {
|
|
75
45
|
...object,
|
|
76
46
|
|
|
77
|
-
compile() {
|
|
78
|
-
const { required, type, key: keyType } = this;
|
|
79
|
-
const { check } = this.value;
|
|
80
|
-
return (value, context, key) => {
|
|
81
|
-
if (!isMap(value)) {
|
|
82
|
-
if (!required && value == null) return;
|
|
83
|
-
issues.type(context, type, value, key);
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
if (value.size === 0) {
|
|
87
|
-
if (required) issues.required(context, key);
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
const nested = key !== undefined;
|
|
91
|
-
if (nested) context.path.push(key);
|
|
92
|
-
for (const pair of value) {
|
|
93
|
-
if (context.count >= context.limit) break;
|
|
94
|
-
const name = pair[0];
|
|
95
|
-
// oxlint-disable-next-line valid-typeof
|
|
96
|
-
if (typeof name !== keyType) {
|
|
97
|
-
issues.key(context, keyType, name);
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
check(pair[1], context, name);
|
|
101
|
-
}
|
|
102
|
-
if (nested) context.path.pop();
|
|
103
|
-
};
|
|
104
|
-
},
|
|
105
|
-
|
|
106
47
|
isInstance(value) {
|
|
107
48
|
return isMap(value);
|
|
108
49
|
},
|
|
109
50
|
};
|
|
110
51
|
|
|
111
|
-
// An array and a Set are walked the same way, by position; only the type
|
|
112
|
-
// test differs.
|
|
113
|
-
const compileList = (type, isList) => {
|
|
114
|
-
const { required } = type;
|
|
115
|
-
const { check } = type.value;
|
|
116
|
-
const name = type.type;
|
|
117
|
-
return (value, context, key) => {
|
|
118
|
-
if (!isList(value)) {
|
|
119
|
-
if (!required && value == null) return;
|
|
120
|
-
issues.type(context, name, value, key);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
const nested = key !== undefined;
|
|
124
|
-
if (nested) context.path.push(key);
|
|
125
|
-
let index = 0;
|
|
126
|
-
for (const item of value) {
|
|
127
|
-
if (context.count >= context.limit) break;
|
|
128
|
-
check(item, context, index);
|
|
129
|
-
index += 1;
|
|
130
|
-
}
|
|
131
|
-
if (nested) context.path.pop();
|
|
132
|
-
};
|
|
133
|
-
};
|
|
134
|
-
|
|
135
52
|
const array = {
|
|
136
53
|
kind: 'struct',
|
|
137
54
|
rules: ['length'],
|
|
@@ -144,10 +61,6 @@ const array = {
|
|
|
144
61
|
this.value = new Type(defs, prep);
|
|
145
62
|
},
|
|
146
63
|
|
|
147
|
-
compile() {
|
|
148
|
-
return compileList(this, this.isInstance);
|
|
149
|
-
},
|
|
150
|
-
|
|
151
64
|
isInstance(value) {
|
|
152
65
|
return Array.isArray(value);
|
|
153
66
|
},
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const { SchemaDefinitionError } = require('../errors.js');
|
|
2
|
+
|
|
3
|
+
const invalidFlags = (type) => {
|
|
4
|
+
const hint = `Type "${type}" needs a list of distinct names: { ${type}: ['read', 'write'] }`;
|
|
5
|
+
return new SchemaDefinitionError('ERR_INVALID_DEFINITION', hint);
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// A set of named switches: an object whose keys are names of the list, each
|
|
9
|
+
// true or false, any of them left out. A key the list does not have is one
|
|
10
|
+
// the schema does not have, reported as `unexpected` under the unknown-keys
|
|
11
|
+
// policy of the check, as a struct reports its own.
|
|
12
|
+
const flags = {
|
|
13
|
+
kind: 'struct',
|
|
14
|
+
options: ['flags'],
|
|
15
|
+
|
|
16
|
+
construct(def) {
|
|
17
|
+
const names = def.flags;
|
|
18
|
+
if (!Array.isArray(names) || names.length === 0) throw invalidFlags(this.type);
|
|
19
|
+
// `__proto__` is no name: a value of names is copied into a plain object
|
|
20
|
+
// under strip, where the key would set its prototype.
|
|
21
|
+
const seen = new Set();
|
|
22
|
+
for (const name of names) {
|
|
23
|
+
if (typeof name !== 'string' || name === '' || name === '__proto__' || seen.has(name)) {
|
|
24
|
+
throw invalidFlags(this.type);
|
|
25
|
+
}
|
|
26
|
+
seen.add(name);
|
|
27
|
+
}
|
|
28
|
+
// A copy: the checks are compiled on first use and read the list then,
|
|
29
|
+
// so the program's array changed later must not reach them.
|
|
30
|
+
this.flags = Object.freeze([...names]);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
module.exports = { flags };
|
package/src/prototypes/json.js
CHANGED
|
@@ -1,19 +1,8 @@
|
|
|
1
|
-
const { issues } = require('../issues.js');
|
|
2
|
-
|
|
3
1
|
const json = {
|
|
4
2
|
kind: 'struct',
|
|
5
3
|
options: [],
|
|
6
4
|
|
|
7
5
|
construct() {},
|
|
8
|
-
|
|
9
|
-
compile() {
|
|
10
|
-
const { required } = this;
|
|
11
|
-
return (value, context, key) => {
|
|
12
|
-
if (value !== null && typeof value === 'object') return;
|
|
13
|
-
if (!required && value == null) return;
|
|
14
|
-
issues.type(context, 'object', value, key);
|
|
15
|
-
};
|
|
16
|
-
},
|
|
17
6
|
};
|
|
18
7
|
|
|
19
8
|
module.exports = { json };
|