@alexify/metaschema 3.0.0 → 3.2.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 +312 -1
- package/README.md +463 -17
- package/SECURITY.md +26 -7
- package/index.d.ts +338 -3
- package/package.json +42 -5
- package/src/codec.js +161 -73
- package/src/compile/code.js +1 -1
- package/src/compile/emit.js +60 -7
- package/src/compile/index.d.ts +6 -0
- package/src/compile/index.js +14 -3
- package/src/context.js +8 -9
- package/src/errors.js +18 -1
- package/src/formats/index.d.ts +19 -0
- package/src/formats/index.js +146 -0
- package/src/generators/fromjsonschema.js +517 -0
- package/src/generators/index.d.ts +19 -2
- package/src/generators/index.js +16 -10
- package/src/generators/jsonschema.js +2 -0
- package/src/issues.js +37 -20
- package/src/lint.js +12 -3
- package/src/locales/en.js +11 -2
- package/src/locales/uk.js +10 -2
- package/src/metadata.js +3 -2
- package/src/model/data.js +122 -0
- package/src/model/diff.js +439 -0
- package/src/model/index.d.ts +68 -0
- package/src/model/index.js +45 -0
- package/src/model/infer.js +483 -0
- package/src/model/merge.js +255 -0
- package/src/model/pattern.js +322 -0
- package/src/model/restore.js +226 -0
- package/src/model/sample.js +398 -0
- package/src/model.js +13 -1
- package/src/preprocessor.js +5 -1
- package/src/prototypes/collections.js +4 -3
- package/src/prototypes/scalars.js +4 -4
- package/src/prototypes/schema.js +6 -5
- package/src/prototypes/union.js +2 -2
- package/src/result.js +6 -0
- package/src/rules.js +53 -4
- package/src/schema.js +28 -7
- package/src/snapshot.js +111 -0
- package/src/stream/index.d.ts +68 -0
- package/src/stream/index.js +247 -0
- package/src/struct.js +64 -18
- package/src/types.js +14 -1
- package/src/util.js +18 -0
package/src/codec.js
CHANGED
|
@@ -7,10 +7,19 @@
|
|
|
7
7
|
// whose subtree carries nothing for a phase is returned as it is, and a
|
|
8
8
|
// container is copied only when one of its parts changed: the value given is
|
|
9
9
|
// never written to.
|
|
10
|
-
const { hasBrand, define } = require('./util.js');
|
|
11
|
-
const {
|
|
10
|
+
const { hasBrand, define, isMap, isSet } = require('./util.js');
|
|
11
|
+
const {
|
|
12
|
+
EMPTY,
|
|
13
|
+
NONE,
|
|
14
|
+
issues,
|
|
15
|
+
isThenable,
|
|
16
|
+
abandon,
|
|
17
|
+
release,
|
|
18
|
+
pathOf,
|
|
19
|
+
toDotPath,
|
|
20
|
+
} = require('./issues.js');
|
|
12
21
|
const { createContext } = require('./context.js');
|
|
13
|
-
const { STRUCT, isStruct } = require('./struct.js');
|
|
22
|
+
const { STRUCT, isStruct, aliasOf } = require('./struct.js');
|
|
14
23
|
const { asyncReturn } = require('./errors.js');
|
|
15
24
|
const collections = require('./prototypes/collections.js');
|
|
16
25
|
const { schema: struct } = require('./prototypes/schema.js');
|
|
@@ -24,6 +33,9 @@ const DECODERS = 2;
|
|
|
24
33
|
const ENCODERS = 4;
|
|
25
34
|
const STRUCTS = 8;
|
|
26
35
|
const ASYNC = 16;
|
|
36
|
+
// A field with aliases below: the pass after the check visits it only when
|
|
37
|
+
// the check read a field from an alias (`context.renamed`).
|
|
38
|
+
const ALIASES = 32;
|
|
27
39
|
|
|
28
40
|
// The shape of a node: a Schema, a struct (the dictionary of its fields), or
|
|
29
41
|
// a field by the identity of its prototype's compile, as the JIT tells them
|
|
@@ -124,7 +136,12 @@ const infoOf = (node) => {
|
|
|
124
136
|
const defaulted = type.default !== undefined;
|
|
125
137
|
if (defaulted) bits |= DEFAULTS;
|
|
126
138
|
if (defaulted || (sub.bits & DEFAULTS) !== 0) parts[DEFAULTS].push(index);
|
|
127
|
-
|
|
139
|
+
// A field with aliases, or with one below, is visited by the pass
|
|
140
|
+
// that decodes, which renames; whether that pass enters the subtree
|
|
141
|
+
// is decided by the bits and the context at the field.
|
|
142
|
+
const aliased = plan[index].aliases.length > 0;
|
|
143
|
+
if (aliased) bits |= ALIASES;
|
|
144
|
+
if (aliased || (sub.bits & (DECODERS | ALIASES)) !== 0) parts[DECODERS].push(index);
|
|
128
145
|
if ((sub.bits & ENCODERS) !== 0) parts[ENCODERS].push(index);
|
|
129
146
|
parts[STRUCTS].push(index);
|
|
130
147
|
}
|
|
@@ -168,14 +185,13 @@ const toMap = (value) => new Map(value);
|
|
|
168
185
|
|
|
169
186
|
const toSet = (list) => new Set(list);
|
|
170
187
|
|
|
171
|
-
// The output of a container under construction
|
|
172
|
-
//
|
|
173
|
-
// the
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return { context, key, value, clone, put, out: null, pending: null };
|
|
188
|
+
// The output of a container under construction, kept in the locals of its
|
|
189
|
+
// walker (no state object per container): null until a part changes, then a
|
|
190
|
+
// copy of the container (`clone`) the parts are written into (`put`).
|
|
191
|
+
const written = (out, value, clone, put, key, result) => {
|
|
192
|
+
const target = out === null ? clone(value) : out;
|
|
193
|
+
put(target, key, result);
|
|
194
|
+
return target;
|
|
179
195
|
};
|
|
180
196
|
|
|
181
197
|
// A part that is still decoding: only a decoder makes a promise (a thenable
|
|
@@ -183,13 +199,11 @@ const enter = (context, key, value, clone, put) => {
|
|
|
183
199
|
// of the data with a `then` of its own is data, never awaited.
|
|
184
200
|
const isPending = (context, value) => context.async !== null && value instanceof Promise;
|
|
185
201
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
state.pending.push(key, result);
|
|
192
|
-
}
|
|
202
|
+
// The parts of a container still decoding, as key, promise, key, promise.
|
|
203
|
+
const queued = (pending, key, result) => {
|
|
204
|
+
const list = pending === null ? [] : pending;
|
|
205
|
+
list.push(key, result);
|
|
206
|
+
return list;
|
|
193
207
|
};
|
|
194
208
|
|
|
195
209
|
// What a promise of the passes resolves to: a box around the value, never
|
|
@@ -197,10 +211,11 @@ const take = (state, key, result, phase) => {
|
|
|
197
211
|
// own and the value of a program may well have one.
|
|
198
212
|
const boxed = (value) => ({ value });
|
|
199
213
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
214
|
+
// What a container comes back as: the value when no part changed, its copy,
|
|
215
|
+
// or a promise of a box when parts are still decoding, written in once they
|
|
216
|
+
// settle (a decoder that failed resolved to nothing and recorded its issue).
|
|
217
|
+
const settled = (value, out, pending, put, wrap) => {
|
|
218
|
+
if (out === null) return value;
|
|
204
219
|
if (pending === null) return wrap === undefined ? out : wrap(out);
|
|
205
220
|
const promises = [];
|
|
206
221
|
for (let index = 1; index < pending.length; index += 2) promises.push(pending[index]);
|
|
@@ -261,8 +276,8 @@ const decode = (info, node, value, context, key) => {
|
|
|
261
276
|
|
|
262
277
|
const walkLeaf = (info, node, value) => value;
|
|
263
278
|
|
|
264
|
-
const walkInner = (info, node, value, context, key, phase) =>
|
|
265
|
-
apply(info.infos[0], info.children[0], value, context, key, phase);
|
|
279
|
+
const walkInner = (info, node, value, context, key, phase, want) =>
|
|
280
|
+
apply(info.infos[0], info.children[0], value, context, key, phase, want);
|
|
266
281
|
|
|
267
282
|
// Before the check (the defaults, a serialization) a graph may loop through
|
|
268
283
|
// its references; the records on the way down are kept in `seen`, which the
|
|
@@ -274,9 +289,9 @@ const LOOP = Symbol('loop');
|
|
|
274
289
|
|
|
275
290
|
const looped = (context) => context.seen !== null && context.seen.has(LOOP);
|
|
276
291
|
|
|
277
|
-
const walkSchema = (info, node, value, context, key, phase) => {
|
|
292
|
+
const walkSchema = (info, node, value, context, key, phase, want) => {
|
|
278
293
|
if (phase === DECODERS || info.refs.length === 0 || typeof value !== 'object') {
|
|
279
|
-
return walkInner(info, node, value, context, key, phase);
|
|
294
|
+
return walkInner(info, node, value, context, key, phase, want);
|
|
280
295
|
}
|
|
281
296
|
if (context.seen === null) context.seen = new Set();
|
|
282
297
|
const { seen } = context;
|
|
@@ -285,88 +300,152 @@ const walkSchema = (info, node, value, context, key, phase) => {
|
|
|
285
300
|
return value;
|
|
286
301
|
}
|
|
287
302
|
seen.add(value);
|
|
288
|
-
const out = walkInner(info, node, value, context, key, phase);
|
|
303
|
+
const out = walkInner(info, node, value, context, key, phase, want);
|
|
289
304
|
seen.delete(value);
|
|
290
305
|
return out;
|
|
291
306
|
};
|
|
292
307
|
|
|
308
|
+
// The copy of a struct with every renamed field in the place of its alias:
|
|
309
|
+
// `take` appended the field's key, and a key deleted from the copy would
|
|
310
|
+
// leave the copy in dictionary mode. `renames` is alias, field, alias, ...
|
|
311
|
+
const renameKeys = (out, renames) => {
|
|
312
|
+
const result = {};
|
|
313
|
+
for (const key in out) {
|
|
314
|
+
const found = renames.indexOf(key);
|
|
315
|
+
if (found === -1) putKey(result, key, out[key]);
|
|
316
|
+
else if (found % 2 === 0) putKey(result, renames[found + 1], out[renames[found + 1]]);
|
|
317
|
+
}
|
|
318
|
+
return result;
|
|
319
|
+
};
|
|
320
|
+
|
|
293
321
|
// The fields of a struct: a default fills a missing key before the check, a
|
|
294
322
|
// decoder replaces the value after it. Stripping rebuilds the object from
|
|
295
|
-
// the known keys; otherwise the object is copied when a field changed.
|
|
323
|
+
// the known keys; otherwise the object is copied when a field changed. A
|
|
324
|
+
// value under an alias of a field is read from there (no default fills the
|
|
325
|
+
// field, and the check sees it there), and the pass after the check writes
|
|
326
|
+
// it under the field's own key, where the alias was; serialize reads the
|
|
327
|
+
// value of the program, which carries the field keys, and never looks.
|
|
296
328
|
const walkFields = (info, fields, value, context, key, phase, want) => {
|
|
297
329
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) return value;
|
|
298
330
|
const { plan, infos, extra } = info;
|
|
299
331
|
const strip = (want & STRUCTS) !== 0;
|
|
300
332
|
const parts = info.parts[strip ? STRUCTS : phase];
|
|
301
|
-
const
|
|
302
|
-
if (
|
|
333
|
+
const nested = key !== undefined;
|
|
334
|
+
if (nested) context.path.push(key);
|
|
335
|
+
let out = strip ? {} : null;
|
|
336
|
+
let pending = null;
|
|
337
|
+
let renames = null;
|
|
303
338
|
for (let position = 0; position < parts.length; position += 1) {
|
|
304
339
|
const index = parts[position];
|
|
305
340
|
const entry = plan[index];
|
|
306
341
|
const name = entry.key;
|
|
342
|
+
let from = name;
|
|
307
343
|
let item = entry.own && !Object.hasOwn(value, name) ? undefined : value[name];
|
|
308
|
-
if (item === undefined && phase
|
|
309
|
-
|
|
310
|
-
if (
|
|
311
|
-
|
|
312
|
-
|
|
344
|
+
if (item === undefined && phase !== ENCODERS) {
|
|
345
|
+
from = aliasOf(value, entry.aliases, name);
|
|
346
|
+
if (from !== name) item = value[from];
|
|
347
|
+
}
|
|
348
|
+
if (phase === DEFAULTS) {
|
|
349
|
+
if (item === undefined) {
|
|
350
|
+
const fallback = entry.type.default;
|
|
351
|
+
if (fallback === undefined) continue;
|
|
352
|
+
item = typeof fallback === 'function' ? fallback() : fallback;
|
|
353
|
+
out = written(out, value, copy, putKey, from, item);
|
|
354
|
+
}
|
|
355
|
+
const result = apply(infos[index], entry.type, item, context, from, phase, want);
|
|
356
|
+
if (result !== item) out = written(out, value, copy, putKey, from, result);
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
const result = apply(infos[index], entry.type, item, context, from, phase, want);
|
|
360
|
+
const renamed = from !== name;
|
|
361
|
+
if (strip ? result !== undefined : renamed || result !== item) {
|
|
362
|
+
out = written(out, value, copy, putKey, name, result);
|
|
363
|
+
if (phase === DECODERS && isPending(context, result)) pending = queued(pending, name, result);
|
|
364
|
+
}
|
|
365
|
+
if (renamed && !strip) {
|
|
366
|
+
if (renames === null) renames = [];
|
|
367
|
+
renames.push(from, name);
|
|
313
368
|
}
|
|
314
|
-
const result = apply(infos[index], entry.type, item, context, name, phase);
|
|
315
|
-
if (strip ? result !== undefined : result !== item) take(state, name, result, phase);
|
|
316
369
|
}
|
|
317
370
|
if (strip) {
|
|
318
371
|
for (let index = 0; index < extra.length; index += 1) {
|
|
319
372
|
const name = extra[index];
|
|
320
|
-
if (Object.hasOwn(value, name)) putKey(
|
|
373
|
+
if (Object.hasOwn(value, name)) putKey(out, name, value[name]);
|
|
321
374
|
}
|
|
322
|
-
}
|
|
323
|
-
|
|
375
|
+
} else if (renames !== null) out = renameKeys(out, renames);
|
|
376
|
+
if (nested) context.path.pop();
|
|
377
|
+
return settled(value, out, pending, putKey, undefined);
|
|
324
378
|
};
|
|
325
379
|
|
|
326
380
|
// The elements of an array, a Set or a tuple, by position; a Set comes back
|
|
327
381
|
// as a Set.
|
|
328
|
-
const walkItems = (children, infos, fixed, value, context, key, phase) => {
|
|
382
|
+
const walkItems = (children, infos, fixed, value, context, key, phase, want) => {
|
|
329
383
|
const set = !Array.isArray(value);
|
|
330
|
-
if (set && (fixed || value
|
|
331
|
-
const
|
|
384
|
+
if (set && (fixed || !isSet(value))) return value;
|
|
385
|
+
const nested = key !== undefined;
|
|
386
|
+
if (nested) context.path.push(key);
|
|
387
|
+
let out = null;
|
|
388
|
+
let pending = null;
|
|
332
389
|
let index = 0;
|
|
333
390
|
for (const item of value) {
|
|
334
391
|
const slot = fixed ? index : 0;
|
|
335
392
|
if (slot >= children.length) break;
|
|
336
|
-
const result = apply(infos[slot], children[slot], item, context, index, phase);
|
|
337
|
-
if (result !== item)
|
|
393
|
+
const result = apply(infos[slot], children[slot], item, context, index, phase, want);
|
|
394
|
+
if (result !== item) {
|
|
395
|
+
out = written(out, value, Array.from, putIndex, index, result);
|
|
396
|
+
if (phase === DECODERS && isPending(context, result)) {
|
|
397
|
+
pending = queued(pending, index, result);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
338
400
|
index += 1;
|
|
339
401
|
}
|
|
340
|
-
|
|
402
|
+
if (nested) context.path.pop();
|
|
403
|
+
return settled(value, out, pending, putIndex, set ? toSet : undefined);
|
|
341
404
|
};
|
|
342
405
|
|
|
343
|
-
const walkList = (info, node, value, context, key, phase) =>
|
|
344
|
-
walkItems(info.children, info.infos, info.shape === TUPLE, value, context, key, phase);
|
|
406
|
+
const walkList = (info, node, value, context, key, phase, want) =>
|
|
407
|
+
walkItems(info.children, info.infos, info.shape === TUPLE, value, context, key, phase, want);
|
|
345
408
|
|
|
346
409
|
// The values of an object or a Map, by key.
|
|
347
|
-
const walkKeyed = (info, node, value, context, key, phase) => {
|
|
348
|
-
const map = value
|
|
410
|
+
const walkKeyed = (info, node, value, context, key, phase, want) => {
|
|
411
|
+
const map = isMap(value);
|
|
349
412
|
if (!map && (typeof value !== 'object' || Array.isArray(value))) return value;
|
|
350
413
|
const child = info.children[0];
|
|
351
414
|
const sub = info.infos[0];
|
|
352
|
-
const
|
|
415
|
+
const clone = map ? toMap : copy;
|
|
416
|
+
const put = map ? putEntry : putKey;
|
|
417
|
+
const nested = key !== undefined;
|
|
418
|
+
if (nested) context.path.push(key);
|
|
419
|
+
let out = null;
|
|
420
|
+
let pending = null;
|
|
353
421
|
if (map) {
|
|
354
422
|
for (const pair of value) {
|
|
355
423
|
const name = pair[0];
|
|
356
424
|
const item = pair[1];
|
|
357
|
-
const result = apply(sub, child, item, context, name, phase);
|
|
358
|
-
if (result !== item)
|
|
425
|
+
const result = apply(sub, child, item, context, name, phase, want);
|
|
426
|
+
if (result !== item) {
|
|
427
|
+
out = written(out, value, clone, put, name, result);
|
|
428
|
+
if (phase === DECODERS && isPending(context, result)) {
|
|
429
|
+
pending = queued(pending, name, result);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
359
432
|
}
|
|
360
433
|
} else {
|
|
361
434
|
// The own keys of the object, in order, as the check iterates them.
|
|
362
435
|
for (const name in value) {
|
|
363
436
|
if (!Object.hasOwn(value, name)) continue;
|
|
364
437
|
const item = value[name];
|
|
365
|
-
const result = apply(sub, child, item, context, name, phase);
|
|
366
|
-
if (result !== item)
|
|
438
|
+
const result = apply(sub, child, item, context, name, phase, want);
|
|
439
|
+
if (result !== item) {
|
|
440
|
+
out = written(out, value, clone, put, name, result);
|
|
441
|
+
if (phase === DECODERS && isPending(context, result)) {
|
|
442
|
+
pending = queued(pending, name, result);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
367
445
|
}
|
|
368
446
|
}
|
|
369
|
-
|
|
447
|
+
if (nested) context.path.pop();
|
|
448
|
+
return settled(value, out, pending, put, undefined);
|
|
370
449
|
};
|
|
371
450
|
|
|
372
451
|
// The context for trying the branches of a plain union, one per parse, made
|
|
@@ -393,12 +472,8 @@ const accepts = (branch, value, scratch, path, key) => {
|
|
|
393
472
|
scratch.issues = NONE;
|
|
394
473
|
scratch.count = 0;
|
|
395
474
|
scratch.path = path;
|
|
396
|
-
const list = scratch.async;
|
|
397
|
-
list.length = 0;
|
|
398
475
|
branch.check(value, scratch, key);
|
|
399
|
-
|
|
400
|
-
if (list[index].promise !== null) abandon(list[index].promise);
|
|
401
|
-
}
|
|
476
|
+
release(scratch.async, 0);
|
|
402
477
|
return scratch.count === 0;
|
|
403
478
|
};
|
|
404
479
|
|
|
@@ -406,21 +481,22 @@ const accepts = (branch, value, scratch, path, key) => {
|
|
|
406
481
|
// first branch whose check records nothing, as the check picks it. Before
|
|
407
482
|
// the check a branch is tried with its own defaults in place, and the value
|
|
408
483
|
// with those defaults is what parse goes on with.
|
|
409
|
-
const walkUnion = (info, node, value, context, key, phase) => {
|
|
484
|
+
const walkUnion = (info, node, value, context, key, phase, want) => {
|
|
410
485
|
const { children, infos, table } = info;
|
|
411
486
|
if (table !== null) {
|
|
412
487
|
if (typeof value !== 'object') return value;
|
|
413
488
|
const index = table.get(value[node.discriminator]);
|
|
414
489
|
if (index === undefined) return value;
|
|
415
|
-
return apply(infos[index], children[index], value, context, key, phase);
|
|
490
|
+
return apply(infos[index], children[index], value, context, key, phase, want);
|
|
416
491
|
}
|
|
417
492
|
const scratch = scratchOf(context);
|
|
418
493
|
for (let index = 0; index < children.length; index += 1) {
|
|
419
494
|
const branch = children[index];
|
|
420
495
|
const sub = infos[index];
|
|
421
|
-
const prepared =
|
|
496
|
+
const prepared =
|
|
497
|
+
phase === DEFAULTS ? apply(sub, branch, value, context, key, phase, want) : value;
|
|
422
498
|
if (!accepts(branch, prepared, scratch, context.path, key)) continue;
|
|
423
|
-
return phase === DEFAULTS ? prepared : apply(sub, branch, value, context, key, phase);
|
|
499
|
+
return phase === DEFAULTS ? prepared : apply(sub, branch, value, context, key, phase, want);
|
|
424
500
|
}
|
|
425
501
|
return value;
|
|
426
502
|
};
|
|
@@ -428,7 +504,7 @@ const walkUnion = (info, node, value, context, key, phase) => {
|
|
|
428
504
|
// A reference resolved at parse time, as the check resolves it: an id stays
|
|
429
505
|
// an id, a record goes through its entity, and a record deeper than the
|
|
430
506
|
// check allows is left as it is for the check to report.
|
|
431
|
-
const walkReference = (info, node, value, context, key, phase) => {
|
|
507
|
+
const walkReference = (info, node, value, context, key, phase, want) => {
|
|
432
508
|
const { one, many, root, embed } = node;
|
|
433
509
|
const target = root.findReference(one || many);
|
|
434
510
|
if (target === null || !embeds(target, embed, context.references)) return value;
|
|
@@ -436,7 +512,7 @@ const walkReference = (info, node, value, context, key, phase) => {
|
|
|
436
512
|
const sub = infoOf(target);
|
|
437
513
|
context.depth += 1;
|
|
438
514
|
let out = value;
|
|
439
|
-
if (one) out = apply(sub, target, value, context, key, phase);
|
|
515
|
+
if (one) out = apply(sub, target, value, context, key, phase, want);
|
|
440
516
|
else if (Array.isArray(value)) {
|
|
441
517
|
// The target as the one child of the list, kept on the info from one
|
|
442
518
|
// visit to the next (it changes only when the namespaces do).
|
|
@@ -444,7 +520,7 @@ const walkReference = (info, node, value, context, key, phase) => {
|
|
|
444
520
|
info.children[0] = target;
|
|
445
521
|
info.infos[0] = sub;
|
|
446
522
|
}
|
|
447
|
-
out = walkItems(info.children, info.infos, false, value, context, key, phase);
|
|
523
|
+
out = walkItems(info.children, info.infos, false, value, context, key, phase, want);
|
|
448
524
|
}
|
|
449
525
|
context.depth -= 1;
|
|
450
526
|
return out;
|
|
@@ -462,13 +538,24 @@ const WALKERS = [
|
|
|
462
538
|
walkReference,
|
|
463
539
|
];
|
|
464
540
|
|
|
541
|
+
// The bits a pass wants of a subtree, decided once per pass: its own, and
|
|
542
|
+
// for the pass after the check the structs (under `strip`) and the fields
|
|
543
|
+
// with aliases (after a check that read one).
|
|
544
|
+
const wantOf = (phase, context) => {
|
|
545
|
+
let want = phase;
|
|
546
|
+
if (phase === DECODERS) {
|
|
547
|
+
if (context.unknown === 'strip') want |= STRUCTS;
|
|
548
|
+
if (context.renamed) want |= ALIASES;
|
|
549
|
+
}
|
|
550
|
+
return want;
|
|
551
|
+
};
|
|
552
|
+
|
|
465
553
|
// One pass over a value from a node whose info is known, by the shape of
|
|
466
554
|
// each node. A serializer runs before the parts of its value, a decoder
|
|
467
|
-
// after them;
|
|
468
|
-
//
|
|
469
|
-
const apply = (info, node, value, context, key, phase) => {
|
|
555
|
+
// after them; a subtree without the bits the pass wants is returned as it
|
|
556
|
+
// is, which is the one test a value nothing applies to costs.
|
|
557
|
+
const apply = (info, node, value, context, key, phase, want) => {
|
|
470
558
|
if (value == null) return value;
|
|
471
|
-
const want = phase === DECODERS && context.unknown === 'strip' ? DECODERS | STRUCTS : phase;
|
|
472
559
|
if ((info.bits & want) === 0) return value;
|
|
473
560
|
let input = value;
|
|
474
561
|
if (phase === ENCODERS && info.encode !== undefined) {
|
|
@@ -485,6 +572,7 @@ module.exports = {
|
|
|
485
572
|
SERIALIZE: ENCODERS,
|
|
486
573
|
infoOf,
|
|
487
574
|
apply,
|
|
575
|
+
wantOf,
|
|
488
576
|
looped,
|
|
489
577
|
hasAsync,
|
|
490
578
|
};
|
package/src/compile/code.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// The builder of the generated source. Generated code holds identifiers,
|
|
2
2
|
// numbers, booleans and JSON strings only: a regular expression, a Set, a
|
|
3
3
|
// function, a nested check or a dictionary of known keys goes into the scope
|
|
4
|
-
// and is reached by name (the ajv and schemasafe rule
|
|
4
|
+
// and is reached by name (the ajv and schemasafe rule), so
|
|
5
5
|
// nothing taken from a schema is ever part of the source text.
|
|
6
6
|
const { IDENTIFIER } = require('../util.js');
|
|
7
7
|
|
package/src/compile/emit.js
CHANGED
|
@@ -8,8 +8,8 @@
|
|
|
8
8
|
// Generated code names three things: `v` the value, `c` the context and `k`
|
|
9
9
|
// the key the root was called with. Every other identifier is fresh.
|
|
10
10
|
const { ENUM_SET_SIZE } = require('../util.js');
|
|
11
|
-
const { issues, runValidate } = require('../issues.js');
|
|
12
|
-
const { codePoints, lengthOf, ruleNames } = require('../rules.js');
|
|
11
|
+
const { issues, runValidate, release } = require('../issues.js');
|
|
12
|
+
const { FORMATS, codePoints, lengthOf, ruleNames, patternLimit } = require('../rules.js');
|
|
13
13
|
const { STRUCT, isStruct } = require('../struct.js');
|
|
14
14
|
const scalars = require('../prototypes/scalars.js');
|
|
15
15
|
const collections = require('../prototypes/collections.js');
|
|
@@ -53,6 +53,14 @@ const typeIssue = (state, expected, x, key) =>
|
|
|
53
53
|
|
|
54
54
|
const notObject = (x) => `${x} === null || typeof ${x} !== "object" || Array.isArray(${x})`;
|
|
55
55
|
|
|
56
|
+
// A built-in by the name of its constructor and by its shape, as util.js
|
|
57
|
+
// tests it: the name alone is what a value parsed from JSON can carry.
|
|
58
|
+
const isDateCode = (x) =>
|
|
59
|
+
`${x}?.constructor?.name === "Date" && typeof ${x}.getTime === "function"`;
|
|
60
|
+
|
|
61
|
+
const isIterableCode = (x, name) =>
|
|
62
|
+
`${x}?.constructor?.name === ${quote(name)} && typeof ${x}[Symbol.iterator] === "function"`;
|
|
63
|
+
|
|
56
64
|
// The conditions of the bounds a field has.
|
|
57
65
|
const outside = (state, x, min, max) => {
|
|
58
66
|
const parts = [];
|
|
@@ -77,14 +85,30 @@ const emitLength = (type, x, key, state) => {
|
|
|
77
85
|
code.push('}');
|
|
78
86
|
};
|
|
79
87
|
|
|
88
|
+
// The condition that keeps a pattern or a format off a string past
|
|
89
|
+
// length.max, as the closures do.
|
|
90
|
+
const withinLength = (type, x, code) => {
|
|
91
|
+
const limit = patternLimit(type);
|
|
92
|
+
return limit === Infinity ? '' : `${x}.length <= ${code.value(limit)} && `;
|
|
93
|
+
};
|
|
94
|
+
|
|
80
95
|
const emitPattern = (type, x, key, state) => {
|
|
81
96
|
const { code } = state;
|
|
82
97
|
const { pattern } = type;
|
|
83
|
-
code.push(`if (!${code.ref(pattern)}.test(${x})) {`);
|
|
98
|
+
code.push(`if (${withinLength(type, x, code)}!${code.ref(pattern)}.test(${x})) {`);
|
|
84
99
|
record(state, issue(state, 'pattern', `${quote(pattern.source)}, ${keyOf(key)}`));
|
|
85
100
|
code.push('}');
|
|
86
101
|
};
|
|
87
102
|
|
|
103
|
+
const emitFormat = (type, x, key, state) => {
|
|
104
|
+
const { code } = state;
|
|
105
|
+
const { format } = type;
|
|
106
|
+
const { test } = FORMATS[format];
|
|
107
|
+
code.push(`if (${withinLength(type, x, code)}!${code.ref(test)}(${x})) {`);
|
|
108
|
+
record(state, issue(state, 'format', `${quote(format)}, ${keyOf(key)}`));
|
|
109
|
+
code.push('}');
|
|
110
|
+
};
|
|
111
|
+
|
|
88
112
|
const emitRange = (type, x, key, state) => {
|
|
89
113
|
const { code } = state;
|
|
90
114
|
const { min, max } = type;
|
|
@@ -97,6 +121,7 @@ const emitRange = (type, x, key, state) => {
|
|
|
97
121
|
const RULES = Object.assign(Object.create(null), {
|
|
98
122
|
length: emitLength,
|
|
99
123
|
pattern: emitPattern,
|
|
124
|
+
format: emitFormat,
|
|
100
125
|
range: emitRange,
|
|
101
126
|
});
|
|
102
127
|
|
|
@@ -155,7 +180,7 @@ const emitInteger = (type, x, key, state) =>
|
|
|
155
180
|
typed(type, x, key, state, `!Number.isInteger(${x})`, typeIssue(state, 'integer', x, key));
|
|
156
181
|
|
|
157
182
|
const emitDate = (type, x, key, state) => {
|
|
158
|
-
const fails = `!(${x}
|
|
183
|
+
const fails = `!(${isDateCode(x)} && !Number.isNaN(${x}.getTime()))`;
|
|
159
184
|
typed(type, x, key, state, fails, typeIssue(state, 'date', x, key));
|
|
160
185
|
};
|
|
161
186
|
|
|
@@ -228,7 +253,7 @@ const emitObject = (type, x, key, state) => {
|
|
|
228
253
|
const emitMap = (type, x, key, state) => {
|
|
229
254
|
const { code } = state;
|
|
230
255
|
const failure = typeIssue(state, type.type, x, key);
|
|
231
|
-
typed(type, x, key, state,
|
|
256
|
+
typed(type, x, key, state, `!(${isIterableCode(x, 'Map')})`, failure, () => {
|
|
232
257
|
if (type.required) {
|
|
233
258
|
code.push(`if (${x}.size === 0) {`);
|
|
234
259
|
record(state, issue(state, 'required', keyOf(key)));
|
|
@@ -258,7 +283,7 @@ const emitMap = (type, x, key, state) => {
|
|
|
258
283
|
const emitList = (type, x, key, state) => {
|
|
259
284
|
const { code } = state;
|
|
260
285
|
const set = type.isInstance === collections.set.isInstance;
|
|
261
|
-
const fails = set ?
|
|
286
|
+
const fails = set ? `!(${isIterableCode(x, 'Set')})` : `!Array.isArray(${x})`;
|
|
262
287
|
typed(type, x, key, state, fails, typeIssue(state, type.type, x, key), () => {
|
|
263
288
|
const children = inside(state, key);
|
|
264
289
|
const index = code.name('i');
|
|
@@ -309,6 +334,34 @@ const emitStruct = (fields, x, key, state) => {
|
|
|
309
334
|
const { required, own } = entry;
|
|
310
335
|
const name = quote(entry.key);
|
|
311
336
|
const item = code.name('x');
|
|
337
|
+
if (entry.aliases.length > 0) {
|
|
338
|
+
// A field with aliases, when the value has nothing under its own key:
|
|
339
|
+
// the first alias the value carries, and the key the check reports
|
|
340
|
+
// under, as `aliasOf` in struct.js finds them.
|
|
341
|
+
const alias = code.name('k');
|
|
342
|
+
const inherited = own ? ` || !Object.hasOwn(${x}, ${name})` : '';
|
|
343
|
+
code.push(`let ${item} = ${prop(x, entry.key)}, ${alias} = ${name};`);
|
|
344
|
+
code.push(`if (${item} === undefined${inherited}) { ${item} = undefined;`);
|
|
345
|
+
// The first alias the value carries: once one is taken the rest are
|
|
346
|
+
// skipped, as the chain of the closure backend stops at it, and the
|
|
347
|
+
// context learns that a field was read from an alias, as there.
|
|
348
|
+
for (const other of entry.aliases) {
|
|
349
|
+
const taken = `${alias} = ${quote(other)}; ${item} = ${prop(x, other)}; c.renamed = true;`;
|
|
350
|
+
code.push(`if (${alias} === ${name} && Object.hasOwn(${x}, ${quote(other)})) { ${taken} }`);
|
|
351
|
+
}
|
|
352
|
+
code.push('}');
|
|
353
|
+
if (required) {
|
|
354
|
+
code.push(`if (${item} === undefined && !Object.hasOwn(${x}, ${alias})) {`);
|
|
355
|
+
record(children, issue(state, 'required', name));
|
|
356
|
+
code.push('} else {');
|
|
357
|
+
} else {
|
|
358
|
+
code.push(`if (${item} !== undefined) {`);
|
|
359
|
+
}
|
|
360
|
+
code.push(`${found} += 1;`);
|
|
361
|
+
emitField(entry.type, item, alias, children);
|
|
362
|
+
code.push('}');
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
312
365
|
code.push(`const ${item} = ${prop(x, entry.key)};`);
|
|
313
366
|
if (required) {
|
|
314
367
|
const missing = own ? '' : `${item} === undefined && `;
|
|
@@ -385,7 +438,7 @@ const emitUnion = (type, x, key, state) => {
|
|
|
385
438
|
code.push('}');
|
|
386
439
|
code.push(`if (c.count === ${before}) break ${label};`);
|
|
387
440
|
code.push(`c.issues.length = ${before}; c.count = ${before};`);
|
|
388
|
-
code.push(`if (c.async !== null) c.async
|
|
441
|
+
code.push(`if (c.async !== null) ${code.ref(release)}(c.async, ${pending});`);
|
|
389
442
|
}
|
|
390
443
|
const names = [];
|
|
391
444
|
for (let index = 0; index < branches.length; index += 1) names.push(nameOf(branches[index]));
|
package/src/compile/index.d.ts
CHANGED
|
@@ -36,3 +36,9 @@ export function register(options?: CompileOptions): void;
|
|
|
36
36
|
|
|
37
37
|
// Whether this environment allows code generation from strings; probed once.
|
|
38
38
|
export function canEval(): boolean;
|
|
39
|
+
|
|
40
|
+
// Whether the check of a schema (of every entity of a model) runs generated
|
|
41
|
+
// code now: what `compile` reported, asked later. False for a schema built
|
|
42
|
+
// before `register()` and after `{ jit: false }`; a target that is neither
|
|
43
|
+
// a Schema nor a Model is a `TypeError`.
|
|
44
|
+
export function compiled(target: Schema | Model): boolean;
|
package/src/compile/index.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
// in the default mode. Nothing happens on import.
|
|
9
9
|
const { hasBrand, RUN, HOOK } = require('../util.js');
|
|
10
10
|
const { Schema } = require('../schema.js');
|
|
11
|
-
const { SchemaDefinitionError } = require('../errors.js');
|
|
11
|
+
const { SchemaDefinitionError, invalidTarget } = require('../errors.js');
|
|
12
12
|
const { Code } = require('./code.js');
|
|
13
13
|
const { emitRoot } = require('./emit.js');
|
|
14
14
|
|
|
@@ -119,7 +119,7 @@ const install = (target, onFallback) => {
|
|
|
119
119
|
|
|
120
120
|
const schemasOf = (target) => {
|
|
121
121
|
if (hasBrand(target, 'Schema')) return [target];
|
|
122
|
-
if (!hasBrand(target, 'Model')) throw
|
|
122
|
+
if (!hasBrand(target, 'Model')) throw invalidTarget('A Schema or a Model is expected');
|
|
123
123
|
const schemas = [];
|
|
124
124
|
for (const schema of target.entities.values()) schemas.push(schema);
|
|
125
125
|
return schemas;
|
|
@@ -153,6 +153,17 @@ const compile = (target, options = {}) => {
|
|
|
153
153
|
return error === undefined ? { accelerated: true } : { accelerated: false, error };
|
|
154
154
|
};
|
|
155
155
|
|
|
156
|
+
// Whether the check of a schema (of every entity of a model) runs the
|
|
157
|
+
// generated code now: what `compile` reported, asked later.
|
|
158
|
+
const compiled = (target) => {
|
|
159
|
+
const schemas = schemasOf(target);
|
|
160
|
+
for (let index = 0; index < schemas.length; index += 1) {
|
|
161
|
+
const schema = schemas[index];
|
|
162
|
+
if (!ORIGINAL.has(schema) || schema[RUN] === ORIGINAL.get(schema)) return false;
|
|
163
|
+
}
|
|
164
|
+
return true;
|
|
165
|
+
};
|
|
166
|
+
|
|
156
167
|
// Compiles every schema built from now on, with these options; a later call
|
|
157
168
|
// replaces them, and `{ jit: false }` stops it. A schema built before the
|
|
158
169
|
// call is not touched. In the default mode `onFallback` is told of every
|
|
@@ -173,4 +184,4 @@ const register = (options = {}) => {
|
|
|
173
184
|
};
|
|
174
185
|
};
|
|
175
186
|
|
|
176
|
-
module.exports = { compile, register, canEval };
|
|
187
|
+
module.exports = { compile, compiled, register, canEval };
|
package/src/context.js
CHANGED
|
@@ -5,17 +5,12 @@
|
|
|
5
5
|
const en = require('./locales/en.js');
|
|
6
6
|
|
|
7
7
|
const { NONE } = require('./issues.js');
|
|
8
|
+
const { invalidOption: invalid } = require('./errors.js');
|
|
8
9
|
|
|
9
10
|
// The limit when there is none: the largest small integer, so the comparison
|
|
10
11
|
// with the count on every field stays an integer comparison.
|
|
11
12
|
const UNLIMITED = 2 ** 30 - 1;
|
|
12
13
|
|
|
13
|
-
const shown = (value) =>
|
|
14
|
-
typeof value === 'bigint' ? `${value}n` : (JSON.stringify(value) ?? String(value));
|
|
15
|
-
|
|
16
|
-
const invalid = (name, expected, value) =>
|
|
17
|
-
new TypeError(`${name} must be ${expected}, got ${shown(value)}`);
|
|
18
|
-
|
|
19
14
|
// A whole number of issues, or no limit at all.
|
|
20
15
|
const maxErrorsOf = (maxErrors) => {
|
|
21
16
|
const valid = (Number.isInteger(maxErrors) || maxErrors === Infinity) && maxErrors >= 1;
|
|
@@ -126,13 +121,17 @@ const createContext = (options, name = '', unknown = 'reject', parsing = false)
|
|
|
126
121
|
references: 'kind',
|
|
127
122
|
root: name,
|
|
128
123
|
messages: en,
|
|
129
|
-
// The validators that returned a promise, in field order,
|
|
130
|
-
//
|
|
131
|
-
// where such a promise is an
|
|
124
|
+
// The validators that returned a promise, in field order, and the ones
|
|
125
|
+
// kept uncalled over them, settled after the walk by checkAsync and
|
|
126
|
+
// parseAsync; null in a synchronous check, where such a promise is an
|
|
127
|
+
// error.
|
|
132
128
|
async: null,
|
|
133
129
|
// The context parse tries the branches of a plain union in, made at the
|
|
134
130
|
// first such union of a pass and reused for the rest; null in a check.
|
|
135
131
|
scratch: null,
|
|
132
|
+
// Whether the check read a field from one of its aliases: the pass
|
|
133
|
+
// after it renames only when it did.
|
|
134
|
+
renamed: false,
|
|
136
135
|
};
|
|
137
136
|
if (options !== undefined) configure(context, options, parsing);
|
|
138
137
|
return context;
|