@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
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DeriveKeyOf,
|
|
3
|
+
DeriveOptions,
|
|
4
|
+
DerivedSchema,
|
|
5
|
+
ExtendDefinition,
|
|
6
|
+
ExtendedSchema,
|
|
7
|
+
KeyofDefinition,
|
|
8
|
+
OmitDefinition,
|
|
9
|
+
PartialDefinition,
|
|
10
|
+
PartialOptions,
|
|
11
|
+
PickDefinition,
|
|
12
|
+
RequiredDefinition,
|
|
13
|
+
RequiredOptions,
|
|
14
|
+
Schema,
|
|
15
|
+
StructGuardOf,
|
|
16
|
+
} from '../../index.js';
|
|
17
|
+
|
|
18
|
+
// Schemas made from a schema of a struct. Each builds a new Schema from the
|
|
19
|
+
// definition the source was built from: the source is never changed. A
|
|
20
|
+
// stored kind or a projection becomes a struct (without the id of the kind,
|
|
21
|
+
// unless the call names it), a memory kind keeps its metadata, the name is
|
|
22
|
+
// the source's unless `options.name`. A key the schema has no field of is
|
|
23
|
+
// `ERR_UNKNOWN_FIELD`, located in the schema; anything but a Schema of a
|
|
24
|
+
// struct is `ERR_INVALID_TARGET`.
|
|
25
|
+
|
|
26
|
+
// The fields named, in the order of the schema. The schema's own functions
|
|
27
|
+
// do not come along.
|
|
28
|
+
export function pick<
|
|
29
|
+
D extends string | object,
|
|
30
|
+
const K extends readonly DeriveKeyOf<D>[],
|
|
31
|
+
const O extends DeriveOptions = {},
|
|
32
|
+
>(
|
|
33
|
+
schema: Schema<D> & StructGuardOf<D>,
|
|
34
|
+
keys: K,
|
|
35
|
+
options?: O,
|
|
36
|
+
): DerivedSchema<D, PickDefinition<D, K[number], O>>;
|
|
37
|
+
|
|
38
|
+
// Every field but the ones named.
|
|
39
|
+
export function omit<
|
|
40
|
+
D extends string | object,
|
|
41
|
+
const K extends readonly DeriveKeyOf<D>[],
|
|
42
|
+
const O extends DeriveOptions = {},
|
|
43
|
+
>(
|
|
44
|
+
schema: Schema<D> & StructGuardOf<D>,
|
|
45
|
+
keys: K,
|
|
46
|
+
options?: O,
|
|
47
|
+
): DerivedSchema<D, OmitDefinition<D, K[number], O>>;
|
|
48
|
+
|
|
49
|
+
// Every field (or the ones in `options.keys`) optional, without its default
|
|
50
|
+
// unless `defaults: true`; `deep` goes into nested structs, not into
|
|
51
|
+
// collections, tuples, unions or references.
|
|
52
|
+
export function partial<
|
|
53
|
+
D extends string | object,
|
|
54
|
+
const O extends PartialOptions<DeriveKeyOf<D>> = {},
|
|
55
|
+
>(schema: Schema<D> & StructGuardOf<D>, options?: O): DerivedSchema<D, PartialDefinition<D, O>>;
|
|
56
|
+
|
|
57
|
+
// Every field (or the ones in `options.keys`) required; the defaults stay.
|
|
58
|
+
export function required<
|
|
59
|
+
D extends string | object,
|
|
60
|
+
const O extends RequiredOptions<DeriveKeyOf<D>> = {},
|
|
61
|
+
>(schema: Schema<D> & StructGuardOf<D>, options?: O): DerivedSchema<D, RequiredDefinition<D, O>>;
|
|
62
|
+
|
|
63
|
+
// The fields of the extension (an object of fields, or a Schema of a
|
|
64
|
+
// struct) over the schema's: one of the same name takes the place of the
|
|
65
|
+
// schema's, the others come after. `merge` of `./model` unifies instead. An
|
|
66
|
+
// extension whose definition is not known (an entity of a model) gives a
|
|
67
|
+
// `Schema` of an unknown one, as a source whose definition is not known does.
|
|
68
|
+
export function extend<
|
|
69
|
+
D extends string | object,
|
|
70
|
+
const X extends object,
|
|
71
|
+
const O extends DeriveOptions = {},
|
|
72
|
+
>(
|
|
73
|
+
schema: Schema<D> & StructGuardOf<D>,
|
|
74
|
+
extension: X,
|
|
75
|
+
options?: O,
|
|
76
|
+
): ExtendedSchema<D, X, ExtendDefinition<D, X, O>>;
|
|
77
|
+
|
|
78
|
+
// The names of the fields a value carries, as a schema of an enum.
|
|
79
|
+
export function keyof<D extends string | object>(
|
|
80
|
+
schema: Schema<D> & StructGuardOf<D>,
|
|
81
|
+
options?: DeriveOptions,
|
|
82
|
+
): DerivedSchema<D, KeyofDefinition<D>>;
|
|
83
|
+
|
|
84
|
+
// Adds the six as methods of Schema (`schema.omit(['id']).partial()`), not
|
|
85
|
+
// enumerable. Once per process; importing the subpath registers nothing.
|
|
86
|
+
export function register(): void;
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
// The `./derive` subpath: schemas made from a schema. A subset of its fields
|
|
2
|
+
// (`pick`, `omit`), its fields optional or required (`partial`, `required`),
|
|
3
|
+
// fields of its own over the ones it has (`extend`), the names of its fields
|
|
4
|
+
// as an enum (`keyof`). The result is a new Schema built from the definition
|
|
5
|
+
// the source was built from (what `merge` of the `./model` subpath reads
|
|
6
|
+
// too), so the source is never touched and every rule of a field comes along.
|
|
7
|
+
// `register()` installs the functions as methods of Schema, so they chain.
|
|
8
|
+
const { hasBrand, define, formatters } = require('../util.js');
|
|
9
|
+
const { isFirstUpper, firstKey } = require('../metautil.js');
|
|
10
|
+
const { Schema } = require('../schema.js');
|
|
11
|
+
const { isStruct } = require('../struct.js');
|
|
12
|
+
const { idFieldOf } = require('../kinds.js');
|
|
13
|
+
const { SchemaDefinitionError, invalidTarget, invalidOption } = require('../errors.js');
|
|
14
|
+
const {
|
|
15
|
+
structDefinitionOf,
|
|
16
|
+
metadataOf,
|
|
17
|
+
structDefinition,
|
|
18
|
+
atSchema,
|
|
19
|
+
namespacesOf,
|
|
20
|
+
} = require('../model/data.js');
|
|
21
|
+
|
|
22
|
+
// The functions of a nested struct that are its own: written for the whole
|
|
23
|
+
// struct, they do not hold for a part of it.
|
|
24
|
+
const CODECS = ['validate', 'parse', 'serialize'];
|
|
25
|
+
|
|
26
|
+
const sourceOf = (schema) => {
|
|
27
|
+
if (!hasBrand(schema, 'Schema') || !isStruct(schema.fields)) {
|
|
28
|
+
throw invalidTarget('A Schema of a struct is expected');
|
|
29
|
+
}
|
|
30
|
+
return schema;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// The options of a call are the call's (ERR_INVALID_OPTION); the metadata
|
|
34
|
+
// among them is a definition, and a wrong one is a definition error.
|
|
35
|
+
const optionsOf = (options = {}) => {
|
|
36
|
+
if (options === null || typeof options !== 'object') {
|
|
37
|
+
throw invalidOption('options', 'an object', options);
|
|
38
|
+
}
|
|
39
|
+
const { metadata } = options;
|
|
40
|
+
if (metadata !== undefined && (metadata === null || typeof metadata !== 'object')) {
|
|
41
|
+
throw new SchemaDefinitionError('ERR_INVALID_OPTIONS', 'Option "metadata" must be an object');
|
|
42
|
+
}
|
|
43
|
+
return options;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// The id field a stored kind adds, which goes with the kind unless a call
|
|
47
|
+
// names it.
|
|
48
|
+
const idOf = idFieldOf;
|
|
49
|
+
|
|
50
|
+
const isStored = (schema) => idFieldOf(schema) !== null;
|
|
51
|
+
|
|
52
|
+
// The names a call gives, each a field (or a calculated field) of the
|
|
53
|
+
// schema; one it does not have is located in it.
|
|
54
|
+
const namesOf = (schema, keys) => {
|
|
55
|
+
if (!Array.isArray(keys)) throw invalidOption('keys', 'a list of field names', keys);
|
|
56
|
+
const names = new Set();
|
|
57
|
+
for (const key of keys) {
|
|
58
|
+
if (typeof key !== 'string') throw invalidOption('keys', 'a list of field names', keys);
|
|
59
|
+
if (!(key in schema.fields)) {
|
|
60
|
+
throw new SchemaDefinitionError('ERR_UNKNOWN_FIELD', 'No such field').locate(
|
|
61
|
+
schema.name,
|
|
62
|
+
key,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
names.add(key);
|
|
66
|
+
}
|
|
67
|
+
return names;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// A stored kind or a projection is a struct in what is derived from it: the
|
|
71
|
+
// first would add its id again and the second copy its parent's fields over
|
|
72
|
+
// the ones derived. A memory kind keeps its metadata.
|
|
73
|
+
const kindOf = (schema) => {
|
|
74
|
+
const { kind, unknown } = schema;
|
|
75
|
+
if (kind === 'projection' || isStored(schema)) return { kind: 'struct', unknown };
|
|
76
|
+
return metadataOf(schema);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// The definitions of the fields, fresh objects a call may change: the id of
|
|
80
|
+
// a stored kind is left out unless `names` has it.
|
|
81
|
+
const fieldsOf = (schema, names) => {
|
|
82
|
+
const fields = structDefinitionOf(schema.fields);
|
|
83
|
+
const id = idOf(schema);
|
|
84
|
+
if (id !== null && (names === null || !names.has(id))) delete fields[id];
|
|
85
|
+
return fields;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// The fields, then the indexes whose columns are all among them (a `many`
|
|
89
|
+
// entry is the field's own), then, with `functions`, the schema's own
|
|
90
|
+
// validate, parse, serialize and format.
|
|
91
|
+
const bodyOf = (schema, fields, functions) => {
|
|
92
|
+
for (const key in schema.indexes) {
|
|
93
|
+
const index = schema.indexes[key];
|
|
94
|
+
if (index.many !== undefined) continue;
|
|
95
|
+
let kept = true;
|
|
96
|
+
for (const column of index.index || index.primary || index.unique) {
|
|
97
|
+
if (!Object.hasOwn(fields, column)) kept = false;
|
|
98
|
+
}
|
|
99
|
+
if (kept) define(fields, key, index);
|
|
100
|
+
}
|
|
101
|
+
if (functions) {
|
|
102
|
+
for (const key in schema.options) {
|
|
103
|
+
const option = schema.options[key];
|
|
104
|
+
if (option !== null) define(fields, key, option);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return fields;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// The schema derived, with the namespaces of the schemas it is derived from.
|
|
111
|
+
const build = (schema, options, body, sources) => {
|
|
112
|
+
const { name = schema.name, metadata } = options;
|
|
113
|
+
const definition = structDefinition(kindOf(schema), metadata, body);
|
|
114
|
+
return new Schema(name, definition, namespacesOf(sources));
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const select = (schema, keys, options, keep) => {
|
|
118
|
+
sourceOf(schema);
|
|
119
|
+
const settings = optionsOf(options);
|
|
120
|
+
const names = namesOf(schema, keys);
|
|
121
|
+
const all = fieldsOf(schema, keep ? names : null);
|
|
122
|
+
const fields = {};
|
|
123
|
+
for (const key of Object.keys(all)) {
|
|
124
|
+
if (names.has(key) === keep) define(fields, key, all[key]);
|
|
125
|
+
}
|
|
126
|
+
return build(schema, settings, bodyOf(schema, fields, false), [schema]);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
// The fields named, in the order of the schema.
|
|
130
|
+
const pick = (schema, keys, options) => select(schema, keys, options, true);
|
|
131
|
+
|
|
132
|
+
// Every field but the ones named.
|
|
133
|
+
const omit = (schema, keys, options) => select(schema, keys, options, false);
|
|
134
|
+
|
|
135
|
+
const isNestedStruct = (def) => def.type === 'schema' && typeof def.schema.type !== 'string';
|
|
136
|
+
|
|
137
|
+
// A field made optional loses its default (a patch leaves a missing key as
|
|
138
|
+
// it is stored), unless `defaults`; `deep` goes on into the fields of a
|
|
139
|
+
// nested struct, whose own functions an optional struct loses.
|
|
140
|
+
const settle = (def, required, deep, defaults) => {
|
|
141
|
+
def.required = required;
|
|
142
|
+
if (!required && !defaults) delete def.default;
|
|
143
|
+
if (!deep || !isNestedStruct(def)) return;
|
|
144
|
+
const fields = def.schema;
|
|
145
|
+
for (const key of Object.keys(fields)) {
|
|
146
|
+
const nested = fields[key];
|
|
147
|
+
if (typeof nested !== 'function') settle(nested, required, deep, defaults);
|
|
148
|
+
}
|
|
149
|
+
if (required) return;
|
|
150
|
+
for (const key of CODECS) {
|
|
151
|
+
delete def[key];
|
|
152
|
+
delete fields[key];
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const toggle = (schema, options, required) => {
|
|
157
|
+
sourceOf(schema);
|
|
158
|
+
const settings = optionsOf(options);
|
|
159
|
+
const { keys, deep = false, defaults = false } = settings;
|
|
160
|
+
const names = keys === undefined ? null : namesOf(schema, keys);
|
|
161
|
+
const fields = fieldsOf(schema, names);
|
|
162
|
+
for (const key of Object.keys(fields)) {
|
|
163
|
+
const def = fields[key];
|
|
164
|
+
if (typeof def === 'function') continue;
|
|
165
|
+
if (names === null || names.has(key)) settle(def, required, deep, defaults);
|
|
166
|
+
}
|
|
167
|
+
return build(schema, settings, bodyOf(schema, fields, required), [schema]);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// Every field (or the ones in `options.keys`) optional. The schema's own
|
|
171
|
+
// functions, written for a whole value, do not come along.
|
|
172
|
+
const partial = (schema, options) => toggle(schema, options, false);
|
|
173
|
+
|
|
174
|
+
// Every field (or the ones in `options.keys`) required.
|
|
175
|
+
const required = (schema, options) => toggle(schema, options, true);
|
|
176
|
+
|
|
177
|
+
// The extension's fields over the schema's: one of the same name takes the
|
|
178
|
+
// place of the schema's (`'age?'` replaces `age`), the others come after.
|
|
179
|
+
// Not a unification: `merge` of the `./model` subpath is that.
|
|
180
|
+
const extend = (schema, extension, options) => {
|
|
181
|
+
sourceOf(schema);
|
|
182
|
+
const settings = optionsOf(options);
|
|
183
|
+
let additions = extension;
|
|
184
|
+
const sources = [schema];
|
|
185
|
+
if (hasBrand(extension, 'Schema')) {
|
|
186
|
+
sourceOf(extension);
|
|
187
|
+
additions = bodyOf(extension, fieldsOf(extension, null), true);
|
|
188
|
+
sources.push(extension);
|
|
189
|
+
} else if (extension === null || typeof extension !== 'object' || Array.isArray(extension)) {
|
|
190
|
+
throw invalidTarget('An extension is a Schema of a struct or an object of fields');
|
|
191
|
+
} else if (isFirstUpper(firstKey(extension))) {
|
|
192
|
+
const reason = 'An extension has fields only; the kind is options.metadata';
|
|
193
|
+
throw atSchema(new SchemaDefinitionError('ERR_INVALID_DEFINITION', reason), schema.name);
|
|
194
|
+
}
|
|
195
|
+
const keys = new Map();
|
|
196
|
+
for (const key of Object.keys(additions)) keys.set(formatters.key(key).field, key);
|
|
197
|
+
const base = bodyOf(schema, fieldsOf(schema, null), true);
|
|
198
|
+
const body = {};
|
|
199
|
+
const placed = new Set();
|
|
200
|
+
for (const key of Object.keys(base)) {
|
|
201
|
+
const own = keys.get(key);
|
|
202
|
+
if (own === undefined) define(body, key, base[key]);
|
|
203
|
+
else if (!placed.has(own)) {
|
|
204
|
+
define(body, own, additions[own]);
|
|
205
|
+
placed.add(own);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
for (const key of Object.keys(additions)) {
|
|
209
|
+
if (!placed.has(key)) define(body, key, additions[key]);
|
|
210
|
+
}
|
|
211
|
+
return build(schema, settings, body, sources);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
// The names of the fields a value carries, as a schema of an enum: the
|
|
215
|
+
// calculated fields and the id of a stored kind are not among them.
|
|
216
|
+
const keyof = (schema, options) => {
|
|
217
|
+
sourceOf(schema);
|
|
218
|
+
const { name = schema.name, metadata } = optionsOf(options);
|
|
219
|
+
if (metadata !== undefined) {
|
|
220
|
+
const reason = 'keyof takes a name only: a schema of an enum has no kind';
|
|
221
|
+
throw atSchema(new SchemaDefinitionError('ERR_INVALID_OPTIONS', reason), schema.name);
|
|
222
|
+
}
|
|
223
|
+
const id = idOf(schema);
|
|
224
|
+
const names = [];
|
|
225
|
+
for (const key in schema.fields) {
|
|
226
|
+
if (key !== id && hasBrand(schema.fields[key], 'Type')) names.push(key);
|
|
227
|
+
}
|
|
228
|
+
return new Schema(name, { enum: names }, Array.from(schema.namespaces));
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
// Installed like class members: not enumerable. A copy per subpath rather
|
|
232
|
+
// than a helper of the core: the core is measured on its own, the subpaths
|
|
233
|
+
// on top of it.
|
|
234
|
+
const install = (target, members) => {
|
|
235
|
+
const descriptors = Object.getOwnPropertyDescriptors(members);
|
|
236
|
+
for (const name in descriptors) {
|
|
237
|
+
descriptors[name].enumerable = false;
|
|
238
|
+
Object.defineProperty(target, name, descriptors[name]);
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
let registered = false;
|
|
243
|
+
|
|
244
|
+
// Adds `schema.pick(keys, options)`, `schema.omit(keys, options)`,
|
|
245
|
+
// `schema.partial(options)`, `schema.required(options)`,
|
|
246
|
+
// `schema.extend(extension, options)` and `schema.keyof(options)`. Once per
|
|
247
|
+
// process.
|
|
248
|
+
const register = () => {
|
|
249
|
+
if (registered) return;
|
|
250
|
+
registered = true;
|
|
251
|
+
install(Schema.prototype, {
|
|
252
|
+
pick(keys, options) {
|
|
253
|
+
return pick(this, keys, options);
|
|
254
|
+
},
|
|
255
|
+
omit(keys, options) {
|
|
256
|
+
return omit(this, keys, options);
|
|
257
|
+
},
|
|
258
|
+
partial(options) {
|
|
259
|
+
return partial(this, options);
|
|
260
|
+
},
|
|
261
|
+
required(options) {
|
|
262
|
+
return required(this, options);
|
|
263
|
+
},
|
|
264
|
+
extend(extension, options) {
|
|
265
|
+
return extend(this, extension, options);
|
|
266
|
+
},
|
|
267
|
+
keyof(options) {
|
|
268
|
+
return keyof(this, options);
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
module.exports = { pick, omit, partial, required, extend, keyof, register };
|
package/src/errors.js
CHANGED
|
@@ -43,12 +43,54 @@ const invalidTarget = (message) => {
|
|
|
43
43
|
return error;
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
+
// A call that needs what `register()` of a subpath installs (the converters
|
|
47
|
+
// of coercion, the module of the mongodb driver) before that ran: the
|
|
48
|
+
// program is wired wrong, not a value or a definition.
|
|
49
|
+
const notRegistered = (what, where) => {
|
|
50
|
+
const error = new TypeError(`${what} needs register() of @alexify/metaschema/${where} first`);
|
|
51
|
+
error.code = 'ERR_NOT_REGISTERED';
|
|
52
|
+
return error;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// The mark the validation half leaves on the process when it is installed
|
|
56
|
+
// (`validation.js`): a schema whose classes have no validation where the
|
|
57
|
+
// mark is there is one of another copy of the package.
|
|
58
|
+
const VALIDATION = Symbol.for('alexify.metaschema.validation');
|
|
59
|
+
|
|
60
|
+
// A schema of `./definition` asked to validate before the core entry was
|
|
61
|
+
// loaded: the half of the core that validates is not installed into its
|
|
62
|
+
// classes, because the core is not loaded, or is loaded from another copy
|
|
63
|
+
// of the package (two versions installed side by side, a bundle with two).
|
|
64
|
+
const noValidation = (what) => {
|
|
65
|
+
const elsewhere = globalThis[VALIDATION] === true;
|
|
66
|
+
const reason = elsewhere
|
|
67
|
+
? 'the core entry loaded is another copy of the package than the one that built the ' +
|
|
68
|
+
'schema (deduplicate @alexify/metaschema)'
|
|
69
|
+
: './definition alone does not validate';
|
|
70
|
+
const error = new TypeError(`${what} needs the core entry @alexify/metaschema: ${reason}`);
|
|
71
|
+
error.code = 'ERR_NOT_REGISTERED';
|
|
72
|
+
error.method = what;
|
|
73
|
+
return error;
|
|
74
|
+
};
|
|
75
|
+
|
|
46
76
|
const shown = (value) =>
|
|
47
77
|
typeof value === 'bigint' ? `${value}n` : (JSON.stringify(value) ?? String(value));
|
|
48
78
|
|
|
49
79
|
// An option of a call that is not what it must be (`maxErrors`, the options
|
|
50
|
-
// of the stream subpath): a TypeError naming it,
|
|
51
|
-
|
|
52
|
-
|
|
80
|
+
// of the stream subpath): a TypeError naming it, with a code of its own, as
|
|
81
|
+
// invalidTarget is.
|
|
82
|
+
const invalidOption = (name, expected, value) => {
|
|
83
|
+
const error = new TypeError(`${name} must be ${expected}, got ${shown(value)}`);
|
|
84
|
+
error.code = 'ERR_INVALID_OPTION';
|
|
85
|
+
return error;
|
|
86
|
+
};
|
|
53
87
|
|
|
54
|
-
module.exports = {
|
|
88
|
+
module.exports = {
|
|
89
|
+
SchemaDefinitionError,
|
|
90
|
+
asyncReturn,
|
|
91
|
+
invalidTarget,
|
|
92
|
+
notRegistered,
|
|
93
|
+
noValidation,
|
|
94
|
+
VALIDATION,
|
|
95
|
+
invalidOption,
|
|
96
|
+
};
|
package/src/formats/index.d.ts
CHANGED
|
@@ -1,19 +1,31 @@
|
|
|
1
1
|
// A format a string field may name: a test over the string, the examples
|
|
2
|
-
// `sample` draws,
|
|
3
|
-
// unless said otherwise; a format
|
|
2
|
+
// `sample` draws, whether `infer` may read the format out of data (true
|
|
3
|
+
// unless said otherwise; a format ordinary words pass says false), and the
|
|
4
|
+
// pattern the JSON Schema export writes beside the name (a string that
|
|
5
|
+
// compiles in unicode mode, a shape that never rejects what `test` accepts).
|
|
4
6
|
export interface FormatDefinition {
|
|
5
7
|
test: (value: string) => boolean;
|
|
6
8
|
examples: readonly string[];
|
|
7
9
|
infer?: boolean;
|
|
10
|
+
pattern?: string;
|
|
8
11
|
}
|
|
9
12
|
|
|
10
13
|
// The formats registered so far, by name: empty until `register()` ran. Read
|
|
11
14
|
// it; `register()` writes it.
|
|
12
15
|
export const FORMATS: { readonly [name: string]: Readonly<FormatDefinition> | undefined };
|
|
13
16
|
|
|
14
|
-
// Registers the built-in formats (
|
|
15
|
-
// `
|
|
16
|
-
//
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
|
|
17
|
+
// Registers the 35 built-in formats (the identifiers `uuid`, `ulid`, `jwt`,
|
|
18
|
+
// `nanoid`, `cuid2`; the dates and times `date-time`, `iso-date-time`,
|
|
19
|
+
// `date`, `time`, `iso-time`, `duration`; the network forms `email`,
|
|
20
|
+
// `ipv4`, `cidrv4`, `ipv6`, `cidrv6`, `mac`, `url`, `uri`, `hostname`; the
|
|
21
|
+
// formats of JSON Schema `uri-reference`, `uri-template`, `json-pointer`,
|
|
22
|
+
// `relative-json-pointer`, `regex`; and `e164`, `iban`, `hex-color`,
|
|
23
|
+
// `base64`, `base64url`, `hex`, `emoji`, `semver`, `slug`, `credit-card`)
|
|
24
|
+
// and the program's own, once per name and process-wide, like the type
|
|
25
|
+
// registry: a name registered again with the same test is nothing, with
|
|
26
|
+
// another test `ERR_FORMAT_REGISTERED`, and an entry that is not a format
|
|
27
|
+
// `ERR_INVALID_FORMAT`. The program's own format under the name of a
|
|
28
|
+
// built-in one takes its place, and the call returns the names of the
|
|
29
|
+
// built-in formats it replaced (an empty list otherwise); the schemas built
|
|
30
|
+
// before keep the built-in test.
|
|
31
|
+
export function register(custom?: Record<string, FormatDefinition>): string[];
|