@alexify/metaschema 3.3.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 +186 -1
- package/README.md +24 -16
- package/SECURITY.md +24 -1
- package/index.d.ts +224 -28
- package/package.json +13 -3
- package/src/checks.js +789 -0
- package/src/codec.js +37 -13
- package/src/coerce/index.js +34 -1
- package/src/compile/emit.js +87 -28
- package/src/compile/index.js +9 -4
- package/src/definition/index.d.ts +19 -0
- package/src/definition/index.js +27 -0
- package/src/derive/index.js +15 -8
- package/src/errors.js +23 -0
- package/src/generators/fromjsonschema.js +14 -0
- package/src/generators/index.js +1 -1
- package/src/generators/jsonschema.js +44 -7
- package/src/generators/typescript.js +11 -0
- package/src/index.js +3 -2
- package/src/issues.js +9 -7
- package/src/kinds.js +4 -1
- package/src/lint.js +15 -1
- package/src/locales/en.js +6 -0
- package/src/locales/uk.js +6 -0
- package/src/metadata.js +2 -24
- package/src/model/data.js +50 -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 +7 -3
- package/src/model/restore.js +52 -7
- package/src/model/sample.js +26 -1
- package/src/model.js +25 -53
- package/src/mongodb/bson.js +15 -3
- package/src/mongodb/document.js +10 -2
- package/src/preprocessor.js +1 -2
- package/src/prototypes/abstract.js +32 -64
- 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 -233
- package/src/snapshot.js +216 -41
- package/src/stream/index.js +1 -1
- package/src/struct.js +6 -91
- package/src/types.js +24 -6
- package/src/util.js +43 -0
- package/src/validation.js +313 -0
package/src/model.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
const { firstKey, isFirstLower } = require('./metautil.js');
|
|
2
2
|
|
|
3
|
-
const { BRAND, INSPECT } = require('./util.js');
|
|
3
|
+
const { BRAND, INSPECT, CYCLES } = require('./util.js');
|
|
4
4
|
const { Schema } = require('./schema.js');
|
|
5
5
|
const { TYPES, createRegistry, typeFactory } = require('./types.js');
|
|
6
|
-
const { SchemaDefinitionError } = require('./errors.js');
|
|
7
|
-
const {
|
|
8
|
-
const { snapshotOf } = require('./snapshot.js');
|
|
6
|
+
const { SchemaDefinitionError, noValidation } = require('./errors.js');
|
|
7
|
+
const { snapshotOf, orderOf } = require('./snapshot.js');
|
|
9
8
|
|
|
10
9
|
const invalidOption = (name, expected, value) => {
|
|
11
10
|
const reason = `Model option "${name}" must be ${expected}, got ${JSON.stringify(value)}`;
|
|
@@ -42,17 +41,13 @@ const coercionsFor = (options) => {
|
|
|
42
41
|
};
|
|
43
42
|
|
|
44
43
|
class Model {
|
|
45
|
-
// The model's own warnings (the cycles met while ordering), found while
|
|
46
|
-
// building; the rest comes from the entities on first use.
|
|
47
|
-
#cycles = [];
|
|
48
|
-
#warnings = null;
|
|
49
|
-
|
|
50
44
|
constructor(types, entities, database = null, options = {}) {
|
|
51
45
|
this.types = typeFactory(types, registryFor(options));
|
|
52
46
|
this.coercions = coercionsFor(options);
|
|
53
47
|
this.entities = new Map();
|
|
54
48
|
this.database = database;
|
|
55
49
|
this.order = new Set();
|
|
50
|
+
this[CYCLES] = [];
|
|
56
51
|
const projections = [];
|
|
57
52
|
for (const pair of entities) {
|
|
58
53
|
const name = pair[0];
|
|
@@ -74,26 +69,6 @@ class Model {
|
|
|
74
69
|
this.#preprocess();
|
|
75
70
|
}
|
|
76
71
|
|
|
77
|
-
// The lint warnings of every entity, the references and types they cannot
|
|
78
|
-
// resolve, and the recursive dependencies, as `Warning [code]: text`.
|
|
79
|
-
get warnings() {
|
|
80
|
-
if (this.#warnings !== null) return this.#warnings;
|
|
81
|
-
const warnings = [];
|
|
82
|
-
const add = (list) => {
|
|
83
|
-
for (let index = 0; index < list.length; index += 1) warnings.push(list[index]);
|
|
84
|
-
};
|
|
85
|
-
for (const pair of this.entities) {
|
|
86
|
-
const name = pair[0];
|
|
87
|
-
const entity = pair[1];
|
|
88
|
-
if (name.startsWith('.')) continue;
|
|
89
|
-
add(entity.warnings);
|
|
90
|
-
add(entity.checkConsistency());
|
|
91
|
-
}
|
|
92
|
-
add(this.#cycles);
|
|
93
|
-
this.#warnings = warnings;
|
|
94
|
-
return warnings;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
72
|
// The model as plain JSON: `{ format, database, types, entities }`, the
|
|
98
73
|
// entities in dependency order with their metadata, fields, indexes and
|
|
99
74
|
// relations, and the types they use. What JSON.stringify(model) writes.
|
|
@@ -105,37 +80,34 @@ class Model {
|
|
|
105
80
|
return `Model ${inspect(this.toJSON(), options)}`;
|
|
106
81
|
}
|
|
107
82
|
|
|
83
|
+
// Depth-first ordering (`orderOf`): an entity after everything it
|
|
84
|
+
// references among the entities, `Identifier` first; a reference back into
|
|
85
|
+
// the path of the walk is a cycle of any length, recorded.
|
|
108
86
|
#preprocess() {
|
|
109
87
|
const { entities, order } = this;
|
|
110
88
|
if (entities.has('Identifier')) order.add('Identifier');
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
// `visiting` holds the current path, so a reference back into it is a cycle
|
|
119
|
-
// of any length, not only one through the entity the walk started from.
|
|
120
|
-
#reorderEntity(name, visiting) {
|
|
121
|
-
const entity = this.entities.get(name);
|
|
122
|
-
if (!entity || this.order.has(name)) return;
|
|
123
|
-
visiting.add(name);
|
|
124
|
-
for (const ref of entity.references) {
|
|
125
|
-
if (ref === name) continue;
|
|
126
|
-
if (visiting.has(ref)) {
|
|
127
|
-
this.#cycles.push(
|
|
128
|
-
warning('recursive-reference', `"${name}" depends on "${ref}" recursively`),
|
|
129
|
-
);
|
|
130
|
-
continue;
|
|
89
|
+
const starts = [];
|
|
90
|
+
for (const pair of entities) if (!pair[0].startsWith('.')) starts.push(pair);
|
|
91
|
+
const edgesOf = (entity) => {
|
|
92
|
+
const edges = [];
|
|
93
|
+
for (const ref of entity.references) {
|
|
94
|
+
const target = entities.get(ref);
|
|
95
|
+
if (target !== undefined) edges.push([ref, target]);
|
|
131
96
|
}
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
this.order.add(name);
|
|
97
|
+
return edges;
|
|
98
|
+
};
|
|
99
|
+
orderOf(starts, edgesOf, (name, ref) => this[CYCLES].push([name, ref]), order);
|
|
136
100
|
}
|
|
137
101
|
}
|
|
138
102
|
|
|
103
|
+
// The warnings of a model are the validation half's, as a schema's are.
|
|
104
|
+
Object.defineProperty(Model.prototype, 'warnings', {
|
|
105
|
+
get() {
|
|
106
|
+
throw noValidation('warnings');
|
|
107
|
+
},
|
|
108
|
+
configurable: true,
|
|
109
|
+
});
|
|
110
|
+
|
|
139
111
|
Model.prototype[BRAND] = 'Model';
|
|
140
112
|
|
|
141
113
|
module.exports = { Model };
|
package/src/mongodb/bson.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
// value of another realm or of another copy of bson is one too.
|
|
8
8
|
const { idFieldOf } = require('../kinds.js');
|
|
9
9
|
const { invalidOption, notRegistered } = require('../errors.js');
|
|
10
|
+
const { isBytes } = require('../util.js');
|
|
10
11
|
const { register: coercion } = require('../coerce/index.js');
|
|
11
12
|
|
|
12
13
|
// The tag every value of bson carries, and the key of a document.
|
|
@@ -22,9 +23,20 @@ const isObjectId = (value) => {
|
|
|
22
23
|
|
|
23
24
|
const isDecimal128 = (value) => tagOf(value) === 'Decimal128' && typeof value.toJSON === 'function';
|
|
24
25
|
|
|
26
|
+
// The bytes of a generic Binary of the driver (subtype 0, or 2, its old
|
|
27
|
+
// form), copied off the memory the driver read it into; null for anything
|
|
28
|
+
// else. A Binary of another subtype (a UUID, an encrypted value, a vector)
|
|
29
|
+
// written back as bytes would lose its subtype, so it is left for the check.
|
|
30
|
+
const binaryBytes = (value) => {
|
|
31
|
+
const binary = tagOf(value) === 'Binary' && typeof value.toJSON === 'function';
|
|
32
|
+
if (!binary || !isBytes(value.buffer)) return null;
|
|
33
|
+
const subtype = value.sub_type;
|
|
34
|
+
if (subtype !== 0 && subtype !== 2) return null;
|
|
35
|
+
return new Uint8Array(value.buffer.subarray(0, value.position));
|
|
36
|
+
};
|
|
37
|
+
|
|
25
38
|
const isBinary = (value) =>
|
|
26
|
-
(tagOf(value) === 'Binary' && typeof value.toJSON === 'function') ||
|
|
27
|
-
Object.prototype.toString.call(value) === '[object Uint8Array]';
|
|
39
|
+
(tagOf(value) === 'Binary' && typeof value.toJSON === 'function') || isBytes(value);
|
|
28
40
|
|
|
29
41
|
const construct = () => {};
|
|
30
42
|
|
|
@@ -115,4 +127,4 @@ const idOf = (schema) => {
|
|
|
115
127
|
return id === null || ID in schema.fields ? null : id;
|
|
116
128
|
};
|
|
117
129
|
|
|
118
|
-
module.exports = { ID, HEX, types, register, driverOf, idOf, isObjectId };
|
|
130
|
+
module.exports = { ID, HEX, types, register, driverOf, idOf, isObjectId, binaryBytes };
|
package/src/mongodb/document.js
CHANGED
|
@@ -11,7 +11,8 @@ const { embeds } = require('../prototypes/reference.js');
|
|
|
11
11
|
const { infoOf } = require('../codec.js');
|
|
12
12
|
const { referencesOf } = require('../context.js');
|
|
13
13
|
const { invalidTarget, invalidOption } = require('../errors.js');
|
|
14
|
-
const {
|
|
14
|
+
const { baseTypeOf } = require('../types.js');
|
|
15
|
+
const { ID, HEX, driverOf, idOf, isObjectId, binaryBytes } = require('./bson.js');
|
|
15
16
|
|
|
16
17
|
// The shapes of infoOf in codec.js, by name, for the three walks of the
|
|
17
18
|
// subpath; `mongodb.test.js` holds them to the numbers of the core.
|
|
@@ -25,9 +26,10 @@ const SHAPES = Object.freeze({
|
|
|
25
26
|
TUPLE: 6,
|
|
26
27
|
UNION: 7,
|
|
27
28
|
REFERENCE: 8,
|
|
29
|
+
FLAGS: 9,
|
|
28
30
|
});
|
|
29
31
|
|
|
30
|
-
const { SCHEMA, FIELDS, NESTED, LIST, KEYED, TUPLE, UNION, REFERENCE } = SHAPES;
|
|
32
|
+
const { LEAF, SCHEMA, FIELDS, NESTED, LIST, KEYED, TUPLE, UNION, REFERENCE } = SHAPES;
|
|
31
33
|
|
|
32
34
|
// A plain object of any realm: its prototype is the root of its realm or
|
|
33
35
|
// none (an Array, a Date, a Map or a value of bson has one in between).
|
|
@@ -139,6 +141,12 @@ const walk = (state, node, value) => {
|
|
|
139
141
|
const index = info.table.get(value[node.discriminator]);
|
|
140
142
|
return index === undefined ? value : walk(state, node.union[index], value);
|
|
141
143
|
}
|
|
144
|
+
// The driver writes a Uint8Array as binData and reads it back as a Binary:
|
|
145
|
+
// its bytes again, where the field holds bytes.
|
|
146
|
+
if (shape === LEAF && !state.out && typeof value === 'object') {
|
|
147
|
+
const bytes = binaryBytes(value);
|
|
148
|
+
if (bytes !== null && baseTypeOf(node).type === 'bytes') return bytes;
|
|
149
|
+
}
|
|
142
150
|
return value;
|
|
143
151
|
};
|
|
144
152
|
|
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,12 +1,20 @@
|
|
|
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`);
|
|
@@ -30,37 +38,16 @@ const checkCodecs = (type) => {
|
|
|
30
38
|
}
|
|
31
39
|
};
|
|
32
40
|
|
|
33
|
-
//
|
|
34
|
-
//
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
// and validate runs only on a value that passed everything before it, so it
|
|
44
|
-
// can rely on the type and the rules. A field with neither is its type check
|
|
45
|
-
// alone.
|
|
46
|
-
const withRules = (type, inner, rules) => {
|
|
47
|
-
const { required, validate } = type;
|
|
48
|
-
return (value, context, key) => {
|
|
49
|
-
if (value == null && !required) return;
|
|
50
|
-
const before = context.count;
|
|
51
|
-
inner(value, context, key);
|
|
52
|
-
if (context.count !== before) return;
|
|
53
|
-
for (let index = 0; index < rules.length; index += 1) {
|
|
54
|
-
if (context.count >= context.limit) break;
|
|
55
|
-
rules[index](value, context, key);
|
|
56
|
-
}
|
|
57
|
-
if (validate && context.count === before) {
|
|
58
|
-
const nested = key !== undefined;
|
|
59
|
-
if (nested) context.path.push(key);
|
|
60
|
-
runValidate(type, validate, value, context);
|
|
61
|
-
if (nested) context.path.pop();
|
|
62
|
-
}
|
|
63
|
-
};
|
|
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
|
+
},
|
|
64
51
|
};
|
|
65
52
|
|
|
66
53
|
class AbstractType {
|
|
@@ -69,9 +56,11 @@ class AbstractType {
|
|
|
69
56
|
static rules = new Set();
|
|
70
57
|
static options = new Set();
|
|
71
58
|
|
|
72
|
-
// The compiled check of the field: a closure built
|
|
73
|
-
// kept out of its definition (toJSON, util.inspect)
|
|
74
|
-
|
|
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;
|
|
75
64
|
|
|
76
65
|
constructor(def, preprocessor) {
|
|
77
66
|
this.root = preprocessor.root;
|
|
@@ -86,45 +75,24 @@ class AbstractType {
|
|
|
86
75
|
continue;
|
|
87
76
|
}
|
|
88
77
|
if (!rules.has(key)) throw notApplicable(key, this.type, rules);
|
|
89
|
-
this[key] = format(value);
|
|
78
|
+
this[key] = format(value, this);
|
|
90
79
|
}
|
|
80
|
+
checkBounds(this);
|
|
91
81
|
this.construct(def, preprocessor);
|
|
92
82
|
if (this.type) this.root.references.add(this.type);
|
|
93
83
|
for (const key of FLAGS) {
|
|
94
84
|
if (this[key] !== undefined && typeof this[key] !== 'boolean') throw invalidFlag(key);
|
|
95
85
|
}
|
|
96
86
|
checkCodecs(this);
|
|
97
|
-
// The rule checks that apply to this field, compiled once: a field
|
|
98
|
-
// without rules is its type check alone, and check is the hot path.
|
|
99
|
-
const checks = compileRules(this);
|
|
100
|
-
const inner = this.compile();
|
|
101
|
-
const plain = checks.length === 0 && !this.validate;
|
|
102
|
-
const check = plain ? inner : withRules(this, inner, checks);
|
|
103
|
-
this.#check = this.nullable === true ? nullable(check) : check;
|
|
104
87
|
}
|
|
105
88
|
|
|
106
89
|
// Records the problems of a value into the context of the current check,
|
|
107
90
|
// under `key` when the field is reached through one.
|
|
108
91
|
get check() {
|
|
92
|
+
if (this.#check === undefined) this.#check = BUILD.field(this);
|
|
109
93
|
return this.#check;
|
|
110
94
|
}
|
|
111
95
|
|
|
112
|
-
// The type check of a field, as a closure built once from the field. A
|
|
113
|
-
// built-in prototype compiles its own; a custom type has a
|
|
114
|
-
// `checkType(value, path)` that returns messages, run through the
|
|
115
|
-
// validator contract.
|
|
116
|
-
compile() {
|
|
117
|
-
const type = this;
|
|
118
|
-
const { required } = this;
|
|
119
|
-
return (value, context, key) => {
|
|
120
|
-
if (value == null && !required) return;
|
|
121
|
-
const nested = key !== undefined;
|
|
122
|
-
if (nested) context.path.push(key);
|
|
123
|
-
runCheckType(type, value, context);
|
|
124
|
-
if (nested) context.path.pop();
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
96
|
toJSON() {
|
|
129
97
|
const { root, ...rest } = this;
|
|
130
98
|
return rest;
|
|
@@ -137,4 +105,4 @@ class AbstractType {
|
|
|
137
105
|
|
|
138
106
|
AbstractType.prototype[BRAND] = 'Type';
|
|
139
107
|
|
|
140
|
-
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 };
|
|
@@ -1,43 +1,8 @@
|
|
|
1
1
|
const { SchemaDefinitionError } = require('../errors.js');
|
|
2
|
-
const { RUN } = require('../util.js');
|
|
3
|
-
const { issues } = require('../issues.js');
|
|
4
2
|
|
|
5
3
|
const invalidEmbed = () =>
|
|
6
4
|
new SchemaDefinitionError('ERR_INVALID_DEFINITION', 'Option "embed" needs a boolean');
|
|
7
5
|
|
|
8
|
-
// A reference is the one place a schema can recurse (Category.parent is a
|
|
9
|
-
// Category), so it is where a value met again while it is still being
|
|
10
|
-
// checked is a cycle, reported instead of recursed into, and where a graph
|
|
11
|
-
// nested deeper than the check allows is reported instead of overflowing the
|
|
12
|
-
// stack.
|
|
13
|
-
const runReferenced = (schema, value, context, key) => {
|
|
14
|
-
if (value === null || typeof value !== 'object') {
|
|
15
|
-
schema[RUN](value, context, key);
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
if (context.depth >= context.maxDepth) {
|
|
19
|
-
issues.depth(context, context.maxDepth, key);
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
let { seen } = context;
|
|
23
|
-
if (seen === null) {
|
|
24
|
-
// The first embedded record creates the set, and the value the check
|
|
25
|
-
// started from joins it then, so a check that embeds nothing pays nothing.
|
|
26
|
-
seen = new Set();
|
|
27
|
-
if (context.source !== null) seen.add(context.source);
|
|
28
|
-
context.seen = seen;
|
|
29
|
-
}
|
|
30
|
-
if (seen.has(value)) {
|
|
31
|
-
issues.circular(context, key);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
seen.add(value);
|
|
35
|
-
context.depth += 1;
|
|
36
|
-
schema[RUN](value, context, key);
|
|
37
|
-
context.depth -= 1;
|
|
38
|
-
seen.delete(value);
|
|
39
|
-
};
|
|
40
|
-
|
|
41
6
|
// Whether a referenced value is the record itself (the graph view) or its
|
|
42
7
|
// id (the storage view): the mode of the check when it says so, otherwise
|
|
43
8
|
// the field's `embed`, otherwise the store of the target's kind. A stored
|
|
@@ -67,47 +32,6 @@ const reference = {
|
|
|
67
32
|
this.type = target;
|
|
68
33
|
this.root.relations.add({ to: target, type: relation });
|
|
69
34
|
},
|
|
70
|
-
|
|
71
|
-
// The target is looked up at check time: entities of a model are built in
|
|
72
|
-
// any order and a namespace may be attached later.
|
|
73
|
-
compile() {
|
|
74
|
-
const { one, many, root, required, embed } = this;
|
|
75
|
-
if (one) {
|
|
76
|
-
return (value, context, key) => {
|
|
77
|
-
if (!required && value == null) return;
|
|
78
|
-
const schema = root.findReference(one);
|
|
79
|
-
if (!schema) {
|
|
80
|
-
issues.reference(context, one, key);
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
if (embeds(schema, embed, context.references)) runReferenced(schema, value, context, key);
|
|
84
|
-
else if (typeof value !== 'string') issues.type(context, 'string', value, key);
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
const expected = `array of ${many}`;
|
|
88
|
-
return (value, context, key) => {
|
|
89
|
-
if (!required && value == null) return;
|
|
90
|
-
const schema = root.findReference(many);
|
|
91
|
-
if (!schema) {
|
|
92
|
-
issues.reference(context, many, key);
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
if (!Array.isArray(value)) {
|
|
96
|
-
issues.type(context, expected, value, key);
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
const nested = key !== undefined;
|
|
100
|
-
if (nested) context.path.push(key);
|
|
101
|
-
const embedded = embeds(schema, embed, context.references);
|
|
102
|
-
for (let index = 0; index < value.length; index += 1) {
|
|
103
|
-
if (context.count >= context.limit) break;
|
|
104
|
-
const item = value[index];
|
|
105
|
-
if (embedded) runReferenced(schema, item, context, index);
|
|
106
|
-
else if (typeof item !== 'string') issues.type(context, 'string', item, index);
|
|
107
|
-
}
|
|
108
|
-
if (nested) context.path.pop();
|
|
109
|
-
};
|
|
110
|
-
},
|
|
111
35
|
};
|
|
112
36
|
|
|
113
37
|
module.exports = { reference, embeds };
|