@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/locales/en.js
CHANGED
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
// message. `field` renders the location prefix of an error line.
|
|
4
4
|
const { shorten, safeString } = require('../util.js');
|
|
5
5
|
|
|
6
|
+
// The first keys of the list and how many more there are: a value with a
|
|
7
|
+
// million keys the schema does not have is one issue, and its message is
|
|
8
|
+
// not the million keys.
|
|
9
|
+
const SHOWN_KEYS = 10;
|
|
10
|
+
|
|
6
11
|
const field = (path) => `Field "${path}"`;
|
|
7
12
|
|
|
8
13
|
const required = () => 'is required';
|
|
@@ -10,16 +15,11 @@ const required = () => 'is required';
|
|
|
10
15
|
const type = ({ expected, key }) =>
|
|
11
16
|
key === undefined ? `not of expected type: ${expected}` : `keys must be of type ${expected}`;
|
|
12
17
|
|
|
13
|
-
// The first keys of the list and how many more there are: a value with a
|
|
14
|
-
// million keys the schema does not have is one issue, and its message is
|
|
15
|
-
// not the million keys.
|
|
16
|
-
const SHOWN = 10;
|
|
17
|
-
|
|
18
18
|
const list = (keys) => {
|
|
19
|
-
const count = Math.min(keys.length,
|
|
19
|
+
const count = Math.min(keys.length, SHOWN_KEYS);
|
|
20
20
|
let text = shorten(keys[0]);
|
|
21
21
|
for (let index = 1; index < count; index += 1) text += `, ${shorten(keys[index])}`;
|
|
22
|
-
return keys.length >
|
|
22
|
+
return keys.length > SHOWN_KEYS ? `${text} and ${keys.length - SHOWN_KEYS} more` : text;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
25
|
const unexpected = ({ keys }) => `has unexpected keys: ${list(keys)}`;
|
package/src/locales/uk.js
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
2
2
|
// check as `messages`: `schema.check(value, { messages: uk })`.
|
|
3
3
|
const { shorten, safeString } = require('../util.js');
|
|
4
4
|
|
|
5
|
+
// Перші ключі списку і скільки їх ще: значення з мільйоном невідомих ключів
|
|
6
|
+
// є одним issue, і його повідомлення не є цим мільйоном.
|
|
7
|
+
const SHOWN_KEYS = 10;
|
|
8
|
+
|
|
5
9
|
const field = (path) => `Поле "${path}"`;
|
|
6
10
|
|
|
7
11
|
const required = () => 'є обовʼязковим';
|
|
@@ -9,15 +13,11 @@ const required = () => 'є обовʼязковим';
|
|
|
9
13
|
const type = ({ expected, key }) =>
|
|
10
14
|
key === undefined ? `не відповідає типу: ${expected}` : `ключі мають бути типу ${expected}`;
|
|
11
15
|
|
|
12
|
-
// Перші ключі списку і скільки їх ще: значення з мільйоном невідомих ключів
|
|
13
|
-
// є одним issue, і його повідомлення не є цим мільйоном.
|
|
14
|
-
const SHOWN = 10;
|
|
15
|
-
|
|
16
16
|
const list = (keys) => {
|
|
17
|
-
const count = Math.min(keys.length,
|
|
17
|
+
const count = Math.min(keys.length, SHOWN_KEYS);
|
|
18
18
|
let text = shorten(keys[0]);
|
|
19
19
|
for (let index = 1; index < count; index += 1) text += `, ${shorten(keys[index])}`;
|
|
20
|
-
return keys.length >
|
|
20
|
+
return keys.length > SHOWN_KEYS ? `${text} і ще ${keys.length - SHOWN_KEYS}` : text;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
const unexpected = ({ keys }) => `має неочікувані ключі: ${list(keys)}`;
|
package/src/metadata.js
CHANGED
|
@@ -12,7 +12,7 @@ const unknownOf = (unknown) => {
|
|
|
12
12
|
if (unknown === undefined) return 'reject';
|
|
13
13
|
if (unknown !== 'reject' && unknown !== 'ignore') {
|
|
14
14
|
const reason = `Schema metadata "unknown" must be "reject" or "ignore", got ${JSON.stringify(unknown)}`;
|
|
15
|
-
throw new SchemaDefinitionError('
|
|
15
|
+
throw new SchemaDefinitionError(reason, { code: 'ALX_ERR_INVALID_OPTIONS' });
|
|
16
16
|
}
|
|
17
17
|
return unknown;
|
|
18
18
|
};
|
|
@@ -60,7 +60,7 @@ class SchemaMetadata {
|
|
|
60
60
|
const { kind, scope, store, allow, parent, unknown, ...custom } = values;
|
|
61
61
|
if (custom.coerce !== undefined && typeof custom.coerce !== 'boolean') {
|
|
62
62
|
const reason = `Schema metadata "coerce" must be a boolean, got ${JSON.stringify(custom.coerce)}`;
|
|
63
|
-
throw new SchemaDefinitionError('
|
|
63
|
+
throw new SchemaDefinitionError(reason, { code: 'ALX_ERR_INVALID_OPTIONS' });
|
|
64
64
|
}
|
|
65
65
|
this.kind = kind || this.kind;
|
|
66
66
|
this.scope = scope || this.scope;
|
package/src/model/data.js
CHANGED
|
@@ -1,36 +1,34 @@
|
|
|
1
1
|
// What the functions of the `./model` subpath share: a snapshot from a Model
|
|
2
2
|
// or as given, and equality of the JSON values a snapshot is made of. A
|
|
3
3
|
// snapshot is input (a file, a registry, another program), so what it says
|
|
4
|
-
// is checked before it is read, and every error is
|
|
4
|
+
// is checked before it is read, and every error is ALX_ERR_INVALID_SNAPSHOT
|
|
5
5
|
// located in the entity, never a bare SyntaxError or TypeError.
|
|
6
6
|
const { hasBrand, define, isDate, isRegExp } = require('../util.js');
|
|
7
7
|
const { FORMAT } = require('../snapshot.js');
|
|
8
8
|
const { SchemaDefinitionError } = require('../errors.js');
|
|
9
9
|
const { isStruct } = require('../struct.js');
|
|
10
|
+
const { isKindKey } = require('../kinds.js');
|
|
10
11
|
|
|
11
12
|
// How deep the walks over a snapshot go: a definition nested deeper is not
|
|
12
13
|
// one a program wrote.
|
|
13
|
-
const
|
|
14
|
+
const MAX_DEPTH = 128;
|
|
14
15
|
|
|
15
|
-
const invalidSnapshot = (reason) =>
|
|
16
|
+
const invalidSnapshot = (reason, options) =>
|
|
17
|
+
new SchemaDefinitionError(reason, { ...options, code: 'ALX_ERR_INVALID_SNAPSHOT' });
|
|
16
18
|
|
|
17
|
-
const tooDeep = () => invalidSnapshot(`A snapshot nested deeper than ${
|
|
19
|
+
const tooDeep = () => invalidSnapshot(`A snapshot nested deeper than ${MAX_DEPTH} levels`);
|
|
18
20
|
|
|
19
|
-
// An error of
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
error.message = `${error.message} in "${name}"`;
|
|
24
|
-
return error;
|
|
25
|
-
};
|
|
21
|
+
// An error of another kind met while a snapshot is read (a bigint or a
|
|
22
|
+
// pattern that does not read throws its own): an error of the snapshot,
|
|
23
|
+
// with that error as its cause.
|
|
24
|
+
const unreadable = (error) => invalidSnapshot(error.message, { cause: error });
|
|
26
25
|
|
|
27
26
|
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
28
27
|
|
|
29
|
-
const isSnapshot = (value) =>
|
|
30
|
-
isObject(value)
|
|
31
|
-
value.
|
|
32
|
-
|
|
33
|
-
(value.types === undefined || isObject(value.types));
|
|
28
|
+
const isSnapshot = (value) => {
|
|
29
|
+
if (!isObject(value) || value.format !== FORMAT || !isObject(value.entities)) return false;
|
|
30
|
+
return value.types === undefined || isObject(value.types);
|
|
31
|
+
};
|
|
34
32
|
|
|
35
33
|
// The entity a snapshot of one schema is about, when it names one: a key
|
|
36
34
|
// format 1 gained in 3.4, read by nothing else.
|
|
@@ -60,7 +58,7 @@ const readSnapshot = (target) => {
|
|
|
60
58
|
const same = (a, b, depth = 0) => {
|
|
61
59
|
if (a === b) return true;
|
|
62
60
|
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false;
|
|
63
|
-
if (depth >
|
|
61
|
+
if (depth > MAX_DEPTH) throw tooDeep();
|
|
64
62
|
// By their shape, which a value of JSON cannot fake: `{ constructor: {
|
|
65
63
|
// name: 'Date' } }` of a snapshot is data.
|
|
66
64
|
const regExp = isRegExp(a);
|
|
@@ -88,6 +86,16 @@ const isFunctionMarker = (value) => isObject(value) && value.$function === true;
|
|
|
88
86
|
|
|
89
87
|
const capitalize = (name) => name.charAt(0).toUpperCase() + name.slice(1);
|
|
90
88
|
|
|
89
|
+
// The key a kind is written under as the first key of a definition (`entity`
|
|
90
|
+
// is `Entity`), null for a name that key would not be read back as a kind
|
|
91
|
+
// from (`dTO`, `api_config`: `isKindKey` of kinds.js): the fields after it
|
|
92
|
+
// would read as something else.
|
|
93
|
+
const kindKeyOf = (kind) => {
|
|
94
|
+
if (typeof kind !== 'string' || kind === '') return null;
|
|
95
|
+
const key = capitalize(kind);
|
|
96
|
+
return isKindKey(key, {}) ? key : null;
|
|
97
|
+
};
|
|
98
|
+
|
|
91
99
|
// The definition a built field was made from, with its nested fields as
|
|
92
100
|
// definitions and its functions kept: what `merge` unifies and `Schema`
|
|
93
101
|
// takes back.
|
|
@@ -128,7 +136,12 @@ const metadataOf = (schema) => {
|
|
|
128
136
|
// its first key, so the fields after it read as a struct whatever their names.
|
|
129
137
|
const structDefinition = (base, metadata, fields) => {
|
|
130
138
|
const { kind = base.kind, ...rest } = { ...base, ...metadata };
|
|
131
|
-
const
|
|
139
|
+
const kindKey = kindKeyOf(kind);
|
|
140
|
+
if (kindKey === null) {
|
|
141
|
+
const reason = `Metadata "kind" must be the name of a kind, got ${JSON.stringify(kind)}`;
|
|
142
|
+
throw new SchemaDefinitionError(reason, { code: 'ALX_ERR_INVALID_OPTIONS' });
|
|
143
|
+
}
|
|
144
|
+
const definition = { [kindKey]: rest };
|
|
132
145
|
for (const key of Object.keys(fields)) define(definition, key, fields[key]);
|
|
133
146
|
return definition;
|
|
134
147
|
};
|
|
@@ -160,10 +173,10 @@ const namespacesOf = (schemas) => {
|
|
|
160
173
|
};
|
|
161
174
|
|
|
162
175
|
module.exports = {
|
|
163
|
-
|
|
176
|
+
MAX_DEPTH,
|
|
164
177
|
invalidSnapshot,
|
|
165
178
|
tooDeep,
|
|
166
|
-
|
|
179
|
+
unreadable,
|
|
167
180
|
isObject,
|
|
168
181
|
isSnapshot,
|
|
169
182
|
checkRoot,
|
|
@@ -171,6 +184,7 @@ module.exports = {
|
|
|
171
184
|
same,
|
|
172
185
|
isFunctionMarker,
|
|
173
186
|
capitalize,
|
|
187
|
+
kindKeyOf,
|
|
174
188
|
definitionOf,
|
|
175
189
|
structDefinitionOf,
|
|
176
190
|
metadataOf,
|
package/src/model/diff.js
CHANGED
|
@@ -13,13 +13,28 @@
|
|
|
13
13
|
const { SchemaDefinitionError, invalidTarget } = require('../errors.js');
|
|
14
14
|
const {
|
|
15
15
|
invalidSnapshot,
|
|
16
|
-
|
|
16
|
+
unreadable,
|
|
17
17
|
isObject,
|
|
18
18
|
readSnapshot,
|
|
19
19
|
same,
|
|
20
20
|
isFunctionMarker,
|
|
21
21
|
} = require('./data.js');
|
|
22
22
|
|
|
23
|
+
const META = ['kind', 'scope', 'store', 'allow', 'parent', 'unknown', 'custom'];
|
|
24
|
+
|
|
25
|
+
// The keys of an entity whose change leaves no reader compatible.
|
|
26
|
+
const INCOMPATIBLE = new Set(['kind', 'store', 'parent', 'type']);
|
|
27
|
+
|
|
28
|
+
// The classes as bits, so that the class of several changes is the AND of
|
|
29
|
+
// theirs: backward and forward make full, either with its opposite makes
|
|
30
|
+
// none.
|
|
31
|
+
const NONE = 0;
|
|
32
|
+
const BACKWARD = 1;
|
|
33
|
+
const FORWARD = 2;
|
|
34
|
+
const FULL = 3;
|
|
35
|
+
|
|
36
|
+
const NAMES = ['none', 'backward', 'forward', 'full'];
|
|
37
|
+
|
|
23
38
|
const change = (kind, op, entity, name, from, to, detail) => {
|
|
24
39
|
const entry = { kind, op, entity, name };
|
|
25
40
|
if (from !== undefined) entry.from = from;
|
|
@@ -34,7 +49,9 @@ const own = (object, key) => (Object.hasOwn(object, key) ? object[key] : undefin
|
|
|
34
49
|
const keysOf = (a, b) => {
|
|
35
50
|
const keys = new Set(Object.keys(a));
|
|
36
51
|
for (const key of Object.keys(b)) keys.add(key);
|
|
37
|
-
|
|
52
|
+
const sorted = Array.from(keys);
|
|
53
|
+
sorted.sort();
|
|
54
|
+
return sorted;
|
|
38
55
|
};
|
|
39
56
|
|
|
40
57
|
// The keys whose values differ between two objects, sorted.
|
|
@@ -136,8 +153,6 @@ const diffRelations = (entity, old, current, changes) => {
|
|
|
136
153
|
}
|
|
137
154
|
};
|
|
138
155
|
|
|
139
|
-
const META = ['kind', 'scope', 'store', 'allow', 'parent', 'unknown', 'custom'];
|
|
140
|
-
|
|
141
156
|
// The metadata of an entity; a schema of one type is its type, so a struct
|
|
142
157
|
// that became one (or back) differs in `type`.
|
|
143
158
|
const metaOf = (entity) => {
|
|
@@ -157,7 +172,7 @@ const isRelations = (list) => {
|
|
|
157
172
|
// object: a definition or a function marker) or a `type`, and the relations
|
|
158
173
|
// as a list of objects, when there.
|
|
159
174
|
const checkEntity = (entity, name) => {
|
|
160
|
-
const fail = (reason) =>
|
|
175
|
+
const fail = (reason) => invalidSnapshot(reason).locate(name, '');
|
|
161
176
|
if (!isObject(entity)) throw fail('An entity of a snapshot must be an object');
|
|
162
177
|
const { fields, type, relations = [] } = entity;
|
|
163
178
|
if (!isObject(fields === undefined ? type : fields) || !isRelations(relations)) {
|
|
@@ -205,16 +220,6 @@ const diff = (prev, next) => {
|
|
|
205
220
|
return changes;
|
|
206
221
|
};
|
|
207
222
|
|
|
208
|
-
// The classes as bits, so that the class of several changes is the AND of
|
|
209
|
-
// theirs: backward and forward make full, either with its opposite makes
|
|
210
|
-
// none.
|
|
211
|
-
const NONE = 0;
|
|
212
|
-
const BACKWARD = 1;
|
|
213
|
-
const FORWARD = 2;
|
|
214
|
-
const FULL = 3;
|
|
215
|
-
|
|
216
|
-
const NAMES = ['none', 'backward', 'forward', 'full'];
|
|
217
|
-
|
|
218
223
|
// A field a reader may do without: an optional one, or one with a default
|
|
219
224
|
// the reader fills in.
|
|
220
225
|
const optional = (field) => field.required === false || field.default !== undefined;
|
|
@@ -330,7 +335,8 @@ const keyLevel = (key, from, to, field) => {
|
|
|
330
335
|
return boundLevel(from, to, false);
|
|
331
336
|
case 'pattern':
|
|
332
337
|
case 'format':
|
|
333
|
-
|
|
338
|
+
if (from === undefined) return FORWARD;
|
|
339
|
+
return to === undefined ? BACKWARD : NONE;
|
|
334
340
|
case 'enum':
|
|
335
341
|
case 'flags':
|
|
336
342
|
return Array.isArray(from) && Array.isArray(to) ? enumLevel(from, to) : NONE;
|
|
@@ -384,7 +390,7 @@ const entityLevel = (entry) => {
|
|
|
384
390
|
if (op === 'removed') return BACKWARD;
|
|
385
391
|
let level = FULL;
|
|
386
392
|
for (const key of detail.keys) {
|
|
387
|
-
if (key
|
|
393
|
+
if (INCOMPATIBLE.has(key)) level &= NONE;
|
|
388
394
|
else if (key === 'unknown') level &= to.unknown === 'ignore' ? BACKWARD : FORWARD;
|
|
389
395
|
}
|
|
390
396
|
return level;
|
|
@@ -409,11 +415,10 @@ const levelOf = (entry) => {
|
|
|
409
415
|
|
|
410
416
|
// What a change carries, as `diff` writes it: a kind and an op, and the
|
|
411
417
|
// keys that differ for `changed`.
|
|
412
|
-
const isChange = (entry) =>
|
|
413
|
-
isObject(entry) &&
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
(entry.op !== 'changed' || Array.isArray(entry.detail?.keys));
|
|
418
|
+
const isChange = (entry) => {
|
|
419
|
+
const named = isObject(entry) && typeof entry.kind === 'string' && typeof entry.op === 'string';
|
|
420
|
+
return named && (entry.op !== 'changed' || Array.isArray(entry.detail?.keys));
|
|
421
|
+
};
|
|
417
422
|
|
|
418
423
|
// The class of a change as bits. Whatever fails while its definitions are
|
|
419
424
|
// read (a bigint that does not read, a nesting the stack cannot take) is an
|
|
@@ -423,7 +428,7 @@ const classOf = (entry) => {
|
|
|
423
428
|
try {
|
|
424
429
|
return levelOf(entry);
|
|
425
430
|
} catch (error) {
|
|
426
|
-
const located = error instanceof SchemaDefinitionError ? error :
|
|
431
|
+
const located = error instanceof SchemaDefinitionError ? error : unreadable(error);
|
|
427
432
|
throw located.locate(entry.entity, entry.name);
|
|
428
433
|
}
|
|
429
434
|
};
|
|
@@ -439,4 +444,4 @@ const compatibility = (changes) => {
|
|
|
439
444
|
return NAMES[level];
|
|
440
445
|
};
|
|
441
446
|
|
|
442
|
-
module.exports = { diff,
|
|
447
|
+
module.exports = { diff, compatibilityOf, compatibility };
|
package/src/model/index.d.ts
CHANGED
|
@@ -12,57 +12,73 @@ import type {
|
|
|
12
12
|
SchemaChange,
|
|
13
13
|
} from '../../index.js';
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
/**
|
|
16
|
+
* The model of a snapshot (`model.toJSON()`), with placeholders for the
|
|
17
|
+
* functions, listed by path in `placeholders`; `options.types` brings the
|
|
18
|
+
* real custom types, the registry is 'isolated' unless `options.registry`
|
|
19
|
+
* says otherwise (a shared one takes no placeholder type). A snapshot that
|
|
20
|
+
* does not read is `ALX_ERR_INVALID_SNAPSHOT`, located in its entity.
|
|
21
|
+
*/
|
|
20
22
|
export function fromJSON(snapshot: ModelSnapshot, options?: RestoreOptions): RestoredModel;
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
/**
|
|
25
|
+
* The changes from `prev` to `next`, each a Model or a snapshot: the
|
|
26
|
+
* entities of `next` in their order (added, or their changes: fields,
|
|
27
|
+
* indexes, relations), the entities that are gone, then the types.
|
|
28
|
+
*/
|
|
25
29
|
export function diff(prev: Model | ModelSnapshot, next: Model | ModelSnapshot): SchemaChange[];
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
/**
|
|
32
|
+
* The compatibility class of a list of changes by the rules of the Confluent
|
|
33
|
+
* Schema Registry: 'backward' when a reader with the new model reads data
|
|
34
|
+
* written with the old one, 'forward' the other way, 'full' when both,
|
|
35
|
+
* 'none' when neither.
|
|
36
|
+
*/
|
|
31
37
|
export function compatibility(changes: SchemaChange[]): Compatibility;
|
|
32
38
|
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
/**
|
|
40
|
+
* The compatibility class of one change, by the same rules: what to filter
|
|
41
|
+
* a list of changes by.
|
|
42
|
+
*/
|
|
35
43
|
export function compatibilityOf(change: SchemaChange): Compatibility;
|
|
36
44
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
/**
|
|
46
|
+
* A value the schema accepts, the same for a seed: every field is drawn
|
|
47
|
+
* from its definition and verified with its own check. A definition no
|
|
48
|
+
* value satisfies is `ALX_ERR_UNSATISFIABLE`, a pattern the sampler cannot read
|
|
49
|
+
* `ALX_ERR_SAMPLE_PATTERN`, a custom type without `metadata.sample`
|
|
50
|
+
* `ALX_ERR_SAMPLE_TYPE`.
|
|
51
|
+
*/
|
|
42
52
|
export function sample<D extends string | object>(
|
|
43
53
|
schema: Schema<D>,
|
|
44
54
|
options?: SampleOptions,
|
|
45
55
|
): Infer<D>;
|
|
46
56
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
57
|
+
/**
|
|
58
|
+
* The schema both schemas describe, by unification: a field in both takes
|
|
59
|
+
* the constraints of both, a field in one comes as it is, and what no value
|
|
60
|
+
* can satisfy (two types, two patterns, bounds with nothing in common, two
|
|
61
|
+
* defaults) is `ALX_ERR_MERGE_CONFLICT` with the field. The kind, its metadata
|
|
62
|
+
* and the name are the first schema's unless the options say otherwise.
|
|
63
|
+
*/
|
|
52
64
|
export function merge(a: Schema, b: Schema, options?: MergeOptions): Schema;
|
|
53
65
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
66
|
+
/**
|
|
67
|
+
* A definition (or a Schema, with `as: 'schema'`) that accepts every sample:
|
|
68
|
+
* the kinds seen at every place, the records of a place fused into one
|
|
69
|
+
* struct with optional keys, a string field of few values as an enum,
|
|
70
|
+
* records of many sparse keys as a dictionary, whole numbers as `integer`.
|
|
71
|
+
* The samples are input: the walk is bounded (`ALX_ERR_INFER_DEPTH`), and the
|
|
72
|
+
* definition is checked against every sample before it is returned
|
|
73
|
+
* (`ALX_ERR_INFER_MISMATCH` is a bug to report). An argument that is not a
|
|
74
|
+
* non-empty iterable is a `SchemaUsageError` with the code `ALX_ERR_INVALID_TARGET`.
|
|
75
|
+
*/
|
|
62
76
|
export function infer(samples: Iterable<unknown>, options: InferOptions & { as: 'schema' }): Schema;
|
|
63
77
|
export function infer(samples: Iterable<unknown>, options?: InferOptions): object | string;
|
|
64
78
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
79
|
+
/**
|
|
80
|
+
* Installs `Model.fromJSON`, `Model.diff`, `Model.compatibility`,
|
|
81
|
+
* `Model.compatibilityOf`, `Schema.merge`, `Schema.infer` and
|
|
82
|
+
* `schema.sample`. Once per process; later calls do nothing.
|
|
83
|
+
*/
|
|
68
84
|
export function register(): void;
|