@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/runtime/node.js
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
1
|
const fsp = require('node:fs').promises;
|
|
2
2
|
|
|
3
3
|
// The one file-system call of the package, behind `saveTypes` of the
|
|
4
|
-
// `./generators` subpath.
|
|
5
|
-
|
|
4
|
+
// `./generators` subpath. The text goes to a file of its own beside the
|
|
5
|
+
// target, then takes the target's place by a rename, which is atomic within
|
|
6
|
+
// a directory: a reader (`tsc --watch`, an editor) never sees half a
|
|
7
|
+
// declaration file, and a write that fails leaves the old file as it was.
|
|
8
|
+
// `wx` refuses a temporary name that exists; the counter keeps the names of
|
|
9
|
+
// two writes of one process apart.
|
|
10
|
+
let written = 0;
|
|
11
|
+
|
|
12
|
+
const writeFile = async (outputFile, text) => {
|
|
13
|
+
written += 1;
|
|
14
|
+
const temporary = `${outputFile}.${process.pid}.${written}.tmp`;
|
|
15
|
+
try {
|
|
16
|
+
await fsp.writeFile(temporary, text, { flag: 'wx' });
|
|
17
|
+
await fsp.rename(temporary, outputFile);
|
|
18
|
+
} catch (error) {
|
|
19
|
+
await fsp.rm(temporary, { force: true });
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
6
23
|
|
|
7
24
|
module.exports = { writeFile };
|
package/src/schema.js
CHANGED
|
@@ -7,6 +7,7 @@ const {
|
|
|
7
7
|
COERCES,
|
|
8
8
|
CODEC,
|
|
9
9
|
STANDARD,
|
|
10
|
+
KINDS,
|
|
10
11
|
hasBrand,
|
|
11
12
|
} = require('./util.js');
|
|
12
13
|
const { TYPES } = require('./types.js');
|
|
@@ -31,6 +32,14 @@ const lazyRun = (schema) => {
|
|
|
31
32
|
return run;
|
|
32
33
|
};
|
|
33
34
|
|
|
35
|
+
// A stub of a validation method: the method installed since, when the
|
|
36
|
+
// validation half has been, else the error that it is not.
|
|
37
|
+
const late = (schema, name, args) => {
|
|
38
|
+
const method = Schema.prototype[name];
|
|
39
|
+
if (method === STUBS[name]) throw noValidation(name);
|
|
40
|
+
return method.apply(schema, args);
|
|
41
|
+
};
|
|
42
|
+
|
|
34
43
|
// The half of the core that builds a schema and reads it: its fields and
|
|
35
44
|
// metadata, its JSON and its snapshot. The validation half (`validation.js`,
|
|
36
45
|
// which the core entry loads) installs `check`, `parse`, `serialize` and
|
|
@@ -58,9 +67,9 @@ class Schema extends SchemaMetadata {
|
|
|
58
67
|
if (hasBrand(raw, 'Schema')) {
|
|
59
68
|
if (name && raw.name && raw.name !== name) {
|
|
60
69
|
const reason = `Schema "${raw.name}" cannot be used as "${name}"`;
|
|
61
|
-
throw new SchemaDefinitionError(
|
|
70
|
+
throw new SchemaDefinitionError(reason);
|
|
62
71
|
}
|
|
63
|
-
raw.attach(
|
|
72
|
+
for (const namespace of namespaces) raw.attach(namespace);
|
|
64
73
|
return raw;
|
|
65
74
|
}
|
|
66
75
|
super();
|
|
@@ -83,11 +92,13 @@ class Schema extends SchemaMetadata {
|
|
|
83
92
|
// the cycle detection (only a struct with references can reach it
|
|
84
93
|
// again), whether parse coerces unless the call says otherwise (the
|
|
85
94
|
// `coerce` of the kind metadata, read once: an own property would shadow
|
|
86
|
-
// the method the `./coerce` subpath installs), and
|
|
95
|
+
// the method the `./coerce` subpath installs), its caches, and the kinds
|
|
96
|
+
// of the program's own the definition named, for the lint.
|
|
87
97
|
this[TRACKED] = isSchemaType && this.relations.size > 0;
|
|
88
98
|
this[COERCES] = Object.hasOwn(this.custom, 'coerce') && this.custom.coerce === true;
|
|
89
99
|
this[CODEC] = null;
|
|
90
100
|
this[STANDARD] = null;
|
|
101
|
+
this[KINDS] = preprocessor.kinds;
|
|
91
102
|
this[RUN] = lazyRun(this);
|
|
92
103
|
const hook = Schema[HOOK];
|
|
93
104
|
if (hook !== undefined) hook(this);
|
|
@@ -97,7 +108,7 @@ class Schema extends SchemaMetadata {
|
|
|
97
108
|
if (this.namespaces.size === 0) return TYPES;
|
|
98
109
|
if (this.#types === null) {
|
|
99
110
|
const types = Object.create(null);
|
|
100
|
-
for (const
|
|
111
|
+
for (const namespace of this.namespaces) Object.assign(types, namespace.types);
|
|
101
112
|
this.#types = types;
|
|
102
113
|
}
|
|
103
114
|
return this.#types;
|
|
@@ -108,8 +119,8 @@ class Schema extends SchemaMetadata {
|
|
|
108
119
|
// first, so a schema with the name of one of their entities (a schema
|
|
109
120
|
// derived from it keeps the name) still reaches that entity.
|
|
110
121
|
findReference(name) {
|
|
111
|
-
for (const
|
|
112
|
-
const entity =
|
|
122
|
+
for (const namespace of this.namespaces) {
|
|
123
|
+
const entity = namespace.entities.get(name);
|
|
113
124
|
if (entity) return entity;
|
|
114
125
|
}
|
|
115
126
|
return name === this.name && name !== '' ? this : null;
|
|
@@ -119,13 +130,13 @@ class Schema extends SchemaMetadata {
|
|
|
119
130
|
// latter through the entities an async validator may be in), so both are
|
|
120
131
|
// rebuilt on the next use.
|
|
121
132
|
attach(...namespaces) {
|
|
122
|
-
for (const
|
|
133
|
+
for (const namespace of namespaces) this.namespaces.add(namespace);
|
|
123
134
|
this.#types = null;
|
|
124
135
|
this[STANDARD] = null;
|
|
125
136
|
}
|
|
126
137
|
|
|
127
138
|
detach(...namespaces) {
|
|
128
|
-
for (const
|
|
139
|
+
for (const namespace of namespaces) this.namespaces.delete(namespace);
|
|
129
140
|
this.#types = null;
|
|
130
141
|
this[STANDARD] = null;
|
|
131
142
|
}
|
|
@@ -181,12 +192,6 @@ const STUBS = {
|
|
|
181
192
|
},
|
|
182
193
|
};
|
|
183
194
|
|
|
184
|
-
const late = (schema, name, args) => {
|
|
185
|
-
const method = Schema.prototype[name];
|
|
186
|
-
if (method === STUBS[name]) throw noValidation(name);
|
|
187
|
-
return method.apply(schema, args);
|
|
188
|
-
};
|
|
189
|
-
|
|
190
195
|
for (const name of Object.keys(STUBS)) {
|
|
191
196
|
Object.defineProperty(Schema.prototype, name, {
|
|
192
197
|
value: STUBS[name],
|
package/src/snapshot.js
CHANGED
|
@@ -49,7 +49,7 @@ const base64Of = (bytes) => {
|
|
|
49
49
|
const valueOf = (value) => {
|
|
50
50
|
const type = typeof value;
|
|
51
51
|
if (type === 'function') return { $function: true };
|
|
52
|
-
if (type === 'bigint') return { $bigint:
|
|
52
|
+
if (type === 'bigint') return { $bigint: `${value}` };
|
|
53
53
|
if (type !== 'object' || value === null) return value;
|
|
54
54
|
if (hasBrand(value, 'Type')) return fieldOf(value);
|
|
55
55
|
if (Array.isArray(value)) return listOf(value);
|
|
@@ -67,7 +67,9 @@ const valueOf = (value) => {
|
|
|
67
67
|
// sorted, so the text does not depend on how the definition was written.
|
|
68
68
|
const fieldOf = (field) => {
|
|
69
69
|
const out = { type: field.type, required: field.required };
|
|
70
|
-
|
|
70
|
+
const keys = Object.keys(field);
|
|
71
|
+
keys.sort();
|
|
72
|
+
for (const key of keys) {
|
|
71
73
|
const item = field[key];
|
|
72
74
|
if (key !== 'root' && item !== undefined) out[key] = valueOf(item);
|
|
73
75
|
}
|
|
@@ -137,10 +139,9 @@ const typeOf = (Type) => {
|
|
|
137
139
|
};
|
|
138
140
|
|
|
139
141
|
const typeClash = (name) =>
|
|
140
|
-
new SchemaDefinitionError(
|
|
141
|
-
'
|
|
142
|
-
|
|
143
|
-
);
|
|
142
|
+
new SchemaDefinitionError(`Two types named "${name}" cannot be represented in one snapshot`, {
|
|
143
|
+
code: 'ALX_ERR_UNREPRESENTABLE',
|
|
144
|
+
});
|
|
144
145
|
|
|
145
146
|
// The types the entities use, by name, and the custom types their aliases
|
|
146
147
|
// name down the chain (a model is restored from them): each from the table
|
|
@@ -172,7 +173,9 @@ const typesOf = (entities, tableOf) => {
|
|
|
172
173
|
}
|
|
173
174
|
}
|
|
174
175
|
const out = {};
|
|
175
|
-
|
|
176
|
+
const names = Array.from(found.keys());
|
|
177
|
+
names.sort();
|
|
178
|
+
for (const name of names) {
|
|
176
179
|
const entry = typeOf(found.get(name));
|
|
177
180
|
if (entry !== null) out[name] = entry;
|
|
178
181
|
}
|
|
@@ -200,10 +203,9 @@ const snapshotOf = (model) => {
|
|
|
200
203
|
};
|
|
201
204
|
|
|
202
205
|
const clash = (name) =>
|
|
203
|
-
new SchemaDefinitionError(
|
|
204
|
-
'
|
|
205
|
-
|
|
206
|
-
);
|
|
206
|
+
new SchemaDefinitionError(`Two schemas named "${name}" cannot be represented in one snapshot`, {
|
|
207
|
+
code: 'ALX_ERR_UNREPRESENTABLE',
|
|
208
|
+
});
|
|
207
209
|
|
|
208
210
|
// The order of the entities of a model and of a snapshot: depth first from
|
|
209
211
|
// each start in turn, every entity after the ones its edges reach
|
|
@@ -283,4 +285,4 @@ const schemaSnapshotOf = (schema) => {
|
|
|
283
285
|
};
|
|
284
286
|
};
|
|
285
287
|
|
|
286
|
-
module.exports = { FORMAT,
|
|
288
|
+
module.exports = { FORMAT, usesOf, snapshotOf, orderOf, schemaSnapshotOf };
|
package/src/stream/index.d.ts
CHANGED
|
@@ -11,48 +11,58 @@ import type {
|
|
|
11
11
|
ValidationResult,
|
|
12
12
|
} from '../../index.js';
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
/**
|
|
15
|
+
* `check` on every record of an iterable, as a generator of
|
|
16
|
+
* `{ index, record, result }` in the order of the records; the options are
|
|
17
|
+
* those of `check`, plus `maxInvalid`, the number of invalid records after
|
|
18
|
+
* which the iteration ends. A target that is not a Schema, or a source that
|
|
19
|
+
* is not iterable, is a `SchemaUsageError` with the code `ALX_ERR_INVALID_TARGET`.
|
|
20
|
+
*/
|
|
19
21
|
export function checkEach<T>(
|
|
20
22
|
schema: Schema,
|
|
21
23
|
records: Iterable<T>,
|
|
22
24
|
options?: EachOptions,
|
|
23
25
|
): Generator<RecordEntry<T, ValidationResult>, void, undefined>;
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
/**
|
|
28
|
+
* `parse` on every record of an iterable: the same, with the result of
|
|
29
|
+
* `parse` in each entry.
|
|
30
|
+
*/
|
|
27
31
|
export function parseEach<T, D extends string | object>(
|
|
28
32
|
schema: Schema<D>,
|
|
29
33
|
records: Iterable<T>,
|
|
30
34
|
options?: ParseEachOptions,
|
|
31
35
|
): Generator<RecordEntry<T, ParseResult<InferOutput<D>>>, void, undefined>;
|
|
32
36
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
/**
|
|
38
|
+
* `checkAsync` on every record of a sync or async iterable, as an async
|
|
39
|
+
* generator in the order of the records, with up to `concurrency` records
|
|
40
|
+
* in flight (one by default).
|
|
41
|
+
*/
|
|
36
42
|
export function checkEachAsync<T>(
|
|
37
43
|
schema: Schema,
|
|
38
44
|
records: Iterable<T> | AsyncIterable<T>,
|
|
39
45
|
options?: EachOptions & EachAsyncOptions,
|
|
40
46
|
): AsyncGenerator<RecordEntry<T, ValidationResult>, void, undefined>;
|
|
41
47
|
|
|
42
|
-
|
|
43
|
-
|
|
48
|
+
/**
|
|
49
|
+
* `parseAsync` on every record of a sync or async iterable: the same, with
|
|
50
|
+
* the result of `parseAsync` in each entry.
|
|
51
|
+
*/
|
|
44
52
|
export function parseEachAsync<T, D extends string | object>(
|
|
45
53
|
schema: Schema<D>,
|
|
46
54
|
records: Iterable<T> | AsyncIterable<T>,
|
|
47
55
|
options?: ParseEachOptions & EachAsyncOptions,
|
|
48
56
|
): AsyncGenerator<RecordEntry<T, ParseResult<InferOutput<D>>>, void, undefined>;
|
|
49
57
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
58
|
+
/**
|
|
59
|
+
* A TransformStream over the records of a stream: a valid record goes on as
|
|
60
|
+
* its parsed value (`mode: 'parse'`, the default) or as it is
|
|
61
|
+
* (`mode: 'check'`); an invalid one is handed to `onInvalid` and dropped, or
|
|
62
|
+
* ends the stream with a `SchemaUsageError` of the code `ALX_ERR_INVALID_RECORD` (with
|
|
63
|
+
* `index`, `record` and `result` on it) when there is no `onInvalid` or
|
|
64
|
+
* `maxInvalid` is reached. `async: true` runs the async methods.
|
|
65
|
+
*/
|
|
56
66
|
export function transform<D extends string | object>(
|
|
57
67
|
schema: Schema<D>,
|
|
58
68
|
options: TransformOptions & { mode: 'check' },
|
|
@@ -62,7 +72,9 @@ export function transform<D extends string | object>(
|
|
|
62
72
|
options?: TransformOptions,
|
|
63
73
|
): TransformStream<unknown, InferOutput<D>>;
|
|
64
74
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
75
|
+
/**
|
|
76
|
+
* Installs `schema.checkEach`, `schema.parseEach`, `schema.checkEachAsync`,
|
|
77
|
+
* `schema.parseEachAsync` and `schema.transform`. Once per process; later
|
|
78
|
+
* calls do nothing.
|
|
79
|
+
*/
|
|
68
80
|
export function register(): void;
|
package/src/stream/index.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
// installs the methods on Schema for the programs that want them.
|
|
10
10
|
const { hasBrand } = require('../util.js');
|
|
11
11
|
const { abandon } = require('../issues.js');
|
|
12
|
-
const { invalidTarget, invalidOption: invalid } = require('../errors.js');
|
|
12
|
+
const { SchemaUsageError, invalidTarget, invalidOption: invalid } = require('../errors.js');
|
|
13
13
|
const { Schema } = require('../validation.js');
|
|
14
14
|
|
|
15
15
|
const optionsOf = (options) => {
|
|
@@ -60,13 +60,10 @@ const schemaOf = (schema) => {
|
|
|
60
60
|
// A sequence of records: anything with an iterator, or an async one where
|
|
61
61
|
// the iteration is async. A string is iterable and surely a mistake.
|
|
62
62
|
const recordsOf = (records, async) => {
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
(typeof records[Symbol.iterator] === 'function' ||
|
|
68
|
-
(async && typeof records[Symbol.asyncIterator] === 'function'));
|
|
69
|
-
if (!iterable) throw invalidTarget('An iterable of records is expected');
|
|
63
|
+
const ruledOut = records === undefined || records === null || typeof records === 'string';
|
|
64
|
+
const sync = !ruledOut && typeof records[Symbol.iterator] === 'function';
|
|
65
|
+
const streamed = !ruledOut && async && typeof records[Symbol.asyncIterator] === 'function';
|
|
66
|
+
if (!sync && !streamed) throw invalidTarget('An iterable of records is expected');
|
|
70
67
|
return records;
|
|
71
68
|
};
|
|
72
69
|
|
|
@@ -76,6 +73,11 @@ const PARSE = (schema, record, options) => schema.parse(record, options);
|
|
|
76
73
|
const CHECK_ASYNC = (schema, record, options) => schema.checkAsync(record, options);
|
|
77
74
|
const PARSE_ASYNC = (schema, record, options) => schema.parseAsync(record, options);
|
|
78
75
|
|
|
76
|
+
const callOf = (async, parse) => {
|
|
77
|
+
if (async) return parse ? PARSE_ASYNC : CHECK_ASYNC;
|
|
78
|
+
return parse ? PARSE : CHECK;
|
|
79
|
+
};
|
|
80
|
+
|
|
79
81
|
// One record after the other. The entry of a record carries its position,
|
|
80
82
|
// since an iterator has none, and the record itself, since a stream cannot
|
|
81
83
|
// be read again. The options are handed to check or parse as given (they
|
|
@@ -92,6 +94,28 @@ const each = function* (schema, records, options, limit, call) {
|
|
|
92
94
|
}
|
|
93
95
|
};
|
|
94
96
|
|
|
97
|
+
// A source the async pair reads with for await. An async iterable is read as
|
|
98
|
+
// it is; a sync one through an async iterator of its own steps, which awaits
|
|
99
|
+
// an element that is a Promise (the documented contract) and nothing else:
|
|
100
|
+
// for await over a sync iterable adopts every thenable, and a record with a
|
|
101
|
+
// `then` of its own would come out as what that `then` resolves to, where a
|
|
102
|
+
// value of the program is data (as for the passes of parse).
|
|
103
|
+
const stepsOf = (records) => {
|
|
104
|
+
if (typeof records[Symbol.asyncIterator] === 'function') return records;
|
|
105
|
+
const iterator = records[Symbol.iterator]();
|
|
106
|
+
const next = () => {
|
|
107
|
+
const step = iterator.next();
|
|
108
|
+
if (step.done || !(step.value instanceof Promise)) return Promise.resolve(step);
|
|
109
|
+
return step.value.then((value) => ({ value, done: false }));
|
|
110
|
+
};
|
|
111
|
+
const close = (value) => {
|
|
112
|
+
const step = typeof iterator.return === 'function' ? iterator.return(value) : { done: true };
|
|
113
|
+
return Promise.resolve(step);
|
|
114
|
+
};
|
|
115
|
+
const steps = { next, return: close };
|
|
116
|
+
return { [Symbol.asyncIterator]: () => steps };
|
|
117
|
+
};
|
|
118
|
+
|
|
95
119
|
// The async pair over a sync or async iterable: up to `width` records are
|
|
96
120
|
// checked at once, the entries come out in the order of the records, and the
|
|
97
121
|
// promises still in flight when the iteration ends early (`maxInvalid`, a
|
|
@@ -102,7 +126,7 @@ const eachAsync = async function* (schema, records, options, limit, width, call)
|
|
|
102
126
|
let failed = 0;
|
|
103
127
|
let index = 0;
|
|
104
128
|
try {
|
|
105
|
-
for await (const record of records) {
|
|
129
|
+
for await (const record of stepsOf(records)) {
|
|
106
130
|
const result = call(schema, record, options);
|
|
107
131
|
// Observed from the start: a record that rejects while an earlier one
|
|
108
132
|
// is still awaited would reject unhandled before its turn (the await
|
|
@@ -160,12 +184,8 @@ const parseEachAsync = (schema, records, options) => {
|
|
|
160
184
|
// The error a stream ends with: the record, its position and its result
|
|
161
185
|
// travel on it, since the stream carries them no further.
|
|
162
186
|
const invalidRecord = (index, record, result) => {
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
error.index = index;
|
|
166
|
-
error.record = record;
|
|
167
|
-
error.result = result;
|
|
168
|
-
return error;
|
|
187
|
+
const message = `Record ${index} is invalid`;
|
|
188
|
+
return new SchemaUsageError(message, { code: 'ALX_ERR_INVALID_RECORD', index, record, result });
|
|
169
189
|
};
|
|
170
190
|
|
|
171
191
|
// A TransformStream over the records of a stream: a valid record goes on as
|
|
@@ -182,16 +202,13 @@ const transform = (schema, options) => {
|
|
|
182
202
|
const async = asyncOf(options);
|
|
183
203
|
const onInvalid = callbackOf(options);
|
|
184
204
|
const parse = mode === 'parse';
|
|
185
|
-
const call = async
|
|
205
|
+
const call = callOf(async, parse);
|
|
186
206
|
let failed = 0;
|
|
187
207
|
let index = 0;
|
|
188
208
|
const settle = (record, result, controller) => {
|
|
189
209
|
const at = index;
|
|
190
210
|
index += 1;
|
|
191
|
-
if (result.valid)
|
|
192
|
-
controller.enqueue(parse ? result.value : record);
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
211
|
+
if (result.valid) return void controller.enqueue(parse ? result.value : record);
|
|
195
212
|
failed += 1;
|
|
196
213
|
if (onInvalid !== undefined) onInvalid(record, result, at);
|
|
197
214
|
if (onInvalid === undefined || failed >= limit) {
|
package/src/struct.js
CHANGED
|
@@ -14,10 +14,10 @@ const STRUCT = Symbol.for('alexify.metaschema.struct');
|
|
|
14
14
|
// fields, where a calculated field lives, but a value has no key by that name.
|
|
15
15
|
const CODEC_KEYS = new Set(['validate', 'parse', 'serialize']);
|
|
16
16
|
|
|
17
|
-
const invalidAliases = (reason) => new SchemaDefinitionError('ERR_INVALID_DEFINITION', reason);
|
|
18
|
-
|
|
19
17
|
const ALIASES = 'Option "aliases" needs a list of key names';
|
|
20
18
|
|
|
19
|
+
const invalidAliases = (reason) => new SchemaDefinitionError(reason);
|
|
20
|
+
|
|
21
21
|
// A key a value may carry: a field, a calculated field or an alias, each
|
|
22
22
|
// once.
|
|
23
23
|
const know = (known, key) => {
|
|
@@ -42,7 +42,7 @@ const aliasesOf = (field, known) => {
|
|
|
42
42
|
// may be named `check`, `name` or `constructor`, and an input key such as
|
|
43
43
|
// `__proto__` or `toString` must be reported as unexpected instead of silently
|
|
44
44
|
// resolving to something on Object.prototype.
|
|
45
|
-
const createStruct = (defs,
|
|
45
|
+
const createStruct = (defs, preprocessor) => {
|
|
46
46
|
const fields = Object.create(null);
|
|
47
47
|
const plan = [];
|
|
48
48
|
const known = Object.create(null);
|
|
@@ -51,7 +51,7 @@ const createStruct = (defs, prep) => {
|
|
|
51
51
|
const entry = defs[key];
|
|
52
52
|
const { field, required } = formatters.key(key);
|
|
53
53
|
try {
|
|
54
|
-
const { Type, defs: typeDefs } =
|
|
54
|
+
const { Type, defs: typeDefs } = preprocessor.parse(entry);
|
|
55
55
|
if (!Type) {
|
|
56
56
|
fields[field] = entry;
|
|
57
57
|
if (!CODEC_KEYS.has(field)) {
|
|
@@ -64,7 +64,7 @@ const createStruct = (defs, prep) => {
|
|
|
64
64
|
// both decide whether the field is required, and the field's check is
|
|
65
65
|
// compiled with the answer, so it is settled before the field is built.
|
|
66
66
|
typeDefs.required = (typeDefs.required ?? true) && required;
|
|
67
|
-
const child = new Type(typeDefs,
|
|
67
|
+
const child = new Type(typeDefs, preprocessor);
|
|
68
68
|
fields[field] = child;
|
|
69
69
|
know(known, field);
|
|
70
70
|
plan.push({
|
|
@@ -75,7 +75,7 @@ const createStruct = (defs, prep) => {
|
|
|
75
75
|
aliases: aliasesOf(child, known),
|
|
76
76
|
});
|
|
77
77
|
} catch (error) {
|
|
78
|
-
if (error instanceof SchemaDefinitionError) error.locate(
|
|
78
|
+
if (error instanceof SchemaDefinitionError) error.locate(preprocessor.root.name, field);
|
|
79
79
|
throw error;
|
|
80
80
|
}
|
|
81
81
|
}
|
package/src/types.js
CHANGED
|
@@ -27,8 +27,11 @@ const PROTOTYPES = {
|
|
|
27
27
|
// that a class made from one inherits from it (below), and what the
|
|
28
28
|
// validation half of the core installs on them (their `compile`) reaches
|
|
29
29
|
// every class made from them, in every registry, whenever it is installed.
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
// `one` and `many` meet the object of `reference` again: setting a prototype an
|
|
31
|
+
// object already has changes nothing.
|
|
32
|
+
const builtinNames = Object.keys(PROTOTYPES);
|
|
33
|
+
for (let index = 0; index < builtinNames.length; index += 1) {
|
|
34
|
+
Object.setPrototypeOf(PROTOTYPES[builtinNames[index]], AbstractType.prototype);
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
// What the class of a type inherits from: a built-in's prototype object, the
|
|
@@ -40,7 +43,9 @@ const parentOf = (prototype) =>
|
|
|
40
43
|
: Object.assign(Object.create(AbstractType.prototype), prototype);
|
|
41
44
|
|
|
42
45
|
const customTypeError = (name, reason) =>
|
|
43
|
-
new SchemaDefinitionError(
|
|
46
|
+
new SchemaDefinitionError(`Custom type "${name}" ${reason}`, {
|
|
47
|
+
code: 'ALX_ERR_INVALID_CUSTOM_TYPE',
|
|
48
|
+
});
|
|
44
49
|
|
|
45
50
|
const createType = (name, prototype) => {
|
|
46
51
|
class Type extends AbstractType {
|
|
@@ -65,10 +70,10 @@ const createType = (name, prototype) => {
|
|
|
65
70
|
|
|
66
71
|
const createTypes = (prototypes) => {
|
|
67
72
|
const types = Object.create(null);
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
types[name] = createType(name,
|
|
73
|
+
const names = Object.keys(prototypes);
|
|
74
|
+
for (let index = 0; index < names.length; index += 1) {
|
|
75
|
+
const name = names[index];
|
|
76
|
+
types[name] = createType(name, prototypes[name]);
|
|
72
77
|
}
|
|
73
78
|
return types;
|
|
74
79
|
};
|
|
@@ -79,10 +84,13 @@ const TYPES = createTypes(PROTOTYPES);
|
|
|
79
84
|
const createRegistry = () => createTypes(PROTOTYPES);
|
|
80
85
|
|
|
81
86
|
// Whether a dictionary of fields reads as a struct under the first-key rule
|
|
82
|
-
// (`preprocessor.js`): the first key that starts with a letter
|
|
83
|
-
// when capitalized, the long form when it is `type` and a shorthand when
|
|
84
|
-
// is a type name of the registry; a struct that reads otherwise needs the
|
|
85
|
-
// `schema` shorthand around it. What generates a definition asks here
|
|
87
|
+
// (`preprocessor.js`): the first key that starts with a letter may be a kind
|
|
88
|
+
// when capitalized, is the long form when it is `type` and a shorthand when
|
|
89
|
+
// it is a type name of the registry; a struct that reads otherwise needs the
|
|
90
|
+
// `schema` shorthand around it. What generates a definition asks here, and
|
|
91
|
+
// the answer for a capitalized key is the careful one on purpose (whatever
|
|
92
|
+
// the key holds, where `isKindKey` of kinds.js looks at it): the definition
|
|
93
|
+
// then reads the same for `Infer`, which sees no key order, and for 3.4.0.
|
|
86
94
|
const readsAsStruct = (fields, types = TYPES) => {
|
|
87
95
|
const first = firstKey(fields);
|
|
88
96
|
if (first === undefined) return true;
|
|
@@ -97,15 +105,15 @@ const updateTypeMetadata = (Type, metadata) => {
|
|
|
97
105
|
|
|
98
106
|
// A custom prototype brings `construct` and `checkType` (or a `compile` of
|
|
99
107
|
// its own); an alias takes its base's, a registered type.
|
|
100
|
-
const checkCustomType = (name,
|
|
101
|
-
const { construct } =
|
|
102
|
-
const checkType =
|
|
108
|
+
const checkCustomType = (name, prototype) => {
|
|
109
|
+
const { construct } = prototype;
|
|
110
|
+
const checkType = prototype.checkType || prototype.compile;
|
|
103
111
|
if (!checkType || !construct) {
|
|
104
112
|
throw customTypeError(name, 'must contain "construct" and "checkType" methods');
|
|
105
113
|
}
|
|
106
|
-
const
|
|
107
|
-
const
|
|
108
|
-
if (!
|
|
114
|
+
const hasCheckType = typeof checkType === 'function';
|
|
115
|
+
const hasConstruct = typeof construct === 'function';
|
|
116
|
+
if (!hasCheckType || !hasConstruct) {
|
|
109
117
|
throw customTypeError(name, 'methods "construct" and "checkType" must be functions');
|
|
110
118
|
}
|
|
111
119
|
};
|
|
@@ -122,9 +130,10 @@ const redefines = (Type, { js, construct, checkType }) => {
|
|
|
122
130
|
};
|
|
123
131
|
|
|
124
132
|
const typeFactory = (customTypes, registry = TYPES) => {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
const
|
|
133
|
+
const names = Object.keys(customTypes);
|
|
134
|
+
for (let index = 0; index < names.length; index += 1) {
|
|
135
|
+
const name = names[index];
|
|
136
|
+
const value = customTypes[name];
|
|
128
137
|
if (value === null || typeof value !== 'object') {
|
|
129
138
|
throw customTypeError(name, 'must be an object with methods "construct" and "checkType"');
|
|
130
139
|
}
|
|
@@ -133,21 +142,21 @@ const typeFactory = (customTypes, registry = TYPES) => {
|
|
|
133
142
|
if (registered) {
|
|
134
143
|
if (redefines(registered, value)) {
|
|
135
144
|
const reason = `Type "${name}" is already registered; only { metadata } may be added`;
|
|
136
|
-
throw new SchemaDefinitionError('
|
|
145
|
+
throw new SchemaDefinitionError(reason, { code: 'ALX_ERR_TYPE_REGISTERED' });
|
|
137
146
|
}
|
|
138
147
|
updateTypeMetadata(registered, metadata);
|
|
139
148
|
continue;
|
|
140
149
|
}
|
|
141
|
-
let
|
|
150
|
+
let prototype = rest;
|
|
142
151
|
if (js !== undefined) {
|
|
143
152
|
const base = registry[js];
|
|
144
153
|
if (!base) {
|
|
145
154
|
const reason = `Unknown js type "${js}" for custom type "${name}"`;
|
|
146
|
-
throw new SchemaDefinitionError('
|
|
155
|
+
throw new SchemaDefinitionError(reason, { code: 'ALX_ERR_UNKNOWN_JS_TYPE' });
|
|
147
156
|
}
|
|
148
|
-
|
|
149
|
-
} else checkCustomType(name,
|
|
150
|
-
const Type = createType(name,
|
|
157
|
+
prototype = base.prototype;
|
|
158
|
+
} else checkCustomType(name, prototype);
|
|
159
|
+
const Type = createType(name, prototype);
|
|
151
160
|
Type.source = { js, construct: rest.construct, checkType: rest.checkType };
|
|
152
161
|
updateTypeMetadata(Type, metadata);
|
|
153
162
|
registry[name] = Type;
|