@alexify/metaschema 3.2.0 → 3.4.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 +307 -1
- package/README.md +173 -89
- package/SECURITY.md +24 -1
- package/index.d.ts +601 -29
- package/package.json +28 -3
- package/src/checks.js +789 -0
- package/src/codec.js +201 -46
- package/src/coerce/index.d.ts +34 -0
- package/src/coerce/index.js +385 -0
- package/src/compile/emit.js +87 -28
- package/src/compile/index.js +9 -4
- package/src/context.js +6 -1
- package/src/definition/index.d.ts +19 -0
- package/src/definition/index.js +27 -0
- package/src/derive/index.d.ts +86 -0
- package/src/derive/index.js +273 -0
- package/src/errors.js +46 -4
- package/src/formats/index.d.ts +20 -8
- package/src/formats/index.js +450 -39
- package/src/generators/fromjsonschema.js +21 -2
- package/src/generators/index.js +1 -1
- package/src/generators/jsonschema.js +90 -21
- package/src/generators/typescript.js +11 -0
- package/src/index.js +3 -2
- package/src/issues.js +9 -7
- package/src/kinds.js +15 -3
- package/src/lint.js +38 -0
- package/src/locales/en.js +6 -0
- package/src/locales/uk.js +6 -0
- package/src/metadata.js +6 -24
- package/src/model/data.js +67 -10
- package/src/model/diff.js +7 -4
- package/src/model/index.js +1 -1
- package/src/model/infer.js +10 -3
- package/src/model/merge.js +16 -9
- package/src/model/restore.js +52 -7
- package/src/model/sample.js +28 -4
- package/src/model.js +54 -56
- package/src/mongodb/bson.js +130 -0
- package/src/mongodb/document.js +217 -0
- package/src/mongodb/index.d.ts +133 -0
- package/src/mongodb/index.js +24 -0
- package/src/mongodb/indexes.js +79 -0
- package/src/mongodb/update.js +258 -0
- package/src/preprocessor.js +1 -2
- package/src/prototypes/abstract.js +41 -67
- package/src/prototypes/collections.js +3 -90
- package/src/prototypes/flags.js +34 -0
- package/src/prototypes/json.js +0 -11
- package/src/prototypes/reference.js +0 -76
- package/src/prototypes/scalars.js +17 -96
- package/src/prototypes/schema.js +1 -10
- package/src/prototypes/tuple.js +0 -26
- package/src/prototypes/union.js +12 -82
- package/src/rules.js +33 -157
- package/src/schema.js +105 -220
- package/src/snapshot.js +216 -41
- package/src/stream/index.js +1 -1
- package/src/struct.js +6 -91
- package/src/types.js +43 -6
- package/src/util.js +43 -0
- package/src/validation.js +313 -0
package/src/schema.js
CHANGED
|
@@ -1,95 +1,46 @@
|
|
|
1
|
-
const {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
const {
|
|
2
|
+
BRAND,
|
|
3
|
+
INSPECT,
|
|
4
|
+
RUN,
|
|
5
|
+
HOOK,
|
|
6
|
+
TRACKED,
|
|
7
|
+
COERCES,
|
|
8
|
+
CODEC,
|
|
9
|
+
STANDARD,
|
|
10
|
+
hasBrand,
|
|
11
|
+
} = require('./util.js');
|
|
5
12
|
const { TYPES } = require('./types.js');
|
|
6
13
|
const { Preprocessor } = require('./preprocessor.js');
|
|
7
14
|
const { SchemaMetadata } = require('./metadata.js');
|
|
8
|
-
const {
|
|
9
|
-
const {
|
|
10
|
-
const {
|
|
11
|
-
const {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
// The validation of a schema inside an existing context: `check` starts one,
|
|
25
|
-
// and a reference field checks its target entity within the context of the
|
|
26
|
-
// outer check, so the error limit and the cycle detection span the whole
|
|
27
|
-
// value. The schema-level validate runs last, and only on a value whose
|
|
28
|
-
// fields all passed, so it can rely on their shape. Built once per schema,
|
|
29
|
-
// as Schema[RUN].
|
|
30
|
-
const compileSchema = (schema) => {
|
|
31
|
-
const { fields, options } = schema;
|
|
32
|
-
const run = isStruct(fields) ? checkOf(fields) : fields.check;
|
|
33
|
-
return (value, context, key) => {
|
|
34
|
-
const before = context.count;
|
|
35
|
-
run(value, context, key);
|
|
36
|
-
const { validate } = options;
|
|
37
|
-
if (validate && context.count === before) {
|
|
38
|
-
const nested = key !== undefined;
|
|
39
|
-
if (nested) context.path.push(key);
|
|
40
|
-
runValidate(schema, validate, value, context);
|
|
41
|
-
if (nested) context.path.pop();
|
|
42
|
-
}
|
|
15
|
+
const { SchemaDefinitionError, noValidation } = require('./errors.js');
|
|
16
|
+
const { createStruct } = require('./struct.js');
|
|
17
|
+
const { BUILD } = require('./prototypes/abstract.js');
|
|
18
|
+
const { schemaSnapshotOf } = require('./snapshot.js');
|
|
19
|
+
|
|
20
|
+
// The check of a schema until it is first used: it builds the closures
|
|
21
|
+
// (`BUILD.schema`, the validation half's), takes their place and runs them.
|
|
22
|
+
// A schema built to be read (rendered, written as a snapshot) never builds
|
|
23
|
+
// them; one the JIT replaced before its first use (`register()` of
|
|
24
|
+
// `./compile`) is not touched.
|
|
25
|
+
const lazyRun = (schema) => {
|
|
26
|
+
const run = (value, context, key) => {
|
|
27
|
+
const check = BUILD.schema(schema);
|
|
28
|
+
if (schema[RUN] === run) schema[RUN] = check;
|
|
29
|
+
return check(value, context, key);
|
|
43
30
|
};
|
|
31
|
+
return run;
|
|
44
32
|
};
|
|
45
33
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
// What parse returns: the output only for a valid value, the issues always.
|
|
52
|
-
const parsed = (context, output) => {
|
|
53
|
-
finalize(context.issues, context.messages);
|
|
54
|
-
const valid = context.count === 0;
|
|
55
|
-
return { valid, value: valid ? output : undefined, issues: context.issues };
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
// The result of parse read as Standard Schema does.
|
|
59
|
-
const standardize = (result) =>
|
|
60
|
-
result.valid ? { value: result.value } : { issues: result.issues };
|
|
61
|
-
|
|
62
|
-
// The walk of an async check: a throw out of it (a getter of the value that
|
|
63
|
-
// throws, the engine's limit on a pattern) leaves the promises the walk
|
|
64
|
-
// queued observed, so that none of them rejects unhandled.
|
|
65
|
-
const walkAsync = (schema, value, context) => {
|
|
66
|
-
try {
|
|
67
|
-
schema[RUN](value, context);
|
|
68
|
-
} catch (error) {
|
|
69
|
-
release(context.async, 0);
|
|
70
|
-
throw error;
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
|
|
34
|
+
// The half of the core that builds a schema and reads it: its fields and
|
|
35
|
+
// metadata, its JSON and its snapshot. The validation half (`validation.js`,
|
|
36
|
+
// which the core entry loads) installs `check`, `parse`, `serialize` and
|
|
37
|
+
// their kin, `~standard` and the warnings of the lint on this class; until
|
|
38
|
+
// then they say so (`./definition` alone).
|
|
74
39
|
class Schema extends SchemaMetadata {
|
|
75
40
|
// The merged type table of the attached namespaces, rebuilt only when they
|
|
76
41
|
// change.
|
|
77
42
|
#types = null;
|
|
78
43
|
|
|
79
|
-
// Whether the root value joins the cycle detection (`context.source`):
|
|
80
|
-
// only a struct with references can reach it again.
|
|
81
|
-
#tracked = false;
|
|
82
|
-
|
|
83
|
-
// The lint of the definition, run on first use: a schema built in a hot
|
|
84
|
-
// path never pays for it.
|
|
85
|
-
#warnings = null;
|
|
86
|
-
|
|
87
|
-
// The Standard Schema props, built on first use.
|
|
88
|
-
#standard = null;
|
|
89
|
-
|
|
90
|
-
// What the passes of parse and serialize know about the schema, on first use.
|
|
91
|
-
#codec = null;
|
|
92
|
-
|
|
93
44
|
static from(source, namespaces) {
|
|
94
45
|
return new Schema('', source, namespaces);
|
|
95
46
|
}
|
|
@@ -128,43 +79,20 @@ class Schema extends SchemaMetadata {
|
|
|
128
79
|
const combined = { ...fields, ...extras };
|
|
129
80
|
this.fields = createStruct(combined, preprocessor);
|
|
130
81
|
}
|
|
131
|
-
|
|
132
|
-
|
|
82
|
+
// What the validation half reads (util.js): whether the root value joins
|
|
83
|
+
// the cycle detection (only a struct with references can reach it
|
|
84
|
+
// again), whether parse coerces unless the call says otherwise (the
|
|
85
|
+
// `coerce` of the kind metadata, read once: an own property would shadow
|
|
86
|
+
// the method the `./coerce` subpath installs), and its caches.
|
|
87
|
+
this[TRACKED] = isSchemaType && this.relations.size > 0;
|
|
88
|
+
this[COERCES] = Object.hasOwn(this.custom, 'coerce') && this.custom.coerce === true;
|
|
89
|
+
this[CODEC] = null;
|
|
90
|
+
this[STANDARD] = null;
|
|
91
|
+
this[RUN] = lazyRun(this);
|
|
133
92
|
const hook = Schema[HOOK];
|
|
134
93
|
if (hook !== undefined) hook(this);
|
|
135
94
|
}
|
|
136
95
|
|
|
137
|
-
// What is not wrong enough to throw: `Warning [code]: text` strings.
|
|
138
|
-
get warnings() {
|
|
139
|
-
if (this.#warnings === null) this.#warnings = lintSchema(this);
|
|
140
|
-
return this.#warnings;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// Standard Schema v1 (https://standardschema.dev): the interface tRPC,
|
|
144
|
-
// TanStack Form, Hono and the other consumers of the specification validate
|
|
145
|
-
// through. `validate` is `parse` with the policies of the schema: the value
|
|
146
|
-
// that comes back has the defaults and the decoders applied (the value
|
|
147
|
-
// itself when there was nothing to apply), and the issues of an invalid one
|
|
148
|
-
// are the result's own (a message and a path of keys each). A schema with
|
|
149
|
-
// an async validator or decoder validates through parseAsync, as the
|
|
150
|
-
// specification allows. The vendor-specific `libraryOptions` of a call are
|
|
151
|
-
// the options of `parse`. An accessor of the prototype stays out of toJSON,
|
|
152
|
-
// inspect and Object.keys, and `validate` closes over the schema because a
|
|
153
|
-
// consumer calls it unbound. The `./generators` subpath adds the
|
|
154
|
-
// `jsonSchema` converter of Standard JSON Schema to the props.
|
|
155
|
-
get '~standard'() {
|
|
156
|
-
if (this.#standard === null) {
|
|
157
|
-
const async = hasAsync(this);
|
|
158
|
-
const validate = (value, options) => {
|
|
159
|
-
const libraryOptions = options == null ? undefined : options.libraryOptions;
|
|
160
|
-
if (async) return this.parseAsync(value, libraryOptions).then(standardize);
|
|
161
|
-
return standardize(this.parse(value, libraryOptions));
|
|
162
|
-
};
|
|
163
|
-
this.#standard = { version: 1, vendor: 'alexify.metaschema', validate };
|
|
164
|
-
}
|
|
165
|
-
return this.#standard;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
96
|
get types() {
|
|
169
97
|
if (this.namespaces.size === 0) return TYPES;
|
|
170
98
|
if (this.#types === null) {
|
|
@@ -175,115 +103,16 @@ class Schema extends SchemaMetadata {
|
|
|
175
103
|
return this.#types;
|
|
176
104
|
}
|
|
177
105
|
|
|
178
|
-
//
|
|
179
|
-
//
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
const { name, references } = this;
|
|
183
|
-
for (const ref of references) {
|
|
184
|
-
if (isFirstUpper(ref)) {
|
|
185
|
-
const entity = this.findReference(ref);
|
|
186
|
-
if (!entity) {
|
|
187
|
-
warn.push(warning('missing-reference', `"${ref}" referenced by "${name}" is not found`));
|
|
188
|
-
}
|
|
189
|
-
} else if (!this.types[ref]) {
|
|
190
|
-
warn.push(warning('missing-type', `type "${ref}" is not found in "${name}"`));
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
return warn;
|
|
194
|
-
}
|
|
195
|
-
|
|
106
|
+
// An entity of the attached models, else the schema itself: a schema that
|
|
107
|
+
// names itself (`parent: '?Category'`) needs no model. The models come
|
|
108
|
+
// first, so a schema with the name of one of their entities (a schema
|
|
109
|
+
// derived from it keeps the name) still reaches that entity.
|
|
196
110
|
findReference(name) {
|
|
197
111
|
for (const ns of this.namespaces) {
|
|
198
112
|
const entity = ns.entities.get(name);
|
|
199
113
|
if (entity) return entity;
|
|
200
114
|
}
|
|
201
|
-
return null;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// Validates a value: every problem is collected as an issue, and the result
|
|
205
|
-
// never throws because of the value. `options`: root (the label of the
|
|
206
|
-
// error lines, the schema name by default), maxErrors, unknown ('reject'
|
|
207
|
-
// or 'ignore' keys the schema does not have), references, messages (a
|
|
208
|
-
// locale). A validator that returns a promise is an error here.
|
|
209
|
-
// `createContext` is called directly in every method here, never through a
|
|
210
|
-
// helper: V8 inlines the allocation into `check` that way (the context is
|
|
211
|
-
// the one allocation of a valid check), and a method in between cost 7%.
|
|
212
|
-
check(source, options) {
|
|
213
|
-
const context = createContext(options, this.name, this.unknown);
|
|
214
|
-
if (this.#tracked) context.source = source;
|
|
215
|
-
this[RUN](source, context);
|
|
216
|
-
return resultOf(context);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
// The same check, with the validators that return a promise in flight
|
|
220
|
-
// together and settled after the walk, in field order.
|
|
221
|
-
async checkAsync(source, options) {
|
|
222
|
-
const context = createContext(options, this.name, this.unknown);
|
|
223
|
-
context.async = [];
|
|
224
|
-
if (this.#tracked) context.source = source;
|
|
225
|
-
walkAsync(this, source, context);
|
|
226
|
-
if (context.async.length > 0) await settle(context);
|
|
227
|
-
return resultOf(context);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
#pass(phase, source, context) {
|
|
231
|
-
if (this.#codec === null) this.#codec = infoOf(this);
|
|
232
|
-
return apply(this.#codec, this, source, context, undefined, phase, wantOf(phase, context));
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// The defaults of the schema over a value, before its check; the objects
|
|
236
|
-
// met on the way down are not the check's, and a value that loops through
|
|
237
|
-
// its references is checked as it was given.
|
|
238
|
-
#prepare(source, context) {
|
|
239
|
-
const prepared = this.#pass(PREPARE, source, context);
|
|
240
|
-
const value = looped(context) ? source : prepared;
|
|
241
|
-
context.seen = null;
|
|
242
|
-
if (this.#tracked) context.source = value;
|
|
243
|
-
return value;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// Validates a value and builds the value of the program from it: the
|
|
247
|
-
// defaults fill the missing keys, the check runs, and a valid value goes
|
|
248
|
-
// through `unknown: 'strip'`, the decoders of its fields (the parts of a
|
|
249
|
-
// value before the value) and the schema's own `parse`. `{ valid, value,
|
|
250
|
-
// issues }`: the value is undefined when invalid, and the value given when
|
|
251
|
-
// there was nothing to apply; the value given is never written to.
|
|
252
|
-
parse(source, options) {
|
|
253
|
-
const context = createContext(options, this.name, this.unknown, true);
|
|
254
|
-
const value = this.#prepare(source, context);
|
|
255
|
-
this[RUN](value, context);
|
|
256
|
-
const output = context.count === 0 ? this.#pass(FINISH, value, context) : undefined;
|
|
257
|
-
return parsed(context, output);
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
async parseAsync(source, options) {
|
|
261
|
-
const context = createContext(options, this.name, this.unknown, true);
|
|
262
|
-
context.async = [];
|
|
263
|
-
const value = this.#prepare(source, context);
|
|
264
|
-
walkAsync(this, value, context);
|
|
265
|
-
if (context.async.length > 0) await settle(context);
|
|
266
|
-
let output;
|
|
267
|
-
if (context.count === 0) {
|
|
268
|
-
// Only a promise of the pass is awaited: a thenable in the value is data.
|
|
269
|
-
output = this.#pass(FINISH, value, context);
|
|
270
|
-
if (output instanceof Promise) output = (await output).value;
|
|
271
|
-
}
|
|
272
|
-
return parsed(context, output);
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// The other direction: the schema's own `serialize`, then the serializers
|
|
276
|
-
// of the fields, from the value down to its parts. `{ value }`, or with
|
|
277
|
-
// `options.check` the verdict of `check` over the result as well.
|
|
278
|
-
serialize(source, options) {
|
|
279
|
-
const context = createContext(options, this.name, this.unknown);
|
|
280
|
-
const value = this.#pass(SERIALIZE, source, context);
|
|
281
|
-
if (options === undefined || options.check !== true) return { value };
|
|
282
|
-
context.seen = null;
|
|
283
|
-
if (this.#tracked) context.source = value;
|
|
284
|
-
this[RUN](value, context);
|
|
285
|
-
finalize(context.issues, context.messages);
|
|
286
|
-
return { valid: context.count === 0, value, issues: context.issues };
|
|
115
|
+
return name === this.name && name !== '' ? this : null;
|
|
287
116
|
}
|
|
288
117
|
|
|
289
118
|
// The types and the Standard Schema props depend on the namespaces (the
|
|
@@ -292,13 +121,13 @@ class Schema extends SchemaMetadata {
|
|
|
292
121
|
attach(...namespaces) {
|
|
293
122
|
for (const ns of namespaces) this.namespaces.add(ns);
|
|
294
123
|
this.#types = null;
|
|
295
|
-
this
|
|
124
|
+
this[STANDARD] = null;
|
|
296
125
|
}
|
|
297
126
|
|
|
298
127
|
detach(...namespaces) {
|
|
299
128
|
for (const ns of namespaces) this.namespaces.delete(ns);
|
|
300
129
|
this.#types = null;
|
|
301
|
-
this
|
|
130
|
+
this[STANDARD] = null;
|
|
302
131
|
}
|
|
303
132
|
|
|
304
133
|
toString() {
|
|
@@ -310,12 +139,68 @@ class Schema extends SchemaMetadata {
|
|
|
310
139
|
return hasBrand(fields, 'Struct') ? { ...fields } : fields.toJSON();
|
|
311
140
|
}
|
|
312
141
|
|
|
142
|
+
// The schema as plain JSON: the snapshot of a model (format 1) holding the
|
|
143
|
+
// schema and every entity it reaches through its references, in
|
|
144
|
+
// dependency order, with the name of the schema as `root`.
|
|
145
|
+
toSnapshot() {
|
|
146
|
+
return schemaSnapshotOf(this);
|
|
147
|
+
}
|
|
148
|
+
|
|
313
149
|
[INSPECT](depth, options, inspect) {
|
|
314
150
|
const label = this.name ? `Schema(${this.name})` : 'Schema';
|
|
315
151
|
return `${label} ${inspect(this.toJSON(), options)}`;
|
|
316
152
|
}
|
|
317
153
|
}
|
|
318
154
|
|
|
155
|
+
// Until the validation half is installed, a schema builds and reads, and a
|
|
156
|
+
// call that validates says what is missing. A stub looks the method up when
|
|
157
|
+
// it is called, so one a program took before the core was loaded (`const {
|
|
158
|
+
// check } = schema`, `check.bind(schema)`) calls the method installed since.
|
|
159
|
+
// Named as the methods are, for the stack.
|
|
160
|
+
const STUBS = {
|
|
161
|
+
checkConsistency(...args) {
|
|
162
|
+
return late(this, 'checkConsistency', args);
|
|
163
|
+
},
|
|
164
|
+
check(...args) {
|
|
165
|
+
return late(this, 'check', args);
|
|
166
|
+
},
|
|
167
|
+
async checkAsync(...args) {
|
|
168
|
+
return late(this, 'checkAsync', args);
|
|
169
|
+
},
|
|
170
|
+
parse(...args) {
|
|
171
|
+
return late(this, 'parse', args);
|
|
172
|
+
},
|
|
173
|
+
async parseAsync(...args) {
|
|
174
|
+
return late(this, 'parseAsync', args);
|
|
175
|
+
},
|
|
176
|
+
serialize(...args) {
|
|
177
|
+
return late(this, 'serialize', args);
|
|
178
|
+
},
|
|
179
|
+
validate(...args) {
|
|
180
|
+
return late(this, 'validate', args);
|
|
181
|
+
},
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const late = (schema, name, args) => {
|
|
185
|
+
const method = Schema.prototype[name];
|
|
186
|
+
if (method === STUBS[name]) throw noValidation(name);
|
|
187
|
+
return method.apply(schema, args);
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
for (const name of Object.keys(STUBS)) {
|
|
191
|
+
Object.defineProperty(Schema.prototype, name, {
|
|
192
|
+
value: STUBS[name],
|
|
193
|
+
writable: true,
|
|
194
|
+
configurable: true,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
Object.defineProperty(Schema.prototype, 'warnings', {
|
|
198
|
+
get() {
|
|
199
|
+
throw noValidation('warnings');
|
|
200
|
+
},
|
|
201
|
+
configurable: true,
|
|
202
|
+
});
|
|
203
|
+
|
|
319
204
|
Schema.prototype[BRAND] = 'Schema';
|
|
320
205
|
|
|
321
206
|
module.exports = { Schema };
|