@alexify/metaschema 3.3.0 → 3.4.1

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.
Files changed (53) hide show
  1. package/CHANGELOG.md +251 -1
  2. package/README.md +31 -20
  3. package/SECURITY.md +24 -1
  4. package/index.d.ts +280 -48
  5. package/package.json +13 -3
  6. package/src/checks.js +789 -0
  7. package/src/codec.js +37 -13
  8. package/src/coerce/index.js +34 -1
  9. package/src/compile/emit.js +87 -28
  10. package/src/compile/index.js +9 -4
  11. package/src/definition/index.d.ts +19 -0
  12. package/src/definition/index.js +27 -0
  13. package/src/derive/index.js +23 -13
  14. package/src/errors.js +23 -0
  15. package/src/generators/fromjsonschema.js +14 -0
  16. package/src/generators/index.js +1 -1
  17. package/src/generators/jsonschema.js +44 -7
  18. package/src/generators/typescript.js +11 -0
  19. package/src/index.js +3 -2
  20. package/src/issues.js +9 -7
  21. package/src/kinds.js +24 -2
  22. package/src/lint.js +64 -2
  23. package/src/locales/en.js +6 -0
  24. package/src/locales/uk.js +6 -0
  25. package/src/metadata.js +2 -24
  26. package/src/model/data.js +68 -11
  27. package/src/model/diff.js +7 -4
  28. package/src/model/index.js +1 -1
  29. package/src/model/infer.js +13 -6
  30. package/src/model/merge.js +7 -3
  31. package/src/model/restore.js +60 -8
  32. package/src/model/sample.js +26 -1
  33. package/src/model.js +25 -53
  34. package/src/mongodb/bson.js +15 -3
  35. package/src/mongodb/document.js +10 -2
  36. package/src/preprocessor.js +14 -5
  37. package/src/prototypes/abstract.js +32 -64
  38. package/src/prototypes/collections.js +3 -90
  39. package/src/prototypes/flags.js +34 -0
  40. package/src/prototypes/json.js +0 -11
  41. package/src/prototypes/reference.js +0 -76
  42. package/src/prototypes/scalars.js +17 -96
  43. package/src/prototypes/schema.js +1 -10
  44. package/src/prototypes/tuple.js +5 -31
  45. package/src/prototypes/union.js +12 -82
  46. package/src/rules.js +33 -157
  47. package/src/schema.js +108 -233
  48. package/src/snapshot.js +216 -41
  49. package/src/stream/index.js +1 -1
  50. package/src/struct.js +6 -91
  51. package/src/types.js +31 -10
  52. package/src/util.js +48 -0
  53. package/src/validation.js +313 -0
@@ -1,4 +1,3 @@
1
- const { issues } = require('../issues.js');
2
1
  const { SchemaDefinitionError } = require('../errors.js');
3
2
  const { isMap, isSet } = require('../util.js');
4
3
 
@@ -9,10 +8,9 @@ const invalidKey = (type) => {
9
8
  return new SchemaDefinitionError('ERR_INVALID_DEFINITION', reason);
10
9
  };
11
10
 
12
- // Collections iterate their source directly: an object with for...in over
13
- // its own keys, a Map and a Set through their iterators, an array by index.
14
- // A collection pushes its own key on the path before its elements and pops
15
- // it after them; an element is checked with its key or index.
11
+ // The collections: a dictionary (`object`, `map`) of a key type and a value
12
+ // definition, a list (`array`, `set`) of an element definition. Their checks
13
+ // are `COMPILE` of checks.js.
16
14
  const object = {
17
15
  rules: ['length'],
18
16
  options: ['key', 'value', 'length'],
@@ -38,34 +36,6 @@ const object = {
38
36
  this.value = new Type(defs, prep);
39
37
  },
40
38
 
41
- compile() {
42
- const { required, type, key: keyType } = this;
43
- const { check } = this.value;
44
- return (value, context, key) => {
45
- if (value === null || typeof value !== 'object' || Array.isArray(value)) {
46
- if (!required && value == null) return;
47
- issues.type(context, type, value, key);
48
- return;
49
- }
50
- const nested = key !== undefined;
51
- if (nested) context.path.push(key);
52
- let count = 0;
53
- for (const name in value) {
54
- if (!Object.hasOwn(value, name)) continue;
55
- if (context.count >= context.limit) break;
56
- count += 1;
57
- // oxlint-disable-next-line valid-typeof
58
- if (typeof name !== keyType) {
59
- issues.key(context, keyType, name);
60
- break;
61
- }
62
- check(value[name], context, name);
63
- }
64
- if (count === 0 && required) issues.required(context);
65
- if (nested) context.path.pop();
66
- };
67
- },
68
-
69
39
  isInstance(value) {
70
40
  return value !== null && typeof value === 'object' && !Array.isArray(value);
71
41
  },
@@ -74,64 +44,11 @@ const object = {
74
44
  const map = {
75
45
  ...object,
76
46
 
77
- compile() {
78
- const { required, type, key: keyType } = this;
79
- const { check } = this.value;
80
- return (value, context, key) => {
81
- if (!isMap(value)) {
82
- if (!required && value == null) return;
83
- issues.type(context, type, value, key);
84
- return;
85
- }
86
- if (value.size === 0) {
87
- if (required) issues.required(context, key);
88
- return;
89
- }
90
- const nested = key !== undefined;
91
- if (nested) context.path.push(key);
92
- for (const pair of value) {
93
- if (context.count >= context.limit) break;
94
- const name = pair[0];
95
- // oxlint-disable-next-line valid-typeof
96
- if (typeof name !== keyType) {
97
- issues.key(context, keyType, name);
98
- break;
99
- }
100
- check(pair[1], context, name);
101
- }
102
- if (nested) context.path.pop();
103
- };
104
- },
105
-
106
47
  isInstance(value) {
107
48
  return isMap(value);
108
49
  },
109
50
  };
110
51
 
111
- // An array and a Set are walked the same way, by position; only the type
112
- // test differs.
113
- const compileList = (type, isList) => {
114
- const { required } = type;
115
- const { check } = type.value;
116
- const name = type.type;
117
- return (value, context, key) => {
118
- if (!isList(value)) {
119
- if (!required && value == null) return;
120
- issues.type(context, name, value, key);
121
- return;
122
- }
123
- const nested = key !== undefined;
124
- if (nested) context.path.push(key);
125
- let index = 0;
126
- for (const item of value) {
127
- if (context.count >= context.limit) break;
128
- check(item, context, index);
129
- index += 1;
130
- }
131
- if (nested) context.path.pop();
132
- };
133
- };
134
-
135
52
  const array = {
136
53
  kind: 'struct',
137
54
  rules: ['length'],
@@ -144,10 +61,6 @@ const array = {
144
61
  this.value = new Type(defs, prep);
145
62
  },
146
63
 
147
- compile() {
148
- return compileList(this, this.isInstance);
149
- },
150
-
151
64
  isInstance(value) {
152
65
  return Array.isArray(value);
153
66
  },
@@ -0,0 +1,34 @@
1
+ const { SchemaDefinitionError } = require('../errors.js');
2
+
3
+ const invalidFlags = (type) => {
4
+ const hint = `Type "${type}" needs a list of distinct names: { ${type}: ['read', 'write'] }`;
5
+ return new SchemaDefinitionError('ERR_INVALID_DEFINITION', hint);
6
+ };
7
+
8
+ // A set of named switches: an object whose keys are names of the list, each
9
+ // true or false, any of them left out. A key the list does not have is one
10
+ // the schema does not have, reported as `unexpected` under the unknown-keys
11
+ // policy of the check, as a struct reports its own.
12
+ const flags = {
13
+ kind: 'struct',
14
+ options: ['flags'],
15
+
16
+ construct(def) {
17
+ const names = def.flags;
18
+ if (!Array.isArray(names) || names.length === 0) throw invalidFlags(this.type);
19
+ // `__proto__` is no name: a value of names is copied into a plain object
20
+ // under strip, where the key would set its prototype.
21
+ const seen = new Set();
22
+ for (const name of names) {
23
+ if (typeof name !== 'string' || name === '' || name === '__proto__' || seen.has(name)) {
24
+ throw invalidFlags(this.type);
25
+ }
26
+ seen.add(name);
27
+ }
28
+ // A copy: the checks are compiled on first use and read the list then,
29
+ // so the program's array changed later must not reach them.
30
+ this.flags = Object.freeze([...names]);
31
+ },
32
+ };
33
+
34
+ module.exports = { flags };
@@ -1,19 +1,8 @@
1
- const { issues } = require('../issues.js');
2
-
3
1
  const json = {
4
2
  kind: 'struct',
5
3
  options: [],
6
4
 
7
5
  construct() {},
8
-
9
- compile() {
10
- const { required } = this;
11
- return (value, context, key) => {
12
- if (value !== null && typeof value === 'object') return;
13
- if (!required && value == null) return;
14
- issues.type(context, 'object', value, key);
15
- };
16
- },
17
6
  };
18
7
 
19
8
  module.exports = { json };
@@ -1,43 +1,8 @@
1
1
  const { SchemaDefinitionError } = require('../errors.js');
2
- const { RUN } = require('../util.js');
3
- const { issues } = require('../issues.js');
4
2
 
5
3
  const invalidEmbed = () =>
6
4
  new SchemaDefinitionError('ERR_INVALID_DEFINITION', 'Option "embed" needs a boolean');
7
5
 
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, and where a graph
11
- // nested deeper than the check allows is reported instead of overflowing the
12
- // stack.
13
- const runReferenced = (schema, value, context, key) => {
14
- if (value === null || typeof value !== 'object') {
15
- schema[RUN](value, context, key);
16
- return;
17
- }
18
- if (context.depth >= context.maxDepth) {
19
- issues.depth(context, context.maxDepth, key);
20
- return;
21
- }
22
- let { seen } = context;
23
- if (seen === null) {
24
- // The first embedded record creates the set, and the value the check
25
- // started from joins it then, so a check that embeds nothing pays nothing.
26
- seen = new Set();
27
- if (context.source !== null) seen.add(context.source);
28
- context.seen = seen;
29
- }
30
- if (seen.has(value)) {
31
- issues.circular(context, key);
32
- return;
33
- }
34
- seen.add(value);
35
- context.depth += 1;
36
- schema[RUN](value, context, key);
37
- context.depth -= 1;
38
- seen.delete(value);
39
- };
40
-
41
6
  // Whether a referenced value is the record itself (the graph view) or its
42
7
  // id (the storage view): the mode of the check when it says so, otherwise
43
8
  // the field's `embed`, otherwise the store of the target's kind. A stored
@@ -67,47 +32,6 @@ const reference = {
67
32
  this.type = target;
68
33
  this.root.relations.add({ to: target, type: relation });
69
34
  },
70
-
71
- // The target is looked up at check time: entities of a model are built in
72
- // any order and a namespace may be attached later.
73
- compile() {
74
- const { one, many, root, required, embed } = this;
75
- if (one) {
76
- return (value, context, key) => {
77
- if (!required && value == null) return;
78
- const schema = root.findReference(one);
79
- if (!schema) {
80
- issues.reference(context, one, key);
81
- return;
82
- }
83
- if (embeds(schema, embed, context.references)) runReferenced(schema, value, context, key);
84
- else if (typeof value !== 'string') issues.type(context, 'string', value, key);
85
- };
86
- }
87
- const expected = `array of ${many}`;
88
- return (value, context, key) => {
89
- if (!required && value == null) return;
90
- const schema = root.findReference(many);
91
- if (!schema) {
92
- issues.reference(context, many, key);
93
- return;
94
- }
95
- if (!Array.isArray(value)) {
96
- issues.type(context, expected, value, key);
97
- return;
98
- }
99
- const nested = key !== undefined;
100
- if (nested) context.path.push(key);
101
- const embedded = embeds(schema, embed, context.references);
102
- for (let index = 0; index < value.length; index += 1) {
103
- if (context.count >= context.limit) break;
104
- const item = value[index];
105
- if (embedded) runReferenced(schema, item, context, index);
106
- else if (typeof item !== 'string') issues.type(context, 'string', item, index);
107
- }
108
- if (nested) context.path.pop();
109
- };
110
- },
111
35
  };
112
36
 
113
37
  module.exports = { reference, embeds };
@@ -1,30 +1,17 @@
1
1
  const { SchemaDefinitionError } = require('../errors.js');
2
- const { issues } = require('../issues.js');
3
- const { ENUM_SET_SIZE, isDate } = require('../util.js');
4
2
 
5
3
  const missingEnum = (type) => {
6
4
  const hint = `Type "${type}" needs a list of values: { type: '${type}', enum: [...] }`;
7
5
  return new SchemaDefinitionError('ERR_INVALID_ENUM', hint);
8
6
  };
9
7
 
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
-
8
+ // The scalar types: what a field of each holds (its check is `COMPILE` of
9
+ // checks.js, the validation half of the core).
19
10
  const scalar = {
20
11
  kind: 'scalar',
21
12
  options: [],
22
13
 
23
14
  construct() {},
24
-
25
- compile() {
26
- return scalarCheck(this.scalar, this.required);
27
- },
28
15
  };
29
16
 
30
17
  const enumerable = {
@@ -36,97 +23,25 @@ const enumerable = {
36
23
  if (!Array.isArray(values) || values.length === 0) throw missingEnum(this.type);
37
24
  this.enum = values;
38
25
  },
39
-
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
26
  };
58
27
 
59
28
  // 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
- };
29
+ const integer = { ...scalar, scalar: 'number', rules: ['min', 'max'], options: ['min', 'max'] };
77
30
 
78
31
  // A Date instance with a time: an invalid Date is a type error.
79
- const date = {
80
- kind: 'scalar',
81
- scalar: 'date',
82
- options: [],
32
+ const date = { ...scalar, scalar: 'date' };
83
33
 
84
- construct() {},
34
+ // A Uint8Array of any realm, a Buffer included: the value of binary data,
35
+ // base64 text on a JSON wire. `length` counts its bytes.
36
+ const bytes = { ...scalar, scalar: 'bytes', rules: ['length'], options: ['length'] };
85
37
 
86
- compile() {
87
- const { required } = this;
88
- return (value, context, key) => {
89
- if (isDate(value) && !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
- };
38
+ const nothing = { ...scalar, scalar: 'null' };
112
39
 
113
40
  // Any value at all, null and undefined included; `required` still says
114
41
  // whether the key must be there.
115
- const accept = () => {};
116
-
117
- const any = {
118
- kind: 'scalar',
119
- scalar: 'any',
120
- options: [],
121
-
122
- construct() {},
42
+ const any = { ...scalar, scalar: 'any' };
123
43
 
124
- compile() {
125
- return accept;
126
- },
127
- };
128
-
129
- const unknown = { ...any, scalar: 'unknown' };
44
+ const unknown = { ...scalar, scalar: 'unknown' };
130
45
 
131
46
  const string = {
132
47
  ...scalar,
@@ -134,7 +49,12 @@ const string = {
134
49
  rules: ['length', 'pattern', 'format'],
135
50
  options: ['length', 'unicode', 'pattern', 'format'],
136
51
  };
137
- const number = { ...scalar, scalar: 'number', rules: ['min', 'max'], options: ['min', 'max'] };
52
+ const number = {
53
+ ...scalar,
54
+ scalar: 'number',
55
+ rules: ['min', 'max', 'decimals'],
56
+ options: ['min', 'max', 'decimals'],
57
+ };
138
58
  const bigint = { ...scalar, scalar: 'bigint', rules: ['min', 'max'], options: ['min', 'max'] };
139
59
  const boolean = { ...scalar, scalar: 'boolean' };
140
60
 
@@ -145,6 +65,7 @@ module.exports = {
145
65
  bigint,
146
66
  boolean,
147
67
  date,
68
+ bytes,
148
69
  null: nothing,
149
70
  any,
150
71
  unknown,
@@ -1,5 +1,5 @@
1
1
  const { hasBrand } = require('../util.js');
2
- const { createStruct, isStruct, checkOf } = require('../struct.js');
2
+ const { createStruct, isStruct } = require('../struct.js');
3
3
  const { SchemaDefinitionError } = require('../errors.js');
4
4
 
5
5
  const missingSchema = (type) => {
@@ -43,15 +43,6 @@ const schema = {
43
43
  else this.schema = createStruct(definition, prep);
44
44
  adopt(this, definition);
45
45
  },
46
-
47
- compile() {
48
- const { required } = this;
49
- const check = isStruct(this.schema) ? checkOf(this.schema) : this.schema.check;
50
- return (value, context, key) => {
51
- if (!required && value == null) return;
52
- check(value, context, key);
53
- };
54
- },
55
46
  };
56
47
 
57
48
  module.exports = { schema };
@@ -1,19 +1,18 @@
1
- const { isFirstUpper } = require('../metautil.js');
2
1
  const { formatters } = require('../util.js');
3
2
  const { SchemaDefinitionError } = require('../errors.js');
4
- const { issues } = require('../issues.js');
5
3
 
6
4
  const invalidTuple = (reason) => new SchemaDefinitionError('ERR_INVALID_TUPLE', `Tuple ${reason}`);
7
5
 
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).
6
+ // A named element is `{ name: 'type' }`: one key that is not `type` or a type
7
+ // name, holding a type name (a string is no metadata of a kind, so `{ X:
8
+ // 'number' }` is a name too). Anything else is a definition of its own (a
9
+ // nested struct, a collection, a tuple).
11
10
  const nameOf = (element, types) => {
12
11
  if (element === null || typeof element !== 'object' || Array.isArray(element)) return null;
13
12
  const keys = Object.keys(element);
14
13
  if (keys.length !== 1 || typeof element[keys[0]] !== 'string') return null;
15
14
  const { field } = formatters.key(keys[0]);
16
- if (field === 'type' || types[field] || isFirstUpper(field)) return null;
15
+ if (field === 'type' || types[field]) return null;
17
16
  return keys[0];
18
17
  };
19
18
 
@@ -43,31 +42,6 @@ const tuple = {
43
42
  }
44
43
  this.value = built;
45
44
  },
46
-
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
- };
70
- },
71
45
  };
72
46
 
73
47
  module.exports = { tuple };
@@ -1,11 +1,7 @@
1
1
  const { SchemaDefinitionError } = require('../errors.js');
2
- const { issues, release } = require('../issues.js');
3
2
 
4
3
  const invalidUnion = (reason) => new SchemaDefinitionError('ERR_INVALID_UNION', `Union ${reason}`);
5
4
 
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
5
  // The values of the discriminator field of a branch: the branch must be a
10
6
  // struct whose field of that name is an enum, so the branch is known from
11
7
  // one lookup.
@@ -18,29 +14,8 @@ const valuesOf = (branch, discriminator, index) => {
18
14
  return field.enum;
19
15
  };
20
16
 
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.
17
+ // A value of one of several definitions: plain, or picked by the value of
18
+ // a discriminator field (`COMPILE.union` of checks.js has the check).
44
19
  const union = {
45
20
  kind: 'struct',
46
21
  options: ['union', 'discriminator'],
@@ -66,12 +41,19 @@ const union = {
66
41
  for (let index = 0; index < built.length; index += 1) {
67
42
  const values = valuesOf(built[index], discriminator, index);
68
43
  // 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) {
44
+ // a default on the discriminator could never decide anything; nor can
45
+ // a missing or a null one, which no branch has among its values.
46
+ const tag = built[index].schema[discriminator];
47
+ if (tag.default !== undefined) {
71
48
  throw invalidUnion(
72
49
  `discriminator "${discriminator}" of branch ${index} cannot have a default`,
73
50
  );
74
51
  }
52
+ if (tag.required === false || tag.nullable === true) {
53
+ throw invalidUnion(
54
+ `discriminator "${discriminator}" of branch ${index} must be required and not nullable`,
55
+ );
56
+ }
75
57
  for (let at = 0; at < values.length; at += 1) {
76
58
  const value = values[at];
77
59
  if (seen.has(value)) {
@@ -82,58 +64,6 @@ const union = {
82
64
  }
83
65
  }
84
66
  },
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) release(context.async, pending);
133
- }
134
- issues.union(context, expected, undefined, key);
135
- };
136
- },
137
67
  };
138
68
 
139
- module.exports = { union, nameOf, tableOf };
69
+ module.exports = { union };