@alexify/metaschema 3.4.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +227 -1
- package/README.md +29 -11
- package/SECURITY.md +5 -4
- package/index.d.ts +912 -480
- package/package.json +8 -6
- package/src/checks.js +72 -107
- package/src/codec.js +67 -58
- package/src/coerce/index.d.ts +31 -21
- package/src/coerce/index.js +64 -47
- package/src/compile/code.js +2 -2
- package/src/compile/emit.js +20 -31
- package/src/compile/index.d.ts +41 -25
- package/src/compile/index.js +45 -32
- package/src/context.js +10 -10
- package/src/definition/index.d.ts +10 -7
- package/src/definition/index.js +2 -1
- package/src/derive/index.d.ts +25 -17
- package/src/derive/index.js +21 -15
- package/src/errors.js +84 -59
- package/src/formats/index.d.ts +27 -21
- package/src/formats/index.js +15 -13
- package/src/generators/{fromjsonschema.js → from-json-schema.js} +170 -127
- package/src/generators/index.d.ts +23 -15
- package/src/generators/index.js +2 -2
- package/src/generators/{jsonschema.js → json-schema.js} +37 -43
- package/src/generators/typescript.js +9 -6
- package/src/index.js +2 -1
- package/src/issues.js +41 -42
- package/src/kinds.js +31 -10
- package/src/lint.js +64 -19
- package/src/locales/en.js +7 -7
- package/src/locales/uk.js +6 -6
- package/src/metadata.js +2 -2
- package/src/model/data.js +34 -20
- package/src/model/diff.js +29 -24
- package/src/model/index.d.ts +51 -35
- package/src/model/infer.js +70 -61
- package/src/model/merge.js +29 -18
- package/src/model/pattern.js +105 -102
- package/src/model/restore.js +33 -21
- package/src/model/sample.js +90 -100
- package/src/model.js +4 -2
- package/src/mongodb/bson.js +12 -12
- package/src/mongodb/document.js +7 -6
- package/src/mongodb/index.d.ts +54 -31
- package/src/mongodb/update.js +12 -9
- package/src/preprocessor.js +25 -15
- package/src/prototypes/abstract.js +8 -8
- package/src/prototypes/collections.js +7 -7
- package/src/prototypes/flags.js +1 -1
- package/src/prototypes/reference.js +3 -4
- package/src/prototypes/scalars.js +1 -1
- package/src/prototypes/schema.js +5 -6
- package/src/prototypes/tuple.js +11 -10
- package/src/prototypes/union.js +5 -4
- package/src/rules.js +23 -20
- package/src/runtime/browser.js +6 -1
- package/src/runtime/node.js +19 -2
- package/src/schema.js +19 -14
- package/src/snapshot.js +14 -12
- package/src/stream/index.d.ts +33 -21
- package/src/stream/index.js +37 -20
- package/src/struct.js +6 -6
- package/src/types.js +35 -26
- package/src/util.js +70 -64
- package/src/validation.js +10 -13
package/src/compile/emit.js
CHANGED
|
@@ -49,7 +49,7 @@ const record = (state, statement, extra = []) => {
|
|
|
49
49
|
|
|
50
50
|
// The state under a container: its key joins the prefix of its children.
|
|
51
51
|
const inside = (state, key) =>
|
|
52
|
-
key === undefined ? state : { ...state, prefix:
|
|
52
|
+
key === undefined ? state : { ...state, prefix: state.prefix.concat([key]) };
|
|
53
53
|
|
|
54
54
|
const issue = (state, name, args) => `${state.code.ref(issues)}.${name}(c, ${args});`;
|
|
55
55
|
|
|
@@ -133,13 +133,14 @@ const emitRange = (type, x, key, state) => {
|
|
|
133
133
|
code.push('}');
|
|
134
134
|
};
|
|
135
135
|
|
|
136
|
-
const RULES =
|
|
136
|
+
const RULES = {
|
|
137
|
+
__proto__: null,
|
|
137
138
|
length: emitLength,
|
|
138
139
|
pattern: emitPattern,
|
|
139
140
|
format: emitFormat,
|
|
140
141
|
decimals: emitDecimals,
|
|
141
142
|
range: emitRange,
|
|
142
|
-
}
|
|
143
|
+
};
|
|
143
144
|
|
|
144
145
|
// The rules of a field in the order the closure backend runs them.
|
|
145
146
|
const rulesOf = (type) => {
|
|
@@ -173,10 +174,7 @@ const typed = (type, x, key, state, fails, failure, body) => {
|
|
|
173
174
|
record(state, failure);
|
|
174
175
|
const rules = type === null ? [] : rulesOf(type);
|
|
175
176
|
const tail = type !== null && (rules.length > 0 || type.validate);
|
|
176
|
-
if (body === undefined && !tail)
|
|
177
|
-
code.push('}');
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
177
|
+
if (body === undefined && !tail) return void code.push('}');
|
|
180
178
|
code.push('} else {');
|
|
181
179
|
const before = body !== undefined && tail ? code.name('b') : '';
|
|
182
180
|
if (before) code.push(`const ${before} = c.count;`);
|
|
@@ -200,15 +198,10 @@ const emitDate = (type, x, key, state) => {
|
|
|
200
198
|
typed(type, x, key, state, fails, typeIssue(state, 'date', x, key));
|
|
201
199
|
};
|
|
202
200
|
|
|
203
|
-
const emitBytes = (type, x, key, state) =>
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
key,
|
|
208
|
-
state,
|
|
209
|
-
`!${state.code.ref(isBytes)}(${x})`,
|
|
210
|
-
typeIssue(state, 'bytes', x, key),
|
|
211
|
-
);
|
|
201
|
+
const emitBytes = (type, x, key, state) => {
|
|
202
|
+
const fails = `!${state.code.ref(isBytes)}(${x})`;
|
|
203
|
+
return typed(type, x, key, state, fails, typeIssue(state, 'bytes', x, key));
|
|
204
|
+
};
|
|
212
205
|
|
|
213
206
|
const emitNull = (type, x, key, state) =>
|
|
214
207
|
typed(type, x, key, state, `${x} !== null`, typeIssue(state, 'null', x, key));
|
|
@@ -225,22 +218,23 @@ const emitEnum = (type, x, key, state) => {
|
|
|
225
218
|
const { code } = state;
|
|
226
219
|
const values = type.enum;
|
|
227
220
|
const { length } = values;
|
|
228
|
-
let fails;
|
|
221
|
+
let fails = '';
|
|
229
222
|
if (length > ENUM_SET_SIZE) {
|
|
230
223
|
fails = `!${code.ref(new Set(values))}.has(${x})`;
|
|
231
224
|
} else {
|
|
232
225
|
// A chain of comparisons with the literals, unless a value has none
|
|
233
226
|
// (NaN, an object): then the list itself, as the closure backend scans it.
|
|
234
227
|
let chain = '';
|
|
228
|
+
let literals = true;
|
|
235
229
|
for (let index = 0; index < length; index += 1) {
|
|
236
230
|
const text = literal(values[index]);
|
|
237
231
|
if (text === undefined) {
|
|
238
|
-
|
|
232
|
+
literals = false;
|
|
239
233
|
break;
|
|
240
234
|
}
|
|
241
235
|
chain += `${index === 0 ? '' : ' && '}${x} !== ${text}`;
|
|
242
236
|
}
|
|
243
|
-
fails =
|
|
237
|
+
fails = literals ? chain : `!${code.ref(values)}.includes(${x})`;
|
|
244
238
|
}
|
|
245
239
|
const failure = issue(state, 'enum', `${code.ref(values)}, ${keyOf(key)}`);
|
|
246
240
|
typed(type, x, key, state, fails, failure);
|
|
@@ -371,7 +365,7 @@ const emitTuple = (type, x, key, state) => {
|
|
|
371
365
|
for (let index = 0; index < length; index += 1) {
|
|
372
366
|
const item = code.name('x');
|
|
373
367
|
code.push(`const ${item} = ${x}[${index}];`);
|
|
374
|
-
emitField(elements[index], item,
|
|
368
|
+
emitField(elements[index], item, `${index}`, children);
|
|
375
369
|
}
|
|
376
370
|
code.push('}');
|
|
377
371
|
});
|
|
@@ -464,10 +458,7 @@ const emitStruct = (fields, x, key, state) => {
|
|
|
464
458
|
// any, is the field's own: a schema of one type has no schema-level one).
|
|
465
459
|
const emitSchema = (type, x, key, state) => {
|
|
466
460
|
const fields = type.schema;
|
|
467
|
-
if (!isStruct(fields))
|
|
468
|
-
emitField(fields, x, key, state);
|
|
469
|
-
return;
|
|
470
|
-
}
|
|
461
|
+
if (!isStruct(fields)) return void emitField(fields, x, key, state);
|
|
471
462
|
const body = () => emitStruct(fields, x, key, state);
|
|
472
463
|
typed(type, x, key, state, notObject(x), typeIssue(state, 'object', x, key), body);
|
|
473
464
|
};
|
|
@@ -504,8 +495,7 @@ const emitUnion = (type, x, key, state) => {
|
|
|
504
495
|
if (!type.validate) return;
|
|
505
496
|
code.push(`if (c.count === ${before}) {`);
|
|
506
497
|
emitTail(type, x, key, state, []);
|
|
507
|
-
code.push('}');
|
|
508
|
-
return;
|
|
498
|
+
return void code.push('}');
|
|
509
499
|
}
|
|
510
500
|
const fails = `${x} === null || typeof ${x} !== "object"`;
|
|
511
501
|
typed(type, x, key, state, fails, typeIssue(state, 'object', x, key), () => {
|
|
@@ -574,17 +564,16 @@ const EMITTERS = new Map([
|
|
|
574
564
|
const emitField = (type, x, key, state) => {
|
|
575
565
|
const emitter = EMITTERS.get(type.compile);
|
|
576
566
|
if (emitter === undefined) {
|
|
577
|
-
record(state, `${state.code.ref(type.check)}(${x}, c, ${keyOf(key)});`);
|
|
578
|
-
return;
|
|
567
|
+
return void record(state, `${state.code.ref(type.check)}(${x}, c, ${keyOf(key)});`);
|
|
579
568
|
}
|
|
580
569
|
const { code } = state;
|
|
581
570
|
const { required } = type;
|
|
582
|
-
let guard =
|
|
571
|
+
let guard = '';
|
|
583
572
|
if (!required) guard = `${x} != null`;
|
|
584
573
|
else if (type.nullable === true) guard = `${x} !== null`;
|
|
585
|
-
if (guard !==
|
|
574
|
+
if (guard !== '') code.push(`if (${guard}) {`);
|
|
586
575
|
emitter(type, x, key, state);
|
|
587
|
-
if (guard !==
|
|
576
|
+
if (guard !== '') code.push('}');
|
|
588
577
|
};
|
|
589
578
|
|
|
590
579
|
// The body of a schema's check, `(v, c, k)`: the key the root was called
|
package/src/compile/index.d.ts
CHANGED
|
@@ -1,44 +1,60 @@
|
|
|
1
1
|
import type { Model, Schema, SchemaDefinitionError } from '../../index.js';
|
|
2
2
|
|
|
3
|
+
/** The options of `compile` and `register`. */
|
|
3
4
|
export interface CompileOptions {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
/**
|
|
6
|
+
* 'auto' (the default) generates the check where the environment allows
|
|
7
|
+
* code generation from strings and keeps the closure backend silently where
|
|
8
|
+
* it does not; true throws `SchemaDefinitionError` with the code
|
|
9
|
+
* `ALX_ERR_NO_EVAL` instead of falling back; false never generates code.
|
|
10
|
+
*/
|
|
8
11
|
jit?: 'auto' | boolean;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Told of every schema the default mode could not compile, with the
|
|
14
|
+
* `ALX_ERR_JIT_BUILD` error (the error of the generator as its `cause`); the
|
|
15
|
+
* schema keeps its closures.
|
|
16
|
+
*/
|
|
12
17
|
onFallback?: (schema: Schema, error: SchemaDefinitionError) => void;
|
|
13
18
|
}
|
|
14
19
|
|
|
20
|
+
/** What `compile` did: whether the check runs generated code now, and why not. */
|
|
15
21
|
export interface Compiled {
|
|
16
|
-
|
|
17
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Whether the check runs generated code now; false in the fallback, where
|
|
24
|
+
* the closure backend is left as it is.
|
|
25
|
+
*/
|
|
18
26
|
accelerated: boolean;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
27
|
+
/**
|
|
28
|
+
* What stopped the first check that could not be generated (`ALX_ERR_JIT_BUILD`,
|
|
29
|
+
* the generator's error as its `cause`); absent where the environment allows
|
|
30
|
+
* no code generation at all, and after `{ jit: false }`.
|
|
31
|
+
*/
|
|
22
32
|
error?: SchemaDefinitionError;
|
|
23
33
|
}
|
|
24
34
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
35
|
+
/**
|
|
36
|
+
* Replaces the check of a schema, or of every entity of a model, with the
|
|
37
|
+
* generated one. The issues are the ones the closure backend records, in the
|
|
38
|
+
* same order. A target that is neither is a `SchemaUsageError`. With `jit: true` a
|
|
39
|
+
* check that cannot be generated throws `ALX_ERR_JIT_BUILD` and nothing is
|
|
40
|
+
* installed; `{ jit: false }` puts the closures back.
|
|
41
|
+
*/
|
|
30
42
|
export function compile(target: Schema | Model, options?: CompileOptions): Compiled;
|
|
31
43
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
44
|
+
/**
|
|
45
|
+
* Compiles every schema built from now on with these options; a later call
|
|
46
|
+
* replaces them, `{ jit: false }` stops it. A schema built before the call is
|
|
47
|
+
* not touched: pass it to `compile`.
|
|
48
|
+
*/
|
|
35
49
|
export function register(options?: CompileOptions): void;
|
|
36
50
|
|
|
37
|
-
|
|
51
|
+
/** Whether this environment allows code generation from strings; probed once. */
|
|
38
52
|
export function canEval(): boolean;
|
|
39
53
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
54
|
+
/**
|
|
55
|
+
* Whether the check of a schema (of every entity of a model) runs generated
|
|
56
|
+
* code now: what `compile` reported, asked later. False for a schema built
|
|
57
|
+
* before `register()` and after `{ jit: false }`; a target that is neither
|
|
58
|
+
* a Schema nor a Model is a `SchemaUsageError`.
|
|
59
|
+
*/
|
|
44
60
|
export function compiled(target: Schema | Model): boolean;
|
package/src/compile/index.js
CHANGED
|
@@ -8,40 +8,63 @@
|
|
|
8
8
|
// in the default mode. Nothing happens on import.
|
|
9
9
|
const { hasBrand, RUN, HOOK } = require('../util.js');
|
|
10
10
|
const { Schema } = require('../validation.js');
|
|
11
|
-
const {
|
|
11
|
+
const { isThenable, abandon } = require('../issues.js');
|
|
12
|
+
const { SchemaDefinitionError, SchemaUsageError, invalidTarget } = require('../errors.js');
|
|
12
13
|
const { Code } = require('./code.js');
|
|
13
14
|
const { emitRoot } = require('./emit.js');
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
// The checks the subpath generated: the closures a schema runs instead may
|
|
17
|
+
// be the ones it was built with or ones compiled later, on first use.
|
|
18
|
+
const GENERATED = new WeakSet();
|
|
19
|
+
|
|
20
|
+
// The check a schema was built with, kept when the generated one replaces it
|
|
21
|
+
// so that `{ jit: false }` can put it back.
|
|
22
|
+
const ORIGINAL = new WeakMap();
|
|
23
|
+
|
|
24
|
+
// An option of the call that is not what it must be: the call is wrong.
|
|
25
|
+
const wrongOption = (reason) => new SchemaUsageError(reason, { code: 'ALX_ERR_INVALID_OPTION' });
|
|
26
|
+
|
|
27
|
+
// An observer never fails the operation it watches (alexiskills,
|
|
28
|
+
// library-errors): what the hook throws, or a promise of it that rejects, is
|
|
29
|
+
// dropped. A copy per subpath, as `install`, so the core carries none.
|
|
30
|
+
const notify = (observer, first, second) => {
|
|
31
|
+
try {
|
|
32
|
+
const returned = observer(first, second);
|
|
33
|
+
if (isThenable(returned)) abandon(returned);
|
|
34
|
+
} catch {
|
|
35
|
+
// ignore: an observer never fails the operation it watches
|
|
36
|
+
}
|
|
37
|
+
};
|
|
16
38
|
|
|
17
39
|
// The options of compile and register: the mode, and the function told of
|
|
18
40
|
// every schema the default mode could not compile.
|
|
19
41
|
const optionsOf = (options) => {
|
|
20
42
|
if (options === null || typeof options !== 'object') {
|
|
21
|
-
throw
|
|
43
|
+
throw wrongOption(`Compile options must be an object, got ${JSON.stringify(options)}`);
|
|
22
44
|
}
|
|
23
45
|
const { jit = 'auto', onFallback } = options;
|
|
24
46
|
if (jit !== 'auto' && jit !== true && jit !== false) {
|
|
25
47
|
const got = JSON.stringify(jit);
|
|
26
|
-
throw
|
|
48
|
+
throw wrongOption(`Compile option "jit" must be "auto", true or false, got ${got}`);
|
|
27
49
|
}
|
|
28
50
|
if (onFallback !== undefined && typeof onFallback !== 'function') {
|
|
29
|
-
throw
|
|
51
|
+
throw wrongOption('Compile option "onFallback" must be a function');
|
|
30
52
|
}
|
|
31
53
|
return { mode: jit, onFallback };
|
|
32
54
|
};
|
|
33
55
|
|
|
34
|
-
let
|
|
56
|
+
let probed = false;
|
|
57
|
+
let evaluates = false;
|
|
35
58
|
|
|
36
59
|
// Whether this environment builds a function from source, probed once.
|
|
37
60
|
const canEval = () => {
|
|
38
|
-
if (
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
61
|
+
if (probed) return evaluates;
|
|
62
|
+
probed = true;
|
|
63
|
+
try {
|
|
64
|
+
// oxlint-disable-next-line no-new-func
|
|
65
|
+
evaluates = typeof new Function('') === 'function';
|
|
66
|
+
} catch {
|
|
67
|
+
// ignore: no code generation from strings here, `evaluates` stays false
|
|
45
68
|
}
|
|
46
69
|
return evaluates;
|
|
47
70
|
};
|
|
@@ -50,7 +73,7 @@ const noEval = () => {
|
|
|
50
73
|
const reason =
|
|
51
74
|
'Code generation from strings is not allowed in this environment; ' +
|
|
52
75
|
'jit: "auto" keeps the closure backend instead';
|
|
53
|
-
return new SchemaDefinitionError('
|
|
76
|
+
return new SchemaDefinitionError(reason, { code: 'ALX_ERR_NO_EVAL' });
|
|
54
77
|
};
|
|
55
78
|
|
|
56
79
|
// Whether a mode generates code here: never for false, where it can for
|
|
@@ -66,15 +89,9 @@ const accelerates = (mode) => {
|
|
|
66
89
|
// error of the emitter or of `new Function` as the cause, under one code.
|
|
67
90
|
const failure = (target, error) => {
|
|
68
91
|
const reason = `The check of "${target.name}" could not be generated: ${error.message}`;
|
|
69
|
-
|
|
70
|
-
wrapped.cause = error;
|
|
71
|
-
return wrapped;
|
|
92
|
+
return new SchemaDefinitionError(reason, { code: 'ALX_ERR_JIT_BUILD', cause: error });
|
|
72
93
|
};
|
|
73
94
|
|
|
74
|
-
// The checks the subpath generated: the closures a schema runs instead may
|
|
75
|
-
// be the ones it was built with or ones compiled later, on first use.
|
|
76
|
-
const GENERATED = new WeakSet();
|
|
77
|
-
|
|
78
95
|
const build = (target) => {
|
|
79
96
|
const code = new Code();
|
|
80
97
|
emitRoot(target, code);
|
|
@@ -85,7 +102,7 @@ const build = (target) => {
|
|
|
85
102
|
return run;
|
|
86
103
|
};
|
|
87
104
|
|
|
88
|
-
// The generated check, or
|
|
105
|
+
// The generated check, or ALX_ERR_JIT_BUILD: the JIT was demanded.
|
|
89
106
|
const generate = (target) => {
|
|
90
107
|
try {
|
|
91
108
|
return build(target);
|
|
@@ -94,10 +111,6 @@ const generate = (target) => {
|
|
|
94
111
|
}
|
|
95
112
|
};
|
|
96
113
|
|
|
97
|
-
// The check a schema was built with, kept when the generated one replaces it
|
|
98
|
-
// so that `{ jit: false }` can put it back.
|
|
99
|
-
const ORIGINAL = new WeakMap();
|
|
100
|
-
|
|
101
114
|
const replace = (target, run) => {
|
|
102
115
|
if (!ORIGINAL.has(target)) ORIGINAL.set(target, target[RUN]);
|
|
103
116
|
target[RUN] = run;
|
|
@@ -111,16 +124,16 @@ const restore = (target) => {
|
|
|
111
124
|
// Replaces the check of a schema with the generated one; a failure to build
|
|
112
125
|
// it is a fallback, reported to `onFallback` and returned.
|
|
113
126
|
const install = (target, onFallback) => {
|
|
114
|
-
let run;
|
|
127
|
+
let run = null;
|
|
115
128
|
try {
|
|
116
129
|
run = build(target);
|
|
117
130
|
} catch (error) {
|
|
118
131
|
const wrapped = failure(target, error);
|
|
119
|
-
if (onFallback !== undefined) onFallback
|
|
132
|
+
if (onFallback !== undefined) notify(onFallback, target, wrapped);
|
|
120
133
|
return wrapped;
|
|
121
134
|
}
|
|
122
135
|
replace(target, run);
|
|
123
|
-
return
|
|
136
|
+
return null;
|
|
124
137
|
};
|
|
125
138
|
|
|
126
139
|
const schemasOf = (target) => {
|
|
@@ -151,12 +164,12 @@ const compile = (target, options = {}) => {
|
|
|
151
164
|
for (let index = 0; index < length; index += 1) replace(schemas[index], runs[index]);
|
|
152
165
|
return { accelerated: true };
|
|
153
166
|
}
|
|
154
|
-
let error;
|
|
167
|
+
let error = null;
|
|
155
168
|
for (let index = 0; index < length; index += 1) {
|
|
156
169
|
const failed = install(schemas[index], onFallback);
|
|
157
|
-
if (failed !==
|
|
170
|
+
if (failed !== null && error === null) error = failed;
|
|
158
171
|
}
|
|
159
|
-
return error ===
|
|
172
|
+
return error === null ? { accelerated: true } : { accelerated: false, error };
|
|
160
173
|
};
|
|
161
174
|
|
|
162
175
|
// Whether the check of a schema (of every entity of a model) runs the
|
package/src/context.js
CHANGED
|
@@ -11,6 +11,15 @@ const { invalidOption: invalid } = require('./errors.js');
|
|
|
11
11
|
// with the count on every field stays an integer comparison.
|
|
12
12
|
const UNLIMITED = 2 ** 30 - 1;
|
|
13
13
|
|
|
14
|
+
// How deep a value may nest through embedded references before the check
|
|
15
|
+
// gives up on it: a graph that deep is a `depth` issue, not a stack overflow.
|
|
16
|
+
const MAX_DEPTH = 128;
|
|
17
|
+
|
|
18
|
+
// A caller's locale table as a null-prototype copy, made once per table: the
|
|
19
|
+
// renderers are looked up by issue code, and a code a validator made up must
|
|
20
|
+
// find nothing on Object.prototype.
|
|
21
|
+
const TABLES = new WeakMap();
|
|
22
|
+
|
|
14
23
|
// A whole number of issues, or no limit at all.
|
|
15
24
|
const maxErrorsOf = (maxErrors) => {
|
|
16
25
|
const valid = (Number.isInteger(maxErrors) || maxErrors === Infinity) && maxErrors >= 1;
|
|
@@ -18,10 +27,6 @@ const maxErrorsOf = (maxErrors) => {
|
|
|
18
27
|
return Math.min(maxErrors, UNLIMITED);
|
|
19
28
|
};
|
|
20
29
|
|
|
21
|
-
// How deep a value may nest through embedded references before the check
|
|
22
|
-
// gives up on it: a graph that deep is a `depth` issue, not a stack overflow.
|
|
23
|
-
const MAX_DEPTH = 128;
|
|
24
|
-
|
|
25
30
|
const maxDepthOf = (maxDepth) => {
|
|
26
31
|
if (!Number.isInteger(maxDepth) || maxDepth < 1) {
|
|
27
32
|
throw invalid('maxDepth', 'an integer of at least 1', maxDepth);
|
|
@@ -29,11 +34,6 @@ const maxDepthOf = (maxDepth) => {
|
|
|
29
34
|
return maxDepth;
|
|
30
35
|
};
|
|
31
36
|
|
|
32
|
-
// A caller's locale table as a null-prototype copy, made once per table: the
|
|
33
|
-
// renderers are looked up by issue code, and a code a validator made up must
|
|
34
|
-
// find nothing on Object.prototype.
|
|
35
|
-
const TABLES = new WeakMap();
|
|
36
|
-
|
|
37
37
|
const messagesOf = (messages) => {
|
|
38
38
|
if (typeof messages === 'function') return messages;
|
|
39
39
|
if (messages === null || typeof messages !== 'object') {
|
|
@@ -142,4 +142,4 @@ const createContext = (options, name = '', unknown = 'reject', parsing = false)
|
|
|
142
142
|
return context;
|
|
143
143
|
};
|
|
144
144
|
|
|
145
|
-
module.exports = {
|
|
145
|
+
module.exports = { UNLIMITED, MAX_DEPTH, referencesOf, createContext };
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The `@alexify/metaschema/definition` subpath: the core's own Schema and
|
|
3
|
+
* Model, built and read without the half that validates. Until the core
|
|
4
|
+
* entry is loaded as well, `check`, `checkAsync`, `parse`, `parseAsync`,
|
|
5
|
+
* `serialize`, `validate`, `checkConsistency`, `warnings`, the `check` of a
|
|
6
|
+
* field and `model.warnings` throw a `SchemaUsageError` with the code
|
|
7
|
+
* `ALX_ERR_NOT_REGISTERED`, and a schema has no `~standard`; the typings are the
|
|
8
|
+
* core's, since loading the core installs all of them into these classes.
|
|
9
|
+
*/
|
|
8
10
|
export {
|
|
9
11
|
KIND,
|
|
10
12
|
KIND_STORED,
|
|
@@ -16,4 +18,5 @@ export {
|
|
|
16
18
|
Schema,
|
|
17
19
|
Model,
|
|
18
20
|
SchemaDefinitionError,
|
|
21
|
+
SchemaUsageError,
|
|
19
22
|
} from '../../index.js';
|
package/src/definition/index.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
const { constants, getKindMetadata } = require('../kinds.js');
|
|
10
10
|
const { Schema } = require('../schema.js');
|
|
11
11
|
const { Model } = require('../model.js');
|
|
12
|
-
const { SchemaDefinitionError } = require('../errors.js');
|
|
12
|
+
const { SchemaDefinitionError, SchemaUsageError } = require('../errors.js');
|
|
13
13
|
|
|
14
14
|
const { KIND, KIND_STORED, KIND_MEMORY, SCOPE, STORE, ALLOW } = constants;
|
|
15
15
|
|
|
@@ -24,4 +24,5 @@ module.exports = {
|
|
|
24
24
|
Schema,
|
|
25
25
|
Model,
|
|
26
26
|
SchemaDefinitionError,
|
|
27
|
+
SchemaUsageError,
|
|
27
28
|
};
|
package/src/derive/index.d.ts
CHANGED
|
@@ -20,11 +20,13 @@ import type {
|
|
|
20
20
|
// stored kind or a projection becomes a struct (without the id of the kind,
|
|
21
21
|
// unless the call names it), a memory kind keeps its metadata, the name is
|
|
22
22
|
// the source's unless `options.name`. A key the schema has no field of is
|
|
23
|
-
// `
|
|
24
|
-
// struct is `
|
|
23
|
+
// `ALX_ERR_UNKNOWN_FIELD`, located in the schema; anything but a Schema of a
|
|
24
|
+
// struct is `ALX_ERR_INVALID_TARGET`.
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
/**
|
|
27
|
+
* The fields named, in the order of the schema. The schema's own functions
|
|
28
|
+
* do not come along.
|
|
29
|
+
*/
|
|
28
30
|
export function pick<
|
|
29
31
|
D extends string | object,
|
|
30
32
|
const K extends readonly DeriveKeyOf<D>[],
|
|
@@ -35,7 +37,7 @@ export function pick<
|
|
|
35
37
|
options?: O,
|
|
36
38
|
): DerivedSchema<D, PickDefinition<D, K[number], O>>;
|
|
37
39
|
|
|
38
|
-
|
|
40
|
+
/** Every field but the ones named. */
|
|
39
41
|
export function omit<
|
|
40
42
|
D extends string | object,
|
|
41
43
|
const K extends readonly DeriveKeyOf<D>[],
|
|
@@ -46,25 +48,29 @@ export function omit<
|
|
|
46
48
|
options?: O,
|
|
47
49
|
): DerivedSchema<D, OmitDefinition<D, K[number], O>>;
|
|
48
50
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Every field (or the ones in `options.keys`) optional, without its default
|
|
53
|
+
* unless `defaults: true`; `deep` goes into nested structs, not into
|
|
54
|
+
* collections, tuples, unions or references.
|
|
55
|
+
*/
|
|
52
56
|
export function partial<
|
|
53
57
|
D extends string | object,
|
|
54
58
|
const O extends PartialOptions<DeriveKeyOf<D>> = {},
|
|
55
59
|
>(schema: Schema<D> & StructGuardOf<D>, options?: O): DerivedSchema<D, PartialDefinition<D, O>>;
|
|
56
60
|
|
|
57
|
-
|
|
61
|
+
/** Every field (or the ones in `options.keys`) required; the defaults stay. */
|
|
58
62
|
export function required<
|
|
59
63
|
D extends string | object,
|
|
60
64
|
const O extends RequiredOptions<DeriveKeyOf<D>> = {},
|
|
61
65
|
>(schema: Schema<D> & StructGuardOf<D>, options?: O): DerivedSchema<D, RequiredDefinition<D, O>>;
|
|
62
66
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
/**
|
|
68
|
+
* The fields of the extension (an object of fields, or a Schema of a
|
|
69
|
+
* struct) over the schema's: one of the same name takes the place of the
|
|
70
|
+
* schema's, the others come after. `merge` of `./model` unifies instead. An
|
|
71
|
+
* extension whose definition is not known (an entity of a model) gives a
|
|
72
|
+
* `Schema` of an unknown one, as a source whose definition is not known does.
|
|
73
|
+
*/
|
|
68
74
|
export function extend<
|
|
69
75
|
D extends string | object,
|
|
70
76
|
const X extends object,
|
|
@@ -75,12 +81,14 @@ export function extend<
|
|
|
75
81
|
options?: O,
|
|
76
82
|
): ExtendedSchema<D, X, ExtendDefinition<D, X, O>>;
|
|
77
83
|
|
|
78
|
-
|
|
84
|
+
/** The names of the fields a value carries, as a schema of an enum. */
|
|
79
85
|
export function keyof<D extends string | object>(
|
|
80
86
|
schema: Schema<D> & StructGuardOf<D>,
|
|
81
87
|
options?: DeriveOptions,
|
|
82
88
|
): DerivedSchema<D, KeyofDefinition<D>>;
|
|
83
89
|
|
|
84
|
-
|
|
85
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Adds the six as methods of Schema (`schema.omit(['id']).partial()`), not
|
|
92
|
+
* enumerable. Once per process; importing the subpath registers nothing.
|
|
93
|
+
*/
|
|
86
94
|
export function register(): void;
|