@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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexify/metaschema",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Zero-dependency metadata schema and interface definition language (IDL) for Node.js and browsers — data validation, domain models and TypeScript generation",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alex Dolid <dolid.sasha@gmail.com>",
|
|
@@ -34,6 +34,18 @@
|
|
|
34
34
|
"./locales/uk": {
|
|
35
35
|
"types": "./src/locales/uk.d.ts",
|
|
36
36
|
"default": "./src/locales/uk.js"
|
|
37
|
+
},
|
|
38
|
+
"./generators": {
|
|
39
|
+
"types": "./src/generators/index.d.ts",
|
|
40
|
+
"default": "./src/generators/index.js"
|
|
41
|
+
},
|
|
42
|
+
"./compile": {
|
|
43
|
+
"types": "./src/compile/index.d.ts",
|
|
44
|
+
"default": "./src/compile/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./model": {
|
|
47
|
+
"types": "./src/model/index.d.ts",
|
|
48
|
+
"default": "./src/model/index.js"
|
|
37
49
|
}
|
|
38
50
|
},
|
|
39
51
|
"directories": {
|
|
@@ -73,25 +85,32 @@
|
|
|
73
85
|
"metarhia"
|
|
74
86
|
],
|
|
75
87
|
"devDependencies": {
|
|
88
|
+
"@sinclair/typebox": "^0.34.52",
|
|
76
89
|
"@standard-schema/spec": "^1.1.0",
|
|
77
90
|
"@vercel/analytics": "^2.0.1",
|
|
78
91
|
"@vercel/speed-insights": "^2.0.0",
|
|
92
|
+
"ajv": "^8.20.0",
|
|
93
|
+
"ajv-formats": "^3.0.1",
|
|
79
94
|
"c8": "^12.0.0",
|
|
80
95
|
"esbuild": "^0.28.2",
|
|
81
96
|
"oxfmt": "^0.72.0",
|
|
82
97
|
"oxlint": "^1.75.0",
|
|
83
98
|
"tsd": "^0.33.0",
|
|
84
99
|
"typescript": "^7.0.2",
|
|
85
|
-
"
|
|
100
|
+
"valibot": "^1.5.0",
|
|
101
|
+
"vitepress": "^1.6.4",
|
|
102
|
+
"zod": "^4.6.5"
|
|
86
103
|
},
|
|
87
104
|
"scripts": {
|
|
88
105
|
"test": "node --test tests/unit/*.test.js",
|
|
106
|
+
"test:jit": "node --require ./tests/helpers/jit.js --test tests/unit/*.test.js",
|
|
107
|
+
"test:nojit": "node --disallow-code-generation-from-strings --require ./tests/helpers/jit.js --test tests/unit/*.test.js",
|
|
89
108
|
"test:coverage": "c8 --all --include 'src/**' --check-coverage --lines 98 --statements 98 --branches 90 --functions 100 --reporter text --reporter lcov node --test tests/unit/*.test.js",
|
|
90
109
|
"test:types": "tsd",
|
|
91
|
-
"check:dts": "tsc --noEmit --strict --target es2022 --skipLibCheck false index.d.ts src/locales/en.d.ts src/locales/uk.d.ts",
|
|
92
|
-
"lint": "oxlint src tests scripts bench",
|
|
93
|
-
"format": "oxfmt src tests scripts bench",
|
|
94
|
-
"format:check": "oxfmt --check src tests scripts bench",
|
|
110
|
+
"check:dts": "tsc --noEmit --strict --target es2022 --skipLibCheck false index.d.ts src/locales/en.d.ts src/locales/uk.d.ts src/generators/index.d.ts src/compile/index.d.ts src/model/index.d.ts",
|
|
111
|
+
"lint": "oxlint index.js browser.js src tests scripts bench",
|
|
112
|
+
"format": "oxfmt index.js browser.js src tests scripts bench",
|
|
113
|
+
"format:check": "oxfmt --check index.js browser.js src tests scripts bench",
|
|
95
114
|
"size": "node scripts/size.js",
|
|
96
115
|
"bench": "node bench/bench.js",
|
|
97
116
|
"docs:dev": "vitepress dev docs",
|
package/src/codec.js
ADDED
|
@@ -0,0 +1,544 @@
|
|
|
1
|
+
// What parse and serialize do beyond the check: the defaults, the decoders,
|
|
2
|
+
// the serializers and the stripping of unknown keys, as passes over the built
|
|
3
|
+
// types. `parse` is the check of the schema (whichever backend installed it)
|
|
4
|
+
// between two passes, defaults before and decoders after; `serialize` is one
|
|
5
|
+
// pass the other way. The passes read the same plan the closures walk and
|
|
6
|
+
// validate nothing themselves, so the JIT needs no second generator. A node
|
|
7
|
+
// whose subtree carries nothing for a phase is returned as it is, and a
|
|
8
|
+
// container is copied only when one of its parts changed: the value given is
|
|
9
|
+
// never written to.
|
|
10
|
+
const { hasBrand, define } = require('./util.js');
|
|
11
|
+
const { EMPTY, NONE, issues, isThenable, abandon, pathOf, toDotPath } = require('./issues.js');
|
|
12
|
+
const { createContext } = require('./context.js');
|
|
13
|
+
const { STRUCT, isStruct, aliasOf } = require('./struct.js');
|
|
14
|
+
const { asyncReturn } = require('./errors.js');
|
|
15
|
+
const collections = require('./prototypes/collections.js');
|
|
16
|
+
const { schema: struct } = require('./prototypes/schema.js');
|
|
17
|
+
const { tuple } = require('./prototypes/tuple.js');
|
|
18
|
+
const { union, tableOf } = require('./prototypes/union.js');
|
|
19
|
+
const { reference, embeds } = require('./prototypes/reference.js');
|
|
20
|
+
|
|
21
|
+
// What a subtree carries, as bits; a phase is the bit it applies.
|
|
22
|
+
const DEFAULTS = 1;
|
|
23
|
+
const DECODERS = 2;
|
|
24
|
+
const ENCODERS = 4;
|
|
25
|
+
const STRUCTS = 8;
|
|
26
|
+
const ASYNC = 16;
|
|
27
|
+
// A field with aliases below: the pass after the check visits it only when
|
|
28
|
+
// the check read a field from an alias (`context.renamed`).
|
|
29
|
+
const ALIASES = 32;
|
|
30
|
+
|
|
31
|
+
// The shape of a node: a Schema, a struct (the dictionary of its fields), or
|
|
32
|
+
// a field by the identity of its prototype's compile, as the JIT tells them
|
|
33
|
+
// apart; anything else (a scalar, a custom type) is a leaf.
|
|
34
|
+
const LEAF = 0;
|
|
35
|
+
const SCHEMA = 1;
|
|
36
|
+
const FIELDS = 2;
|
|
37
|
+
const NESTED = 3;
|
|
38
|
+
const LIST = 4;
|
|
39
|
+
const KEYED = 5;
|
|
40
|
+
const TUPLE = 6;
|
|
41
|
+
const UNION = 7;
|
|
42
|
+
const REFERENCE = 8;
|
|
43
|
+
|
|
44
|
+
const SHAPES = new Map([
|
|
45
|
+
[struct.compile, NESTED],
|
|
46
|
+
[collections.array.compile, LIST],
|
|
47
|
+
[collections.object.compile, KEYED],
|
|
48
|
+
[collections.map.compile, KEYED],
|
|
49
|
+
[tuple.compile, TUPLE],
|
|
50
|
+
[union.compile, UNION],
|
|
51
|
+
[reference.compile, REFERENCE],
|
|
52
|
+
]);
|
|
53
|
+
|
|
54
|
+
const shapeOf = (node) => {
|
|
55
|
+
if (hasBrand(node, 'Schema')) return SCHEMA;
|
|
56
|
+
if (isStruct(node)) return FIELDS;
|
|
57
|
+
const shape = SHAPES.get(node.compile);
|
|
58
|
+
return shape === undefined ? LEAF : shape;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const isAsyncFunction = (fn) => typeof fn === 'function' && fn.constructor.name === 'AsyncFunction';
|
|
62
|
+
|
|
63
|
+
// What the passes know about a node, computed on first use and kept apart
|
|
64
|
+
// from the node: its shape, the bits of its subtree, its own decoder and
|
|
65
|
+
// serializer, its children with their infos, the fields of a struct a phase
|
|
66
|
+
// has to visit (`parts`, by phase bit; every field under `strip`), and the
|
|
67
|
+
// reference fields beneath it (their targets are resolved at parse time, so a
|
|
68
|
+
// reference counts as carrying everything).
|
|
69
|
+
const INFO = new WeakMap();
|
|
70
|
+
|
|
71
|
+
const infoOf = (node) => {
|
|
72
|
+
let info = INFO.get(node);
|
|
73
|
+
if (info !== undefined) return info;
|
|
74
|
+
const shape = shapeOf(node);
|
|
75
|
+
// A struct's own functions belong to the node that holds it.
|
|
76
|
+
const own = shape === SCHEMA ? node.options : shape === FIELDS ? EMPTY : node;
|
|
77
|
+
const decode = own.parse || undefined;
|
|
78
|
+
const encode = own.serialize || undefined;
|
|
79
|
+
let bits = decode === undefined ? 0 : DECODERS;
|
|
80
|
+
if (encode !== undefined) bits |= ENCODERS;
|
|
81
|
+
if (isAsyncFunction(own.validate) || isAsyncFunction(decode) || isAsyncFunction(own.checkType)) {
|
|
82
|
+
bits |= ASYNC;
|
|
83
|
+
}
|
|
84
|
+
const children = [];
|
|
85
|
+
const infos = [];
|
|
86
|
+
const refs = [];
|
|
87
|
+
let plan = null;
|
|
88
|
+
let extra = null;
|
|
89
|
+
let table = null;
|
|
90
|
+
let parts = null;
|
|
91
|
+
if (shape === SCHEMA) children.push(node.fields);
|
|
92
|
+
else if (shape === FIELDS) {
|
|
93
|
+
const compiled = node[STRUCT];
|
|
94
|
+
plan = compiled.plan;
|
|
95
|
+
extra = compiled.calculated;
|
|
96
|
+
bits |= STRUCTS;
|
|
97
|
+
} else if (shape === NESTED) children.push(node.schema);
|
|
98
|
+
else if (shape === LIST || shape === KEYED) children.push(node.value);
|
|
99
|
+
else if (shape === TUPLE) {
|
|
100
|
+
for (let index = 0; index < node.value.length; index += 1) children.push(node.value[index]);
|
|
101
|
+
} else if (shape === UNION) {
|
|
102
|
+
const branches = node.union;
|
|
103
|
+
if (node.discriminator !== undefined) table = tableOf(node);
|
|
104
|
+
for (let index = 0; index < branches.length; index += 1) children.push(branches[index]);
|
|
105
|
+
} else if (shape === REFERENCE) {
|
|
106
|
+
bits |= DEFAULTS | DECODERS | ENCODERS | STRUCTS;
|
|
107
|
+
refs.push(node);
|
|
108
|
+
}
|
|
109
|
+
if (plan === null) {
|
|
110
|
+
for (let index = 0; index < children.length; index += 1) {
|
|
111
|
+
const sub = infoOf(children[index]);
|
|
112
|
+
infos.push(sub);
|
|
113
|
+
bits |= sub.bits;
|
|
114
|
+
for (let at = 0; at < sub.refs.length; at += 1) refs.push(sub.refs[at]);
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
// One pass over the plan: the child and its info, and the fields each
|
|
118
|
+
// phase has to visit (every field under strip).
|
|
119
|
+
parts = { [DEFAULTS]: [], [DECODERS]: [], [ENCODERS]: [], [STRUCTS]: [] };
|
|
120
|
+
for (let index = 0; index < plan.length; index += 1) {
|
|
121
|
+
const { type } = plan[index];
|
|
122
|
+
const sub = infoOf(type);
|
|
123
|
+
children.push(type);
|
|
124
|
+
infos.push(sub);
|
|
125
|
+
bits |= sub.bits;
|
|
126
|
+
for (let at = 0; at < sub.refs.length; at += 1) refs.push(sub.refs[at]);
|
|
127
|
+
const defaulted = type.default !== undefined;
|
|
128
|
+
if (defaulted) bits |= DEFAULTS;
|
|
129
|
+
if (defaulted || (sub.bits & DEFAULTS) !== 0) parts[DEFAULTS].push(index);
|
|
130
|
+
// A field with aliases, or with one below, is visited by the pass
|
|
131
|
+
// that decodes, which renames; whether that pass enters the subtree
|
|
132
|
+
// is decided by the bits and the context at the field.
|
|
133
|
+
const aliased = plan[index].aliases.length > 0;
|
|
134
|
+
if (aliased) bits |= ALIASES;
|
|
135
|
+
if (aliased || (sub.bits & (DECODERS | ALIASES)) !== 0) parts[DECODERS].push(index);
|
|
136
|
+
if ((sub.bits & ENCODERS) !== 0) parts[ENCODERS].push(index);
|
|
137
|
+
parts[STRUCTS].push(index);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
info = { shape, bits, decode, encode, children, infos, plan, parts, extra, table, refs };
|
|
141
|
+
INFO.set(node, info);
|
|
142
|
+
return info;
|
|
143
|
+
};
|
|
144
|
+
// Whether a schema has an async function among its validators, decoders and
|
|
145
|
+
// custom checkTypes, the entities it references included: `~standard` then
|
|
146
|
+
// validates through parseAsync.
|
|
147
|
+
const hasAsync = (schema, visited = new Set()) => {
|
|
148
|
+
const info = infoOf(schema);
|
|
149
|
+
if ((info.bits & ASYNC) !== 0) return true;
|
|
150
|
+
visited.add(schema);
|
|
151
|
+
for (const ref of info.refs) {
|
|
152
|
+
const target = ref.root.findReference(ref.one || ref.many);
|
|
153
|
+
if (target !== null && !visited.has(target) && hasAsync(target, visited)) return true;
|
|
154
|
+
}
|
|
155
|
+
return false;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// Writes into an object of the output: a key of the plan or of the value is
|
|
159
|
+
// data, so `__proto__` lands as an own property.
|
|
160
|
+
const putKey = (out, key, value) => {
|
|
161
|
+
if (key === '__proto__') define(out, key, value);
|
|
162
|
+
else out[key] = value;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const putIndex = (out, index, value) => {
|
|
166
|
+
out[index] = value;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
const putEntry = (out, key, value) => {
|
|
170
|
+
out.set(key, value);
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const copy = (value) => ({ ...value });
|
|
174
|
+
|
|
175
|
+
const toMap = (value) => new Map(value);
|
|
176
|
+
|
|
177
|
+
const toSet = (list) => new Set(list);
|
|
178
|
+
|
|
179
|
+
// The output of a container under construction: the container is copied at
|
|
180
|
+
// the first part that changed (`clone`), a part is written with `put`, and
|
|
181
|
+
// the parts that decoded asynchronously are written in once they settle (a
|
|
182
|
+
// decoder that failed resolved to nothing and recorded its issue). A
|
|
183
|
+
// container pushes its key on the path for its parts.
|
|
184
|
+
const enter = (context, key, value, clone, put) => {
|
|
185
|
+
if (key !== undefined) context.path.push(key);
|
|
186
|
+
return { context, key, value, clone, put, out: null, pending: null };
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// A part that is still decoding: only a decoder makes a promise (a thenable
|
|
190
|
+
// it returns is adopted as one, below), and only in an async parse; a value
|
|
191
|
+
// of the data with a `then` of its own is data, never awaited.
|
|
192
|
+
const isPending = (context, value) => context.async !== null && value instanceof Promise;
|
|
193
|
+
|
|
194
|
+
const take = (state, key, result, phase) => {
|
|
195
|
+
if (state.out === null) state.out = state.clone(state.value);
|
|
196
|
+
state.put(state.out, key, result);
|
|
197
|
+
if (phase === DECODERS && isPending(state.context, result)) {
|
|
198
|
+
if (state.pending === null) state.pending = [];
|
|
199
|
+
state.pending.push(key, result);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
// What a promise of the passes resolves to: a box around the value, never
|
|
204
|
+
// the value itself, because a promise adopts a value with a `then` of its
|
|
205
|
+
// own and the value of a program may well have one.
|
|
206
|
+
const boxed = (value) => ({ value });
|
|
207
|
+
|
|
208
|
+
const leave = (state, wrap) => {
|
|
209
|
+
const { context, key, out, pending, put } = state;
|
|
210
|
+
if (key !== undefined) context.path.pop();
|
|
211
|
+
if (out === null) return state.value;
|
|
212
|
+
if (pending === null) return wrap === undefined ? out : wrap(out);
|
|
213
|
+
const promises = [];
|
|
214
|
+
for (let index = 1; index < pending.length; index += 2) promises.push(pending[index]);
|
|
215
|
+
return Promise.all(promises).then((boxes) => {
|
|
216
|
+
for (let index = 0; index < boxes.length; index += 1) {
|
|
217
|
+
put(out, pending[index * 2], boxes[index].value);
|
|
218
|
+
}
|
|
219
|
+
return boxed(wrap === undefined ? out : wrap(out));
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// Runs `fn` with the path of the context set to where a value was met: a
|
|
224
|
+
// promise settles after the walk has unwound.
|
|
225
|
+
const at = (context, path, fn) => {
|
|
226
|
+
const saved = context.path;
|
|
227
|
+
context.path = path;
|
|
228
|
+
try {
|
|
229
|
+
return fn();
|
|
230
|
+
} finally {
|
|
231
|
+
context.path = saved;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
// The decoder of a node over a value whose parts decoded: its exception is an
|
|
236
|
+
// issue at the field, and the result is gone; a promise is awaited where
|
|
237
|
+
// parseAsync allows it and an error in a synchronous parse. Once a decoder
|
|
238
|
+
// failed, none runs: the result is invalid already.
|
|
239
|
+
const decode = (info, node, value, context, key) => {
|
|
240
|
+
const fn = info.decode;
|
|
241
|
+
if (fn === undefined || context.count > 0) return value;
|
|
242
|
+
if (isPending(context, value)) {
|
|
243
|
+
const where = pathOf(context, key);
|
|
244
|
+
return value.then((box) => {
|
|
245
|
+
const out = at(context, where, () => decode(info, node, box.value, context));
|
|
246
|
+
return out instanceof Promise ? out : boxed(out);
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
let out;
|
|
250
|
+
try {
|
|
251
|
+
out = fn.call(node, value);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
issues.exception(context, error, key);
|
|
254
|
+
return undefined;
|
|
255
|
+
}
|
|
256
|
+
if (!isThenable(out)) return out;
|
|
257
|
+
const where = pathOf(context, key);
|
|
258
|
+
if (context.async === null) {
|
|
259
|
+
abandon(out);
|
|
260
|
+
throw asyncReturn('decoder', toDotPath(where, context.root), 'parseAsync');
|
|
261
|
+
}
|
|
262
|
+
// A real Promise of a box from here on, whatever the decoder returned; a
|
|
263
|
+
// decoder that failed resolves to nothing and records its issue.
|
|
264
|
+
return Promise.resolve(out).then(boxed, (error) => {
|
|
265
|
+
if (context.count < context.limit) at(context, where, () => issues.exception(context, error));
|
|
266
|
+
return boxed(undefined);
|
|
267
|
+
});
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const walkLeaf = (info, node, value) => value;
|
|
271
|
+
|
|
272
|
+
const walkInner = (info, node, value, context, key, phase, want) =>
|
|
273
|
+
apply(info.infos[0], info.children[0], value, context, key, phase, want);
|
|
274
|
+
|
|
275
|
+
// Before the check (the defaults, a serialization) a graph may loop through
|
|
276
|
+
// its references; the records on the way down are kept in `seen`, which the
|
|
277
|
+
// caller clears before the check. A loop is marked there: the copies the
|
|
278
|
+
// defaults make cannot close it, so parse checks such a value as it was
|
|
279
|
+
// given, and the check reports the loop. After the check a loop cannot be
|
|
280
|
+
// there.
|
|
281
|
+
const LOOP = Symbol('loop');
|
|
282
|
+
|
|
283
|
+
const looped = (context) => context.seen !== null && context.seen.has(LOOP);
|
|
284
|
+
|
|
285
|
+
const walkSchema = (info, node, value, context, key, phase, want) => {
|
|
286
|
+
if (phase === DECODERS || info.refs.length === 0 || typeof value !== 'object') {
|
|
287
|
+
return walkInner(info, node, value, context, key, phase, want);
|
|
288
|
+
}
|
|
289
|
+
if (context.seen === null) context.seen = new Set();
|
|
290
|
+
const { seen } = context;
|
|
291
|
+
if (seen.has(value)) {
|
|
292
|
+
seen.add(LOOP);
|
|
293
|
+
return value;
|
|
294
|
+
}
|
|
295
|
+
seen.add(value);
|
|
296
|
+
const out = walkInner(info, node, value, context, key, phase, want);
|
|
297
|
+
seen.delete(value);
|
|
298
|
+
return out;
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
// The copy of a struct with every renamed field in the place of its alias:
|
|
302
|
+
// `take` appended the field's key, and a key deleted from the copy would
|
|
303
|
+
// leave the copy in dictionary mode. `renames` is alias, field, alias, ...
|
|
304
|
+
const renameKeys = (out, renames) => {
|
|
305
|
+
const result = {};
|
|
306
|
+
for (const key in out) {
|
|
307
|
+
const found = renames.indexOf(key);
|
|
308
|
+
if (found === -1) putKey(result, key, out[key]);
|
|
309
|
+
else if (found % 2 === 0) putKey(result, renames[found + 1], out[renames[found + 1]]);
|
|
310
|
+
}
|
|
311
|
+
return result;
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
// The fields of a struct: a default fills a missing key before the check, a
|
|
315
|
+
// decoder replaces the value after it. Stripping rebuilds the object from
|
|
316
|
+
// the known keys; otherwise the object is copied when a field changed. A
|
|
317
|
+
// value under an alias of a field is read from there (no default fills the
|
|
318
|
+
// field, and the check sees it there), and the pass after the check writes
|
|
319
|
+
// it under the field's own key, where the alias was; serialize reads the
|
|
320
|
+
// value of the program, which carries the field keys, and never looks.
|
|
321
|
+
const walkFields = (info, fields, value, context, key, phase, want) => {
|
|
322
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) return value;
|
|
323
|
+
const { plan, infos, extra } = info;
|
|
324
|
+
const strip = (want & STRUCTS) !== 0;
|
|
325
|
+
const parts = info.parts[strip ? STRUCTS : phase];
|
|
326
|
+
const state = enter(context, key, value, copy, putKey);
|
|
327
|
+
if (strip) state.out = {};
|
|
328
|
+
let renames = null;
|
|
329
|
+
for (let position = 0; position < parts.length; position += 1) {
|
|
330
|
+
const index = parts[position];
|
|
331
|
+
const entry = plan[index];
|
|
332
|
+
const name = entry.key;
|
|
333
|
+
let from = name;
|
|
334
|
+
let item = entry.own && !Object.hasOwn(value, name) ? undefined : value[name];
|
|
335
|
+
if (item === undefined && phase !== ENCODERS) {
|
|
336
|
+
from = aliasOf(value, entry.aliases, name);
|
|
337
|
+
if (from !== name) item = value[from];
|
|
338
|
+
}
|
|
339
|
+
if (phase === DEFAULTS) {
|
|
340
|
+
if (item === undefined) {
|
|
341
|
+
const fallback = entry.type.default;
|
|
342
|
+
if (fallback === undefined) continue;
|
|
343
|
+
item = typeof fallback === 'function' ? fallback() : fallback;
|
|
344
|
+
take(state, from, item, phase);
|
|
345
|
+
}
|
|
346
|
+
const result = apply(infos[index], entry.type, item, context, from, phase, want);
|
|
347
|
+
if (result !== item) take(state, from, result, phase);
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
const result = apply(infos[index], entry.type, item, context, from, phase, want);
|
|
351
|
+
const renamed = from !== name;
|
|
352
|
+
if (strip ? result !== undefined : renamed || result !== item) take(state, name, result, phase);
|
|
353
|
+
if (renamed && !strip) {
|
|
354
|
+
if (renames === null) renames = [];
|
|
355
|
+
renames.push(from, name);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (strip) {
|
|
359
|
+
for (let index = 0; index < extra.length; index += 1) {
|
|
360
|
+
const name = extra[index];
|
|
361
|
+
if (Object.hasOwn(value, name)) putKey(state.out, name, value[name]);
|
|
362
|
+
}
|
|
363
|
+
} else if (renames !== null) state.out = renameKeys(state.out, renames);
|
|
364
|
+
return leave(state);
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
// The elements of an array, a Set or a tuple, by position; a Set comes back
|
|
368
|
+
// as a Set.
|
|
369
|
+
const walkItems = (children, infos, fixed, value, context, key, phase, want) => {
|
|
370
|
+
const set = !Array.isArray(value);
|
|
371
|
+
if (set && (fixed || value.constructor?.name !== 'Set')) return value;
|
|
372
|
+
const state = enter(context, key, value, Array.from, putIndex);
|
|
373
|
+
let index = 0;
|
|
374
|
+
for (const item of value) {
|
|
375
|
+
const slot = fixed ? index : 0;
|
|
376
|
+
if (slot >= children.length) break;
|
|
377
|
+
const result = apply(infos[slot], children[slot], item, context, index, phase, want);
|
|
378
|
+
if (result !== item) take(state, index, result, phase);
|
|
379
|
+
index += 1;
|
|
380
|
+
}
|
|
381
|
+
return leave(state, set ? toSet : undefined);
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
const walkList = (info, node, value, context, key, phase, want) =>
|
|
385
|
+
walkItems(info.children, info.infos, info.shape === TUPLE, value, context, key, phase, want);
|
|
386
|
+
|
|
387
|
+
// The values of an object or a Map, by key.
|
|
388
|
+
const walkKeyed = (info, node, value, context, key, phase, want) => {
|
|
389
|
+
const map = value.constructor?.name === 'Map';
|
|
390
|
+
if (!map && (typeof value !== 'object' || Array.isArray(value))) return value;
|
|
391
|
+
const child = info.children[0];
|
|
392
|
+
const sub = info.infos[0];
|
|
393
|
+
const state = enter(context, key, value, map ? toMap : copy, map ? putEntry : putKey);
|
|
394
|
+
if (map) {
|
|
395
|
+
for (const pair of value) {
|
|
396
|
+
const name = pair[0];
|
|
397
|
+
const item = pair[1];
|
|
398
|
+
const result = apply(sub, child, item, context, name, phase, want);
|
|
399
|
+
if (result !== item) take(state, name, result, phase);
|
|
400
|
+
}
|
|
401
|
+
} else {
|
|
402
|
+
// The own keys of the object, in order, as the check iterates them.
|
|
403
|
+
for (const name in value) {
|
|
404
|
+
if (!Object.hasOwn(value, name)) continue;
|
|
405
|
+
const item = value[name];
|
|
406
|
+
const result = apply(sub, child, item, context, name, phase, want);
|
|
407
|
+
if (result !== item) take(state, name, result, phase);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return leave(state);
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
// The context for trying the branches of a plain union, one per parse, made
|
|
414
|
+
// at the first plain union met: the policies and the root of the parse, one
|
|
415
|
+
// issue at most, and a list for the promises so that a validator is set aside
|
|
416
|
+
// as the check sets it aside instead of failing the trial.
|
|
417
|
+
const scratchOf = (context) => {
|
|
418
|
+
let scratch = context.scratch;
|
|
419
|
+
if (scratch === null) {
|
|
420
|
+
scratch = createContext(undefined, context.root, context.unknown);
|
|
421
|
+
scratch.references = context.references;
|
|
422
|
+
scratch.limit = 1;
|
|
423
|
+
scratch.async = [];
|
|
424
|
+
context.scratch = scratch;
|
|
425
|
+
}
|
|
426
|
+
return scratch;
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
// Whether a branch accepts the value, as the check decides it: under the
|
|
430
|
+
// path of the parse (the branch pushes the key as the union's check does).
|
|
431
|
+
// The validators the trial set aside are not run: the check of the value
|
|
432
|
+
// picks the branch and runs them for real.
|
|
433
|
+
const accepts = (branch, value, scratch, path, key) => {
|
|
434
|
+
scratch.issues = NONE;
|
|
435
|
+
scratch.count = 0;
|
|
436
|
+
scratch.path = path;
|
|
437
|
+
const list = scratch.async;
|
|
438
|
+
list.length = 0;
|
|
439
|
+
branch.check(value, scratch, key);
|
|
440
|
+
for (let index = 0; index < list.length; index += 1) {
|
|
441
|
+
if (list[index].promise !== null) abandon(list[index].promise);
|
|
442
|
+
}
|
|
443
|
+
return scratch.count === 0;
|
|
444
|
+
};
|
|
445
|
+
|
|
446
|
+
// The branch of a union a value belongs to: by the discriminator, or the
|
|
447
|
+
// first branch whose check records nothing, as the check picks it. Before
|
|
448
|
+
// the check a branch is tried with its own defaults in place, and the value
|
|
449
|
+
// with those defaults is what parse goes on with.
|
|
450
|
+
const walkUnion = (info, node, value, context, key, phase, want) => {
|
|
451
|
+
const { children, infos, table } = info;
|
|
452
|
+
if (table !== null) {
|
|
453
|
+
if (typeof value !== 'object') return value;
|
|
454
|
+
const index = table.get(value[node.discriminator]);
|
|
455
|
+
if (index === undefined) return value;
|
|
456
|
+
return apply(infos[index], children[index], value, context, key, phase, want);
|
|
457
|
+
}
|
|
458
|
+
const scratch = scratchOf(context);
|
|
459
|
+
for (let index = 0; index < children.length; index += 1) {
|
|
460
|
+
const branch = children[index];
|
|
461
|
+
const sub = infos[index];
|
|
462
|
+
const prepared =
|
|
463
|
+
phase === DEFAULTS ? apply(sub, branch, value, context, key, phase, want) : value;
|
|
464
|
+
if (!accepts(branch, prepared, scratch, context.path, key)) continue;
|
|
465
|
+
return phase === DEFAULTS ? prepared : apply(sub, branch, value, context, key, phase, want);
|
|
466
|
+
}
|
|
467
|
+
return value;
|
|
468
|
+
};
|
|
469
|
+
|
|
470
|
+
// A reference resolved at parse time, as the check resolves it: an id stays
|
|
471
|
+
// an id, a record goes through its entity, and a record deeper than the
|
|
472
|
+
// check allows is left as it is for the check to report.
|
|
473
|
+
const walkReference = (info, node, value, context, key, phase, want) => {
|
|
474
|
+
const { one, many, root, embed } = node;
|
|
475
|
+
const target = root.findReference(one || many);
|
|
476
|
+
if (target === null || !embeds(target, embed, context.references)) return value;
|
|
477
|
+
if (context.depth >= context.maxDepth) return value;
|
|
478
|
+
const sub = infoOf(target);
|
|
479
|
+
context.depth += 1;
|
|
480
|
+
let out = value;
|
|
481
|
+
if (one) out = apply(sub, target, value, context, key, phase, want);
|
|
482
|
+
else if (Array.isArray(value)) {
|
|
483
|
+
// The target as the one child of the list, kept on the info from one
|
|
484
|
+
// visit to the next (it changes only when the namespaces do).
|
|
485
|
+
if (info.children[0] !== target) {
|
|
486
|
+
info.children[0] = target;
|
|
487
|
+
info.infos[0] = sub;
|
|
488
|
+
}
|
|
489
|
+
out = walkItems(info.children, info.infos, false, value, context, key, phase, want);
|
|
490
|
+
}
|
|
491
|
+
context.depth -= 1;
|
|
492
|
+
return out;
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
const WALKERS = [
|
|
496
|
+
walkLeaf,
|
|
497
|
+
walkSchema,
|
|
498
|
+
walkFields,
|
|
499
|
+
walkInner,
|
|
500
|
+
walkList,
|
|
501
|
+
walkKeyed,
|
|
502
|
+
walkList,
|
|
503
|
+
walkUnion,
|
|
504
|
+
walkReference,
|
|
505
|
+
];
|
|
506
|
+
|
|
507
|
+
// The bits a pass wants of a subtree, decided once per pass: its own, and
|
|
508
|
+
// for the pass after the check the structs (under `strip`) and the fields
|
|
509
|
+
// with aliases (after a check that read one).
|
|
510
|
+
const wantOf = (phase, context) => {
|
|
511
|
+
let want = phase;
|
|
512
|
+
if (phase === DECODERS) {
|
|
513
|
+
if (context.unknown === 'strip') want |= STRUCTS;
|
|
514
|
+
if (context.renamed) want |= ALIASES;
|
|
515
|
+
}
|
|
516
|
+
return want;
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
// One pass over a value from a node whose info is known, by the shape of
|
|
520
|
+
// each node. A serializer runs before the parts of its value, a decoder
|
|
521
|
+
// after them; a subtree without the bits the pass wants is returned as it
|
|
522
|
+
// is, which is the one test a value nothing applies to costs.
|
|
523
|
+
const apply = (info, node, value, context, key, phase, want) => {
|
|
524
|
+
if (value == null) return value;
|
|
525
|
+
if ((info.bits & want) === 0) return value;
|
|
526
|
+
let input = value;
|
|
527
|
+
if (phase === ENCODERS && info.encode !== undefined) {
|
|
528
|
+
input = info.encode.call(node, value);
|
|
529
|
+
if (input == null) return input;
|
|
530
|
+
}
|
|
531
|
+
const out = WALKERS[info.shape](info, node, input, context, key, phase, want);
|
|
532
|
+
return phase === DECODERS ? decode(info, node, out, context, key) : out;
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
module.exports = {
|
|
536
|
+
PREPARE: DEFAULTS,
|
|
537
|
+
FINISH: DECODERS,
|
|
538
|
+
SERIALIZE: ENCODERS,
|
|
539
|
+
infoOf,
|
|
540
|
+
apply,
|
|
541
|
+
wantOf,
|
|
542
|
+
looped,
|
|
543
|
+
hasAsync,
|
|
544
|
+
};
|