@alexify/metaschema 1.0.0 → 3.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 +551 -1
- package/README.md +235 -34
- package/SECURITY.md +32 -11
- package/index.d.ts +798 -23
- package/package.json +30 -6
- package/src/codec.js +490 -0
- package/src/compile/code.js +92 -0
- package/src/compile/emit.js +512 -0
- package/src/compile/index.d.ts +38 -0
- package/src/compile/index.js +176 -0
- package/src/context.js +141 -0
- package/src/errors.js +10 -1
- package/src/generators/index.d.ts +29 -0
- package/src/generators/index.js +90 -0
- package/src/generators/jsonschema.js +535 -0
- package/src/generators/typescript.js +237 -0
- package/src/index.js +1 -3
- package/src/issues.js +291 -0
- package/src/kinds.js +9 -12
- package/src/lint.js +98 -0
- package/src/locales/en.d.ts +5 -0
- package/src/locales/en.js +66 -0
- package/src/locales/uk.d.ts +5 -0
- package/src/locales/uk.js +62 -0
- package/src/metadata.js +65 -119
- package/src/metautil.js +8 -1
- package/src/model.js +31 -20
- package/src/preprocessor.js +45 -27
- package/src/prototypes/abstract.js +101 -51
- package/src/prototypes/collections.js +106 -41
- package/src/prototypes/json.js +9 -5
- package/src/prototypes/reference.js +91 -20
- package/src/prototypes/scalars.js +124 -16
- package/src/prototypes/schema.js +31 -6
- package/src/prototypes/tuple.js +57 -29
- package/src/prototypes/union.js +139 -0
- package/src/result.js +115 -0
- package/src/rules.js +185 -0
- package/src/runtime/browser.js +2 -2
- package/src/runtime/node.js +4 -2
- package/src/schema.js +181 -68
- package/src/struct.js +101 -33
- package/src/types.js +14 -14
- package/src/util.js +41 -50
|
@@ -1,27 +1,35 @@
|
|
|
1
1
|
const { SchemaDefinitionError } = require('../errors.js');
|
|
2
|
-
const {
|
|
2
|
+
const { issues } = require('../issues.js');
|
|
3
|
+
const { ENUM_SET_SIZE } = require('../util.js');
|
|
3
4
|
|
|
4
5
|
const missingEnum = (type) => {
|
|
5
6
|
const hint = `Type "${type}" needs a list of values: { type: '${type}', enum: [...] }`;
|
|
6
7
|
return new SchemaDefinitionError('ERR_INVALID_ENUM', hint);
|
|
7
8
|
};
|
|
8
9
|
|
|
10
|
+
// A single comparison on the way through for a valid value; the optional
|
|
11
|
+
// case is looked at only when it fails.
|
|
12
|
+
const scalarCheck = (scalar, required) => (value, context, key) => {
|
|
13
|
+
// oxlint-disable-next-line valid-typeof
|
|
14
|
+
if (typeof value === scalar) return;
|
|
15
|
+
if (!required && value == null) return;
|
|
16
|
+
issues.type(context, scalar, value, key);
|
|
17
|
+
};
|
|
18
|
+
|
|
9
19
|
const scalar = {
|
|
10
20
|
kind: 'scalar',
|
|
21
|
+
options: [],
|
|
11
22
|
|
|
12
23
|
construct() {},
|
|
13
24
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (typeof value !== this.scalar) {
|
|
17
|
-
return issue('type', path, `not of expected type: ${this.scalar}`);
|
|
18
|
-
}
|
|
19
|
-
return null;
|
|
25
|
+
compile() {
|
|
26
|
+
return scalarCheck(this.scalar, this.required);
|
|
20
27
|
},
|
|
21
28
|
};
|
|
22
29
|
|
|
23
30
|
const enumerable = {
|
|
24
31
|
kind: 'scalar',
|
|
32
|
+
options: ['enum'],
|
|
25
33
|
|
|
26
34
|
construct(def) {
|
|
27
35
|
const values = def.enum;
|
|
@@ -29,16 +37,116 @@ const enumerable = {
|
|
|
29
37
|
this.enum = values;
|
|
30
38
|
},
|
|
31
39
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
40
|
+
compile() {
|
|
41
|
+
const values = this.enum;
|
|
42
|
+
const { required } = this;
|
|
43
|
+
if (values.length > ENUM_SET_SIZE) {
|
|
44
|
+
const set = new Set(values);
|
|
45
|
+
return (value, context, key) => {
|
|
46
|
+
if (set.has(value)) return;
|
|
47
|
+
if (!required && value == null) return;
|
|
48
|
+
issues.enum(context, values, key);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
return (value, context, key) => {
|
|
52
|
+
if (values.includes(value)) return;
|
|
53
|
+
if (!required && value == null) return;
|
|
54
|
+
issues.enum(context, values, key);
|
|
55
|
+
};
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// A number without a fraction; it renders as a number in TypeScript.
|
|
60
|
+
const integer = {
|
|
61
|
+
kind: 'scalar',
|
|
62
|
+
scalar: 'number',
|
|
63
|
+
rules: ['min', 'max'],
|
|
64
|
+
options: ['min', 'max'],
|
|
65
|
+
|
|
66
|
+
construct() {},
|
|
67
|
+
|
|
68
|
+
compile() {
|
|
69
|
+
const { required } = this;
|
|
70
|
+
return (value, context, key) => {
|
|
71
|
+
if (Number.isInteger(value)) return;
|
|
72
|
+
if (!required && value == null) return;
|
|
73
|
+
issues.type(context, 'integer', value, key);
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// A Date instance with a time: an invalid Date is a type error.
|
|
79
|
+
const date = {
|
|
80
|
+
kind: 'scalar',
|
|
81
|
+
scalar: 'date',
|
|
82
|
+
options: [],
|
|
83
|
+
|
|
84
|
+
construct() {},
|
|
85
|
+
|
|
86
|
+
compile() {
|
|
87
|
+
const { required } = this;
|
|
88
|
+
return (value, context, key) => {
|
|
89
|
+
if (value?.constructor?.name === 'Date' && !Number.isNaN(value.getTime())) return;
|
|
90
|
+
if (!required && value == null) return;
|
|
91
|
+
issues.type(context, 'date', value, key);
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const nothing = {
|
|
97
|
+
kind: 'scalar',
|
|
98
|
+
scalar: 'null',
|
|
99
|
+
options: [],
|
|
100
|
+
|
|
101
|
+
construct() {},
|
|
102
|
+
|
|
103
|
+
compile() {
|
|
104
|
+
const { required } = this;
|
|
105
|
+
return (value, context, key) => {
|
|
106
|
+
if (value === null) return;
|
|
107
|
+
if (value === undefined && !required) return;
|
|
108
|
+
issues.type(context, 'null', value, key);
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// Any value at all, null and undefined included; `required` still says
|
|
114
|
+
// whether the key must be there.
|
|
115
|
+
const accept = () => {};
|
|
116
|
+
|
|
117
|
+
const any = {
|
|
118
|
+
kind: 'scalar',
|
|
119
|
+
scalar: 'any',
|
|
120
|
+
options: [],
|
|
121
|
+
|
|
122
|
+
construct() {},
|
|
123
|
+
|
|
124
|
+
compile() {
|
|
125
|
+
return accept;
|
|
36
126
|
},
|
|
37
127
|
};
|
|
38
128
|
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
const
|
|
42
|
-
|
|
129
|
+
const unknown = { ...any, scalar: 'unknown' };
|
|
130
|
+
|
|
131
|
+
const string = {
|
|
132
|
+
...scalar,
|
|
133
|
+
scalar: 'string',
|
|
134
|
+
rules: ['length', 'pattern'],
|
|
135
|
+
options: ['length', 'unicode', 'pattern'],
|
|
136
|
+
};
|
|
137
|
+
const number = { ...scalar, scalar: 'number', rules: ['min', 'max'], options: ['min', 'max'] };
|
|
138
|
+
const bigint = { ...scalar, scalar: 'bigint', rules: ['min', 'max'], options: ['min', 'max'] };
|
|
139
|
+
const boolean = { ...scalar, scalar: 'boolean' };
|
|
43
140
|
|
|
44
|
-
module.exports = {
|
|
141
|
+
module.exports = {
|
|
142
|
+
string,
|
|
143
|
+
number,
|
|
144
|
+
integer,
|
|
145
|
+
bigint,
|
|
146
|
+
boolean,
|
|
147
|
+
date,
|
|
148
|
+
null: nothing,
|
|
149
|
+
any,
|
|
150
|
+
unknown,
|
|
151
|
+
enum: enumerable,
|
|
152
|
+
};
|
package/src/prototypes/schema.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const { hasBrand } = require('../util.js');
|
|
2
|
-
const { createStruct,
|
|
2
|
+
const { createStruct, isStruct, checkOf } = require('../struct.js');
|
|
3
3
|
const { SchemaDefinitionError } = require('../errors.js');
|
|
4
4
|
|
|
5
5
|
const missingSchema = (type) => {
|
|
@@ -10,21 +10,46 @@ const missingSchema = (type) => {
|
|
|
10
10
|
const isDefinition = (value) =>
|
|
11
11
|
value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
12
12
|
|
|
13
|
+
// The decoder and the serializer of a nested struct: the functions of its
|
|
14
|
+
// definition (the options of a Schema it reuses), as its validate is, unless
|
|
15
|
+
// the field's own definition gave them.
|
|
16
|
+
const adopt = (type, source) => {
|
|
17
|
+
if (type.parse === undefined && typeof source.parse === 'function') type.parse = source.parse;
|
|
18
|
+
if (type.serialize === undefined && typeof source.serialize === 'function') {
|
|
19
|
+
type.serialize = source.serialize;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// A nested struct: built from a definition object, or borrowed from a Schema
|
|
24
|
+
// instance, whose fields, references and schema-level validate are reused.
|
|
13
25
|
const schema = {
|
|
14
26
|
kind: 'struct',
|
|
27
|
+
options: ['schema'],
|
|
15
28
|
|
|
16
29
|
construct(defs, prep) {
|
|
17
30
|
const { schema: definition, required } = defs;
|
|
18
31
|
if (!isDefinition(definition)) throw missingSchema(this.type);
|
|
19
32
|
this.required = required ?? true;
|
|
20
|
-
|
|
21
|
-
|
|
33
|
+
if (hasBrand(definition, 'Schema')) {
|
|
34
|
+
prep.root.updateFromSchema(definition);
|
|
35
|
+
this.schema = definition.fields;
|
|
36
|
+
this.validate = definition.options.validate || undefined;
|
|
37
|
+
adopt(this, definition.options);
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (isStruct(definition)) this.schema = definition;
|
|
22
41
|
else this.schema = createStruct(definition, prep);
|
|
23
|
-
this.validate =
|
|
42
|
+
this.validate = definition.validate || undefined;
|
|
43
|
+
adopt(this, definition);
|
|
24
44
|
},
|
|
25
45
|
|
|
26
|
-
|
|
27
|
-
|
|
46
|
+
compile() {
|
|
47
|
+
const { required } = this;
|
|
48
|
+
const check = isStruct(this.schema) ? checkOf(this.schema) : this.schema.check;
|
|
49
|
+
return (value, context, key) => {
|
|
50
|
+
if (!required && value == null) return;
|
|
51
|
+
check(value, context, key);
|
|
52
|
+
};
|
|
28
53
|
},
|
|
29
54
|
};
|
|
30
55
|
|
package/src/prototypes/tuple.js
CHANGED
|
@@ -1,44 +1,72 @@
|
|
|
1
|
+
const { isFirstUpper } = require('../metautil.js');
|
|
2
|
+
const { formatters } = require('../util.js');
|
|
1
3
|
const { SchemaDefinitionError } = require('../errors.js');
|
|
2
|
-
const {
|
|
3
|
-
const { issue } = require('../util.js');
|
|
4
|
+
const { issues } = require('../issues.js');
|
|
4
5
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const invalidTuple = (reason) => new SchemaDefinitionError('ERR_INVALID_TUPLE', `Tuple ${reason}`);
|
|
7
|
+
|
|
8
|
+
// A named element is `{ name: 'type' }`: one key that is not `type`, a type
|
|
9
|
+
// name or a kind, holding a type name. Anything else is a definition of its
|
|
10
|
+
// own (a nested struct, a collection, a tuple).
|
|
11
|
+
const nameOf = (element, types) => {
|
|
12
|
+
if (element === null || typeof element !== 'object' || Array.isArray(element)) return null;
|
|
13
|
+
const keys = Object.keys(element);
|
|
14
|
+
if (keys.length !== 1 || typeof element[keys[0]] !== 'string') return null;
|
|
15
|
+
const { field } = formatters.key(keys[0]);
|
|
16
|
+
if (field === 'type' || types[field] || isFirstUpper(field)) return null;
|
|
17
|
+
return keys[0];
|
|
9
18
|
};
|
|
10
19
|
|
|
11
20
|
const tuple = {
|
|
12
21
|
kind: 'struct',
|
|
22
|
+
options: ['value', 'tuple'],
|
|
13
23
|
|
|
14
24
|
construct(def, prep) {
|
|
15
25
|
const elements = def.value || def.tuple;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
|
|
26
|
+
if (!Array.isArray(elements)) throw invalidTuple('needs a list of element definitions');
|
|
27
|
+
const built = [];
|
|
28
|
+
for (let index = 0; index < elements.length; index += 1) {
|
|
29
|
+
const element = elements[index];
|
|
30
|
+
const key = nameOf(element, prep.types);
|
|
31
|
+
const source = key === null ? element : element[key];
|
|
32
|
+
const { Type, defs } = prep.parse(source);
|
|
33
|
+
if (!Type) throw invalidTuple(`element ${index} cannot be a function`);
|
|
34
|
+
let name = null;
|
|
35
|
+
if (key !== null) {
|
|
36
|
+
const parsed = formatters.key(key);
|
|
37
|
+
defs.required = (defs.required ?? true) && parsed.required;
|
|
38
|
+
name = parsed.field;
|
|
39
|
+
}
|
|
23
40
|
const type = new Type(defs, prep);
|
|
24
|
-
if (name) type.name = name;
|
|
25
|
-
|
|
26
|
-
}
|
|
41
|
+
if (name !== null) type.name = name;
|
|
42
|
+
built.push(type);
|
|
43
|
+
}
|
|
44
|
+
this.value = built;
|
|
27
45
|
},
|
|
28
46
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
47
|
+
compile() {
|
|
48
|
+
const { required, type } = this;
|
|
49
|
+
const checks = [];
|
|
50
|
+
for (let index = 0; index < this.value.length; index += 1) checks.push(this.value[index].check);
|
|
51
|
+
const { length } = checks;
|
|
52
|
+
return (value, context, key) => {
|
|
53
|
+
if (!Array.isArray(value)) {
|
|
54
|
+
if (!required && value == null) return;
|
|
55
|
+
issues.type(context, type, value, key);
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (value.length > length) {
|
|
59
|
+
issues.length(context, undefined, length, value.length, key);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const nested = key !== undefined;
|
|
63
|
+
if (nested) context.path.push(key);
|
|
64
|
+
for (let index = 0; index < length; index += 1) {
|
|
65
|
+
if (context.count >= context.limit) break;
|
|
66
|
+
checks[index](value[index], context, index);
|
|
67
|
+
}
|
|
68
|
+
if (nested) context.path.pop();
|
|
69
|
+
};
|
|
42
70
|
},
|
|
43
71
|
};
|
|
44
72
|
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
const { SchemaDefinitionError } = require('../errors.js');
|
|
2
|
+
const { issues } = require('../issues.js');
|
|
3
|
+
|
|
4
|
+
const invalidUnion = (reason) => new SchemaDefinitionError('ERR_INVALID_UNION', `Union ${reason}`);
|
|
5
|
+
|
|
6
|
+
// What a branch is called in the message of a value that matched none.
|
|
7
|
+
const nameOf = (branch) => (branch.type === 'schema' ? 'object' : branch.type);
|
|
8
|
+
|
|
9
|
+
// The values of the discriminator field of a branch: the branch must be a
|
|
10
|
+
// struct whose field of that name is an enum, so the branch is known from
|
|
11
|
+
// one lookup.
|
|
12
|
+
const valuesOf = (branch, discriminator, index) => {
|
|
13
|
+
const field = branch.type === 'schema' ? branch.schema[discriminator] : undefined;
|
|
14
|
+
if (!field || !Array.isArray(field.enum)) {
|
|
15
|
+
const reason = `branch ${index} needs an enum field "${discriminator}" to discriminate on`;
|
|
16
|
+
throw invalidUnion(reason);
|
|
17
|
+
}
|
|
18
|
+
return field.enum;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// The branch of a discriminated union by the value of its discriminator, as
|
|
22
|
+
// a Map of value to branch index: built once per field, on first use, and
|
|
23
|
+
// shared by the check, the passes of parse and the JIT.
|
|
24
|
+
const TABLES = new WeakMap();
|
|
25
|
+
|
|
26
|
+
const tableOf = (field) => {
|
|
27
|
+
let table = TABLES.get(field);
|
|
28
|
+
if (table === undefined) {
|
|
29
|
+
table = new Map();
|
|
30
|
+
const { union: branches, discriminator } = field;
|
|
31
|
+
for (let index = 0; index < branches.length; index += 1) {
|
|
32
|
+
const values = branches[index].schema[discriminator].enum;
|
|
33
|
+
for (let at = 0; at < values.length; at += 1) table.set(values[at], index);
|
|
34
|
+
}
|
|
35
|
+
TABLES.set(field, table);
|
|
36
|
+
}
|
|
37
|
+
return table;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// A value of one of several definitions. Without a discriminator the
|
|
41
|
+
// branches are tried in order and the first one that records nothing wins;
|
|
42
|
+
// what a failed branch recorded is dropped. With one, the branch is picked
|
|
43
|
+
// by the value of that field in one Map lookup.
|
|
44
|
+
const union = {
|
|
45
|
+
kind: 'struct',
|
|
46
|
+
options: ['union', 'discriminator'],
|
|
47
|
+
|
|
48
|
+
construct(def, prep) {
|
|
49
|
+
const branches = def.union;
|
|
50
|
+
if (!Array.isArray(branches) || branches.length === 0) {
|
|
51
|
+
throw invalidUnion('needs a list of branches: { union: [...] }');
|
|
52
|
+
}
|
|
53
|
+
const built = [];
|
|
54
|
+
for (let index = 0; index < branches.length; index += 1) {
|
|
55
|
+
const { Type, defs } = prep.parse(branches[index]);
|
|
56
|
+
if (!Type) throw invalidUnion(`branch ${index} cannot be a function`);
|
|
57
|
+
built.push(new Type(defs, prep));
|
|
58
|
+
}
|
|
59
|
+
this.union = built;
|
|
60
|
+
const { discriminator } = this;
|
|
61
|
+
if (discriminator === undefined) return;
|
|
62
|
+
if (typeof discriminator !== 'string' || discriminator === '') {
|
|
63
|
+
throw invalidUnion('discriminator needs a field name');
|
|
64
|
+
}
|
|
65
|
+
const seen = new Set();
|
|
66
|
+
for (let index = 0; index < built.length; index += 1) {
|
|
67
|
+
const values = valuesOf(built[index], discriminator, index);
|
|
68
|
+
// The branch is picked by the value before any default is applied, so
|
|
69
|
+
// a default on the discriminator could never decide anything.
|
|
70
|
+
if (built[index].schema[discriminator].default !== undefined) {
|
|
71
|
+
throw invalidUnion(
|
|
72
|
+
`discriminator "${discriminator}" of branch ${index} cannot have a default`,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
for (let at = 0; at < values.length; at += 1) {
|
|
76
|
+
const value = values[at];
|
|
77
|
+
if (seen.has(value)) {
|
|
78
|
+
const shown = JSON.stringify(value);
|
|
79
|
+
throw invalidUnion(`discriminator value ${shown} is in two branches`);
|
|
80
|
+
}
|
|
81
|
+
seen.add(value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
compile() {
|
|
87
|
+
const { required, discriminator } = this;
|
|
88
|
+
if (discriminator === undefined) return this.compilePlain(required);
|
|
89
|
+
const checks = [];
|
|
90
|
+
for (let index = 0; index < this.union.length; index += 1) checks.push(this.union[index].check);
|
|
91
|
+
const table = tableOf(this);
|
|
92
|
+
const expected = [];
|
|
93
|
+
for (const value of table.keys()) expected.push(value);
|
|
94
|
+
return (value, context, key) => {
|
|
95
|
+
if (value === null || typeof value !== 'object') {
|
|
96
|
+
if (!required && value == null) return;
|
|
97
|
+
issues.type(context, 'object', value, key);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const index = table.get(value[discriminator]);
|
|
101
|
+
if (index !== undefined) {
|
|
102
|
+
checks[index](value, context, key);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const nested = key !== undefined;
|
|
106
|
+
if (nested) context.path.push(key);
|
|
107
|
+
issues.union(context, expected, discriminator, discriminator);
|
|
108
|
+
if (nested) context.path.pop();
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
compilePlain(required) {
|
|
113
|
+
const checks = [];
|
|
114
|
+
const expected = [];
|
|
115
|
+
for (let index = 0; index < this.union.length; index += 1) {
|
|
116
|
+
const branch = this.union[index];
|
|
117
|
+
checks.push(branch.check);
|
|
118
|
+
expected.push(nameOf(branch));
|
|
119
|
+
}
|
|
120
|
+
const { length } = checks;
|
|
121
|
+
return (value, context, key) => {
|
|
122
|
+
if (!required && value == null) return;
|
|
123
|
+
const { count } = context;
|
|
124
|
+
// The validators a failed branch set aside in an async check go with
|
|
125
|
+
// its issues: they belong to a branch the value does not match.
|
|
126
|
+
const pending = context.async === null ? 0 : context.async.length;
|
|
127
|
+
for (let index = 0; index < length; index += 1) {
|
|
128
|
+
checks[index](value, context, key);
|
|
129
|
+
if (context.count === count) return;
|
|
130
|
+
context.issues.length = count;
|
|
131
|
+
context.count = count;
|
|
132
|
+
if (context.async !== null) context.async.length = pending;
|
|
133
|
+
}
|
|
134
|
+
issues.union(context, expected, undefined, key);
|
|
135
|
+
};
|
|
136
|
+
},
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
module.exports = { union, nameOf, tableOf };
|
package/src/result.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
const { BRAND, INSPECT, hasBrand, define } = require('./util.js');
|
|
2
|
+
const { EMPTY, NONE, absorb, finalize, describe, toDotPath } = require('./issues.js');
|
|
3
|
+
const en = require('./locales/en.js');
|
|
4
|
+
|
|
5
|
+
class ValidationResult {
|
|
6
|
+
#root;
|
|
7
|
+
#messages;
|
|
8
|
+
#errors = null;
|
|
9
|
+
#summary = null;
|
|
10
|
+
|
|
11
|
+
// A check builds its result from its context, which carries the root label,
|
|
12
|
+
// the locale of the call and the issues under these names; a validator that
|
|
13
|
+
// builds one of its own needs none of them: its issues are relative to the
|
|
14
|
+
// field the validator belongs to.
|
|
15
|
+
constructor(options = {}) {
|
|
16
|
+
const { root = '', messages = en, issues = NONE } = options;
|
|
17
|
+
this.#root = root;
|
|
18
|
+
this.#messages = messages;
|
|
19
|
+
this.issues = issues;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get valid() {
|
|
23
|
+
return this.issues.length === 0;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// The messages with their location, rendered on first use.
|
|
27
|
+
get errors() {
|
|
28
|
+
if (this.#errors === null) {
|
|
29
|
+
const errors = [];
|
|
30
|
+
for (const issue of this.issues) errors.push(describe(this.#messages, this.#root, issue));
|
|
31
|
+
this.#errors = errors;
|
|
32
|
+
}
|
|
33
|
+
return this.#errors;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get summary() {
|
|
37
|
+
if (this.#summary === null) this.#summary = this.errors.join('\n');
|
|
38
|
+
return this.#summary;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
add(error, code = 'custom') {
|
|
42
|
+
const context = { issues: this.issues, count: this.issues.length, limit: Infinity, path: [] };
|
|
43
|
+
absorb(context, error, code, EMPTY);
|
|
44
|
+
this.issues = context.issues;
|
|
45
|
+
finalize(this.issues, this.#messages);
|
|
46
|
+
this.#errors = null;
|
|
47
|
+
this.#summary = null;
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Messages by dotted path, for forms: `formErrors` holds the messages of
|
|
52
|
+
// the value itself, `fieldErrors` those of its fields.
|
|
53
|
+
flatten() {
|
|
54
|
+
const formErrors = [];
|
|
55
|
+
const fieldErrors = {};
|
|
56
|
+
for (const issue of this.issues) {
|
|
57
|
+
const { path, message } = issue;
|
|
58
|
+
if (path.length === 0) {
|
|
59
|
+
formErrors.push(message);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const key = toDotPath(path);
|
|
63
|
+
const list = Object.hasOwn(fieldErrors, key)
|
|
64
|
+
? fieldErrors[key]
|
|
65
|
+
: define(fieldErrors, key, []);
|
|
66
|
+
list.push(message);
|
|
67
|
+
}
|
|
68
|
+
return { formErrors, fieldErrors };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Messages as a tree that follows the value: `properties` by key, `items`
|
|
72
|
+
// by index.
|
|
73
|
+
tree() {
|
|
74
|
+
const root = { errors: [] };
|
|
75
|
+
for (const issue of this.issues) {
|
|
76
|
+
let node = root;
|
|
77
|
+
for (const key of issue.path) {
|
|
78
|
+
if (typeof key === 'number') {
|
|
79
|
+
if (node.items === undefined) node.items = [];
|
|
80
|
+
if (node.items[key] === undefined) node.items[key] = { errors: [] };
|
|
81
|
+
node = node.items[key];
|
|
82
|
+
} else {
|
|
83
|
+
if (node.properties === undefined) node.properties = {};
|
|
84
|
+
const name = String(key);
|
|
85
|
+
node = Object.hasOwn(node.properties, name)
|
|
86
|
+
? node.properties[name]
|
|
87
|
+
: define(node.properties, name, { errors: [] });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
node.errors.push(issue.message);
|
|
91
|
+
}
|
|
92
|
+
return root;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The issues `add` would record for a validator's return value.
|
|
96
|
+
static issuesOf(error, path = [], code = 'custom') {
|
|
97
|
+
const context = { issues: [], count: 0, limit: Infinity, path };
|
|
98
|
+
absorb(context, error, code, EMPTY);
|
|
99
|
+
finalize(context.issues, en);
|
|
100
|
+
return context.issues;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static isInstance(error) {
|
|
104
|
+
return hasBrand(error, 'ValidationResult');
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
[INSPECT](depth, options, inspect) {
|
|
108
|
+
const { valid, errors, issues } = this;
|
|
109
|
+
return `ValidationResult ${inspect({ valid, errors, issues }, options)}`;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
ValidationResult.prototype[BRAND] = 'ValidationResult';
|
|
114
|
+
|
|
115
|
+
module.exports = { ValidationResult };
|