@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
package/src/metadata.js
CHANGED
|
@@ -1,50 +1,47 @@
|
|
|
1
1
|
const { getKindMetadata } = require('./kinds.js');
|
|
2
2
|
const { ValidationResult } = require('./result.js');
|
|
3
|
-
const { SchemaDefinitionError } = require('./errors.js');
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
const { SchemaDefinitionError, asyncReturn } = require('./errors.js');
|
|
4
|
+
const { isThenable } = require('./issues.js');
|
|
5
|
+
|
|
6
|
+
// The functions of a schema definition that are the schema's own: its
|
|
7
|
+
// validate, its decoder, its serializer and `format`.
|
|
8
|
+
const OPTIONS = new Set(['validate', 'parse', 'serialize', 'format']);
|
|
7
9
|
|
|
8
10
|
// The unknown-keys policy of a schema is the default of its `check`, so a
|
|
9
11
|
// value with keys the schema does not have is rejected or ignored without
|
|
10
12
|
// an option on every call; the option of a call still wins.
|
|
11
13
|
const unknownOf = (unknown) => {
|
|
12
14
|
if (unknown === undefined) return 'reject';
|
|
13
|
-
if (
|
|
15
|
+
if (unknown !== 'reject' && unknown !== 'ignore') {
|
|
14
16
|
const reason = `Schema metadata "unknown" must be "reject" or "ignore", got ${JSON.stringify(unknown)}`;
|
|
15
17
|
throw new SchemaDefinitionError('ERR_INVALID_OPTIONS', reason);
|
|
16
18
|
}
|
|
17
19
|
return unknown;
|
|
18
20
|
};
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
22
|
+
// `indexes` is a null-prototype dictionary keyed by definition keys, which may
|
|
23
|
+
// be called `extract` or `constructor`; `options` only ever holds the four
|
|
24
|
+
// keys below and stays a plain object with fast properties: the compiled
|
|
25
|
+
// check reads `options.validate` on every call, and V8 keeps an object made
|
|
26
|
+
// by `Object.create(null)` in dictionary mode.
|
|
27
|
+
const createOptions = () => ({ validate: null, format: null, parse: null, serialize: null });
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
}
|
|
29
|
+
const extractOption = (options, key, field) => {
|
|
30
|
+
const isOption = typeof field === 'function' && OPTIONS.has(key);
|
|
31
|
+
if (isOption) options[key] = field;
|
|
32
|
+
return isOption;
|
|
33
|
+
};
|
|
35
34
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
}
|
|
35
|
+
// Index definitions (`index`, `primary`, `unique` arrays) are taken out of the
|
|
36
|
+
// fields; a `many` relation is recorded as well but stays a field, so only
|
|
37
|
+
// whether the entry was taken is reported.
|
|
38
|
+
const extractIndex = (indexes, key, field) => {
|
|
39
|
+
if (field === null || typeof field !== 'object') return false;
|
|
40
|
+
const { index, primary, unique, many } = field;
|
|
41
|
+
const isIndex = Array.isArray(index || primary || unique);
|
|
42
|
+
if (isIndex || many) indexes[key] = field;
|
|
43
|
+
return isIndex;
|
|
44
|
+
};
|
|
48
45
|
|
|
49
46
|
class SchemaMetadata {
|
|
50
47
|
constructor() {
|
|
@@ -54,8 +51,8 @@ class SchemaMetadata {
|
|
|
54
51
|
this.allow = 'write';
|
|
55
52
|
this.parent = '';
|
|
56
53
|
this.unknown = 'reject';
|
|
57
|
-
this.indexes =
|
|
58
|
-
this.options =
|
|
54
|
+
this.indexes = Object.create(null);
|
|
55
|
+
this.options = createOptions();
|
|
59
56
|
this.custom = {};
|
|
60
57
|
this.references = new Set();
|
|
61
58
|
this.relations = new Set();
|
|
@@ -85,26 +82,26 @@ class SchemaMetadata {
|
|
|
85
82
|
|
|
86
83
|
extractMetadata(defs) {
|
|
87
84
|
const fields = Object.create(null);
|
|
88
|
-
for (const
|
|
89
|
-
const
|
|
90
|
-
const
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const taken = this[collection].extract(key, field);
|
|
94
|
-
if (taken) extracted = true;
|
|
95
|
-
}
|
|
96
|
-
if (!extracted) fields[key] = field;
|
|
85
|
+
for (const key of Object.keys(defs)) {
|
|
86
|
+
const field = defs[key];
|
|
87
|
+
const option = extractOption(this.options, key, field);
|
|
88
|
+
const index = extractIndex(this.indexes, key, field);
|
|
89
|
+
if (!option && !index) fields[key] = field;
|
|
97
90
|
}
|
|
98
91
|
return fields;
|
|
99
92
|
}
|
|
100
93
|
|
|
101
|
-
// Runs only the schema-level validate function, as a result of its own
|
|
94
|
+
// Runs only the schema-level validate function, as a result of its own; a
|
|
95
|
+
// function that returns a promise is an error here, as in check.
|
|
102
96
|
validate(value, path = '') {
|
|
103
97
|
if (!this.options.validate) return null;
|
|
104
98
|
const result = new ValidationResult();
|
|
105
99
|
try {
|
|
106
|
-
|
|
100
|
+
const output = this.options.validate.call(this, value, path);
|
|
101
|
+
if (isThenable(output)) throw asyncReturn('validator', path, 'checkAsync');
|
|
102
|
+
return result.add(output);
|
|
107
103
|
} catch (error) {
|
|
104
|
+
if (error.code === 'ERR_ASYNC_VALIDATOR') throw error;
|
|
108
105
|
return result.add({
|
|
109
106
|
code: 'exception',
|
|
110
107
|
message: `validation failed ${error}`,
|
package/src/metautil.js
CHANGED
|
@@ -11,7 +11,14 @@ const isFirstLetter = (s) => isFirstUpper(s) || isFirstLower(s);
|
|
|
11
11
|
|
|
12
12
|
const toLowerCamel = (s) => s.charAt(0).toLowerCase() + s.slice(1);
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
// The first own key that starts with a letter, without a list of the keys:
|
|
15
|
+
// this runs once for every object of a definition.
|
|
16
|
+
const firstKey = (obj) => {
|
|
17
|
+
for (const key in obj) {
|
|
18
|
+
if (Object.hasOwn(obj, key) && isFirstLetter(key)) return key;
|
|
19
|
+
}
|
|
20
|
+
return undefined;
|
|
21
|
+
};
|
|
15
22
|
|
|
16
23
|
module.exports = {
|
|
17
24
|
inRange,
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// What the functions of the `./model` subpath share: a snapshot from a Model
|
|
2
|
+
// or as given, and equality of the JSON values a snapshot is made of. A
|
|
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 ERR_INVALID_SNAPSHOT
|
|
5
|
+
// located in the entity, never a bare SyntaxError or TypeError.
|
|
6
|
+
const { hasBrand, define } = require('../util.js');
|
|
7
|
+
const { FORMAT } = require('../snapshot.js');
|
|
8
|
+
const { SchemaDefinitionError } = require('../errors.js');
|
|
9
|
+
const { isStruct } = require('../struct.js');
|
|
10
|
+
|
|
11
|
+
// How deep the walks over a snapshot go: a definition nested deeper is not
|
|
12
|
+
// one a program wrote.
|
|
13
|
+
const DEPTH = 128;
|
|
14
|
+
|
|
15
|
+
const invalidSnapshot = (reason) => new SchemaDefinitionError('ERR_INVALID_SNAPSHOT', reason);
|
|
16
|
+
|
|
17
|
+
const tooDeep = () => invalidSnapshot(`A snapshot nested deeper than ${DEPTH} levels`);
|
|
18
|
+
|
|
19
|
+
// An error of a schema as a whole, which has no field to be located at: the
|
|
20
|
+
// schema is the location, written as `locate` writes one.
|
|
21
|
+
const atSchema = (error, name) => {
|
|
22
|
+
error.schema = name;
|
|
23
|
+
error.message = `${error.message} in "${name}"`;
|
|
24
|
+
return error;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const isObject = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
28
|
+
|
|
29
|
+
const isSnapshot = (value) =>
|
|
30
|
+
isObject(value) &&
|
|
31
|
+
value.format === FORMAT &&
|
|
32
|
+
isObject(value.entities) &&
|
|
33
|
+
(value.types === undefined || isObject(value.types));
|
|
34
|
+
|
|
35
|
+
// The snapshot of a model, or the snapshot given.
|
|
36
|
+
const snapshotOf = (target) => {
|
|
37
|
+
if (hasBrand(target, 'Model')) return target.toJSON();
|
|
38
|
+
if (!isSnapshot(target)) {
|
|
39
|
+
throw invalidSnapshot(`A Model or a snapshot of format ${FORMAT} with entities is expected`);
|
|
40
|
+
}
|
|
41
|
+
return target;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// Whether two values of a definition are the same: the same primitives (a
|
|
45
|
+
// function by identity), the same elements in order, the same keys with the
|
|
46
|
+
// same values in any order; a RegExp by its text and a Date by its time, for
|
|
47
|
+
// the definitions `merge` compares (a snapshot holds neither).
|
|
48
|
+
const same = (a, b, depth = 0) => {
|
|
49
|
+
if (a === b) return true;
|
|
50
|
+
if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false;
|
|
51
|
+
if (depth > DEPTH) throw tooDeep();
|
|
52
|
+
const name = a.constructor?.name;
|
|
53
|
+
if (name === 'RegExp' || name === 'Date') {
|
|
54
|
+
if (name !== b.constructor?.name) return false;
|
|
55
|
+
return name === 'Date'
|
|
56
|
+
? a.getTime() === b.getTime()
|
|
57
|
+
: a.source === b.source && a.flags === b.flags;
|
|
58
|
+
}
|
|
59
|
+
if (Array.isArray(a)) {
|
|
60
|
+
if (!Array.isArray(b) || a.length !== b.length) return false;
|
|
61
|
+
for (let index = 0; index < a.length; index += 1) {
|
|
62
|
+
if (!same(a[index], b[index], depth + 1)) return false;
|
|
63
|
+
}
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
if (Array.isArray(b)) return false;
|
|
67
|
+
const keys = Object.keys(a);
|
|
68
|
+
if (keys.length !== Object.keys(b).length) return false;
|
|
69
|
+
for (const key of keys) {
|
|
70
|
+
if (!Object.hasOwn(b, key) || !same(a[key], b[key], depth + 1)) return false;
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const isFunctionMarker = (value) => isObject(value) && value.$function === true;
|
|
76
|
+
|
|
77
|
+
const capitalize = (name) => name.charAt(0).toUpperCase() + name.slice(1);
|
|
78
|
+
|
|
79
|
+
// The definition a built field was made from, with its nested fields as
|
|
80
|
+
// definitions and its functions kept: what `merge` unifies and `Schema`
|
|
81
|
+
// takes back.
|
|
82
|
+
const definitionOf = (field) => {
|
|
83
|
+
const def = { type: field.type };
|
|
84
|
+
for (const key of Object.keys(field)) {
|
|
85
|
+
if (key !== 'root' && key !== 'type' && field[key] !== undefined) {
|
|
86
|
+
define(def, key, partOf(field[key]));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return def;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const partOf = (value) => {
|
|
93
|
+
if (hasBrand(value, 'Type')) return definitionOf(value);
|
|
94
|
+
if (isStruct(value)) return structDefinitionOf(value);
|
|
95
|
+
if (Array.isArray(value)) {
|
|
96
|
+
const list = [];
|
|
97
|
+
for (const item of value) list.push(partOf(item));
|
|
98
|
+
return list;
|
|
99
|
+
}
|
|
100
|
+
return value;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const structDefinitionOf = (fields) => {
|
|
104
|
+
const def = {};
|
|
105
|
+
for (const key in fields) define(def, key, partOf(fields[key]));
|
|
106
|
+
return def;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
module.exports = {
|
|
110
|
+
DEPTH,
|
|
111
|
+
invalidSnapshot,
|
|
112
|
+
tooDeep,
|
|
113
|
+
atSchema,
|
|
114
|
+
isObject,
|
|
115
|
+
isSnapshot,
|
|
116
|
+
snapshotOf,
|
|
117
|
+
same,
|
|
118
|
+
isFunctionMarker,
|
|
119
|
+
capitalize,
|
|
120
|
+
definitionOf,
|
|
121
|
+
structDefinitionOf,
|
|
122
|
+
};
|