@alexify/metaschema 3.4.1 → 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 +162 -1
- package/README.md +22 -7
- package/SECURITY.md +5 -4
- package/index.d.ts +856 -460
- 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 +14 -11
- 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 +18 -16
- package/src/lint.js +15 -18
- 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 +17 -20
- package/src/model/diff.js +29 -24
- package/src/model/index.d.ts +51 -35
- package/src/model/infer.js +69 -60
- package/src/model/merge.js +29 -18
- package/src/model/pattern.js +105 -102
- package/src/model/restore.js +24 -19
- 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 +13 -13
- 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 +6 -5
- 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 +15 -13
- 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 +28 -22
- package/src/util.js +65 -64
- package/src/validation.js +10 -13
package/src/stream/index.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
// installs the methods on Schema for the programs that want them.
|
|
10
10
|
const { hasBrand } = require('../util.js');
|
|
11
11
|
const { abandon } = require('../issues.js');
|
|
12
|
-
const { invalidTarget, invalidOption: invalid } = require('../errors.js');
|
|
12
|
+
const { SchemaUsageError, invalidTarget, invalidOption: invalid } = require('../errors.js');
|
|
13
13
|
const { Schema } = require('../validation.js');
|
|
14
14
|
|
|
15
15
|
const optionsOf = (options) => {
|
|
@@ -60,13 +60,10 @@ const schemaOf = (schema) => {
|
|
|
60
60
|
// A sequence of records: anything with an iterator, or an async one where
|
|
61
61
|
// the iteration is async. A string is iterable and surely a mistake.
|
|
62
62
|
const recordsOf = (records, async) => {
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
(typeof records[Symbol.iterator] === 'function' ||
|
|
68
|
-
(async && typeof records[Symbol.asyncIterator] === 'function'));
|
|
69
|
-
if (!iterable) throw invalidTarget('An iterable of records is expected');
|
|
63
|
+
const ruledOut = records === undefined || records === null || typeof records === 'string';
|
|
64
|
+
const sync = !ruledOut && typeof records[Symbol.iterator] === 'function';
|
|
65
|
+
const streamed = !ruledOut && async && typeof records[Symbol.asyncIterator] === 'function';
|
|
66
|
+
if (!sync && !streamed) throw invalidTarget('An iterable of records is expected');
|
|
70
67
|
return records;
|
|
71
68
|
};
|
|
72
69
|
|
|
@@ -76,6 +73,11 @@ const PARSE = (schema, record, options) => schema.parse(record, options);
|
|
|
76
73
|
const CHECK_ASYNC = (schema, record, options) => schema.checkAsync(record, options);
|
|
77
74
|
const PARSE_ASYNC = (schema, record, options) => schema.parseAsync(record, options);
|
|
78
75
|
|
|
76
|
+
const callOf = (async, parse) => {
|
|
77
|
+
if (async) return parse ? PARSE_ASYNC : CHECK_ASYNC;
|
|
78
|
+
return parse ? PARSE : CHECK;
|
|
79
|
+
};
|
|
80
|
+
|
|
79
81
|
// One record after the other. The entry of a record carries its position,
|
|
80
82
|
// since an iterator has none, and the record itself, since a stream cannot
|
|
81
83
|
// be read again. The options are handed to check or parse as given (they
|
|
@@ -92,6 +94,28 @@ const each = function* (schema, records, options, limit, call) {
|
|
|
92
94
|
}
|
|
93
95
|
};
|
|
94
96
|
|
|
97
|
+
// A source the async pair reads with for await. An async iterable is read as
|
|
98
|
+
// it is; a sync one through an async iterator of its own steps, which awaits
|
|
99
|
+
// an element that is a Promise (the documented contract) and nothing else:
|
|
100
|
+
// for await over a sync iterable adopts every thenable, and a record with a
|
|
101
|
+
// `then` of its own would come out as what that `then` resolves to, where a
|
|
102
|
+
// value of the program is data (as for the passes of parse).
|
|
103
|
+
const stepsOf = (records) => {
|
|
104
|
+
if (typeof records[Symbol.asyncIterator] === 'function') return records;
|
|
105
|
+
const iterator = records[Symbol.iterator]();
|
|
106
|
+
const next = () => {
|
|
107
|
+
const step = iterator.next();
|
|
108
|
+
if (step.done || !(step.value instanceof Promise)) return Promise.resolve(step);
|
|
109
|
+
return step.value.then((value) => ({ value, done: false }));
|
|
110
|
+
};
|
|
111
|
+
const close = (value) => {
|
|
112
|
+
const step = typeof iterator.return === 'function' ? iterator.return(value) : { done: true };
|
|
113
|
+
return Promise.resolve(step);
|
|
114
|
+
};
|
|
115
|
+
const steps = { next, return: close };
|
|
116
|
+
return { [Symbol.asyncIterator]: () => steps };
|
|
117
|
+
};
|
|
118
|
+
|
|
95
119
|
// The async pair over a sync or async iterable: up to `width` records are
|
|
96
120
|
// checked at once, the entries come out in the order of the records, and the
|
|
97
121
|
// promises still in flight when the iteration ends early (`maxInvalid`, a
|
|
@@ -102,7 +126,7 @@ const eachAsync = async function* (schema, records, options, limit, width, call)
|
|
|
102
126
|
let failed = 0;
|
|
103
127
|
let index = 0;
|
|
104
128
|
try {
|
|
105
|
-
for await (const record of records) {
|
|
129
|
+
for await (const record of stepsOf(records)) {
|
|
106
130
|
const result = call(schema, record, options);
|
|
107
131
|
// Observed from the start: a record that rejects while an earlier one
|
|
108
132
|
// is still awaited would reject unhandled before its turn (the await
|
|
@@ -160,12 +184,8 @@ const parseEachAsync = (schema, records, options) => {
|
|
|
160
184
|
// The error a stream ends with: the record, its position and its result
|
|
161
185
|
// travel on it, since the stream carries them no further.
|
|
162
186
|
const invalidRecord = (index, record, result) => {
|
|
163
|
-
const
|
|
164
|
-
|
|
165
|
-
error.index = index;
|
|
166
|
-
error.record = record;
|
|
167
|
-
error.result = result;
|
|
168
|
-
return error;
|
|
187
|
+
const message = `Record ${index} is invalid`;
|
|
188
|
+
return new SchemaUsageError(message, { code: 'ALX_ERR_INVALID_RECORD', index, record, result });
|
|
169
189
|
};
|
|
170
190
|
|
|
171
191
|
// A TransformStream over the records of a stream: a valid record goes on as
|
|
@@ -182,16 +202,13 @@ const transform = (schema, options) => {
|
|
|
182
202
|
const async = asyncOf(options);
|
|
183
203
|
const onInvalid = callbackOf(options);
|
|
184
204
|
const parse = mode === 'parse';
|
|
185
|
-
const call = async
|
|
205
|
+
const call = callOf(async, parse);
|
|
186
206
|
let failed = 0;
|
|
187
207
|
let index = 0;
|
|
188
208
|
const settle = (record, result, controller) => {
|
|
189
209
|
const at = index;
|
|
190
210
|
index += 1;
|
|
191
|
-
if (result.valid)
|
|
192
|
-
controller.enqueue(parse ? result.value : record);
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
211
|
+
if (result.valid) return void controller.enqueue(parse ? result.value : record);
|
|
195
212
|
failed += 1;
|
|
196
213
|
if (onInvalid !== undefined) onInvalid(record, result, at);
|
|
197
214
|
if (onInvalid === undefined || failed >= limit) {
|
package/src/struct.js
CHANGED
|
@@ -14,10 +14,10 @@ const STRUCT = Symbol.for('alexify.metaschema.struct');
|
|
|
14
14
|
// fields, where a calculated field lives, but a value has no key by that name.
|
|
15
15
|
const CODEC_KEYS = new Set(['validate', 'parse', 'serialize']);
|
|
16
16
|
|
|
17
|
-
const invalidAliases = (reason) => new SchemaDefinitionError('ERR_INVALID_DEFINITION', reason);
|
|
18
|
-
|
|
19
17
|
const ALIASES = 'Option "aliases" needs a list of key names';
|
|
20
18
|
|
|
19
|
+
const invalidAliases = (reason) => new SchemaDefinitionError(reason);
|
|
20
|
+
|
|
21
21
|
// A key a value may carry: a field, a calculated field or an alias, each
|
|
22
22
|
// once.
|
|
23
23
|
const know = (known, key) => {
|
|
@@ -42,7 +42,7 @@ const aliasesOf = (field, known) => {
|
|
|
42
42
|
// may be named `check`, `name` or `constructor`, and an input key such as
|
|
43
43
|
// `__proto__` or `toString` must be reported as unexpected instead of silently
|
|
44
44
|
// resolving to something on Object.prototype.
|
|
45
|
-
const createStruct = (defs,
|
|
45
|
+
const createStruct = (defs, preprocessor) => {
|
|
46
46
|
const fields = Object.create(null);
|
|
47
47
|
const plan = [];
|
|
48
48
|
const known = Object.create(null);
|
|
@@ -51,7 +51,7 @@ const createStruct = (defs, prep) => {
|
|
|
51
51
|
const entry = defs[key];
|
|
52
52
|
const { field, required } = formatters.key(key);
|
|
53
53
|
try {
|
|
54
|
-
const { Type, defs: typeDefs } =
|
|
54
|
+
const { Type, defs: typeDefs } = preprocessor.parse(entry);
|
|
55
55
|
if (!Type) {
|
|
56
56
|
fields[field] = entry;
|
|
57
57
|
if (!CODEC_KEYS.has(field)) {
|
|
@@ -64,7 +64,7 @@ const createStruct = (defs, prep) => {
|
|
|
64
64
|
// both decide whether the field is required, and the field's check is
|
|
65
65
|
// compiled with the answer, so it is settled before the field is built.
|
|
66
66
|
typeDefs.required = (typeDefs.required ?? true) && required;
|
|
67
|
-
const child = new Type(typeDefs,
|
|
67
|
+
const child = new Type(typeDefs, preprocessor);
|
|
68
68
|
fields[field] = child;
|
|
69
69
|
know(known, field);
|
|
70
70
|
plan.push({
|
|
@@ -75,7 +75,7 @@ const createStruct = (defs, prep) => {
|
|
|
75
75
|
aliases: aliasesOf(child, known),
|
|
76
76
|
});
|
|
77
77
|
} catch (error) {
|
|
78
|
-
if (error instanceof SchemaDefinitionError) error.locate(
|
|
78
|
+
if (error instanceof SchemaDefinitionError) error.locate(preprocessor.root.name, field);
|
|
79
79
|
throw error;
|
|
80
80
|
}
|
|
81
81
|
}
|
package/src/types.js
CHANGED
|
@@ -27,8 +27,11 @@ const PROTOTYPES = {
|
|
|
27
27
|
// that a class made from one inherits from it (below), and what the
|
|
28
28
|
// validation half of the core installs on them (their `compile`) reaches
|
|
29
29
|
// every class made from them, in every registry, whenever it is installed.
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
// `one` and `many` meet the object of `reference` again: setting a prototype an
|
|
31
|
+
// object already has changes nothing.
|
|
32
|
+
const builtinNames = Object.keys(PROTOTYPES);
|
|
33
|
+
for (let index = 0; index < builtinNames.length; index += 1) {
|
|
34
|
+
Object.setPrototypeOf(PROTOTYPES[builtinNames[index]], AbstractType.prototype);
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
// What the class of a type inherits from: a built-in's prototype object, the
|
|
@@ -40,7 +43,9 @@ const parentOf = (prototype) =>
|
|
|
40
43
|
: Object.assign(Object.create(AbstractType.prototype), prototype);
|
|
41
44
|
|
|
42
45
|
const customTypeError = (name, reason) =>
|
|
43
|
-
new SchemaDefinitionError(
|
|
46
|
+
new SchemaDefinitionError(`Custom type "${name}" ${reason}`, {
|
|
47
|
+
code: 'ALX_ERR_INVALID_CUSTOM_TYPE',
|
|
48
|
+
});
|
|
44
49
|
|
|
45
50
|
const createType = (name, prototype) => {
|
|
46
51
|
class Type extends AbstractType {
|
|
@@ -65,10 +70,10 @@ const createType = (name, prototype) => {
|
|
|
65
70
|
|
|
66
71
|
const createTypes = (prototypes) => {
|
|
67
72
|
const types = Object.create(null);
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const
|
|
71
|
-
types[name] = createType(name,
|
|
73
|
+
const names = Object.keys(prototypes);
|
|
74
|
+
for (let index = 0; index < names.length; index += 1) {
|
|
75
|
+
const name = names[index];
|
|
76
|
+
types[name] = createType(name, prototypes[name]);
|
|
72
77
|
}
|
|
73
78
|
return types;
|
|
74
79
|
};
|
|
@@ -100,15 +105,15 @@ const updateTypeMetadata = (Type, metadata) => {
|
|
|
100
105
|
|
|
101
106
|
// A custom prototype brings `construct` and `checkType` (or a `compile` of
|
|
102
107
|
// its own); an alias takes its base's, a registered type.
|
|
103
|
-
const checkCustomType = (name,
|
|
104
|
-
const { construct } =
|
|
105
|
-
const checkType =
|
|
108
|
+
const checkCustomType = (name, prototype) => {
|
|
109
|
+
const { construct } = prototype;
|
|
110
|
+
const checkType = prototype.checkType || prototype.compile;
|
|
106
111
|
if (!checkType || !construct) {
|
|
107
112
|
throw customTypeError(name, 'must contain "construct" and "checkType" methods');
|
|
108
113
|
}
|
|
109
|
-
const
|
|
110
|
-
const
|
|
111
|
-
if (!
|
|
114
|
+
const hasCheckType = typeof checkType === 'function';
|
|
115
|
+
const hasConstruct = typeof construct === 'function';
|
|
116
|
+
if (!hasCheckType || !hasConstruct) {
|
|
112
117
|
throw customTypeError(name, 'methods "construct" and "checkType" must be functions');
|
|
113
118
|
}
|
|
114
119
|
};
|
|
@@ -125,9 +130,10 @@ const redefines = (Type, { js, construct, checkType }) => {
|
|
|
125
130
|
};
|
|
126
131
|
|
|
127
132
|
const typeFactory = (customTypes, registry = TYPES) => {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const
|
|
133
|
+
const names = Object.keys(customTypes);
|
|
134
|
+
for (let index = 0; index < names.length; index += 1) {
|
|
135
|
+
const name = names[index];
|
|
136
|
+
const value = customTypes[name];
|
|
131
137
|
if (value === null || typeof value !== 'object') {
|
|
132
138
|
throw customTypeError(name, 'must be an object with methods "construct" and "checkType"');
|
|
133
139
|
}
|
|
@@ -136,21 +142,21 @@ const typeFactory = (customTypes, registry = TYPES) => {
|
|
|
136
142
|
if (registered) {
|
|
137
143
|
if (redefines(registered, value)) {
|
|
138
144
|
const reason = `Type "${name}" is already registered; only { metadata } may be added`;
|
|
139
|
-
throw new SchemaDefinitionError('
|
|
145
|
+
throw new SchemaDefinitionError(reason, { code: 'ALX_ERR_TYPE_REGISTERED' });
|
|
140
146
|
}
|
|
141
147
|
updateTypeMetadata(registered, metadata);
|
|
142
148
|
continue;
|
|
143
149
|
}
|
|
144
|
-
let
|
|
150
|
+
let prototype = rest;
|
|
145
151
|
if (js !== undefined) {
|
|
146
152
|
const base = registry[js];
|
|
147
153
|
if (!base) {
|
|
148
154
|
const reason = `Unknown js type "${js}" for custom type "${name}"`;
|
|
149
|
-
throw new SchemaDefinitionError('
|
|
155
|
+
throw new SchemaDefinitionError(reason, { code: 'ALX_ERR_UNKNOWN_JS_TYPE' });
|
|
150
156
|
}
|
|
151
|
-
|
|
152
|
-
} else checkCustomType(name,
|
|
153
|
-
const Type = createType(name,
|
|
157
|
+
prototype = base.prototype;
|
|
158
|
+
} else checkCustomType(name, prototype);
|
|
159
|
+
const Type = createType(name, prototype);
|
|
154
160
|
Type.source = { js, construct: rest.construct, checkType: rest.checkType };
|
|
155
161
|
updateTypeMetadata(Type, metadata);
|
|
156
162
|
registry[name] = Type;
|
package/src/util.js
CHANGED
|
@@ -7,26 +7,6 @@ const BRAND = Symbol.for('alexify.metaschema.brand');
|
|
|
7
7
|
// browser build free of any Node import.
|
|
8
8
|
const INSPECT = Symbol.for('nodejs.util.inspect.custom');
|
|
9
9
|
|
|
10
|
-
const hasBrand = (value, name) => value?.[BRAND] === name;
|
|
11
|
-
|
|
12
|
-
// A built-in is recognised by the name of its constructor, which nothing
|
|
13
|
-
// renames, and by its shape, which a value parsed from JSON cannot fake: an
|
|
14
|
-
// own key called `constructor` holding `{ name: 'Map' }` is data, and
|
|
15
|
-
// iterating such a value, or calling `getTime` on it, would throw out of a
|
|
16
|
-
// check. The name is tested first, since most values fail it at once.
|
|
17
|
-
const iterableNamed = (name) => (value) =>
|
|
18
|
-
value?.constructor?.name === name && typeof value[Symbol.iterator] === 'function';
|
|
19
|
-
|
|
20
|
-
const isMap = iterableNamed('Map');
|
|
21
|
-
|
|
22
|
-
const isSet = iterableNamed('Set');
|
|
23
|
-
|
|
24
|
-
const isDate = (value) =>
|
|
25
|
-
value?.constructor?.name === 'Date' && typeof value.getTime === 'function';
|
|
26
|
-
|
|
27
|
-
const isRegExp = (value) =>
|
|
28
|
-
value?.constructor?.name === 'RegExp' && typeof value.exec === 'function';
|
|
29
|
-
|
|
30
10
|
// The bytes of a Uint8Array of any realm, a Buffer included, by the name the
|
|
31
11
|
// engine keeps in the view itself: the tag getter of the typed arrays reads
|
|
32
12
|
// that slot and gives undefined for anything else, which a key or a symbol
|
|
@@ -36,31 +16,10 @@ const TYPED_TAG = Object.getOwnPropertyDescriptor(
|
|
|
36
16
|
Symbol.toStringTag,
|
|
37
17
|
).get;
|
|
38
18
|
|
|
39
|
-
const isBytes = (value) => TYPED_TAG.call(value) === 'Uint8Array';
|
|
40
|
-
|
|
41
|
-
// The type of a value as a message names it: an array and null by name.
|
|
42
|
-
const typeOf = (value) => {
|
|
43
|
-
if (value === null) return 'null';
|
|
44
|
-
if (Array.isArray(value)) return 'array';
|
|
45
|
-
return typeof value;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
19
|
// A JavaScript identifier: what generated code may write as `object.key` and
|
|
49
20
|
// a TypeScript declaration as a bare name.
|
|
50
21
|
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
51
22
|
|
|
52
|
-
// Defines an own data property, so a key such as `__proto__` taken from a
|
|
53
|
-
// path or a definition lands on the object instead of changing its prototype.
|
|
54
|
-
const define = (target, key, value) => {
|
|
55
|
-
Object.defineProperty(target, key, {
|
|
56
|
-
value,
|
|
57
|
-
enumerable: true,
|
|
58
|
-
writable: true,
|
|
59
|
-
configurable: true,
|
|
60
|
-
});
|
|
61
|
-
return value;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
23
|
// The method that validates a value inside an existing context, shared by
|
|
65
24
|
// Schema and the reference type that checks its target through it. A global
|
|
66
25
|
// symbol like BRAND, so a schema from another copy of the package has it too.
|
|
@@ -96,20 +55,7 @@ const ENUM_SET_SIZE = 8;
|
|
|
96
55
|
|
|
97
56
|
// Key names taken from the value under check go into messages; a huge key
|
|
98
57
|
// must not become a huge message.
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
const shorten = (key) => {
|
|
102
|
-
const name = String(key);
|
|
103
|
-
return name.length > KEY_LIMIT ? `${name.slice(0, KEY_LIMIT)}...` : name;
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
// The text of a value that may have none (a symbol, an object without a
|
|
107
|
-
// prototype), for a message: what a validator threw or returned is anything.
|
|
108
|
-
const safeString = (value) => {
|
|
109
|
-
if (typeof value === 'symbol') return value.toString();
|
|
110
|
-
const bare = value !== null && typeof value === 'object' && typeof value.toString !== 'function';
|
|
111
|
-
return bare ? Object.prototype.toString.call(value) : String(value);
|
|
112
|
-
};
|
|
58
|
+
const KEY_MAX_CHARS = 100;
|
|
113
59
|
|
|
114
60
|
const formatters = {
|
|
115
61
|
type: (type, req = true) => {
|
|
@@ -127,18 +73,65 @@ const formatters = {
|
|
|
127
73
|
},
|
|
128
74
|
};
|
|
129
75
|
|
|
76
|
+
const hasBrand = (value, name) => value?.[BRAND] === name;
|
|
77
|
+
|
|
78
|
+
// A built-in is recognised by the name of its constructor, which nothing
|
|
79
|
+
// renames, and by its shape, which a value parsed from JSON cannot fake: an
|
|
80
|
+
// own key called `constructor` holding `{ name: 'Map' }` is data, and
|
|
81
|
+
// iterating such a value, or calling `getTime` on it, would throw out of a
|
|
82
|
+
// check. The name is tested first, since most values fail it at once.
|
|
83
|
+
const iterableNamed = (name) => (value) =>
|
|
84
|
+
value?.constructor?.name === name && typeof value[Symbol.iterator] === 'function';
|
|
85
|
+
|
|
86
|
+
const isMap = iterableNamed('Map');
|
|
87
|
+
|
|
88
|
+
const isSet = iterableNamed('Set');
|
|
89
|
+
|
|
90
|
+
const isDate = (value) =>
|
|
91
|
+
value?.constructor?.name === 'Date' && typeof value.getTime === 'function';
|
|
92
|
+
|
|
93
|
+
const isRegExp = (value) =>
|
|
94
|
+
value?.constructor?.name === 'RegExp' && typeof value.exec === 'function';
|
|
95
|
+
|
|
96
|
+
const isBytes = (value) => TYPED_TAG.call(value) === 'Uint8Array';
|
|
97
|
+
|
|
98
|
+
// The type of a value as a message names it: an array and null by name.
|
|
99
|
+
const typeOf = (value) => {
|
|
100
|
+
if (value === null) return 'null';
|
|
101
|
+
if (Array.isArray(value)) return 'array';
|
|
102
|
+
return typeof value;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// Defines an own data property, so a key such as `__proto__` taken from a
|
|
106
|
+
// path or a definition lands on the object instead of changing its prototype.
|
|
107
|
+
const define = (target, key, value) => {
|
|
108
|
+
Object.defineProperty(target, key, {
|
|
109
|
+
value,
|
|
110
|
+
enumerable: true,
|
|
111
|
+
writable: true,
|
|
112
|
+
configurable: true,
|
|
113
|
+
});
|
|
114
|
+
return value;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// `String`, not a template literal: a key may be a symbol.
|
|
118
|
+
const shorten = (key) => {
|
|
119
|
+
const name = String(key);
|
|
120
|
+
return name.length > KEY_MAX_CHARS ? `${name.slice(0, KEY_MAX_CHARS)}...` : name;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
// The text of a value that may have none (a symbol, an object without a
|
|
124
|
+
// prototype), for a message: what a validator threw or returned is anything.
|
|
125
|
+
const safeString = (value) => {
|
|
126
|
+
if (typeof value === 'symbol') return value.toString();
|
|
127
|
+
const bare = value !== null && typeof value === 'object' && typeof value.toString !== 'function';
|
|
128
|
+
return bare ? Object.prototype.toString.call(value) : `${value}`;
|
|
129
|
+
};
|
|
130
|
+
|
|
130
131
|
module.exports = {
|
|
131
132
|
BRAND,
|
|
132
133
|
INSPECT,
|
|
133
|
-
hasBrand,
|
|
134
|
-
isMap,
|
|
135
|
-
isSet,
|
|
136
|
-
isDate,
|
|
137
|
-
isRegExp,
|
|
138
|
-
isBytes,
|
|
139
|
-
typeOf,
|
|
140
134
|
IDENTIFIER,
|
|
141
|
-
define,
|
|
142
135
|
RUN,
|
|
143
136
|
HOOK,
|
|
144
137
|
TRACKED,
|
|
@@ -148,7 +141,15 @@ module.exports = {
|
|
|
148
141
|
KINDS,
|
|
149
142
|
CYCLES,
|
|
150
143
|
ENUM_SET_SIZE,
|
|
144
|
+
formatters,
|
|
145
|
+
hasBrand,
|
|
146
|
+
isMap,
|
|
147
|
+
isSet,
|
|
148
|
+
isDate,
|
|
149
|
+
isRegExp,
|
|
150
|
+
isBytes,
|
|
151
|
+
typeOf,
|
|
152
|
+
define,
|
|
151
153
|
shorten,
|
|
152
154
|
safeString,
|
|
153
|
-
formatters,
|
|
154
155
|
};
|
package/src/validation.js
CHANGED
|
@@ -31,6 +31,10 @@ const {
|
|
|
31
31
|
wantOf,
|
|
32
32
|
} = require('./codec.js');
|
|
33
33
|
|
|
34
|
+
// The warnings of a schema and of a model, computed on first use: a schema
|
|
35
|
+
// built in a hot path never pays for the lint.
|
|
36
|
+
const LINTED = new WeakMap();
|
|
37
|
+
|
|
34
38
|
// The validation of a schema inside an existing context: `check` starts one,
|
|
35
39
|
// and a reference field checks its target entity within the context of the
|
|
36
40
|
// outer check, so the error limit and the cycle detection span the whole
|
|
@@ -93,10 +97,6 @@ const pass = (schema, phase, source, context, extra) => {
|
|
|
93
97
|
return apply(info, schema, source, context, undefined, phase, want);
|
|
94
98
|
};
|
|
95
99
|
|
|
96
|
-
// The warnings of a schema and of a model, computed on first use: a schema
|
|
97
|
-
// built in a hot path never pays for the lint.
|
|
98
|
-
const LINTED = new WeakMap();
|
|
99
|
-
|
|
100
100
|
const linted = (target, lint) => {
|
|
101
101
|
let warnings = LINTED.get(target);
|
|
102
102
|
if (warnings === undefined) {
|
|
@@ -217,13 +217,10 @@ const members = {
|
|
|
217
217
|
if (this[TRACKED]) context.source = value;
|
|
218
218
|
walkAsync(this, value, context);
|
|
219
219
|
if (context.async.length > 0) await settle(context);
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
if (output instanceof Promise) output = (await output).value;
|
|
225
|
-
}
|
|
226
|
-
return parsed(context, output);
|
|
220
|
+
if (context.count !== 0) return parsed(context, undefined);
|
|
221
|
+
// Only a promise of the pass is awaited: a thenable in the value is data.
|
|
222
|
+
const output = pass(this, FINISH, value, context, 0);
|
|
223
|
+
return parsed(context, output instanceof Promise ? (await output).value : output);
|
|
227
224
|
},
|
|
228
225
|
|
|
229
226
|
// The other direction: the schema's own `serialize`, then the serializers
|
|
@@ -250,7 +247,7 @@ const members = {
|
|
|
250
247
|
if (isThenable(output)) throw asyncReturn('validator', path, 'checkAsync');
|
|
251
248
|
return result.add(output);
|
|
252
249
|
} catch (error) {
|
|
253
|
-
if (error?.code === '
|
|
250
|
+
if (error?.code === 'ALX_ERR_ASYNC_VALIDATOR') throw error;
|
|
254
251
|
return result.add({
|
|
255
252
|
code: 'exception',
|
|
256
253
|
message: `validation failed ${safeString(error)}`,
|
|
@@ -275,7 +272,7 @@ const members = {
|
|
|
275
272
|
if (this[STANDARD] === null) {
|
|
276
273
|
const async = hasAsync(this);
|
|
277
274
|
const validate = (value, options) => {
|
|
278
|
-
const libraryOptions = options
|
|
275
|
+
const libraryOptions = options?.libraryOptions;
|
|
279
276
|
if (async) return this.parseAsync(value, libraryOptions).then(standardize);
|
|
280
277
|
return standardize(this.parse(value, libraryOptions));
|
|
281
278
|
};
|