@alexify/metaschema 2.0.0 → 3.1.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 +443 -1
- package/README.md +124 -12
- package/SECURITY.md +27 -8
- package/index.d.ts +537 -84
- package/package.json +25 -6
- package/src/codec.js +544 -0
- package/src/compile/code.js +92 -0
- package/src/compile/emit.js +540 -0
- package/src/compile/index.d.ts +38 -0
- package/src/compile/index.js +176 -0
- package/src/context.js +68 -19
- package/src/errors.js +19 -1
- package/src/generators/fromjsonschema.js +520 -0
- package/src/generators/index.d.ts +46 -0
- package/src/generators/index.js +95 -0
- package/src/generators/jsonschema.js +535 -0
- package/src/generators/typescript.js +237 -0
- package/src/index.js +0 -2
- package/src/issues.js +117 -22
- package/src/kinds.js +9 -12
- package/src/lint.js +31 -14
- package/src/locales/en.js +10 -2
- package/src/locales/uk.js +7 -2
- package/src/metadata.js +41 -44
- package/src/metautil.js +8 -1
- package/src/model/data.js +122 -0
- package/src/model/diff.js +438 -0
- package/src/model/index.d.ts +56 -0
- package/src/model/index.js +42 -0
- package/src/model/merge.js +254 -0
- package/src/model/pattern.js +322 -0
- package/src/model/restore.js +226 -0
- package/src/model/sample.js +394 -0
- package/src/model.js +18 -15
- package/src/preprocessor.js +9 -11
- package/src/prototypes/abstract.js +14 -1
- package/src/prototypes/collections.js +20 -3
- package/src/prototypes/reference.js +17 -3
- package/src/prototypes/scalars.js +1 -3
- package/src/prototypes/schema.js +15 -2
- package/src/prototypes/tuple.js +14 -9
- package/src/prototypes/union.js +57 -18
- package/src/result.js +5 -14
- package/src/rules.js +55 -26
- package/src/runtime/browser.js +2 -2
- package/src/runtime/node.js +4 -2
- package/src/schema.js +129 -101
- package/src/snapshot.js +111 -0
- package/src/struct.js +80 -23
- package/src/types.js +5 -11
- package/src/util.js +39 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
// The changes between two versions of a model (`diff`), as data, and the
|
|
2
|
+
// compatibility class of a list of changes (`compatibility`) by the rules of
|
|
3
|
+
// the Confluent Schema Registry: backward when a reader with the new model
|
|
4
|
+
// reads data written with the old one, forward when a reader with the old
|
|
5
|
+
// model reads data written with the new one, full when both, none when
|
|
6
|
+
// neither. Two models compare through their snapshots, so a committed
|
|
7
|
+
// snapshot compares with a model, and the rules assume a reader that ignores
|
|
8
|
+
// the keys it does not know (`check` with `unknown: 'ignore'`, `parse` with
|
|
9
|
+
// `strip`), as those registries do. A snapshot and a list of changes are
|
|
10
|
+
// input: the keys read are own keys, an entity is checked before it is
|
|
11
|
+
// read, and whatever fails while a change is classified is an error of the
|
|
12
|
+
// snapshot located at the change.
|
|
13
|
+
const { SchemaDefinitionError, invalidTarget } = require('../errors.js');
|
|
14
|
+
const {
|
|
15
|
+
invalidSnapshot,
|
|
16
|
+
atSchema,
|
|
17
|
+
isObject,
|
|
18
|
+
snapshotOf,
|
|
19
|
+
same,
|
|
20
|
+
isFunctionMarker,
|
|
21
|
+
} = require('./data.js');
|
|
22
|
+
|
|
23
|
+
const change = (kind, op, entity, name, from, to, detail) => {
|
|
24
|
+
const entry = { kind, op, entity, name };
|
|
25
|
+
if (from !== undefined) entry.from = from;
|
|
26
|
+
if (to !== undefined) entry.to = to;
|
|
27
|
+
if (detail !== undefined) entry.detail = detail;
|
|
28
|
+
return entry;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const own = (object, key) => (Object.hasOwn(object, key) ? object[key] : undefined);
|
|
32
|
+
|
|
33
|
+
// The keys of either object, sorted.
|
|
34
|
+
const keysOf = (a, b) => {
|
|
35
|
+
const keys = new Set(Object.keys(a));
|
|
36
|
+
for (const key of Object.keys(b)) keys.add(key);
|
|
37
|
+
return Array.from(keys).sort();
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// The keys whose values differ between two objects, sorted.
|
|
41
|
+
const changedKeys = (a, b, keys = keysOf(a, b)) => {
|
|
42
|
+
const changed = [];
|
|
43
|
+
for (const key of keys) if (!same(own(a, key), own(b, key))) changed.push(key);
|
|
44
|
+
return changed;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// The old key a new field was renamed from: an alias of the new field that
|
|
48
|
+
// is a key of the old struct and not of the new one.
|
|
49
|
+
const renamedFrom = (field, old, current) => {
|
|
50
|
+
const { aliases } = field;
|
|
51
|
+
if (!Array.isArray(aliases)) return null;
|
|
52
|
+
for (const alias of aliases) {
|
|
53
|
+
if (Object.hasOwn(old, alias) && !Object.hasOwn(current, alias)) return alias;
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const without = (keys, name) => {
|
|
59
|
+
const rest = [];
|
|
60
|
+
for (const key of keys) if (key !== name) rest.push(key);
|
|
61
|
+
return rest;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// The fields of a struct: the new ones in their order (changed, renamed
|
|
65
|
+
// through an alias, or added), then the ones that are gone.
|
|
66
|
+
const diffFields = (entity, old, current, changes) => {
|
|
67
|
+
const renamed = new Set();
|
|
68
|
+
for (const name of Object.keys(current)) {
|
|
69
|
+
const to = current[name];
|
|
70
|
+
if (Object.hasOwn(old, name)) {
|
|
71
|
+
const from = old[name];
|
|
72
|
+
if (same(from, to)) continue;
|
|
73
|
+
changes.push(
|
|
74
|
+
change('field', 'changed', entity, name, from, to, { keys: changedKeys(from, to) }),
|
|
75
|
+
);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const previous = renamedFrom(to, old, current);
|
|
79
|
+
if (previous === null) {
|
|
80
|
+
changes.push(change('field', 'added', entity, name, undefined, to));
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
renamed.add(previous);
|
|
84
|
+
const from = old[previous];
|
|
85
|
+
const detail = { renamed: previous };
|
|
86
|
+
const keys = without(changedKeys(from, to), 'aliases');
|
|
87
|
+
if (keys.length > 0) detail.keys = keys;
|
|
88
|
+
changes.push(change('field', 'renamed', entity, name, from, to, detail));
|
|
89
|
+
}
|
|
90
|
+
for (const name of Object.keys(old)) {
|
|
91
|
+
if (Object.hasOwn(current, name) || renamed.has(name)) continue;
|
|
92
|
+
const from = old[name];
|
|
93
|
+
const detail = from.deprecated ? { deprecated: true } : undefined;
|
|
94
|
+
changes.push(change('field', 'removed', entity, name, from, undefined, detail));
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// Two dictionaries of definitions (the indexes of an entity, the types of
|
|
99
|
+
// a model): added, removed and changed by key.
|
|
100
|
+
const diffKeyed = (kind, entity, old, current, changes) => {
|
|
101
|
+
for (const name of keysOf(old, current)) {
|
|
102
|
+
const from = own(old, name);
|
|
103
|
+
const to = own(current, name);
|
|
104
|
+
if (from === undefined) changes.push(change(kind, 'added', entity, name, undefined, to));
|
|
105
|
+
else if (to === undefined) changes.push(change(kind, 'removed', entity, name, from));
|
|
106
|
+
else if (!same(from, to)) {
|
|
107
|
+
changes.push(
|
|
108
|
+
change(kind, 'changed', entity, name, from, to, { keys: changedKeys(from, to) }),
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const labelOf = (relation) => `${relation.type} ${relation.to}`;
|
|
115
|
+
|
|
116
|
+
// The relations as a list: the ones that are gone, then the new ones.
|
|
117
|
+
const diffRelations = (entity, old, current, changes) => {
|
|
118
|
+
const counts = new Map();
|
|
119
|
+
for (const relation of current) {
|
|
120
|
+
const label = labelOf(relation);
|
|
121
|
+
counts.set(label, (counts.get(label) || 0) + 1);
|
|
122
|
+
}
|
|
123
|
+
for (const relation of old) {
|
|
124
|
+
const label = labelOf(relation);
|
|
125
|
+
const count = counts.get(label) || 0;
|
|
126
|
+
if (count > 0) counts.set(label, count - 1);
|
|
127
|
+
else changes.push(change('relation', 'removed', entity, relation.to, relation));
|
|
128
|
+
}
|
|
129
|
+
for (const relation of current) {
|
|
130
|
+
const label = labelOf(relation);
|
|
131
|
+
const count = counts.get(label);
|
|
132
|
+
if (count > 0) {
|
|
133
|
+
counts.set(label, count - 1);
|
|
134
|
+
changes.push(change('relation', 'added', entity, relation.to, undefined, relation));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const META = ['kind', 'scope', 'store', 'allow', 'parent', 'unknown', 'custom'];
|
|
140
|
+
|
|
141
|
+
// The metadata of an entity; a schema of one type is its type, so a struct
|
|
142
|
+
// that became one (or back) differs in `type`.
|
|
143
|
+
const metaOf = (entity) => {
|
|
144
|
+
const meta = {};
|
|
145
|
+
for (const key of META) if (entity[key] !== undefined) meta[key] = entity[key];
|
|
146
|
+
if (entity.fields === undefined) meta.type = entity.type;
|
|
147
|
+
return meta;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const isRelations = (list) => {
|
|
151
|
+
if (!Array.isArray(list)) return false;
|
|
152
|
+
for (const item of list) if (!isObject(item)) return false;
|
|
153
|
+
return true;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// What `diff` reads of an entity: an object with `fields` (every one an
|
|
157
|
+
// object: a definition or a function marker) or a `type`, and the relations
|
|
158
|
+
// as a list of objects, when there.
|
|
159
|
+
const checkEntity = (entity, name) => {
|
|
160
|
+
const fail = (reason) => atSchema(invalidSnapshot(reason), name);
|
|
161
|
+
if (!isObject(entity)) throw fail('An entity of a snapshot must be an object');
|
|
162
|
+
const { fields, type, relations = [] } = entity;
|
|
163
|
+
if (!isObject(fields === undefined ? type : fields) || !isRelations(relations)) {
|
|
164
|
+
throw fail('An entity needs fields or a type, and relations as a list');
|
|
165
|
+
}
|
|
166
|
+
for (const key in fields) {
|
|
167
|
+
if (!isObject(fields[key])) {
|
|
168
|
+
throw invalidSnapshot('A field must be an object').locate(name, key);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const diffEntity = (name, old, current, changes) => {
|
|
174
|
+
checkEntity(old, name);
|
|
175
|
+
checkEntity(current, name);
|
|
176
|
+
const from = metaOf(old);
|
|
177
|
+
const to = metaOf(current);
|
|
178
|
+
const keys = changedKeys(from, to);
|
|
179
|
+
if (keys.length > 0) changes.push(change('entity', 'changed', name, name, from, to, { keys }));
|
|
180
|
+
if (old.fields !== undefined && current.fields !== undefined) {
|
|
181
|
+
diffFields(name, old.fields, current.fields, changes);
|
|
182
|
+
}
|
|
183
|
+
diffKeyed('index', name, old.indexes || {}, current.indexes || {}, changes);
|
|
184
|
+
diffRelations(name, old.relations || [], current.relations || [], changes);
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// The changes from `prev` to `next`, each a Model or a snapshot: the
|
|
188
|
+
// entities of `next` in their order (added, or their changes), the entities
|
|
189
|
+
// that are gone, then the types.
|
|
190
|
+
const diff = (prev, next) => {
|
|
191
|
+
const old = snapshotOf(prev);
|
|
192
|
+
const current = snapshotOf(next);
|
|
193
|
+
const changes = [];
|
|
194
|
+
for (const name of Object.keys(current.entities)) {
|
|
195
|
+
const to = current.entities[name];
|
|
196
|
+
if (Object.hasOwn(old.entities, name)) diffEntity(name, old.entities[name], to, changes);
|
|
197
|
+
else changes.push(change('entity', 'added', name, name, undefined, to));
|
|
198
|
+
}
|
|
199
|
+
for (const name of Object.keys(old.entities)) {
|
|
200
|
+
if (!Object.hasOwn(current.entities, name)) {
|
|
201
|
+
changes.push(change('entity', 'removed', name, name, old.entities[name]));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
diffKeyed('type', '', old.types || {}, current.types || {}, changes);
|
|
205
|
+
return changes;
|
|
206
|
+
};
|
|
207
|
+
|
|
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
|
+
// A field a reader may do without: an optional one, or one with a default
|
|
219
|
+
// the reader fills in.
|
|
220
|
+
const optional = (field) => field.required === false || field.default !== undefined;
|
|
221
|
+
|
|
222
|
+
const numberOf = (value) =>
|
|
223
|
+
value !== null && typeof value === 'object' ? BigInt(value.$bigint) : value;
|
|
224
|
+
|
|
225
|
+
// A lower bound raised, or an upper bound lowered, accepts less (the new
|
|
226
|
+
// reader rejects old data: forward); the other way accepts more (backward).
|
|
227
|
+
const boundLevel = (from, to, lower) => {
|
|
228
|
+
if (to === undefined) return BACKWARD;
|
|
229
|
+
if (from === undefined) return FORWARD;
|
|
230
|
+
const tighter = lower ? numberOf(to) > numberOf(from) : numberOf(to) < numberOf(from);
|
|
231
|
+
return tighter ? FORWARD : BACKWARD;
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const lengthLevel = (from = {}, to = {}) => {
|
|
235
|
+
let level = FULL;
|
|
236
|
+
if (!same(from.min, to.min)) level &= boundLevel(from.min, to.min, true);
|
|
237
|
+
if (!same(from.max, to.max)) level &= boundLevel(from.max, to.max, false);
|
|
238
|
+
return level;
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
const includes = (list, value) => {
|
|
242
|
+
for (const item of list) if (same(item, value)) return true;
|
|
243
|
+
return false;
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
// Values added accept more (backward), values removed accept less (forward).
|
|
247
|
+
const enumLevel = (from, to) => {
|
|
248
|
+
let level = FULL;
|
|
249
|
+
for (const value of to) if (!includes(from, value)) level &= BACKWARD;
|
|
250
|
+
for (const value of from) if (!includes(to, value)) level &= FORWARD;
|
|
251
|
+
return level;
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
// Both lists compared by position; a branch added accepts more, one removed
|
|
255
|
+
// accepts less; a tuple is the same length or nothing.
|
|
256
|
+
const listLevel = (from, to, extensible) => {
|
|
257
|
+
let level = FULL;
|
|
258
|
+
const shared = Math.min(from.length, to.length);
|
|
259
|
+
for (let index = 0; index < shared; index += 1) level &= fieldLevel(from[index], to[index]);
|
|
260
|
+
if (from.length === to.length) return level;
|
|
261
|
+
if (!extensible) return NONE;
|
|
262
|
+
return level & (to.length > from.length ? BACKWARD : FORWARD);
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
// A function of a struct or a field: a validate added accepts less, one
|
|
266
|
+
// removed accepts more; a decoder or a serializer changes nothing the data
|
|
267
|
+
// carries.
|
|
268
|
+
const functionLevel = (key, from, to) => {
|
|
269
|
+
if (key !== 'validate') return FULL;
|
|
270
|
+
if (from === undefined) return FORWARD;
|
|
271
|
+
if (to === undefined) return BACKWARD;
|
|
272
|
+
return FULL;
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// The fields of a nested struct, by the rules of the fields of an entity.
|
|
276
|
+
const structLevel = (old, current) => {
|
|
277
|
+
let level = FULL;
|
|
278
|
+
const renamed = new Set();
|
|
279
|
+
for (const name of Object.keys(current)) {
|
|
280
|
+
const to = current[name];
|
|
281
|
+
const from = own(old, name);
|
|
282
|
+
if (isFunctionMarker(to) || isFunctionMarker(from)) {
|
|
283
|
+
if (!same(from, to)) level &= functionLevel(name, from, to);
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
if (from !== undefined) {
|
|
287
|
+
level &= fieldLevel(from, to);
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
const previous = renamedFrom(to, old, current);
|
|
291
|
+
if (previous === null) {
|
|
292
|
+
level &= optional(to) ? FULL : FORWARD;
|
|
293
|
+
continue;
|
|
294
|
+
}
|
|
295
|
+
renamed.add(previous);
|
|
296
|
+
level &= (optional(old[previous]) ? FULL : BACKWARD) & fieldLevel(old[previous], to);
|
|
297
|
+
}
|
|
298
|
+
for (const name of Object.keys(old)) {
|
|
299
|
+
if (Object.hasOwn(current, name) || renamed.has(name)) continue;
|
|
300
|
+
const from = old[name];
|
|
301
|
+
if (isFunctionMarker(from)) level &= functionLevel(name, from, undefined);
|
|
302
|
+
else level &= optional(from) ? FULL : BACKWARD;
|
|
303
|
+
}
|
|
304
|
+
return level;
|
|
305
|
+
};
|
|
306
|
+
|
|
307
|
+
// An alias removed leaves old data under that key unreadable.
|
|
308
|
+
const aliasesLevel = (from = [], to = []) => {
|
|
309
|
+
for (const alias of from) if (!to.includes(alias)) return FORWARD;
|
|
310
|
+
return FULL;
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
// The class of one key of a field definition that changed.
|
|
314
|
+
const keyLevel = (key, from, to, field) => {
|
|
315
|
+
switch (key) {
|
|
316
|
+
case 'required':
|
|
317
|
+
return to === false ? BACKWARD : FORWARD;
|
|
318
|
+
case 'default':
|
|
319
|
+
if (from === undefined || to !== undefined) return FULL;
|
|
320
|
+
return field.required === false ? FULL : FORWARD;
|
|
321
|
+
case 'nullable':
|
|
322
|
+
return to === true ? BACKWARD : FORWARD;
|
|
323
|
+
case 'length':
|
|
324
|
+
return lengthLevel(from, to);
|
|
325
|
+
case 'min':
|
|
326
|
+
return boundLevel(from, to, true);
|
|
327
|
+
case 'max':
|
|
328
|
+
return boundLevel(from, to, false);
|
|
329
|
+
case 'pattern':
|
|
330
|
+
return from === undefined ? FORWARD : to === undefined ? BACKWARD : NONE;
|
|
331
|
+
case 'enum':
|
|
332
|
+
return Array.isArray(from) && Array.isArray(to) ? enumLevel(from, to) : NONE;
|
|
333
|
+
case 'validate':
|
|
334
|
+
case 'parse':
|
|
335
|
+
case 'serialize':
|
|
336
|
+
return functionLevel(key, from, to);
|
|
337
|
+
case 'aliases':
|
|
338
|
+
return aliasesLevel(from, to);
|
|
339
|
+
case 'schema':
|
|
340
|
+
return isObject(from) && isObject(to) ? structLevel(from, to) : NONE;
|
|
341
|
+
case 'value':
|
|
342
|
+
if (Array.isArray(from) && Array.isArray(to)) return listLevel(from, to, false);
|
|
343
|
+
return isObject(from) && isObject(to) ? fieldLevel(from, to) : NONE;
|
|
344
|
+
case 'union':
|
|
345
|
+
return Array.isArray(from) && Array.isArray(to) ? listLevel(from, to, true) : NONE;
|
|
346
|
+
case 'key':
|
|
347
|
+
case 'one':
|
|
348
|
+
case 'many':
|
|
349
|
+
case 'embed':
|
|
350
|
+
case 'discriminator':
|
|
351
|
+
return NONE;
|
|
352
|
+
default:
|
|
353
|
+
return FULL;
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
// Two definitions of one field: a type widened from integer to number reads
|
|
358
|
+
// old data (backward), narrowed the other way is read by old readers
|
|
359
|
+
// (forward), any other type is another thing; then every key that changed.
|
|
360
|
+
const fieldLevel = (from, to) => {
|
|
361
|
+
if (isFunctionMarker(from) || isFunctionMarker(to)) return same(from, to) ? FULL : NONE;
|
|
362
|
+
let level = FULL;
|
|
363
|
+
if (from.type !== to.type) {
|
|
364
|
+
if (from.type === 'integer' && to.type === 'number') level = BACKWARD;
|
|
365
|
+
else if (from.type === 'number' && to.type === 'integer') level = FORWARD;
|
|
366
|
+
else return NONE;
|
|
367
|
+
}
|
|
368
|
+
for (const key of keysOf(from, to)) {
|
|
369
|
+
const a = own(from, key);
|
|
370
|
+
const b = own(to, key);
|
|
371
|
+
if (key === 'type' || same(a, b)) continue;
|
|
372
|
+
level &= keyLevel(key, a, b, to);
|
|
373
|
+
}
|
|
374
|
+
return level;
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
const entityLevel = (entry) => {
|
|
378
|
+
const { op, detail, to } = entry;
|
|
379
|
+
if (op === 'added') return FULL;
|
|
380
|
+
if (op === 'removed') return BACKWARD;
|
|
381
|
+
let level = FULL;
|
|
382
|
+
for (const key of detail.keys) {
|
|
383
|
+
if (key === 'kind' || key === 'store' || key === 'parent' || key === 'type') level &= NONE;
|
|
384
|
+
else if (key === 'unknown') level &= to.unknown === 'ignore' ? BACKWARD : FORWARD;
|
|
385
|
+
}
|
|
386
|
+
return level;
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
const levelOf = (entry) => {
|
|
390
|
+
const { kind, op, from, to } = entry;
|
|
391
|
+
if (kind === 'entity') return entityLevel(entry);
|
|
392
|
+
if (kind === 'type') {
|
|
393
|
+
if (op !== 'changed') return FULL;
|
|
394
|
+
return same(entry.detail.keys, ['metadata']) ? FULL : NONE;
|
|
395
|
+
}
|
|
396
|
+
if (kind !== 'field') return FULL;
|
|
397
|
+
// A function among the fields: a calculated field, or a function of the
|
|
398
|
+
// schema itself.
|
|
399
|
+
if (isFunctionMarker(from) || isFunctionMarker(to)) return functionLevel(entry.name, from, to);
|
|
400
|
+
if (op === 'added') return optional(to) ? FULL : FORWARD;
|
|
401
|
+
if (op === 'removed') return optional(from) ? FULL : BACKWARD;
|
|
402
|
+
const level = fieldLevel(from, to);
|
|
403
|
+
return op === 'renamed' ? level & (optional(from) ? FULL : BACKWARD) : level;
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
// What a change carries, as `diff` writes it: a kind and an op, and the
|
|
407
|
+
// keys that differ for `changed`.
|
|
408
|
+
const isChange = (entry) =>
|
|
409
|
+
isObject(entry) &&
|
|
410
|
+
typeof entry.kind === 'string' &&
|
|
411
|
+
typeof entry.op === 'string' &&
|
|
412
|
+
(entry.op !== 'changed' || Array.isArray(entry.detail?.keys));
|
|
413
|
+
|
|
414
|
+
// The class of a change as bits. Whatever fails while its definitions are
|
|
415
|
+
// read (a bigint that does not read, a nesting the stack cannot take) is an
|
|
416
|
+
// error of the snapshot, located at the change.
|
|
417
|
+
const classOf = (entry) => {
|
|
418
|
+
if (!isChange(entry)) throw invalidTarget('A list of changes is expected');
|
|
419
|
+
try {
|
|
420
|
+
return levelOf(entry);
|
|
421
|
+
} catch (error) {
|
|
422
|
+
const located = error instanceof SchemaDefinitionError ? error : invalidSnapshot(error.message);
|
|
423
|
+
throw located.locate(entry.entity, entry.name);
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
// The class of one change: 'full', 'backward', 'forward' or 'none'.
|
|
428
|
+
const compatibilityOf = (entry) => NAMES[classOf(entry)];
|
|
429
|
+
|
|
430
|
+
// The class of a list of changes: the class every change allows.
|
|
431
|
+
const compatibility = (changes) => {
|
|
432
|
+
if (!Array.isArray(changes)) throw invalidTarget('A list of changes is expected');
|
|
433
|
+
let level = FULL;
|
|
434
|
+
for (const entry of changes) level &= classOf(entry);
|
|
435
|
+
return NAMES[level];
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
module.exports = { diff, compatibility, compatibilityOf };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Compatibility,
|
|
3
|
+
Infer,
|
|
4
|
+
MergeOptions,
|
|
5
|
+
Model,
|
|
6
|
+
ModelSnapshot,
|
|
7
|
+
RestoredModel,
|
|
8
|
+
RestoreOptions,
|
|
9
|
+
SampleOptions,
|
|
10
|
+
Schema,
|
|
11
|
+
SchemaChange,
|
|
12
|
+
} from '../../index.js';
|
|
13
|
+
|
|
14
|
+
// The model of a snapshot (`model.toJSON()`), with placeholders for the
|
|
15
|
+
// functions, listed by path in `placeholders`; `options.types` brings the
|
|
16
|
+
// real custom types, the registry is 'isolated' unless `options.registry`
|
|
17
|
+
// says otherwise (a shared one takes no placeholder type). A snapshot that
|
|
18
|
+
// does not read is `ERR_INVALID_SNAPSHOT`, located in its entity.
|
|
19
|
+
export function fromJSON(snapshot: ModelSnapshot, options?: RestoreOptions): RestoredModel;
|
|
20
|
+
|
|
21
|
+
// The changes from `prev` to `next`, each a Model or a snapshot: the
|
|
22
|
+
// entities of `next` in their order (added, or their changes: fields,
|
|
23
|
+
// indexes, relations), the entities that are gone, then the types.
|
|
24
|
+
export function diff(prev: Model | ModelSnapshot, next: Model | ModelSnapshot): SchemaChange[];
|
|
25
|
+
|
|
26
|
+
// The compatibility class of a list of changes by the rules of the Confluent
|
|
27
|
+
// Schema Registry: 'backward' when a reader with the new model reads data
|
|
28
|
+
// written with the old one, 'forward' the other way, 'full' when both,
|
|
29
|
+
// 'none' when neither.
|
|
30
|
+
export function compatibility(changes: SchemaChange[]): Compatibility;
|
|
31
|
+
|
|
32
|
+
// The compatibility class of one change, by the same rules: what to filter
|
|
33
|
+
// a list of changes by.
|
|
34
|
+
export function compatibilityOf(change: SchemaChange): Compatibility;
|
|
35
|
+
|
|
36
|
+
// A value the schema accepts, the same for a seed: every field is drawn
|
|
37
|
+
// from its definition and verified with its own check. A definition no
|
|
38
|
+
// value satisfies is `ERR_UNSATISFIABLE`, a pattern the sampler cannot read
|
|
39
|
+
// `ERR_SAMPLE_PATTERN`, a custom type without `metadata.sample`
|
|
40
|
+
// `ERR_SAMPLE_TYPE`.
|
|
41
|
+
export function sample<D extends string | object>(
|
|
42
|
+
schema: Schema<D>,
|
|
43
|
+
options?: SampleOptions,
|
|
44
|
+
): Infer<D>;
|
|
45
|
+
|
|
46
|
+
// The schema both schemas describe, by unification: a field in both takes
|
|
47
|
+
// the constraints of both, a field in one comes as it is, and what no value
|
|
48
|
+
// can satisfy (two types, two patterns, bounds with nothing in common, two
|
|
49
|
+
// defaults) is `ERR_MERGE_CONFLICT` with the field. The kind, its metadata
|
|
50
|
+
// and the name are the first schema's unless the options say otherwise.
|
|
51
|
+
export function merge(a: Schema, b: Schema, options?: MergeOptions): Schema;
|
|
52
|
+
|
|
53
|
+
// Installs `Model.fromJSON`, `Model.diff`, `Model.compatibility`,
|
|
54
|
+
// `Model.compatibilityOf`, `Schema.merge` and `schema.sample`. Once per
|
|
55
|
+
// process; later calls do nothing.
|
|
56
|
+
export function register(): void;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// The `./model` subpath: the model as the source of truth. A model restored
|
|
2
|
+
// from its snapshot (`fromJSON`), the changes between two versions and their
|
|
3
|
+
// compatibility class (`diff`, `compatibility`, `compatibilityOf`), a value
|
|
4
|
+
// a schema accepts (`sample`), two schemas as one (`merge`), kept out of
|
|
5
|
+
// the core entry so
|
|
6
|
+
// that a program that only validates does not carry them. The functions take
|
|
7
|
+
// Models, Schemas or snapshots; `register()` installs them as members of the
|
|
8
|
+
// classes for the programs that want the methods.
|
|
9
|
+
const { Schema } = require('../schema.js');
|
|
10
|
+
const { Model } = require('../model.js');
|
|
11
|
+
const { fromJSON } = require('./restore.js');
|
|
12
|
+
const { diff, compatibility, compatibilityOf } = require('./diff.js');
|
|
13
|
+
const { sample } = require('./sample.js');
|
|
14
|
+
const { merge } = require('./merge.js');
|
|
15
|
+
|
|
16
|
+
// Installed like class members: not enumerable.
|
|
17
|
+
const install = (target, members) => {
|
|
18
|
+
const descriptors = Object.getOwnPropertyDescriptors(members);
|
|
19
|
+
for (const name in descriptors) {
|
|
20
|
+
descriptors[name].enumerable = false;
|
|
21
|
+
Object.defineProperty(target, name, descriptors[name]);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
let registered = false;
|
|
26
|
+
|
|
27
|
+
// Adds `Model.fromJSON`, `Model.diff`, `Model.compatibility` and
|
|
28
|
+
// `Model.compatibilityOf`, `Schema.merge` and `schema.sample(options)`.
|
|
29
|
+
// Once per process.
|
|
30
|
+
const register = () => {
|
|
31
|
+
if (registered) return;
|
|
32
|
+
registered = true;
|
|
33
|
+
install(Model, { fromJSON, diff, compatibility, compatibilityOf });
|
|
34
|
+
install(Schema, { merge });
|
|
35
|
+
install(Schema.prototype, {
|
|
36
|
+
sample(options) {
|
|
37
|
+
return sample(this, options);
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
module.exports = { fromJSON, diff, compatibility, compatibilityOf, sample, merge, register };
|