@alexify/metaschema 1.0.0 → 2.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 +228 -1
- package/README.md +124 -28
- package/SECURITY.md +6 -4
- package/index.d.ts +558 -19
- package/package.json +11 -2
- package/src/context.js +95 -0
- package/src/index.js +1 -1
- package/src/issues.js +196 -0
- package/src/lint.js +90 -0
- package/src/locales/en.d.ts +5 -0
- package/src/locales/en.js +58 -0
- package/src/locales/uk.d.ts +5 -0
- package/src/locales/uk.js +57 -0
- package/src/metadata.js +25 -76
- package/src/model.js +23 -7
- package/src/preprocessor.js +42 -19
- package/src/prototypes/abstract.js +86 -49
- package/src/prototypes/collections.js +86 -38
- package/src/prototypes/json.js +9 -5
- package/src/prototypes/reference.js +77 -20
- package/src/prototypes/scalars.js +126 -16
- package/src/prototypes/schema.js +19 -6
- package/src/prototypes/tuple.js +52 -29
- package/src/prototypes/union.js +100 -0
- package/src/result.js +124 -0
- package/src/rules.js +156 -0
- package/src/schema.js +133 -39
- package/src/struct.js +89 -32
- package/src/types.js +9 -3
- package/src/util.js +5 -53
|
@@ -1,9 +1,42 @@
|
|
|
1
1
|
const { SchemaDefinitionError } = require('../errors.js');
|
|
2
|
-
const {
|
|
3
|
-
const {
|
|
2
|
+
const { RUN } = require('../util.js');
|
|
3
|
+
const { issues } = require('../issues.js');
|
|
4
|
+
|
|
5
|
+
const invalidEmbed = () =>
|
|
6
|
+
new SchemaDefinitionError('ERR_INVALID_DEFINITION', 'Option "embed" needs a boolean');
|
|
7
|
+
|
|
8
|
+
// A reference is the one place a schema can recurse (Category.parent is a
|
|
9
|
+
// Category), so it is where a value met again while it is still being
|
|
10
|
+
// checked is a cycle, reported instead of recursed into.
|
|
11
|
+
const runReferenced = (schema, value, context, key) => {
|
|
12
|
+
if (value === null || typeof value !== 'object') {
|
|
13
|
+
schema[RUN](value, context, key);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (context.seen === null) context.seen = new Set();
|
|
17
|
+
const { seen } = context;
|
|
18
|
+
if (seen.has(value)) {
|
|
19
|
+
issues.circular(context, key);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
seen.add(value);
|
|
23
|
+
schema[RUN](value, context, key);
|
|
24
|
+
seen.delete(value);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
// Whether a referenced value is the record itself (the graph view) or its
|
|
28
|
+
// id (the storage view): the mode of the check when it says so, otherwise
|
|
29
|
+
// the field's `embed`, otherwise the store of the target's kind. A stored
|
|
30
|
+
// kind lives in its own table, so a row holds its id; a memory kind has no
|
|
31
|
+
// table of its own, so it is part of the row.
|
|
32
|
+
const embeds = (schema, embed, mode) => {
|
|
33
|
+
if (mode !== 'kind') return mode === 'embed';
|
|
34
|
+
return embed === undefined ? schema.store !== 'persistent' : embed;
|
|
35
|
+
};
|
|
4
36
|
|
|
5
37
|
const reference = {
|
|
6
38
|
kind: 'struct',
|
|
39
|
+
options: ['one', 'many', 'embed'],
|
|
7
40
|
|
|
8
41
|
construct(def) {
|
|
9
42
|
const key = def.many === undefined ? 'one' : 'many';
|
|
@@ -12,31 +45,55 @@ const reference = {
|
|
|
12
45
|
const reason = `Reference "${key}" needs an entity name, got ${JSON.stringify(target)}`;
|
|
13
46
|
throw new SchemaDefinitionError('ERR_INVALID_REFERENCE', reason);
|
|
14
47
|
}
|
|
15
|
-
|
|
48
|
+
if (this.embed !== undefined && typeof this.embed !== 'boolean') throw invalidEmbed();
|
|
49
|
+
// Read from the referencing side: a `many` field holds the many end of
|
|
50
|
+
// a one-to-many relation, a single reference the many end of many-to-one.
|
|
51
|
+
const relation = key === 'many' ? 'one-to-many' : 'many-to-one';
|
|
16
52
|
this[key] = target;
|
|
17
53
|
this.type = target;
|
|
18
54
|
this.root.relations.add({ to: target, type: relation });
|
|
19
55
|
},
|
|
20
56
|
|
|
21
|
-
|
|
22
|
-
|
|
57
|
+
// The target is looked up at check time: entities of a model are built in
|
|
58
|
+
// any order and a namespace may be attached later.
|
|
59
|
+
compile() {
|
|
60
|
+
const { one, many, root, required, embed } = this;
|
|
23
61
|
if (one) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
for (let index = 0; index < source.length; index += 1) {
|
|
35
|
-
if (result.full) break;
|
|
36
|
-
result.add(schema.check(source[index], `${path}[${index}]`));
|
|
62
|
+
return (value, context, key) => {
|
|
63
|
+
if (!required && value == null) return;
|
|
64
|
+
const schema = root.findReference(one);
|
|
65
|
+
if (!schema) {
|
|
66
|
+
issues.reference(context, one, key);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (embeds(schema, embed, context.references)) runReferenced(schema, value, context, key);
|
|
70
|
+
else if (typeof value !== 'string') issues.type(context, 'string', value, key);
|
|
71
|
+
};
|
|
37
72
|
}
|
|
38
|
-
|
|
73
|
+
const expected = `array of ${many}`;
|
|
74
|
+
return (value, context, key) => {
|
|
75
|
+
if (!required && value == null) return;
|
|
76
|
+
const schema = root.findReference(many);
|
|
77
|
+
if (!schema) {
|
|
78
|
+
issues.reference(context, many, key);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (!Array.isArray(value)) {
|
|
82
|
+
issues.type(context, expected, value, key);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const nested = key !== undefined;
|
|
86
|
+
if (nested) context.path.push(key);
|
|
87
|
+
const embedded = embeds(schema, embed, context.references);
|
|
88
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
89
|
+
if (context.count >= context.limit) break;
|
|
90
|
+
const item = value[index];
|
|
91
|
+
if (embedded) runReferenced(schema, item, context, index);
|
|
92
|
+
else if (typeof item !== 'string') issues.type(context, 'string', item, index);
|
|
93
|
+
}
|
|
94
|
+
if (nested) context.path.pop();
|
|
95
|
+
};
|
|
39
96
|
},
|
|
40
97
|
};
|
|
41
98
|
|
|
42
|
-
module.exports = { reference };
|
|
99
|
+
module.exports = { reference, embeds };
|
|
@@ -1,27 +1,37 @@
|
|
|
1
1
|
const { SchemaDefinitionError } = require('../errors.js');
|
|
2
|
-
const {
|
|
2
|
+
const { issues } = require('../issues.js');
|
|
3
3
|
|
|
4
4
|
const missingEnum = (type) => {
|
|
5
5
|
const hint = `Type "${type}" needs a list of values: { type: '${type}', enum: [...] }`;
|
|
6
6
|
return new SchemaDefinitionError('ERR_INVALID_ENUM', hint);
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
+
// A single comparison on the way through for a valid value; the optional
|
|
10
|
+
// case is looked at only when it fails.
|
|
11
|
+
const scalarCheck = (scalar, required) => (value, context, key) => {
|
|
12
|
+
// oxlint-disable-next-line valid-typeof
|
|
13
|
+
if (typeof value === scalar) return;
|
|
14
|
+
if (!required && value == null) return;
|
|
15
|
+
issues.type(context, scalar, value, key);
|
|
16
|
+
};
|
|
17
|
+
|
|
9
18
|
const scalar = {
|
|
10
19
|
kind: 'scalar',
|
|
20
|
+
options: [],
|
|
11
21
|
|
|
12
22
|
construct() {},
|
|
13
23
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (typeof value !== this.scalar) {
|
|
17
|
-
return issue('type', path, `not of expected type: ${this.scalar}`);
|
|
18
|
-
}
|
|
19
|
-
return null;
|
|
24
|
+
compile() {
|
|
25
|
+
return scalarCheck(this.scalar, this.required);
|
|
20
26
|
},
|
|
21
27
|
};
|
|
22
28
|
|
|
29
|
+
// Past this many values a Set lookup beats a linear scan.
|
|
30
|
+
const ENUM_SET_SIZE = 8;
|
|
31
|
+
|
|
23
32
|
const enumerable = {
|
|
24
33
|
kind: 'scalar',
|
|
34
|
+
options: ['enum'],
|
|
25
35
|
|
|
26
36
|
construct(def) {
|
|
27
37
|
const values = def.enum;
|
|
@@ -29,16 +39,116 @@ const enumerable = {
|
|
|
29
39
|
this.enum = values;
|
|
30
40
|
},
|
|
31
41
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
42
|
+
compile() {
|
|
43
|
+
const values = this.enum;
|
|
44
|
+
const { required } = this;
|
|
45
|
+
if (values.length > ENUM_SET_SIZE) {
|
|
46
|
+
const set = new Set(values);
|
|
47
|
+
return (value, context, key) => {
|
|
48
|
+
if (set.has(value)) return;
|
|
49
|
+
if (!required && value == null) return;
|
|
50
|
+
issues.enum(context, values, key);
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
return (value, context, key) => {
|
|
54
|
+
if (values.includes(value)) return;
|
|
55
|
+
if (!required && value == null) return;
|
|
56
|
+
issues.enum(context, values, key);
|
|
57
|
+
};
|
|
36
58
|
},
|
|
37
59
|
};
|
|
38
60
|
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
61
|
+
// A number without a fraction; it renders as a number in TypeScript.
|
|
62
|
+
const integer = {
|
|
63
|
+
kind: 'scalar',
|
|
64
|
+
scalar: 'number',
|
|
65
|
+
rules: ['min', 'max'],
|
|
66
|
+
options: ['min', 'max'],
|
|
67
|
+
|
|
68
|
+
construct() {},
|
|
43
69
|
|
|
44
|
-
|
|
70
|
+
compile() {
|
|
71
|
+
const { required } = this;
|
|
72
|
+
return (value, context, key) => {
|
|
73
|
+
if (Number.isInteger(value)) return;
|
|
74
|
+
if (!required && value == null) return;
|
|
75
|
+
issues.type(context, 'integer', value, key);
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// A Date instance with a time: an invalid Date is a type error.
|
|
81
|
+
const date = {
|
|
82
|
+
kind: 'scalar',
|
|
83
|
+
scalar: 'date',
|
|
84
|
+
options: [],
|
|
85
|
+
|
|
86
|
+
construct() {},
|
|
87
|
+
|
|
88
|
+
compile() {
|
|
89
|
+
const { required } = this;
|
|
90
|
+
return (value, context, key) => {
|
|
91
|
+
if (value?.constructor?.name === 'Date' && !Number.isNaN(value.getTime())) return;
|
|
92
|
+
if (!required && value == null) return;
|
|
93
|
+
issues.type(context, 'date', value, key);
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const nothing = {
|
|
99
|
+
kind: 'scalar',
|
|
100
|
+
scalar: 'null',
|
|
101
|
+
options: [],
|
|
102
|
+
|
|
103
|
+
construct() {},
|
|
104
|
+
|
|
105
|
+
compile() {
|
|
106
|
+
const { required } = this;
|
|
107
|
+
return (value, context, key) => {
|
|
108
|
+
if (value === null) return;
|
|
109
|
+
if (value === undefined && !required) return;
|
|
110
|
+
issues.type(context, 'null', value, key);
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// Any value at all, null and undefined included; `required` still says
|
|
116
|
+
// whether the key must be there.
|
|
117
|
+
const accept = () => {};
|
|
118
|
+
|
|
119
|
+
const any = {
|
|
120
|
+
kind: 'scalar',
|
|
121
|
+
scalar: 'any',
|
|
122
|
+
options: [],
|
|
123
|
+
|
|
124
|
+
construct() {},
|
|
125
|
+
|
|
126
|
+
compile() {
|
|
127
|
+
return accept;
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const unknown = { ...any, scalar: 'unknown' };
|
|
132
|
+
|
|
133
|
+
const string = {
|
|
134
|
+
...scalar,
|
|
135
|
+
scalar: 'string',
|
|
136
|
+
rules: ['length', 'pattern'],
|
|
137
|
+
options: ['length', 'unicode', 'pattern'],
|
|
138
|
+
};
|
|
139
|
+
const number = { ...scalar, scalar: 'number', rules: ['min', 'max'], options: ['min', 'max'] };
|
|
140
|
+
const bigint = { ...scalar, scalar: 'bigint', rules: ['min', 'max'], options: ['min', 'max'] };
|
|
141
|
+
const boolean = { ...scalar, scalar: 'boolean' };
|
|
142
|
+
|
|
143
|
+
module.exports = {
|
|
144
|
+
string,
|
|
145
|
+
number,
|
|
146
|
+
integer,
|
|
147
|
+
bigint,
|
|
148
|
+
boolean,
|
|
149
|
+
date,
|
|
150
|
+
null: nothing,
|
|
151
|
+
any,
|
|
152
|
+
unknown,
|
|
153
|
+
enum: enumerable,
|
|
154
|
+
};
|
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,34 @@ const missingSchema = (type) => {
|
|
|
10
10
|
const isDefinition = (value) =>
|
|
11
11
|
value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
12
12
|
|
|
13
|
+
// A nested struct: built from a definition object, or borrowed from a Schema
|
|
14
|
+
// instance, whose fields, references and schema-level validate are reused.
|
|
13
15
|
const schema = {
|
|
14
16
|
kind: 'struct',
|
|
17
|
+
options: ['schema'],
|
|
15
18
|
|
|
16
19
|
construct(defs, prep) {
|
|
17
20
|
const { schema: definition, required } = defs;
|
|
18
21
|
if (!isDefinition(definition)) throw missingSchema(this.type);
|
|
19
22
|
this.required = required ?? true;
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
if (hasBrand(definition, 'Schema')) {
|
|
24
|
+
prep.root.updateFromSchema(definition);
|
|
25
|
+
this.schema = definition.fields;
|
|
26
|
+
this.validate = definition.options.validate || undefined;
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (isStruct(definition)) this.schema = definition;
|
|
22
30
|
else this.schema = createStruct(definition, prep);
|
|
23
|
-
this.validate =
|
|
31
|
+
this.validate = definition.validate || undefined;
|
|
24
32
|
},
|
|
25
33
|
|
|
26
|
-
|
|
27
|
-
|
|
34
|
+
compile() {
|
|
35
|
+
const { required } = this;
|
|
36
|
+
const check = isStruct(this.schema) ? checkOf(this.schema) : this.schema.check;
|
|
37
|
+
return (value, context, key) => {
|
|
38
|
+
if (!required && value == null) return;
|
|
39
|
+
check(value, context, key);
|
|
40
|
+
};
|
|
28
41
|
},
|
|
29
42
|
};
|
|
30
43
|
|
package/src/prototypes/tuple.js
CHANGED
|
@@ -1,44 +1,67 @@
|
|
|
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
|
-
const
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
if (
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
+
if (!Array.isArray(elements)) throw invalidTuple('needs a list of element definitions');
|
|
27
|
+
this.value = elements.map((element, index) => {
|
|
28
|
+
const key = nameOf(element, prep.types);
|
|
29
|
+
const source = key === null ? element : element[key];
|
|
30
|
+
const { Type, defs } = prep.parse(source);
|
|
31
|
+
if (!Type) throw invalidTuple(`element ${index} cannot be a function`);
|
|
32
|
+
if (key !== null) {
|
|
33
|
+
const { field, required } = formatters.key(key);
|
|
34
|
+
defs.required = (defs.required ?? true) && required;
|
|
35
|
+
const type = new Type(defs, prep);
|
|
36
|
+
type.name = field;
|
|
37
|
+
return type;
|
|
38
|
+
}
|
|
39
|
+
return new Type(defs, prep);
|
|
26
40
|
});
|
|
27
41
|
},
|
|
28
42
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
43
|
+
compile() {
|
|
44
|
+
const { required, type } = this;
|
|
45
|
+
const checks = this.value.map((element) => element.check);
|
|
46
|
+
const { length } = checks;
|
|
47
|
+
return (value, context, key) => {
|
|
48
|
+
if (!Array.isArray(value)) {
|
|
49
|
+
if (!required && value == null) return;
|
|
50
|
+
issues.type(context, type, value, key);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (value.length > length) {
|
|
54
|
+
issues.length(context, undefined, length, value.length, key);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
const nested = key !== undefined;
|
|
58
|
+
if (nested) context.path.push(key);
|
|
59
|
+
for (let index = 0; index < length; index += 1) {
|
|
60
|
+
if (context.count >= context.limit) break;
|
|
61
|
+
checks[index](value[index], context, index);
|
|
62
|
+
}
|
|
63
|
+
if (nested) context.path.pop();
|
|
64
|
+
};
|
|
42
65
|
},
|
|
43
66
|
};
|
|
44
67
|
|
|
@@ -0,0 +1,100 @@
|
|
|
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
|
+
// A value of one of several definitions. Without a discriminator the
|
|
22
|
+
// branches are tried in order and the first one that records nothing wins;
|
|
23
|
+
// what a failed branch recorded is dropped. With one, the branch is picked
|
|
24
|
+
// by the value of that field in one Map lookup.
|
|
25
|
+
const union = {
|
|
26
|
+
kind: 'struct',
|
|
27
|
+
options: ['union', 'discriminator'],
|
|
28
|
+
|
|
29
|
+
construct(def, prep) {
|
|
30
|
+
const branches = def.union;
|
|
31
|
+
if (!Array.isArray(branches) || branches.length === 0) {
|
|
32
|
+
throw invalidUnion('needs a list of branches: { union: [...] }');
|
|
33
|
+
}
|
|
34
|
+
this.union = branches.map((branch, index) => {
|
|
35
|
+
const { Type, defs } = prep.parse(branch);
|
|
36
|
+
if (!Type) throw invalidUnion(`branch ${index} cannot be a function`);
|
|
37
|
+
return new Type(defs, prep);
|
|
38
|
+
});
|
|
39
|
+
const { discriminator } = this;
|
|
40
|
+
if (discriminator === undefined) return;
|
|
41
|
+
if (typeof discriminator !== 'string' || discriminator === '') {
|
|
42
|
+
throw invalidUnion('discriminator needs a field name');
|
|
43
|
+
}
|
|
44
|
+
const seen = new Set();
|
|
45
|
+
this.union.forEach((branch, index) => {
|
|
46
|
+
for (const value of valuesOf(branch, discriminator, index)) {
|
|
47
|
+
if (seen.has(value)) {
|
|
48
|
+
const shown = JSON.stringify(value);
|
|
49
|
+
throw invalidUnion(`discriminator value ${shown} is in two branches`);
|
|
50
|
+
}
|
|
51
|
+
seen.add(value);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
compile() {
|
|
57
|
+
const { required, discriminator } = this;
|
|
58
|
+
if (discriminator === undefined) return this.compilePlain(required);
|
|
59
|
+
const table = new Map();
|
|
60
|
+
for (const branch of this.union) {
|
|
61
|
+
for (const value of branch.schema[discriminator].enum) table.set(value, branch.check);
|
|
62
|
+
}
|
|
63
|
+
const expected = Array.from(table.keys());
|
|
64
|
+
return (value, context, key) => {
|
|
65
|
+
if (value === null || typeof value !== 'object') {
|
|
66
|
+
if (!required && value == null) return;
|
|
67
|
+
issues.type(context, 'object', value, key);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const check = table.get(value[discriminator]);
|
|
71
|
+
if (check !== undefined) {
|
|
72
|
+
check(value, context, key);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const nested = key !== undefined;
|
|
76
|
+
if (nested) context.path.push(key);
|
|
77
|
+
issues.union(context, expected, discriminator, discriminator);
|
|
78
|
+
if (nested) context.path.pop();
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
compilePlain(required) {
|
|
83
|
+
const checks = this.union.map((branch) => branch.check);
|
|
84
|
+
const expected = this.union.map(nameOf);
|
|
85
|
+
const { length } = checks;
|
|
86
|
+
return (value, context, key) => {
|
|
87
|
+
if (!required && value == null) return;
|
|
88
|
+
const { count } = context;
|
|
89
|
+
for (let index = 0; index < length; index += 1) {
|
|
90
|
+
checks[index](value, context, key);
|
|
91
|
+
if (context.count === count) return;
|
|
92
|
+
context.issues.length = count;
|
|
93
|
+
context.count = count;
|
|
94
|
+
}
|
|
95
|
+
issues.union(context, expected, undefined, key);
|
|
96
|
+
};
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
module.exports = { union };
|
package/src/result.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
const { BRAND, INSPECT, hasBrand } = require('./util.js');
|
|
2
|
+
const { EMPTY, NONE, absorb, finalize, describe, toDotPath } = require('./issues.js');
|
|
3
|
+
const en = require('./locales/en.js');
|
|
4
|
+
|
|
5
|
+
// Defines an own data property, so a key such as `__proto__` taken from a
|
|
6
|
+
// path lands on the object instead of changing its prototype.
|
|
7
|
+
const define = (target, key, value) => {
|
|
8
|
+
Object.defineProperty(target, key, {
|
|
9
|
+
value,
|
|
10
|
+
enumerable: true,
|
|
11
|
+
writable: true,
|
|
12
|
+
configurable: true,
|
|
13
|
+
});
|
|
14
|
+
return value;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
class ValidationResult {
|
|
18
|
+
#root;
|
|
19
|
+
#messages;
|
|
20
|
+
#errors = null;
|
|
21
|
+
|
|
22
|
+
// A check builds its result from its context, which carries the root label,
|
|
23
|
+
// the locale of the call and the issues under these names; a validator that
|
|
24
|
+
// builds one of its own needs none of them: its issues are relative to the
|
|
25
|
+
// field the validator belongs to.
|
|
26
|
+
constructor(options = {}) {
|
|
27
|
+
const { root = '', messages = en, issues = NONE } = options;
|
|
28
|
+
this.#root = root;
|
|
29
|
+
this.#messages = messages;
|
|
30
|
+
this.issues = issues;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get valid() {
|
|
34
|
+
return this.issues.length === 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// The messages with their location, rendered on first use.
|
|
38
|
+
get errors() {
|
|
39
|
+
if (this.#errors === null) {
|
|
40
|
+
const errors = [];
|
|
41
|
+
for (const issue of this.issues) errors.push(describe(this.#messages, this.#root, issue));
|
|
42
|
+
this.#errors = errors;
|
|
43
|
+
}
|
|
44
|
+
return this.#errors;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get summary() {
|
|
48
|
+
return this.errors.join('\n');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
add(error, code = 'custom') {
|
|
52
|
+
const context = { issues: this.issues, count: this.issues.length, limit: Infinity, path: [] };
|
|
53
|
+
absorb(context, error, code, EMPTY);
|
|
54
|
+
this.issues = context.issues;
|
|
55
|
+
finalize(this.issues, this.#messages);
|
|
56
|
+
this.#errors = null;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Messages by dotted path, for forms: `formErrors` holds the messages of
|
|
61
|
+
// the value itself, `fieldErrors` those of its fields.
|
|
62
|
+
flatten() {
|
|
63
|
+
const formErrors = [];
|
|
64
|
+
const fieldErrors = {};
|
|
65
|
+
for (const issue of this.issues) {
|
|
66
|
+
const { path, message } = issue;
|
|
67
|
+
if (path.length === 0) {
|
|
68
|
+
formErrors.push(message);
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const key = toDotPath(path);
|
|
72
|
+
const list = Object.hasOwn(fieldErrors, key)
|
|
73
|
+
? fieldErrors[key]
|
|
74
|
+
: define(fieldErrors, key, []);
|
|
75
|
+
list.push(message);
|
|
76
|
+
}
|
|
77
|
+
return { formErrors, fieldErrors };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Messages as a tree that follows the value: `properties` by key, `items`
|
|
81
|
+
// by index.
|
|
82
|
+
tree() {
|
|
83
|
+
const root = { errors: [] };
|
|
84
|
+
for (const issue of this.issues) {
|
|
85
|
+
let node = root;
|
|
86
|
+
for (const key of issue.path) {
|
|
87
|
+
if (typeof key === 'number') {
|
|
88
|
+
if (node.items === undefined) node.items = [];
|
|
89
|
+
if (node.items[key] === undefined) node.items[key] = { errors: [] };
|
|
90
|
+
node = node.items[key];
|
|
91
|
+
} else {
|
|
92
|
+
if (node.properties === undefined) node.properties = {};
|
|
93
|
+
const name = String(key);
|
|
94
|
+
node = Object.hasOwn(node.properties, name)
|
|
95
|
+
? node.properties[name]
|
|
96
|
+
: define(node.properties, name, { errors: [] });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
node.errors.push(issue.message);
|
|
100
|
+
}
|
|
101
|
+
return root;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// The issues `add` would record for a validator's return value.
|
|
105
|
+
static issuesOf(error, path = [], code = 'custom') {
|
|
106
|
+
const context = { issues: [], count: 0, limit: Infinity, path };
|
|
107
|
+
absorb(context, error, code, EMPTY);
|
|
108
|
+
finalize(context.issues, en);
|
|
109
|
+
return context.issues;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
static isInstance(error) {
|
|
113
|
+
return hasBrand(error, 'ValidationResult');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
[INSPECT](depth, options, inspect) {
|
|
117
|
+
const { valid, errors, issues } = this;
|
|
118
|
+
return `ValidationResult ${inspect({ valid, errors, issues }, options)}`;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
ValidationResult.prototype[BRAND] = 'ValidationResult';
|
|
123
|
+
|
|
124
|
+
module.exports = { ValidationResult };
|