@alexify/metaschema 3.2.0 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +307 -1
- package/README.md +173 -89
- package/SECURITY.md +24 -1
- package/index.d.ts +601 -29
- package/package.json +28 -3
- package/src/checks.js +789 -0
- package/src/codec.js +201 -46
- package/src/coerce/index.d.ts +34 -0
- package/src/coerce/index.js +385 -0
- package/src/compile/emit.js +87 -28
- package/src/compile/index.js +9 -4
- package/src/context.js +6 -1
- package/src/definition/index.d.ts +19 -0
- package/src/definition/index.js +27 -0
- package/src/derive/index.d.ts +86 -0
- package/src/derive/index.js +273 -0
- package/src/errors.js +46 -4
- package/src/formats/index.d.ts +20 -8
- package/src/formats/index.js +450 -39
- package/src/generators/fromjsonschema.js +21 -2
- package/src/generators/index.js +1 -1
- package/src/generators/jsonschema.js +90 -21
- package/src/generators/typescript.js +11 -0
- package/src/index.js +3 -2
- package/src/issues.js +9 -7
- package/src/kinds.js +15 -3
- package/src/lint.js +38 -0
- package/src/locales/en.js +6 -0
- package/src/locales/uk.js +6 -0
- package/src/metadata.js +6 -24
- package/src/model/data.js +67 -10
- package/src/model/diff.js +7 -4
- package/src/model/index.js +1 -1
- package/src/model/infer.js +10 -3
- package/src/model/merge.js +16 -9
- package/src/model/restore.js +52 -7
- package/src/model/sample.js +28 -4
- package/src/model.js +54 -56
- package/src/mongodb/bson.js +130 -0
- package/src/mongodb/document.js +217 -0
- package/src/mongodb/index.d.ts +133 -0
- package/src/mongodb/index.js +24 -0
- package/src/mongodb/indexes.js +79 -0
- package/src/mongodb/update.js +258 -0
- package/src/preprocessor.js +1 -2
- package/src/prototypes/abstract.js +41 -67
- package/src/prototypes/collections.js +3 -90
- package/src/prototypes/flags.js +34 -0
- package/src/prototypes/json.js +0 -11
- package/src/prototypes/reference.js +0 -76
- package/src/prototypes/scalars.js +17 -96
- package/src/prototypes/schema.js +1 -10
- package/src/prototypes/tuple.js +0 -26
- package/src/prototypes/union.js +12 -82
- package/src/rules.js +33 -157
- package/src/schema.js +105 -220
- package/src/snapshot.js +216 -41
- package/src/stream/index.js +1 -1
- package/src/struct.js +6 -91
- package/src/types.js +43 -6
- package/src/util.js +43 -0
- package/src/validation.js +313 -0
package/src/snapshot.js
CHANGED
|
@@ -4,11 +4,18 @@
|
|
|
4
4
|
// a committed snapshot is a baseline a later model can be compared with
|
|
5
5
|
// (Drizzle's approach to migrations, without a database). A function has no
|
|
6
6
|
// JSON form and is written as `{ $function: true }` wherever it stands (a
|
|
7
|
-
// validate, a decoder, a default factory, a calculated field); a bigint
|
|
8
|
-
// Date are tagged the same way, a RegExp is its
|
|
9
|
-
// program (`database`, the `custom` metadata of a
|
|
10
|
-
// is written as
|
|
11
|
-
|
|
7
|
+
// validate, a decoder, a default factory, a calculated field); a bigint, a
|
|
8
|
+
// Date, bytes, a Map and a Set are tagged the same way, a RegExp is its
|
|
9
|
+
// source. The metadata of the program (`database`, the `custom` metadata of a
|
|
10
|
+
// kind, an index definition) is written as its values are, a copy: what a
|
|
11
|
+
// snapshot holds is never an object of the model. `fromJSON` of the
|
|
12
|
+
// `./model` subpath reads it back.
|
|
13
|
+
// `schema.toSnapshot()` is the same document for one schema and the
|
|
14
|
+
// entities it reaches through its references, with the name of the schema
|
|
15
|
+
// as `root`.
|
|
16
|
+
const { isFirstUpper } = require('./metautil.js');
|
|
17
|
+
const { hasBrand, define, isBytes, isMap, isSet, isDate, isRegExp } = require('./util.js');
|
|
18
|
+
const { SchemaDefinitionError } = require('./errors.js');
|
|
12
19
|
|
|
13
20
|
const FORMAT = 1;
|
|
14
21
|
|
|
@@ -22,25 +29,37 @@ const objectOf = (value) => {
|
|
|
22
29
|
return out;
|
|
23
30
|
};
|
|
24
31
|
|
|
32
|
+
const listOf = (values) => {
|
|
33
|
+
const list = [];
|
|
34
|
+
for (const item of values) list.push(valueOf(item));
|
|
35
|
+
return list;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
// The bytes of a view as base64 text: what a default of `bytes` is in JSON.
|
|
39
|
+
const base64Of = (bytes) => {
|
|
40
|
+
let text = '';
|
|
41
|
+
for (let index = 0; index < bytes.length; index += 1) text += String.fromCharCode(bytes[index]);
|
|
42
|
+
return btoa(text);
|
|
43
|
+
};
|
|
44
|
+
|
|
25
45
|
// A value of a definition as JSON: a built field as its snapshot, a tag for
|
|
26
|
-
// a function, a bigint
|
|
27
|
-
//
|
|
46
|
+
// a function, a bigint, a Date, bytes, a Map (its entries) and a Set (its
|
|
47
|
+
// values); what has no JSON form at all (a symbol, NaN) is left to
|
|
48
|
+
// JSON.stringify, which drops or nulls it.
|
|
28
49
|
const valueOf = (value) => {
|
|
29
50
|
const type = typeof value;
|
|
30
51
|
if (type === 'function') return { $function: true };
|
|
31
52
|
if (type === 'bigint') return { $bigint: String(value) };
|
|
32
53
|
if (type !== 'object' || value === null) return value;
|
|
33
54
|
if (hasBrand(value, 'Type')) return fieldOf(value);
|
|
34
|
-
if (Array.isArray(value))
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const name = value.constructor?.name;
|
|
40
|
-
if (name === 'RegExp') {
|
|
55
|
+
if (Array.isArray(value)) return listOf(value);
|
|
56
|
+
if (isBytes(value)) return { $bytes: base64Of(value) };
|
|
57
|
+
if (isMap(value)) return { $map: listOf(value) };
|
|
58
|
+
if (isSet(value)) return { $set: listOf(value) };
|
|
59
|
+
if (isRegExp(value)) {
|
|
41
60
|
return value.flags === 'u' ? value.source : { source: value.source, flags: value.flags };
|
|
42
61
|
}
|
|
43
|
-
if (
|
|
62
|
+
if (isDate(value)) return { $date: value.toISOString() };
|
|
44
63
|
return objectOf(value);
|
|
45
64
|
};
|
|
46
65
|
|
|
@@ -61,51 +80,207 @@ const fieldOf = (field) => {
|
|
|
61
80
|
// the ones of a nested struct are.
|
|
62
81
|
const entityOf = (schema) => {
|
|
63
82
|
const { kind, scope, store, allow, parent, unknown, custom, options, fields, indexes } = schema;
|
|
64
|
-
const out = { kind, scope, store, allow, parent, unknown, custom };
|
|
83
|
+
const out = { kind, scope, store, allow, parent, unknown, custom: objectOf(custom) };
|
|
65
84
|
if (hasBrand(fields, 'Struct')) {
|
|
66
85
|
out.fields = objectOf(fields);
|
|
67
86
|
for (const key in options) if (options[key] !== null) out.fields[key] = valueOf(options[key]);
|
|
68
87
|
} else out.type = fieldOf(fields);
|
|
69
88
|
const own = {};
|
|
70
89
|
// A `many` field is recorded with the indexes and is not one.
|
|
71
|
-
for (const key in indexes)
|
|
90
|
+
for (const key in indexes) {
|
|
91
|
+
if (indexes[key].many === undefined) define(own, key, valueOf(indexes[key]));
|
|
92
|
+
}
|
|
72
93
|
out.indexes = own;
|
|
73
94
|
out.relations = valueOf(Array.from(schema.relations));
|
|
74
95
|
return out;
|
|
75
96
|
};
|
|
76
97
|
|
|
77
|
-
// The
|
|
98
|
+
// The names the fields of a schema use (a type, an entity), each with the
|
|
99
|
+
// schema that resolves it: the root of the field, which is the schema, or a
|
|
100
|
+
// schema it reuses as a field (`{ inner: Inner }`), whose fields keep their
|
|
101
|
+
// own. `references` holds the names of both, without saying which schema
|
|
102
|
+
// resolves a name; this does, at any depth. A default and examples are
|
|
103
|
+
// values and hold no fields.
|
|
104
|
+
const usesOf = (schema) => {
|
|
105
|
+
const uses = [];
|
|
106
|
+
const walk = (value) => {
|
|
107
|
+
if (hasBrand(value, 'Type')) {
|
|
108
|
+
uses.push([value.type, value.root]);
|
|
109
|
+
for (const key of Object.keys(value)) {
|
|
110
|
+
if (key !== 'root' && key !== 'default' && key !== 'examples') walk(value[key]);
|
|
111
|
+
}
|
|
112
|
+
} else if (Array.isArray(value)) {
|
|
113
|
+
for (let index = 0; index < value.length; index += 1) walk(value[index]);
|
|
114
|
+
} else if (hasBrand(value, 'Struct')) {
|
|
115
|
+
for (const key of Object.keys(value)) walk(value[key]);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
walk(schema.fields);
|
|
119
|
+
return uses;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
// A type as the snapshot writes it: an alias as its `js`, a type with a
|
|
78
123
|
// check of its own as `check: true` with its kind, a built-in only when it
|
|
79
|
-
// carries metadata.
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
124
|
+
// carries metadata (a built-in is in every registry), else null.
|
|
125
|
+
const typeOf = (Type) => {
|
|
126
|
+
const { source } = Type;
|
|
127
|
+
const entry = {};
|
|
128
|
+
if (source !== undefined) {
|
|
129
|
+
if (source.js !== undefined) entry.js = source.js;
|
|
130
|
+
else {
|
|
131
|
+
entry.kind = Type.kind;
|
|
132
|
+
entry.check = true;
|
|
133
|
+
}
|
|
84
134
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
135
|
+
entry.metadata = objectOf(Type.metadata);
|
|
136
|
+
return source !== undefined || Object.keys(entry.metadata).length > 0 ? entry : null;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const typeClash = (name) =>
|
|
140
|
+
new SchemaDefinitionError(
|
|
141
|
+
'ERR_UNREPRESENTABLE',
|
|
142
|
+
`Two types named "${name}" cannot be represented in one snapshot`,
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
// The types the entities use, by name, and the custom types their aliases
|
|
146
|
+
// name down the chain (a model is restored from them): each from the table
|
|
147
|
+
// of the schema whose fields use it (`tableOf` for an entity, its own for a
|
|
148
|
+
// schema it reuses). Two custom types of one name that are written apart
|
|
149
|
+
// (two registries) cannot be one snapshot.
|
|
150
|
+
const typesOf = (entities, tableOf) => {
|
|
151
|
+
const found = new Map();
|
|
152
|
+
const add = (name, table) => {
|
|
153
|
+
const Type = table[name];
|
|
154
|
+
if (Type === undefined) return;
|
|
155
|
+
const known = found.get(name);
|
|
156
|
+
if (known !== undefined) {
|
|
157
|
+
const differs = known !== Type && (known.source !== undefined || Type.source !== undefined);
|
|
158
|
+
if (differs && JSON.stringify(typeOf(known)) !== JSON.stringify(typeOf(Type))) {
|
|
159
|
+
throw typeClash(name);
|
|
96
160
|
}
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
found.set(name, Type);
|
|
164
|
+
const js = Type.source?.js;
|
|
165
|
+
if (js !== undefined && table[js]?.source !== undefined) add(js, table);
|
|
166
|
+
};
|
|
167
|
+
for (const entity of entities) {
|
|
168
|
+
const uses = usesOf(entity);
|
|
169
|
+
for (let index = 0; index < uses.length; index += 1) {
|
|
170
|
+
const root = uses[index][1];
|
|
171
|
+
add(uses[index][0], root === entity ? tableOf(entity) : root.types);
|
|
97
172
|
}
|
|
98
|
-
|
|
99
|
-
|
|
173
|
+
}
|
|
174
|
+
const out = {};
|
|
175
|
+
for (const name of Array.from(found.keys()).sort()) {
|
|
176
|
+
const entry = typeOf(found.get(name));
|
|
177
|
+
if (entry !== null) out[name] = entry;
|
|
100
178
|
}
|
|
101
179
|
return out;
|
|
102
180
|
};
|
|
103
181
|
|
|
104
|
-
// The entities in dependency order
|
|
105
|
-
|
|
182
|
+
// The entities by name, in dependency order: what a model writes in
|
|
183
|
+
// `model.order`, a schema in the order `gather` met them.
|
|
184
|
+
const entitiesOf = (names, schemaOf) => {
|
|
106
185
|
const entities = {};
|
|
107
|
-
for (const name of
|
|
108
|
-
return
|
|
186
|
+
for (const name of names) define(entities, name, entityOf(schemaOf(name)));
|
|
187
|
+
return entities;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const snapshotOf = (model) => {
|
|
191
|
+
const { order } = model;
|
|
192
|
+
const schemas = [];
|
|
193
|
+
for (const name of order) schemas.push(model.entities.get(name));
|
|
194
|
+
return {
|
|
195
|
+
format: FORMAT,
|
|
196
|
+
database: valueOf(model.database),
|
|
197
|
+
types: typesOf(schemas, () => model.types),
|
|
198
|
+
entities: entitiesOf(order, (name) => model.entities.get(name)),
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const clash = (name) =>
|
|
203
|
+
new SchemaDefinitionError(
|
|
204
|
+
'ERR_UNREPRESENTABLE',
|
|
205
|
+
`Two schemas named "${name}" cannot be represented in one snapshot`,
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
// The order of the entities of a model and of a snapshot: depth first from
|
|
209
|
+
// each start in turn, every entity after the ones its edges reach
|
|
210
|
+
// (`edgesOf` gives `[name, schema]` pairs), a cycle cut where it closes and
|
|
211
|
+
// told to `onCycle`. A loop rather than a recursion, so that a chain of
|
|
212
|
+
// entities of any length orders. `nodes` holds the schema met under each
|
|
213
|
+
// name: one name for two schemas is a clash. `order` may come with names
|
|
214
|
+
// already in it (a model puts `Identifier` first).
|
|
215
|
+
const orderOf = (starts, edgesOf, onCycle, order = new Set(), nodes = new Map()) => {
|
|
216
|
+
const path = new Set();
|
|
217
|
+
const stack = [];
|
|
218
|
+
const enter = (name, schema) => {
|
|
219
|
+
nodes.set(name, schema);
|
|
220
|
+
path.add(name);
|
|
221
|
+
stack.push({ name, edges: edgesOf(schema), index: 0 });
|
|
222
|
+
};
|
|
223
|
+
for (const start of starts) {
|
|
224
|
+
if (!nodes.has(start[0]) && !order.has(start[0])) enter(start[0], start[1]);
|
|
225
|
+
while (stack.length > 0) {
|
|
226
|
+
const frame = stack[stack.length - 1];
|
|
227
|
+
if (frame.index === frame.edges.length) {
|
|
228
|
+
stack.pop();
|
|
229
|
+
path.delete(frame.name);
|
|
230
|
+
order.add(frame.name);
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
const edge = frame.edges[frame.index];
|
|
234
|
+
frame.index += 1;
|
|
235
|
+
const name = edge[0];
|
|
236
|
+
const known = nodes.get(name);
|
|
237
|
+
if (known !== undefined && known !== edge[1]) throw clash(name);
|
|
238
|
+
if (name === frame.name || order.has(name)) continue;
|
|
239
|
+
if (path.has(name)) onCycle(frame.name, name);
|
|
240
|
+
else enter(name, edge[1]);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return order;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// The entities a schema reaches, by the name a reference reached each
|
|
247
|
+
// through (an anonymous schema of a model is the entity of the name the
|
|
248
|
+
// model gives it): its references resolved as its fields resolve them
|
|
249
|
+
// (`usesOf`), and the parent of a projection, which restoring it needs.
|
|
250
|
+
const edgesOf = (schema) => {
|
|
251
|
+
const uses = usesOf(schema);
|
|
252
|
+
if (schema.parent !== '') uses.push([schema.parent, schema]);
|
|
253
|
+
const edges = [];
|
|
254
|
+
for (let index = 0; index < uses.length; index += 1) {
|
|
255
|
+
const ref = uses[index][0];
|
|
256
|
+
if (!isFirstUpper(ref)) continue;
|
|
257
|
+
const target = uses[index][1].findReference(ref);
|
|
258
|
+
if (target !== null) edges.push([ref, target]);
|
|
259
|
+
}
|
|
260
|
+
return edges;
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
// The snapshot of one schema: the format of a model's, with the schema's
|
|
264
|
+
// name as `root` and no database metadata. An anonymous schema is the
|
|
265
|
+
// entity ''. The entities are in the order a walk from the schema meets
|
|
266
|
+
// them, each after the ones it references (a cycle cut where it closes),
|
|
267
|
+
// `Identifier` first: not the order of a model, which depends on the order
|
|
268
|
+
// its entities were given in, so that a schema restored from its snapshot
|
|
269
|
+
// writes the same text again, a cycle included.
|
|
270
|
+
const schemaSnapshotOf = (schema) => {
|
|
271
|
+
const found = new Map();
|
|
272
|
+
const order = Array.from(orderOf([[schema.name, schema]], edgesOf, () => {}, new Set(), found));
|
|
273
|
+
const at = order.indexOf('Identifier');
|
|
274
|
+
if (at > 0) order.unshift(order.splice(at, 1)[0]);
|
|
275
|
+
const schemas = [];
|
|
276
|
+
for (const name of order) schemas.push(found.get(name));
|
|
277
|
+
return {
|
|
278
|
+
format: FORMAT,
|
|
279
|
+
root: schema.name,
|
|
280
|
+
database: null,
|
|
281
|
+
types: typesOf(schemas, (entity) => entity.types),
|
|
282
|
+
entities: entitiesOf(order, (name) => found.get(name)),
|
|
283
|
+
};
|
|
109
284
|
};
|
|
110
285
|
|
|
111
|
-
module.exports = { FORMAT, snapshotOf };
|
|
286
|
+
module.exports = { FORMAT, snapshotOf, schemaSnapshotOf, usesOf, orderOf };
|
package/src/stream/index.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
const { hasBrand } = require('../util.js');
|
|
11
11
|
const { abandon } = require('../issues.js');
|
|
12
12
|
const { invalidTarget, invalidOption: invalid } = require('../errors.js');
|
|
13
|
-
const { Schema } = require('../
|
|
13
|
+
const { Schema } = require('../validation.js');
|
|
14
14
|
|
|
15
15
|
const optionsOf = (options) => {
|
|
16
16
|
if (options === undefined || (options !== null && typeof options === 'object')) return options;
|
package/src/struct.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
const { BRAND, hasBrand, formatters } = require('./util.js');
|
|
2
|
-
const { issues } = require('./issues.js');
|
|
3
2
|
const { SchemaDefinitionError } = require('./errors.js');
|
|
4
3
|
|
|
5
4
|
// What a struct computes once at construction, kept on the dictionary under a
|
|
6
5
|
// global symbol like BRAND, so a struct built by another copy of the package
|
|
7
|
-
// exposes it too: { plan, known, calculated
|
|
8
|
-
//
|
|
9
|
-
// value may carry
|
|
10
|
-
//
|
|
6
|
+
// exposes it too: { plan, known, calculated }, the plan being one frozen
|
|
7
|
+
// entry per validated field ({ key, type, required, own, aliases }), `known`
|
|
8
|
+
// the dictionary of the keys a value may carry and `calculated` the keys of
|
|
9
|
+
// the calculated fields among them. The check over the plan is `checkOf`.
|
|
11
10
|
const STRUCT = Symbol.for('alexify.metaschema.struct');
|
|
12
11
|
|
|
13
12
|
// The functions of a nested definition that are the struct's own validate,
|
|
@@ -19,87 +18,6 @@ const invalidAliases = (reason) => new SchemaDefinitionError('ERR_INVALID_DEFINI
|
|
|
19
18
|
|
|
20
19
|
const ALIASES = 'Option "aliases" needs a list of key names';
|
|
21
20
|
|
|
22
|
-
// The key a value carries for a field that has nothing under its own key: a
|
|
23
|
-
// field renamed with `aliases: ['old']` is still read from its old key, and
|
|
24
|
-
// the field's own key when the value has none of them.
|
|
25
|
-
const aliasOf = (value, aliases, name) => {
|
|
26
|
-
for (const alias of aliases) if (Object.hasOwn(value, alias)) return alias;
|
|
27
|
-
return name;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
// A field is read with a keyed load, and `Object.hasOwn` is consulted only
|
|
31
|
-
// when the value is undefined, to tell a missing key from one set to
|
|
32
|
-
// undefined. A field named after a member of Object.prototype (`toString`,
|
|
33
|
-
// `constructor`, ...) would read the inherited function from a plain object,
|
|
34
|
-
// so such a field (`own`) always asks `Object.hasOwn` first.
|
|
35
|
-
const scanUnknown = (value, known, context) => {
|
|
36
|
-
let unexpected = null;
|
|
37
|
-
for (const name in value) {
|
|
38
|
-
if (name in known || !Object.hasOwn(value, name)) continue;
|
|
39
|
-
if (unexpected === null) unexpected = [];
|
|
40
|
-
unexpected.push(name);
|
|
41
|
-
}
|
|
42
|
-
if (unexpected !== null) issues.unexpected(context, unexpected);
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
// The check of a struct, as a closure over its plan: an index loop with no
|
|
46
|
-
// lookups in the raw definition, no brand checks and no path strings. Every
|
|
47
|
-
// plan entry has the same shape and every field check is a function chosen
|
|
48
|
-
// when the field was built, called with the key of the field so that a leaf
|
|
49
|
-
// never touches the path. Unknown keys are looked for only when the value has
|
|
50
|
-
// more keys than the plan found, so a value with exactly the expected keys is
|
|
51
|
-
// never scanned twice.
|
|
52
|
-
// A field with nothing under its own key (absent, inherited, or own and set
|
|
53
|
-
// to undefined): an alias may hold the value, and the check reports it under
|
|
54
|
-
// the key it found it under. Then the same for an optional field, a required
|
|
55
|
-
// one asks which (own and undefined is a type error). Kept out of the loop
|
|
56
|
-
// of the struct check, whose body is the fast path of a present field; the
|
|
57
|
-
// return is what the field adds to the keys found.
|
|
58
|
-
const checkAbsent = (entry, value, context, name) => {
|
|
59
|
-
const under = aliasOf(value, entry.aliases, name);
|
|
60
|
-
let item;
|
|
61
|
-
if (under !== name) {
|
|
62
|
-
item = value[under];
|
|
63
|
-
context.renamed = true;
|
|
64
|
-
}
|
|
65
|
-
if (item === undefined) {
|
|
66
|
-
if (!entry.required) return 0;
|
|
67
|
-
if (!Object.hasOwn(value, under)) {
|
|
68
|
-
issues.required(context, name);
|
|
69
|
-
return 0;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
entry.check(item, context, under);
|
|
73
|
-
return 1;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
const compileStruct = (plan, known) => (value, context, key) => {
|
|
77
|
-
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
78
|
-
issues.type(context, 'object', value, key);
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
const nested = key !== undefined;
|
|
82
|
-
if (nested) context.path.push(key);
|
|
83
|
-
let found = 0;
|
|
84
|
-
for (let index = 0; index < plan.length; index += 1) {
|
|
85
|
-
if (context.count >= context.limit) break;
|
|
86
|
-
const entry = plan[index];
|
|
87
|
-
const name = entry.key;
|
|
88
|
-
const item = value[name];
|
|
89
|
-
if (item !== undefined && (!entry.own || Object.hasOwn(value, name))) {
|
|
90
|
-
found += 1;
|
|
91
|
-
entry.check(item, context, name);
|
|
92
|
-
} else found += checkAbsent(entry, value, context, name);
|
|
93
|
-
}
|
|
94
|
-
if (context.unknown === 'reject' && context.count < context.limit) {
|
|
95
|
-
let total = 0;
|
|
96
|
-
// oxlint-disable-next-line no-unused-vars
|
|
97
|
-
for (const name in value) total += 1;
|
|
98
|
-
if (total !== found) scanUnknown(value, known, context);
|
|
99
|
-
}
|
|
100
|
-
if (nested) context.path.pop();
|
|
101
|
-
};
|
|
102
|
-
|
|
103
21
|
// A key a value may carry: a field, a calculated field or an alias, each
|
|
104
22
|
// once.
|
|
105
23
|
const know = (known, key) => {
|
|
@@ -155,7 +73,6 @@ const createStruct = (defs, prep) => {
|
|
|
155
73
|
required: child.required,
|
|
156
74
|
own: field in Object.prototype,
|
|
157
75
|
aliases: aliasesOf(child, known),
|
|
158
|
-
check: child.check,
|
|
159
76
|
});
|
|
160
77
|
} catch (error) {
|
|
161
78
|
if (error instanceof SchemaDefinitionError) error.locate(prep.root.name, field);
|
|
@@ -164,7 +81,7 @@ const createStruct = (defs, prep) => {
|
|
|
164
81
|
}
|
|
165
82
|
Object.freeze(plan);
|
|
166
83
|
Object.freeze(calculated);
|
|
167
|
-
const compiled = Object.freeze({ plan, known, calculated
|
|
84
|
+
const compiled = Object.freeze({ plan, known, calculated });
|
|
168
85
|
Object.defineProperty(fields, BRAND, { value: 'Struct' });
|
|
169
86
|
Object.defineProperty(fields, STRUCT, { value: compiled });
|
|
170
87
|
return fields;
|
|
@@ -172,6 +89,4 @@ const createStruct = (defs, prep) => {
|
|
|
172
89
|
|
|
173
90
|
const isStruct = (fields) => hasBrand(fields, 'Struct');
|
|
174
91
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
module.exports = { STRUCT, createStruct, isStruct, checkOf, aliasOf };
|
|
92
|
+
module.exports = { STRUCT, createStruct, isStruct };
|
package/src/types.js
CHANGED
|
@@ -6,6 +6,7 @@ const { schema } = require('./prototypes/schema.js');
|
|
|
6
6
|
const { tuple } = require('./prototypes/tuple.js');
|
|
7
7
|
const { union } = require('./prototypes/union.js');
|
|
8
8
|
const { json } = require('./prototypes/json.js');
|
|
9
|
+
const { flags } = require('./prototypes/flags.js');
|
|
9
10
|
const { SchemaDefinitionError } = require('./errors.js');
|
|
10
11
|
const { firstKey, isFirstUpper } = require('./metautil.js');
|
|
11
12
|
|
|
@@ -19,8 +20,25 @@ const PROTOTYPES = {
|
|
|
19
20
|
tuple,
|
|
20
21
|
union,
|
|
21
22
|
json,
|
|
23
|
+
flags,
|
|
22
24
|
};
|
|
23
25
|
|
|
26
|
+
// The prototype objects of the built-in types inherit from AbstractType, so
|
|
27
|
+
// that a class made from one inherits from it (below), and what the
|
|
28
|
+
// validation half of the core installs on them (their `compile`) reaches
|
|
29
|
+
// every class made from them, in every registry, whenever it is installed.
|
|
30
|
+
for (const prototype of new Set(Object.values(PROTOTYPES))) {
|
|
31
|
+
Object.setPrototypeOf(prototype, AbstractType.prototype);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// What the class of a type inherits from: a built-in's prototype object, the
|
|
35
|
+
// class of the base of an alias, or for a custom type a copy of its entry
|
|
36
|
+
// (the program's own object is left as it is).
|
|
37
|
+
const parentOf = (prototype) =>
|
|
38
|
+
Object.prototype.isPrototypeOf.call(AbstractType.prototype, prototype)
|
|
39
|
+
? prototype
|
|
40
|
+
: Object.assign(Object.create(AbstractType.prototype), prototype);
|
|
41
|
+
|
|
24
42
|
const customTypeError = (name, reason) =>
|
|
25
43
|
new SchemaDefinitionError('ERR_INVALID_CUSTOM_TYPE', `Custom type "${name}" ${reason}`);
|
|
26
44
|
|
|
@@ -40,7 +58,8 @@ const createType = (name, prototype) => {
|
|
|
40
58
|
this.type = type;
|
|
41
59
|
}
|
|
42
60
|
}
|
|
43
|
-
Object.
|
|
61
|
+
Object.setPrototypeOf(Type.prototype, parentOf(prototype));
|
|
62
|
+
Type.prototype.type = name;
|
|
44
63
|
return Type;
|
|
45
64
|
};
|
|
46
65
|
|
|
@@ -76,8 +95,8 @@ const updateTypeMetadata = (Type, metadata) => {
|
|
|
76
95
|
Object.assign(Type.metadata, metadata);
|
|
77
96
|
};
|
|
78
97
|
|
|
79
|
-
// A custom prototype brings `construct` and `checkType
|
|
80
|
-
//
|
|
98
|
+
// A custom prototype brings `construct` and `checkType` (or a `compile` of
|
|
99
|
+
// its own); an alias takes its base's, a registered type.
|
|
81
100
|
const checkCustomType = (name, proto) => {
|
|
82
101
|
const { construct } = proto;
|
|
83
102
|
const checkType = proto.checkType || proto.compile;
|
|
@@ -127,8 +146,7 @@ const typeFactory = (customTypes, registry = TYPES) => {
|
|
|
127
146
|
throw new SchemaDefinitionError('ERR_UNKNOWN_JS_TYPE', reason);
|
|
128
147
|
}
|
|
129
148
|
proto = base.prototype;
|
|
130
|
-
}
|
|
131
|
-
checkCustomType(name, proto);
|
|
149
|
+
} else checkCustomType(name, proto);
|
|
132
150
|
const Type = createType(name, proto);
|
|
133
151
|
Type.source = { js, construct: rest.construct, checkType: rest.checkType };
|
|
134
152
|
updateTypeMetadata(Type, metadata);
|
|
@@ -137,4 +155,23 @@ const typeFactory = (customTypes, registry = TYPES) => {
|
|
|
137
155
|
return registry;
|
|
138
156
|
};
|
|
139
157
|
|
|
140
|
-
|
|
158
|
+
// The type a field validates as: its own class, or the one its alias chain
|
|
159
|
+
// ends in (`{ money: { js: 'number' } }` validates as a number, and so does
|
|
160
|
+
// `{ cents: { js: 'money' } }`); a custom type with a check of its own ends
|
|
161
|
+
// the chain with `source.js` undefined, and a name the registry cannot
|
|
162
|
+
// resolve (a namespace detached since) ends it where it stands. Read by the
|
|
163
|
+
// generators, the samples and the mongodb subpath, which look at `.type` and
|
|
164
|
+
// `.source` of what comes back.
|
|
165
|
+
const baseTypeOf = (field) => {
|
|
166
|
+
const { types } = field.root;
|
|
167
|
+
let Type = field.constructor;
|
|
168
|
+
for (;;) {
|
|
169
|
+
const js = Type.source?.js;
|
|
170
|
+
if (js === undefined) return Type;
|
|
171
|
+
const next = types[js];
|
|
172
|
+
if (next === undefined) return Type;
|
|
173
|
+
Type = next;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
module.exports = { PROTOTYPES, TYPES, createRegistry, readsAsStruct, typeFactory, baseTypeOf };
|
package/src/util.js
CHANGED
|
@@ -24,6 +24,27 @@ const isSet = iterableNamed('Set');
|
|
|
24
24
|
const isDate = (value) =>
|
|
25
25
|
value?.constructor?.name === 'Date' && typeof value.getTime === 'function';
|
|
26
26
|
|
|
27
|
+
const isRegExp = (value) =>
|
|
28
|
+
value?.constructor?.name === 'RegExp' && typeof value.exec === 'function';
|
|
29
|
+
|
|
30
|
+
// The bytes of a Uint8Array of any realm, a Buffer included, by the name the
|
|
31
|
+
// engine keeps in the view itself: the tag getter of the typed arrays reads
|
|
32
|
+
// that slot and gives undefined for anything else, which a key or a symbol
|
|
33
|
+
// of the value cannot change.
|
|
34
|
+
const TYPED_TAG = Object.getOwnPropertyDescriptor(
|
|
35
|
+
Object.getPrototypeOf(Uint8Array.prototype),
|
|
36
|
+
Symbol.toStringTag,
|
|
37
|
+
).get;
|
|
38
|
+
|
|
39
|
+
const isBytes = (value) => TYPED_TAG.call(value) === 'Uint8Array';
|
|
40
|
+
|
|
41
|
+
// The type of a value as a message names it: an array and null by name.
|
|
42
|
+
const typeOf = (value) => {
|
|
43
|
+
if (value === null) return 'null';
|
|
44
|
+
if (Array.isArray(value)) return 'array';
|
|
45
|
+
return typeof value;
|
|
46
|
+
};
|
|
47
|
+
|
|
27
48
|
// A JavaScript identifier: what generated code may write as `object.key` and
|
|
28
49
|
// a TypeScript declaration as a bare name.
|
|
29
50
|
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
@@ -51,6 +72,20 @@ const RUN = Symbol.for('alexify.metaschema.run');
|
|
|
51
72
|
// core sets it, and it is read once per construction, never per check.
|
|
52
73
|
const HOOK = Symbol.for('alexify.metaschema.hook');
|
|
53
74
|
|
|
75
|
+
// What the validation half of the core keeps on a schema (`validation.js`),
|
|
76
|
+
// set when the schema is built: whether the root value joins the cycle
|
|
77
|
+
// detection (a struct with references), whether parse coerces unless the
|
|
78
|
+
// call says otherwise (the `coerce` of the kind metadata), and the caches
|
|
79
|
+
// of the passes of parse and of the Standard Schema props, rebuilt when the
|
|
80
|
+
// namespaces change. Own symbols: none of it is part of the definition.
|
|
81
|
+
const TRACKED = Symbol('tracked');
|
|
82
|
+
const COERCES = Symbol('coerces');
|
|
83
|
+
const CODEC = Symbol('codec');
|
|
84
|
+
const STANDARD = Symbol('standard');
|
|
85
|
+
// The recursive dependencies a model met while ordering its entities, as
|
|
86
|
+
// [entity, reference] pairs: the validation half writes the warnings.
|
|
87
|
+
const CYCLES = Symbol('cycles');
|
|
88
|
+
|
|
54
89
|
// Past this many values an enum is checked through a Set instead of a chain
|
|
55
90
|
// of comparisons, on both backends.
|
|
56
91
|
const ENUM_SET_SIZE = 8;
|
|
@@ -95,10 +130,18 @@ module.exports = {
|
|
|
95
130
|
isMap,
|
|
96
131
|
isSet,
|
|
97
132
|
isDate,
|
|
133
|
+
isRegExp,
|
|
134
|
+
isBytes,
|
|
135
|
+
typeOf,
|
|
98
136
|
IDENTIFIER,
|
|
99
137
|
define,
|
|
100
138
|
RUN,
|
|
101
139
|
HOOK,
|
|
140
|
+
TRACKED,
|
|
141
|
+
COERCES,
|
|
142
|
+
CODEC,
|
|
143
|
+
STANDARD,
|
|
144
|
+
CYCLES,
|
|
102
145
|
ENUM_SET_SIZE,
|
|
103
146
|
shorten,
|
|
104
147
|
safeString,
|