@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
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// anyOf/oneOf as a union, `$ref` into the definitions, the rules (`minLength`,
|
|
5
5
|
// `maximum`, `minItems`, ...), null as nullable or optional, and the
|
|
6
6
|
// annotations. A keyword outside it (`allOf` with several schemas, `not`,
|
|
7
|
-
// `if`, `patternProperties`, `exclusiveMinimum`, ...) is
|
|
7
|
+
// `if`, `patternProperties`, `exclusiveMinimum`, ...) is ALX_ERR_UNSUPPORTED_SCHEMA
|
|
8
8
|
// with its place in the document, or dropped with `unsupported: 'ignore'`.
|
|
9
9
|
// The document is untrusted input: a pointer reads own keys only, a `$ref` is
|
|
10
10
|
// converted once per pointer, and the nesting is bounded.
|
|
@@ -13,9 +13,8 @@ const { define, shorten } = require('../util.js');
|
|
|
13
13
|
const { Schema } = require('../schema.js');
|
|
14
14
|
const { readsAsStruct } = require('../types.js');
|
|
15
15
|
const { FORMATS } = require('../rules.js');
|
|
16
|
-
const {
|
|
17
|
-
|
|
18
|
-
const invalidOptions = (reason) => new SchemaDefinitionError('ERR_INVALID_OPTIONS', reason);
|
|
16
|
+
const { isThenable, abandon } = require('../issues.js');
|
|
17
|
+
const { SchemaDefinitionError, SchemaUsageError, invalidTarget } = require('../errors.js');
|
|
19
18
|
|
|
20
19
|
// The keys of a JSON object are strings, so a number key is this pattern.
|
|
21
20
|
const NUMBER_KEY = '^-?\\d+(\\.\\d+)?$';
|
|
@@ -57,12 +56,16 @@ const KNOWN = new Set([
|
|
|
57
56
|
|
|
58
57
|
// The steps `multipleOf` writes for a number of 0 to 15 decimals, by count.
|
|
59
58
|
const DECIMALS = [];
|
|
60
|
-
for (let count = 0; count <= 15; count += 1) DECIMALS.push(10 ** -count);
|
|
61
59
|
|
|
62
60
|
// How deep a document may nest: past it the conversion stops whatever the
|
|
63
61
|
// mode, since a lenient conversion must not recurse without end either.
|
|
64
62
|
const MAX_DEPTH = 128;
|
|
65
63
|
|
|
64
|
+
// An option of the call that is not what it must be: the call is wrong.
|
|
65
|
+
const wrongOption = (reason) => new SchemaUsageError(reason, { code: 'ALX_ERR_INVALID_OPTION' });
|
|
66
|
+
|
|
67
|
+
for (let count = 0; count <= 15; count += 1) DECIMALS.push(10 ** -count);
|
|
68
|
+
|
|
66
69
|
const option = (options, name, allowed) => {
|
|
67
70
|
const value = options[name];
|
|
68
71
|
if (value === undefined) return allowed[0];
|
|
@@ -71,19 +74,19 @@ const option = (options, name, allowed) => {
|
|
|
71
74
|
for (let index = 0; index < allowed.length; index += 1) {
|
|
72
75
|
list += `${index === 0 ? '' : ', '}${JSON.stringify(allowed[index])}`;
|
|
73
76
|
}
|
|
74
|
-
throw
|
|
77
|
+
throw wrongOption(`Option "${name}" must be one of ${list}, got ${JSON.stringify(value)}`);
|
|
75
78
|
}
|
|
76
79
|
return value;
|
|
77
80
|
};
|
|
78
81
|
|
|
79
82
|
const createContext = (document, options) => {
|
|
80
83
|
if (options === null || typeof options !== 'object') {
|
|
81
|
-
throw
|
|
84
|
+
throw wrongOption('Options must be an object');
|
|
82
85
|
}
|
|
83
86
|
const { name = '', onUnsupported } = options;
|
|
84
|
-
if (typeof name !== 'string') throw
|
|
87
|
+
if (typeof name !== 'string') throw wrongOption('Option "name" must be a string');
|
|
85
88
|
if (onUnsupported !== undefined && typeof onUnsupported !== 'function') {
|
|
86
|
-
throw
|
|
89
|
+
throw wrongOption('Option "onUnsupported" must be a function');
|
|
87
90
|
}
|
|
88
91
|
return {
|
|
89
92
|
document,
|
|
@@ -114,16 +117,28 @@ const pointerOf = (path) => {
|
|
|
114
117
|
return pointer;
|
|
115
118
|
};
|
|
116
119
|
|
|
120
|
+
// An observer never fails the operation it watches (alexiskills,
|
|
121
|
+
// library-errors): what the hook throws, or a promise of it that rejects, is
|
|
122
|
+
// dropped. A copy per subpath, as `install`, so the core carries none.
|
|
123
|
+
const notify = (observer, first, second) => {
|
|
124
|
+
try {
|
|
125
|
+
const returned = observer(first, second);
|
|
126
|
+
if (isThenable(returned)) abandon(returned);
|
|
127
|
+
} catch {
|
|
128
|
+
// ignore: an observer never fails the operation it watches
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
117
132
|
// A node outside the subset: the error with its place, or a drop that
|
|
118
133
|
// `onUnsupported` is told of (in the throw mode the error carries both).
|
|
119
134
|
const unsupported = (context, what) => {
|
|
120
135
|
const where = pointerOf(context.path);
|
|
121
136
|
if (context.lenient) {
|
|
122
|
-
if (context.onUnsupported !== undefined) context.onUnsupported
|
|
137
|
+
if (context.onUnsupported !== undefined) notify(context.onUnsupported, what, where);
|
|
123
138
|
return;
|
|
124
139
|
}
|
|
125
140
|
const reason = `${what} is not supported in a JSON Schema at "${where}"`;
|
|
126
|
-
throw new SchemaDefinitionError('
|
|
141
|
+
throw new SchemaDefinitionError(reason, { code: 'ALX_ERR_UNSUPPORTED_SCHEMA' });
|
|
127
142
|
};
|
|
128
143
|
|
|
129
144
|
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
@@ -171,7 +186,7 @@ const resolve = (context, ref) => {
|
|
|
171
186
|
const keys = [];
|
|
172
187
|
let node = context.document;
|
|
173
188
|
for (const segment of segments) {
|
|
174
|
-
let key;
|
|
189
|
+
let key = '';
|
|
175
190
|
try {
|
|
176
191
|
key = decodeURIComponent(segment);
|
|
177
192
|
} catch {
|
|
@@ -240,115 +255,141 @@ const structOf = (context, node, used) => {
|
|
|
240
255
|
return fields;
|
|
241
256
|
};
|
|
242
257
|
|
|
243
|
-
//
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
if (typeof pattern !== 'string') unsupported(context, 'A "pattern" that is not a string');
|
|
257
|
-
else if (compilesWithU(pattern)) def.pattern = pattern;
|
|
258
|
-
else {
|
|
259
|
-
const shown = JSON.stringify(pattern);
|
|
260
|
-
unsupported(context, `The pattern ${shown} (not a valid pattern under the u flag)`);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
// A format the formats subpath registered is the rule; any other stays
|
|
264
|
-
// the annotation JSON Schema says it is. OpenAPI's `byte` is base64, and
|
|
265
|
-
// the shape the export writes beside a format is the format's own.
|
|
266
|
-
const format = take('format');
|
|
267
|
-
const name = format === 'byte' && FORMATS.byte === undefined ? 'base64' : format;
|
|
268
|
-
if (typeof name === 'string' && FORMATS[name] !== undefined) {
|
|
269
|
-
def.format = name;
|
|
270
|
-
if (def.pattern === FORMATS[name].pattern) delete def.pattern;
|
|
271
|
-
}
|
|
272
|
-
// Base64 text (what `bytes` exports as) is the base64 format when the
|
|
273
|
-
// formats are registered, as OpenAPI's `byte` is.
|
|
274
|
-
if (def.format === undefined && node.contentEncoding === 'base64' && FORMATS.base64) {
|
|
275
|
-
def.format = 'base64';
|
|
258
|
+
// The rules of a JSON Schema type, into the definition: `take` reads a
|
|
259
|
+
// keyword and marks it used.
|
|
260
|
+
const stringRules = (context, node, def, take) => {
|
|
261
|
+
const min = take('minLength');
|
|
262
|
+
const max = take('maxLength');
|
|
263
|
+
if (min !== undefined || max !== undefined) def.length = { min, max };
|
|
264
|
+
const pattern = take('pattern');
|
|
265
|
+
if (pattern !== undefined) {
|
|
266
|
+
if (typeof pattern !== 'string') unsupported(context, 'A "pattern" that is not a string');
|
|
267
|
+
else if (compilesWithU(pattern)) def.pattern = pattern;
|
|
268
|
+
else {
|
|
269
|
+
const shown = JSON.stringify(pattern);
|
|
270
|
+
unsupported(context, `The pattern ${shown} (not a valid pattern under the u flag)`);
|
|
276
271
|
}
|
|
277
|
-
return def;
|
|
278
272
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
if (type === 'integer' ? node.multipleOf === 1 : decimals !== -1) take('multipleOf');
|
|
288
|
-
if (type === 'number' && decimals !== -1) def.decimals = decimals;
|
|
289
|
-
return def;
|
|
273
|
+
// A format the formats subpath registered is the rule; any other stays
|
|
274
|
+
// the annotation JSON Schema says it is. OpenAPI's `byte` is base64, and
|
|
275
|
+
// the shape the export writes beside a format is the format's own.
|
|
276
|
+
const format = take('format');
|
|
277
|
+
const name = format === 'byte' && FORMATS.byte === undefined ? 'base64' : format;
|
|
278
|
+
if (typeof name === 'string' && FORMATS[name] !== undefined) {
|
|
279
|
+
def.format = name;
|
|
280
|
+
if (def.pattern === FORMATS[name].pattern) delete def.pattern;
|
|
290
281
|
}
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
282
|
+
// Base64 text (what `bytes` exports as) is the base64 format when the
|
|
283
|
+
// formats are registered, as OpenAPI's `byte` is.
|
|
284
|
+
if (def.format === undefined && node.contentEncoding === 'base64' && FORMATS.base64) {
|
|
285
|
+
def.format = 'base64';
|
|
286
|
+
}
|
|
287
|
+
return def;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
const numberRules = (node, def, take) => {
|
|
291
|
+
const { type } = def;
|
|
292
|
+
const min = take('minimum');
|
|
293
|
+
const max = take('maximum');
|
|
294
|
+
if (min !== undefined) def.min = min;
|
|
295
|
+
if (max !== undefined) def.max = max;
|
|
296
|
+
// A multiple of 10⁻ⁿ is a number of n decimals and 1 says nothing more
|
|
297
|
+
// of an integer; any other step is a keyword outside the subset.
|
|
298
|
+
const decimals = DECIMALS.indexOf(node.multipleOf);
|
|
299
|
+
if (type === 'integer' ? node.multipleOf === 1 : decimals !== -1) take('multipleOf');
|
|
300
|
+
if (type === 'number' && decimals !== -1) def.decimals = decimals;
|
|
301
|
+
return def;
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
// The elements of a tuple: the prefixItems of 2020-12, else an items list
|
|
305
|
+
// of draft-07.
|
|
306
|
+
const elementsOf = (prefix, items) => {
|
|
307
|
+
if (Array.isArray(prefix)) return prefix;
|
|
308
|
+
return Array.isArray(items) ? items : null;
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
const arrayRules = (context, def, take) => {
|
|
312
|
+
const items = take('items');
|
|
313
|
+
const prefix = take('prefixItems');
|
|
314
|
+
const min = take('minItems');
|
|
315
|
+
const max = take('maxItems');
|
|
316
|
+
const elements = elementsOf(prefix, items);
|
|
317
|
+
if (elements !== null) {
|
|
318
|
+
// A tuple: the elements up to minItems are required, the rest optional.
|
|
319
|
+
take('additionalItems');
|
|
320
|
+
if (items !== false && !Array.isArray(items) && items !== undefined) {
|
|
321
|
+
unsupported(context, 'A tuple with more items');
|
|
322
|
+
}
|
|
323
|
+
def.type = 'tuple';
|
|
324
|
+
def.value = [];
|
|
325
|
+
const key = Array.isArray(prefix) ? 'prefixItems' : 'items';
|
|
326
|
+
context.path.push(key);
|
|
327
|
+
for (let index = 0; index < elements.length; index += 1) {
|
|
328
|
+
context.path.push(index);
|
|
329
|
+
def.value.push(convert(context, elements[index], min === undefined || index < min));
|
|
312
330
|
context.path.pop();
|
|
313
|
-
return def;
|
|
314
331
|
}
|
|
315
|
-
def.type = take('uniqueItems') === true ? 'set' : 'array';
|
|
316
|
-
context.path.push('items');
|
|
317
|
-
def.value = items === undefined ? 'any' : convert(context, items);
|
|
318
332
|
context.path.pop();
|
|
319
|
-
if (min !== undefined || max !== undefined) def.length = { min, max };
|
|
320
333
|
return def;
|
|
321
334
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
const names = take('propertyNames');
|
|
339
|
-
if (names !== undefined) {
|
|
340
|
-
if (isObject(names) && names.pattern === NUMBER_KEY) def.key = 'number';
|
|
341
|
-
else unsupported(context, 'A "propertyNames" other than the number key pattern');
|
|
335
|
+
def.type = take('uniqueItems') === true ? 'set' : 'array';
|
|
336
|
+
context.path.push('items');
|
|
337
|
+
def.value = items === undefined ? 'any' : convert(context, items);
|
|
338
|
+
context.path.pop();
|
|
339
|
+
if (min !== undefined || max !== undefined) def.length = { min, max };
|
|
340
|
+
return def;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
const objectRules = (context, node, def, take, used) => {
|
|
344
|
+
const min = take('minProperties');
|
|
345
|
+
const max = take('maxProperties');
|
|
346
|
+
if (isObject(node.properties)) {
|
|
347
|
+
def.type = 'schema';
|
|
348
|
+
def.schema = structOf(context, node, used);
|
|
349
|
+
if (min !== undefined || max !== undefined) {
|
|
350
|
+
unsupported(context, 'A struct with minProperties or maxProperties');
|
|
342
351
|
}
|
|
343
|
-
context.path.push('additionalProperties');
|
|
344
|
-
def.value = convert(context, values);
|
|
345
|
-
context.path.pop();
|
|
346
|
-
if (min !== undefined || max !== undefined) def.length = { min, max };
|
|
347
352
|
return def;
|
|
348
353
|
}
|
|
354
|
+
const { additionalProperties: values } = node;
|
|
355
|
+
if (values === undefined || values === true) return { type: 'json' };
|
|
356
|
+
if (values === false) unsupported(context, 'An object with no properties');
|
|
357
|
+
def.type = 'object';
|
|
358
|
+
def.key = 'string';
|
|
359
|
+
const names = take('propertyNames');
|
|
360
|
+
if (names !== undefined) {
|
|
361
|
+
if (isObject(names) && names.pattern === NUMBER_KEY) def.key = 'number';
|
|
362
|
+
else unsupported(context, 'A "propertyNames" other than the number key pattern');
|
|
363
|
+
}
|
|
364
|
+
context.path.push('additionalProperties');
|
|
365
|
+
def.value = convert(context, values);
|
|
366
|
+
context.path.pop();
|
|
367
|
+
if (min !== undefined || max !== undefined) def.length = { min, max };
|
|
349
368
|
return def;
|
|
350
369
|
};
|
|
351
370
|
|
|
371
|
+
// The rules of a JSON Schema type, `(context, node, def, take, used)`; the
|
|
372
|
+
// type is the document's, so the table has no prototype to look it up in.
|
|
373
|
+
const RULES_OF = {
|
|
374
|
+
__proto__: null,
|
|
375
|
+
string: stringRules,
|
|
376
|
+
number: (context, node, def, take) => numberRules(node, def, take),
|
|
377
|
+
integer: (context, node, def, take) => numberRules(node, def, take),
|
|
378
|
+
array: (context, node, def, take) => arrayRules(context, def, take),
|
|
379
|
+
object: objectRules,
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
// A JSON Schema type as a definition: the rules of the type, then the rest.
|
|
383
|
+
const typed = (context, node, type, used) => {
|
|
384
|
+
const def = { type };
|
|
385
|
+
const take = (name) => {
|
|
386
|
+
used.add(name);
|
|
387
|
+
return node[name];
|
|
388
|
+
};
|
|
389
|
+
const rules = RULES_OF[type];
|
|
390
|
+
return rules === undefined ? def : rules(context, node, def, take, used);
|
|
391
|
+
};
|
|
392
|
+
|
|
352
393
|
// The branches of an anyOf or oneOf: null among them makes the value
|
|
353
394
|
// nullable, one branch is that branch, more are a union (with OpenAPI's
|
|
354
395
|
// discriminator when the node has one).
|
|
@@ -383,6 +424,16 @@ const branches = (context, node, key, used) => {
|
|
|
383
424
|
return { def, nullable };
|
|
384
425
|
};
|
|
385
426
|
|
|
427
|
+
// The type of a node without `type`, read from the keywords it carries.
|
|
428
|
+
const implicitType = (node) => {
|
|
429
|
+
if (isObject(node.properties)) return 'object';
|
|
430
|
+
if (node.items !== undefined || node.prefixItems !== undefined) return 'array';
|
|
431
|
+
const bounded = node.minLength !== undefined || node.maxLength !== undefined;
|
|
432
|
+
if (node.pattern !== undefined || bounded) return 'string';
|
|
433
|
+
if (node.minimum !== undefined || node.maximum !== undefined) return 'number';
|
|
434
|
+
return 'any';
|
|
435
|
+
};
|
|
436
|
+
|
|
386
437
|
// A node of the document as a definition object `{ type, ... }`.
|
|
387
438
|
const nodeOf = (context, node, used) => {
|
|
388
439
|
if (node.$ref !== undefined) {
|
|
@@ -446,18 +497,7 @@ const nodeOf = (context, node, used) => {
|
|
|
446
497
|
return def;
|
|
447
498
|
}
|
|
448
499
|
}
|
|
449
|
-
if (type === undefined)
|
|
450
|
-
if (isObject(node.properties)) type = 'object';
|
|
451
|
-
else if (node.items !== undefined || node.prefixItems !== undefined) type = 'array';
|
|
452
|
-
else if (
|
|
453
|
-
node.pattern !== undefined ||
|
|
454
|
-
node.minLength !== undefined ||
|
|
455
|
-
node.maxLength !== undefined
|
|
456
|
-
) {
|
|
457
|
-
type = 'string';
|
|
458
|
-
} else if (node.minimum !== undefined || node.maximum !== undefined) type = 'number';
|
|
459
|
-
else type = 'any';
|
|
460
|
-
}
|
|
500
|
+
if (type === undefined) type = implicitType(node);
|
|
461
501
|
if (!KNOWN.has(type)) {
|
|
462
502
|
unsupported(context, `The type ${JSON.stringify(type)}`);
|
|
463
503
|
return { type: 'any' };
|
|
@@ -475,7 +515,7 @@ const longOf = (context, node) => {
|
|
|
475
515
|
if (context.depth > MAX_DEPTH) {
|
|
476
516
|
const where = pointerOf(context.path);
|
|
477
517
|
const reason = `Nesting deeper than ${MAX_DEPTH} levels at "${where}"`;
|
|
478
|
-
throw new SchemaDefinitionError('
|
|
518
|
+
throw new SchemaDefinitionError(reason, { code: 'ALX_ERR_UNSUPPORTED_SCHEMA' });
|
|
479
519
|
}
|
|
480
520
|
if (node === true || (isObject(node) && Object.keys(node).length === 0)) return { type: 'any' };
|
|
481
521
|
if (node === false) {
|
|
@@ -517,19 +557,22 @@ const convert = (context, node, required = true) => compactDef(longOf(context, n
|
|
|
517
557
|
// The root: a struct with its kind, the unknown-keys policy
|
|
518
558
|
// (`additionalProperties: false` closes it, as the generator writes it) and
|
|
519
559
|
// its annotations as the metadata of the kind, or the definition of one type.
|
|
560
|
+
const rootDefinition = (def, document) => {
|
|
561
|
+
if (def.type !== 'schema') return compact(def, true);
|
|
562
|
+
const { schema, type, nullable, ...rest } = def;
|
|
563
|
+
const meta = document.additionalProperties === false ? rest : { unknown: 'ignore', ...rest };
|
|
564
|
+
const definition = { Struct: meta };
|
|
565
|
+
for (const key of Object.keys(schema)) define(definition, key, schema[key]);
|
|
566
|
+
return definition;
|
|
567
|
+
};
|
|
568
|
+
|
|
520
569
|
const fromJSONSchema = (document, options = {}) => {
|
|
521
570
|
const context = createContext(document, options);
|
|
522
571
|
if (!isObject(document) && document !== true) {
|
|
523
572
|
throw invalidTarget('A JSON Schema document (an object) is expected');
|
|
524
573
|
}
|
|
525
574
|
const def = longOf(context, document);
|
|
526
|
-
|
|
527
|
-
if (def.type === 'schema') {
|
|
528
|
-
const { schema, type, nullable, ...rest } = def;
|
|
529
|
-
const meta = document.additionalProperties === false ? rest : { unknown: 'ignore', ...rest };
|
|
530
|
-
definition = { Struct: meta };
|
|
531
|
-
for (const key of Object.keys(schema)) define(definition, key, schema[key]);
|
|
532
|
-
} else definition = compact(def, true);
|
|
575
|
+
const definition = rootDefinition(def, document);
|
|
533
576
|
return context.as === 'schema' ? new Schema(context.name, definition) : definition;
|
|
534
577
|
};
|
|
535
578
|
|
|
@@ -8,29 +8,35 @@ import type {
|
|
|
8
8
|
Schema,
|
|
9
9
|
} from '../../index.js';
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
/**
|
|
12
|
+
* The JSON Schema document of a schema, or of a model: every entity as a
|
|
13
|
+
* definition, or the document of the entity `options.root`.
|
|
14
|
+
*/
|
|
13
15
|
export function toJSONSchema(target: Schema, options?: JSONSchemaOptions): JSONSchema;
|
|
14
16
|
export function toJSONSchema(target: Model, options?: ModelJSONSchemaOptions): JSONSchema;
|
|
15
17
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
/**
|
|
19
|
+
* The TypeScript interface of a schema (a type alias for a schema of one
|
|
20
|
+
* type), or the interfaces of every entity of a model in dependency order.
|
|
21
|
+
*/
|
|
18
22
|
export function toTypeScript(target: Schema | Model, options?: InterfaceOptions): string;
|
|
19
23
|
|
|
20
|
-
|
|
24
|
+
/** Writes `toTypeScript(target, options)` to a file. Rejects in the browser. */
|
|
21
25
|
export function saveTypes(
|
|
22
26
|
outputFile: string,
|
|
23
27
|
target: Schema | Model,
|
|
24
28
|
options?: InterfaceOptions,
|
|
25
29
|
): Promise<void>;
|
|
26
30
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
/**
|
|
32
|
+
* A definition from a JSON Schema document (a Schema with `as: 'schema'`),
|
|
33
|
+
* for the subset a validation schema is made of; a keyword outside it is
|
|
34
|
+
* `ALX_ERR_UNSUPPORTED_SCHEMA` with its place in the document, or dropped with
|
|
35
|
+
* `unsupported: 'ignore'` (and told to `onUnsupported`). The document is
|
|
36
|
+
* untrusted input: a `$ref` is converted once per pointer, a pointer reads
|
|
37
|
+
* own keys only, and a nesting deeper than 128 levels is refused in both
|
|
38
|
+
* modes.
|
|
39
|
+
*/
|
|
34
40
|
export function fromJSONSchema(
|
|
35
41
|
document: JSONSchema | true,
|
|
36
42
|
options: FromJSONSchemaOptions & { as: 'schema' },
|
|
@@ -40,7 +46,9 @@ export function fromJSONSchema(
|
|
|
40
46
|
options?: FromJSONSchemaOptions,
|
|
41
47
|
): object | string;
|
|
42
48
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Installs `schema.toJSONSchema`, `schema.toInterface`, `model.toJSONSchema`,
|
|
51
|
+
* `model.toTypeScript`, `model.dts`, `Schema.fromJSONSchema` and
|
|
52
|
+
* `schema['~standard'].jsonSchema`. Once per process; later calls do nothing.
|
|
53
|
+
*/
|
|
46
54
|
export function register(): void;
|
package/src/generators/index.js
CHANGED
|
@@ -9,9 +9,9 @@ const { invalidTarget } = require('../errors.js');
|
|
|
9
9
|
const { Schema } = require('../validation.js');
|
|
10
10
|
const { Model } = require('../model.js');
|
|
11
11
|
const { writeFile } = require('../runtime/node.js');
|
|
12
|
-
const generators = require('./
|
|
12
|
+
const generators = require('./json-schema.js');
|
|
13
13
|
const typescript = require('./typescript.js');
|
|
14
|
-
const { fromJSONSchema } = require('./
|
|
14
|
+
const { fromJSONSchema } = require('./from-json-schema.js');
|
|
15
15
|
|
|
16
16
|
const isModel = (target) => {
|
|
17
17
|
if (hasBrand(target, 'Schema')) return false;
|