@modulify/validator 0.2.1 → 0.3.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.
- package/CHANGELOG.md +29 -1
- package/README.md +12 -2
- package/dist/assert.cjs +99 -63
- package/dist/assert.d.cts +37 -0
- package/dist/assert.d.mts +37 -0
- package/dist/assert.d.ts +24 -3
- package/dist/assert.mjs +94 -64
- package/dist/assertions.cjs +283 -178
- package/dist/assertions.d.cts +56 -0
- package/dist/assertions.d.mts +56 -0
- package/dist/assertions.d.ts +43 -45
- package/dist/assertions.mjs +274 -201
- package/dist/checkers.cjs +23 -0
- package/dist/checkers.d.cts +8 -0
- package/dist/checkers.d.mts +8 -0
- package/dist/checkers.d.ts +1 -1
- package/dist/checkers.mjs +16 -0
- package/dist/combinators.cjs +305 -304
- package/dist/combinators.d.cts +16 -0
- package/dist/combinators.d.mts +16 -0
- package/dist/combinators.d.ts +15 -16
- package/dist/combinators.mjs +305 -314
- package/dist/constraints.cjs +23 -0
- package/dist/constraints.d.cts +4 -0
- package/dist/constraints.d.mts +4 -0
- package/dist/constraints.d.ts +2 -2
- package/dist/constraints.mjs +21 -0
- package/dist/extractors.cjs +6 -0
- package/dist/extractors.d.cts +2 -0
- package/dist/extractors.d.mts +2 -0
- package/dist/extractors.mjs +5 -0
- package/dist/index.cjs +140 -219
- package/dist/index.d.cts +12 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.d.ts +9 -9
- package/dist/index.mjs +93 -222
- package/dist/json-schema.cjs +364 -489
- package/dist/json-schema.d.cts +14 -0
- package/dist/json-schema.d.mts +14 -0
- package/dist/json-schema.d.ts +3 -3
- package/dist/json-schema.mjs +365 -492
- package/dist/metadata.cjs +98 -7
- package/dist/metadata.d.cts +8 -0
- package/dist/metadata.d.mts +8 -0
- package/dist/metadata.d.ts +2 -2
- package/dist/metadata.mjs +93 -7
- package/dist/predicates.cjs +98 -41
- package/dist/predicates.d.cts +77 -0
- package/dist/predicates.d.mts +77 -0
- package/dist/predicates.d.ts +25 -4
- package/dist/predicates.mjs +92 -66
- package/dist/types/index.d.cts +984 -0
- package/dist/types/index.d.mts +984 -0
- package/dist/types/index.d.ts +984 -0
- package/dist/types/json-schema.d.cts +75 -0
- package/dist/types/json-schema.d.mts +75 -0
- package/dist/types/json-schema.d.ts +75 -0
- package/dist/violations.cjs +81 -0
- package/dist/violations.d.cts +29 -0
- package/dist/violations.d.mts +29 -0
- package/dist/violations.d.ts +1 -1
- package/dist/violations.mjs +80 -0
- package/docs/RELEASING.md +66 -0
- package/docs/en/00-index.md +2 -0
- package/docs/en/01-shape-api.md +35 -10
- package/docs/en/02-metadata-and-introspection.md +2 -1
- package/docs/en/03-violations.md +1 -1
- package/docs/en/04-json-schema-export.md +5 -0
- package/docs/en/05-public-api.md +35 -3
- package/docs/en/06-common-recipes.md +2 -1
- package/docs/en/07-ai-reference.md +5 -2
- package/docs/en/08-violation-code-types.md +28 -2
- package/docs/en/09-migration.md +75 -0
- package/docs/ru/00-index.md +2 -0
- package/docs/ru/01-shape-api.md +35 -10
- package/docs/ru/02-metadata-and-introspection.md +2 -1
- package/docs/ru/03-violations.md +1 -1
- package/docs/ru/04-json-schema-export.md +5 -0
- package/docs/ru/05-public-api.md +35 -3
- package/docs/ru/06-common-recipes.md +2 -1
- package/docs/ru/07-ai-reference.md +5 -2
- package/docs/ru/08-violation-code-types.md +28 -2
- package/docs/ru/09-migration.md +76 -0
- package/docs/ru/README.md +10 -2
- package/package.json +63 -37
- package/types/index.d.ts +275 -115
- package/dist/metadata.cjs.js +0 -130
- package/dist/metadata.es.js +0 -131
package/dist/combinators.mjs
CHANGED
|
@@ -1,341 +1,332 @@
|
|
|
1
|
+
import { arrayify, matchesConstraints } from "./constraints.mjs";
|
|
2
|
+
import { attachConstraintDescriptor, describeConstraints } from "./metadata.mjs";
|
|
1
3
|
import { assert } from "./assert.mjs";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
import { isArray, isExact, isNull, isRecord, isUndefined } from "./predicates.mjs";
|
|
5
|
+
//#region src/combinators.ts
|
|
6
|
+
var describeConstraintMap = (constraints) => {
|
|
7
|
+
const described = {};
|
|
8
|
+
Reflect.ownKeys(constraints).forEach((key) => {
|
|
9
|
+
described[key] = describeConstraints(constraints[key]);
|
|
10
|
+
});
|
|
11
|
+
return described;
|
|
10
12
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
metadata: Object.freeze({ ...descriptor.metadata })
|
|
18
|
-
};
|
|
13
|
+
var normalizeRuleDescriptor = (descriptor) => {
|
|
14
|
+
if (!descriptor.metadata) return descriptor;
|
|
15
|
+
return {
|
|
16
|
+
...descriptor,
|
|
17
|
+
metadata: Object.freeze({ ...descriptor.metadata })
|
|
18
|
+
};
|
|
19
19
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
var normalizeAsyncRuleDescriptor = (descriptor) => ({
|
|
21
|
+
...normalizeRuleDescriptor(descriptor),
|
|
22
|
+
async: true
|
|
23
|
+
});
|
|
24
|
+
var passthrough = (kind, accepts, constraints) => /* @__PURE__ */ attachConstraintDescriptor({
|
|
25
|
+
check(value) {
|
|
26
|
+
return accepts(value) || matchesConstraints(value, constraints);
|
|
27
|
+
},
|
|
28
|
+
run(validate, value, path) {
|
|
29
|
+
return accepts(value) ? [] : [validate(value, constraints, path)];
|
|
30
|
+
}
|
|
27
31
|
}, () => ({
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
kind,
|
|
33
|
+
child: describeConstraints(constraints)
|
|
30
34
|
}));
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
var keysOf = (value) => Object.keys(value);
|
|
36
|
+
var hasOwn = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
|
|
37
|
+
var hasUnknownKeys = (value, descriptor) => Object.keys(value).some((key) => !hasOwn(descriptor, key));
|
|
38
|
+
var collectUnknownKeyViolations = (value, path, descriptor) => Object.keys(value).filter((key) => !hasOwn(descriptor, key)).map((key) => [{
|
|
39
|
+
value: value[key],
|
|
40
|
+
path: [...path, key],
|
|
41
|
+
violates: {
|
|
42
|
+
kind: "validator",
|
|
43
|
+
name: "shape",
|
|
44
|
+
code: "shape.unknown-key",
|
|
45
|
+
args: []
|
|
46
|
+
}
|
|
38
47
|
}]);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
});
|
|
46
|
-
return picked;
|
|
48
|
+
var pickDescriptor = (descriptor, keys) => {
|
|
49
|
+
const picked = {};
|
|
50
|
+
keys.forEach((key) => {
|
|
51
|
+
if (hasOwn(descriptor, key)) picked[key] = descriptor[key];
|
|
52
|
+
});
|
|
53
|
+
return picked;
|
|
47
54
|
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
55
|
+
var omitDescriptor = (descriptor, keys) => {
|
|
56
|
+
const omitted = { ...descriptor };
|
|
57
|
+
keys.forEach((key) => {
|
|
58
|
+
delete omitted[key];
|
|
59
|
+
});
|
|
60
|
+
return omitted;
|
|
54
61
|
};
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
62
|
+
var extendDescriptor = (descriptor, extension) => ({
|
|
63
|
+
...descriptor,
|
|
64
|
+
...extension
|
|
58
65
|
});
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
var partialDescriptor = (descriptor) => {
|
|
67
|
+
const partial = {};
|
|
68
|
+
keysOf(descriptor).forEach((key) => {
|
|
69
|
+
partial[key] = optional(descriptor[key]);
|
|
70
|
+
});
|
|
71
|
+
return partial;
|
|
65
72
|
};
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
return Reflect.get(Object(current), key);
|
|
73
|
+
var getPathValue = (value, path) => path.reduce((current, key) => {
|
|
74
|
+
if (current === null || current === void 0) return;
|
|
75
|
+
return Reflect.get(Object(current), key);
|
|
71
76
|
}, value);
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
77
|
+
var toPath = (selector) => typeof selector === "object" ? [...selector] : [selector];
|
|
78
|
+
var toShapeRefinementViolations = (value, path, result) => {
|
|
79
|
+
if (result === null || result === void 0) return [];
|
|
80
|
+
return arrayify(result).filter((issue) => issue !== null && issue !== void 0).map((issue) => {
|
|
81
|
+
const issuePath = issue.path ? [...issue.path] : [];
|
|
82
|
+
return {
|
|
83
|
+
value: issue.value ?? getPathValue(value, issuePath),
|
|
84
|
+
path: [...path, ...issuePath],
|
|
85
|
+
violates: {
|
|
86
|
+
kind: "validator",
|
|
87
|
+
name: "shape",
|
|
88
|
+
code: issue.code,
|
|
89
|
+
args: issue.args ?? []
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
var collectShapeRefinementViolations = (value, path, refinements) => refinements.flatMap((refinement) => {
|
|
95
|
+
const result = refinement(value);
|
|
96
|
+
if (result instanceof Promise) throw new Error("Found asynchronous object-level shape refinement");
|
|
97
|
+
return toShapeRefinementViolations(value, path, result);
|
|
91
98
|
});
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return isRecord(value) && keys.every((key) => matchesConstraints(value[key], descriptor[key])) && (unknownKeys === "passthrough" || !hasUnknownKeys(value, descriptor)) && collectShapeRefinementViolations(value, [], refinements).length === 0;
|
|
99
|
-
},
|
|
100
|
-
run(validate, value, path) {
|
|
101
|
-
if (!isRecord(value)) {
|
|
102
|
-
return [[{
|
|
103
|
-
value,
|
|
104
|
-
path,
|
|
105
|
-
violates: { kind: "validator", name: "shape", code: "type.record", args: [] }
|
|
106
|
-
}]];
|
|
107
|
-
}
|
|
108
|
-
const validations = keys.reduce((all, key) => [
|
|
109
|
-
...all,
|
|
110
|
-
validate(value[key], descriptor[key], [...path, key])
|
|
111
|
-
], []);
|
|
112
|
-
if (unknownKeys === "strict") {
|
|
113
|
-
validations.push(...collectUnknownKeyViolations(value, path, descriptor));
|
|
114
|
-
}
|
|
115
|
-
if (validations.some((validation) => validation instanceof Promise)) {
|
|
116
|
-
return [Promise.all(validations.map((validation) => Promise.resolve(validation))).then((results) => {
|
|
117
|
-
const structuralViolations2 = results.flat();
|
|
118
|
-
return structuralViolations2.length > 0 ? structuralViolations2 : collectShapeRefinementViolations(value, path, refinements);
|
|
119
|
-
})];
|
|
120
|
-
}
|
|
121
|
-
const structuralViolations = validations.flat();
|
|
122
|
-
return structuralViolations.length > 0 ? validations : [collectShapeRefinementViolations(value, path, refinements)];
|
|
123
|
-
},
|
|
124
|
-
refine(refinement, ruleDescriptor = { kind: "refine" }) {
|
|
125
|
-
return createObjectShape(
|
|
126
|
-
descriptor,
|
|
127
|
-
unknownKeys,
|
|
128
|
-
[...refinements, refinement],
|
|
129
|
-
[...rules, normalizeRuleDescriptor(ruleDescriptor)]
|
|
130
|
-
);
|
|
131
|
-
},
|
|
132
|
-
fieldsMatch(selectedKeys) {
|
|
133
|
-
const [left, right] = selectedKeys;
|
|
134
|
-
const leftPath = toPath(left);
|
|
135
|
-
const rightPath = toPath(right);
|
|
136
|
-
const ruleDescriptor = {
|
|
137
|
-
kind: "fieldsMatch",
|
|
138
|
-
selectors: [left, right]
|
|
139
|
-
};
|
|
140
|
-
return createObjectShape(
|
|
141
|
-
descriptor,
|
|
142
|
-
unknownKeys,
|
|
143
|
-
[...refinements, (value) => {
|
|
144
|
-
return getPathValue(value, leftPath) === getPathValue(value, rightPath) ? [] : [{
|
|
145
|
-
path: rightPath,
|
|
146
|
-
code: "shape.fields.mismatch",
|
|
147
|
-
args: [selectedKeys]
|
|
148
|
-
}];
|
|
149
|
-
}],
|
|
150
|
-
[...rules, ruleDescriptor]
|
|
151
|
-
);
|
|
152
|
-
},
|
|
153
|
-
strict() {
|
|
154
|
-
return createObjectShape(descriptor, "strict", refinements, rules);
|
|
155
|
-
},
|
|
156
|
-
passthrough() {
|
|
157
|
-
return createObjectShape(descriptor, "passthrough", refinements, rules);
|
|
158
|
-
},
|
|
159
|
-
pick(selectedKeys) {
|
|
160
|
-
return createObjectShape(pickDescriptor(descriptor, selectedKeys), unknownKeys, [], []);
|
|
161
|
-
},
|
|
162
|
-
omit(selectedKeys) {
|
|
163
|
-
return createObjectShape(omitDescriptor(descriptor, selectedKeys), unknownKeys, [], []);
|
|
164
|
-
},
|
|
165
|
-
partial() {
|
|
166
|
-
return createObjectShape(partialDescriptor(descriptor), unknownKeys, [], []);
|
|
167
|
-
},
|
|
168
|
-
extend(extension) {
|
|
169
|
-
return createObjectShape(extendDescriptor(descriptor, extension), unknownKeys, [], []);
|
|
170
|
-
},
|
|
171
|
-
merge(shape2) {
|
|
172
|
-
return createObjectShape(extendDescriptor(descriptor, shape2.descriptor), unknownKeys, [], []);
|
|
173
|
-
}
|
|
174
|
-
}, () => ({
|
|
175
|
-
kind: "shape",
|
|
176
|
-
unknownKeys,
|
|
177
|
-
fields: describeConstraintMap(descriptor),
|
|
178
|
-
rules
|
|
179
|
-
}));
|
|
99
|
+
var collectAsyncShapeRefinementViolations = (value, path, refinements) => {
|
|
100
|
+
const results = refinements.map((refinement) => refinement(value));
|
|
101
|
+
if (results.some((result) => result instanceof Promise)) return Promise.all(results.map((result) => Promise.resolve(result))).then((resolved) => {
|
|
102
|
+
return resolved.flatMap((result) => toShapeRefinementViolations(value, path, result));
|
|
103
|
+
});
|
|
104
|
+
return results.flatMap((result) => toShapeRefinementViolations(value, path, result));
|
|
180
105
|
};
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
106
|
+
var createObjectShape = (descriptor, unknownKeys, refinements, rules) => {
|
|
107
|
+
const keys = keysOf(descriptor);
|
|
108
|
+
const addAsyncRefinement = (refinement, ruleDescriptor = { kind: "refine" }) => createObjectShape(descriptor, unknownKeys, [...refinements, refinement], [...rules, normalizeAsyncRuleDescriptor(ruleDescriptor)]);
|
|
109
|
+
const addSyncRefinement = (refinement, ruleDescriptor = { kind: "refine" }) => createObjectShape(descriptor, unknownKeys, [...refinements, refinement], [...rules, normalizeRuleDescriptor(ruleDescriptor)]);
|
|
110
|
+
return /* @__PURE__ */ attachConstraintDescriptor({
|
|
111
|
+
descriptor,
|
|
112
|
+
unknownKeys,
|
|
113
|
+
check(value) {
|
|
114
|
+
return isRecord(value) && keys.every((key) => matchesConstraints(value[key], descriptor[key])) && (unknownKeys === "passthrough" || !hasUnknownKeys(value, descriptor)) && collectShapeRefinementViolations(value, [], refinements).length === 0;
|
|
115
|
+
},
|
|
116
|
+
run(validate, value, path) {
|
|
117
|
+
if (!isRecord(value)) return [[{
|
|
118
|
+
value,
|
|
119
|
+
path,
|
|
120
|
+
violates: {
|
|
121
|
+
kind: "validator",
|
|
122
|
+
name: "shape",
|
|
123
|
+
code: "type.record",
|
|
124
|
+
args: []
|
|
125
|
+
}
|
|
126
|
+
}]];
|
|
127
|
+
const validations = keys.reduce((all, key) => [...all, validate(value[key], descriptor[key], [...path, key])], []);
|
|
128
|
+
if (unknownKeys === "strict") validations.push(...collectUnknownKeyViolations(value, path, descriptor));
|
|
129
|
+
if (validations.some((validation) => validation instanceof Promise)) return [Promise.all(validations.map((validation) => Promise.resolve(validation))).then((results) => {
|
|
130
|
+
const structuralViolations = results.flat();
|
|
131
|
+
return structuralViolations.length > 0 ? structuralViolations : Promise.resolve(collectAsyncShapeRefinementViolations(value, path, refinements));
|
|
132
|
+
})];
|
|
133
|
+
return validations.flat().length > 0 ? validations : [validate.sync ? collectShapeRefinementViolations(value, path, refinements) : collectAsyncShapeRefinementViolations(value, path, refinements)];
|
|
134
|
+
},
|
|
135
|
+
refine: /* @__PURE__ */ Object.assign(addAsyncRefinement, { sync: addSyncRefinement }),
|
|
136
|
+
fieldsMatch(selectedKeys) {
|
|
137
|
+
const [left, right] = selectedKeys;
|
|
138
|
+
const leftPath = toPath(left);
|
|
139
|
+
const rightPath = toPath(right);
|
|
140
|
+
const ruleDescriptor = {
|
|
141
|
+
kind: "fieldsMatch",
|
|
142
|
+
selectors: [left, right]
|
|
143
|
+
};
|
|
144
|
+
return createObjectShape(descriptor, unknownKeys, [...refinements, (value) => {
|
|
145
|
+
return getPathValue(value, leftPath) === getPathValue(value, rightPath) ? [] : [{
|
|
146
|
+
path: rightPath,
|
|
147
|
+
code: "shape.fields.mismatch",
|
|
148
|
+
args: [selectedKeys]
|
|
149
|
+
}];
|
|
150
|
+
}], [...rules, ruleDescriptor]);
|
|
151
|
+
},
|
|
152
|
+
strict() {
|
|
153
|
+
return createObjectShape(descriptor, "strict", refinements, rules);
|
|
154
|
+
},
|
|
155
|
+
passthrough() {
|
|
156
|
+
return createObjectShape(descriptor, "passthrough", refinements, rules);
|
|
157
|
+
},
|
|
158
|
+
pick(selectedKeys) {
|
|
159
|
+
return createObjectShape(pickDescriptor(descriptor, selectedKeys), unknownKeys, [], []);
|
|
160
|
+
},
|
|
161
|
+
omit(selectedKeys) {
|
|
162
|
+
return createObjectShape(omitDescriptor(descriptor, selectedKeys), unknownKeys, [], []);
|
|
163
|
+
},
|
|
164
|
+
partial() {
|
|
165
|
+
return createObjectShape(partialDescriptor(descriptor), unknownKeys, [], []);
|
|
166
|
+
},
|
|
167
|
+
extend(extension) {
|
|
168
|
+
return createObjectShape(extendDescriptor(descriptor, extension), unknownKeys, [], []);
|
|
169
|
+
},
|
|
170
|
+
merge(shape) {
|
|
171
|
+
return createObjectShape(extendDescriptor(descriptor, shape.descriptor), unknownKeys, [], []);
|
|
172
|
+
}
|
|
173
|
+
}, () => ({
|
|
174
|
+
kind: "shape",
|
|
175
|
+
unknownKeys,
|
|
176
|
+
fields: describeConstraintMap(descriptor),
|
|
177
|
+
rules
|
|
178
|
+
}));
|
|
191
179
|
};
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
180
|
+
var toUnionFailure = (branches, value, path, branchResults) => {
|
|
181
|
+
const matched = branchResults.find((result) => result.length === 0);
|
|
182
|
+
if (matched) return matched;
|
|
183
|
+
return [{
|
|
184
|
+
value,
|
|
185
|
+
path,
|
|
186
|
+
violates: {
|
|
187
|
+
kind: "validator",
|
|
188
|
+
name: "union",
|
|
189
|
+
code: "union.no-match",
|
|
190
|
+
args: [branches.length]
|
|
191
|
+
}
|
|
192
|
+
}, ...branchResults.flat()];
|
|
200
193
|
};
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
194
|
+
var collectUnionViolations = (branches, validate, value, path) => {
|
|
195
|
+
const branchResults = branches.map((branch) => validate(value, branch, path));
|
|
196
|
+
if (branchResults.some((result) => result instanceof Promise)) return Promise.all(branchResults.map((result) => Promise.resolve(result))).then((results) => {
|
|
197
|
+
return toUnionFailure(branches, value, path, results);
|
|
198
|
+
});
|
|
199
|
+
return toUnionFailure(branches, value, path, branchResults);
|
|
200
|
+
};
|
|
201
|
+
var each = (constraints) => /* @__PURE__ */ attachConstraintDescriptor({
|
|
202
|
+
check(value) {
|
|
203
|
+
return isArray(value) && value.every((item) => matchesConstraints(item, constraints));
|
|
204
|
+
},
|
|
205
|
+
run(validate, value, path) {
|
|
206
|
+
return isArray(value) ? value.map((item, index) => validate(item, constraints, [...path, index])) : [[{
|
|
207
|
+
value,
|
|
208
|
+
path,
|
|
209
|
+
violates: {
|
|
210
|
+
kind: "validator",
|
|
211
|
+
name: "each",
|
|
212
|
+
code: "type.array",
|
|
213
|
+
args: []
|
|
214
|
+
}
|
|
215
|
+
}]];
|
|
216
|
+
}
|
|
212
217
|
}, () => ({
|
|
213
|
-
|
|
214
|
-
|
|
218
|
+
kind: "each",
|
|
219
|
+
item: describeConstraints(constraints)
|
|
215
220
|
}));
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
221
|
+
var tuple = (constraints) => /* @__PURE__ */ attachConstraintDescriptor({
|
|
222
|
+
check(value) {
|
|
223
|
+
return isArray(value) && value.length === constraints.length && constraints.every((constraint, index) => matchesConstraints(value[index], constraint));
|
|
224
|
+
},
|
|
225
|
+
run(validate, value, path) {
|
|
226
|
+
if (!isArray(value)) return [[{
|
|
227
|
+
value,
|
|
228
|
+
path,
|
|
229
|
+
violates: {
|
|
230
|
+
kind: "validator",
|
|
231
|
+
name: "tuple",
|
|
232
|
+
code: "type.array",
|
|
233
|
+
args: []
|
|
234
|
+
}
|
|
235
|
+
}]];
|
|
236
|
+
if (value.length !== constraints.length) return [[{
|
|
237
|
+
value,
|
|
238
|
+
path,
|
|
239
|
+
violates: {
|
|
240
|
+
kind: "validator",
|
|
241
|
+
name: "tuple",
|
|
242
|
+
code: "tuple.length",
|
|
243
|
+
args: [constraints.length]
|
|
244
|
+
}
|
|
245
|
+
}]];
|
|
246
|
+
return constraints.map((constraint, index) => validate(value[index], constraint, [...path, index]));
|
|
247
|
+
}
|
|
237
248
|
}, () => ({
|
|
238
|
-
|
|
239
|
-
|
|
249
|
+
kind: "tuple",
|
|
250
|
+
items: constraints.map((constraint) => describeConstraints(constraint))
|
|
240
251
|
}));
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
252
|
+
var union = (constraints) => /* @__PURE__ */ attachConstraintDescriptor({
|
|
253
|
+
check(value) {
|
|
254
|
+
return constraints.some((constraint) => matchesConstraints(value, constraint));
|
|
255
|
+
},
|
|
256
|
+
run(validate, value, path) {
|
|
257
|
+
return [collectUnionViolations(constraints, validate, value, path)];
|
|
258
|
+
}
|
|
248
259
|
}, () => ({
|
|
249
|
-
|
|
250
|
-
|
|
260
|
+
kind: "union",
|
|
261
|
+
branches: constraints.map((constraint) => describeConstraints(constraint))
|
|
251
262
|
}));
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
}
|
|
263
|
+
var discriminatedUnion = (key, variants) => /* @__PURE__ */ attachConstraintDescriptor({
|
|
264
|
+
check(value) {
|
|
265
|
+
if (!isRecord(value)) return false;
|
|
266
|
+
const discriminator = value[key];
|
|
267
|
+
return Object.prototype.hasOwnProperty.call(variants, discriminator) && matchesConstraints(value, variants[discriminator]);
|
|
268
|
+
},
|
|
269
|
+
run(validate, value, path) {
|
|
270
|
+
if (!isRecord(value)) return [[{
|
|
271
|
+
value,
|
|
272
|
+
path,
|
|
273
|
+
violates: {
|
|
274
|
+
kind: "validator",
|
|
275
|
+
name: "discriminatedUnion",
|
|
276
|
+
code: "type.record",
|
|
277
|
+
args: []
|
|
278
|
+
}
|
|
279
|
+
}]];
|
|
280
|
+
const discriminator = value[key];
|
|
281
|
+
if (!Object.prototype.hasOwnProperty.call(variants, discriminator)) return [[{
|
|
282
|
+
value: discriminator,
|
|
283
|
+
path: [...path, key],
|
|
284
|
+
violates: {
|
|
285
|
+
kind: "validator",
|
|
286
|
+
name: "discriminatedUnion",
|
|
287
|
+
code: "union.invalid-discriminator",
|
|
288
|
+
args: [Reflect.ownKeys(variants)]
|
|
289
|
+
}
|
|
290
|
+
}]];
|
|
291
|
+
return [validate(value, variants[discriminator], path)];
|
|
292
|
+
}
|
|
283
293
|
}, () => ({
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
294
|
+
kind: "discriminatedUnion",
|
|
295
|
+
key,
|
|
296
|
+
variants: describeConstraintMap(variants)
|
|
287
297
|
}));
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
298
|
+
var record = (constraints) => /* @__PURE__ */ attachConstraintDescriptor({
|
|
299
|
+
check(value) {
|
|
300
|
+
return isRecord(value) && Object.values(value).every((item) => matchesConstraints(item, constraints));
|
|
301
|
+
},
|
|
302
|
+
run(validate, value, path) {
|
|
303
|
+
if (!isRecord(value)) return [[{
|
|
304
|
+
value,
|
|
305
|
+
path,
|
|
306
|
+
violates: {
|
|
307
|
+
kind: "validator",
|
|
308
|
+
name: "record",
|
|
309
|
+
code: "type.record",
|
|
310
|
+
args: []
|
|
311
|
+
}
|
|
312
|
+
}]];
|
|
313
|
+
return Object.keys(value).map((key) => validate(value[key], constraints, [...path, key]));
|
|
314
|
+
}
|
|
302
315
|
}, () => ({
|
|
303
|
-
|
|
304
|
-
|
|
316
|
+
kind: "record",
|
|
317
|
+
values: describeConstraints(constraints)
|
|
305
318
|
}));
|
|
306
|
-
|
|
307
|
-
|
|
319
|
+
var shape = (descriptor) => {
|
|
320
|
+
return /* @__PURE__ */ createObjectShape(descriptor, "passthrough", [], []);
|
|
308
321
|
};
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
322
|
+
var exact = (value) => /* @__PURE__ */ assert(isExact(value), {
|
|
323
|
+
name: "exact",
|
|
324
|
+
bail: true,
|
|
325
|
+
code: "value.exact",
|
|
326
|
+
args: [value]
|
|
314
327
|
});
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
const nullable = (constraints) => passthrough(
|
|
321
|
-
"nullable",
|
|
322
|
-
isNull,
|
|
323
|
-
constraints
|
|
324
|
-
);
|
|
325
|
-
const nullish = (constraints) => passthrough(
|
|
326
|
-
"nullish",
|
|
327
|
-
(value) => isNull(value) || isUndefined(value),
|
|
328
|
-
constraints
|
|
329
|
-
);
|
|
330
|
-
export {
|
|
331
|
-
discriminatedUnion,
|
|
332
|
-
each,
|
|
333
|
-
exact,
|
|
334
|
-
nullable,
|
|
335
|
-
nullish,
|
|
336
|
-
optional,
|
|
337
|
-
record,
|
|
338
|
-
shape,
|
|
339
|
-
tuple,
|
|
340
|
-
union
|
|
341
|
-
};
|
|
328
|
+
var optional = (constraints) => /* @__PURE__ */ passthrough("optional", isUndefined, constraints);
|
|
329
|
+
var nullable = (constraints) => /* @__PURE__ */ passthrough("nullable", isNull, constraints);
|
|
330
|
+
var nullish = (constraints) => /* @__PURE__ */ passthrough("nullish", (value) => isNull(value) || isUndefined(value), constraints);
|
|
331
|
+
//#endregion
|
|
332
|
+
export { discriminatedUnion, each, exact, nullable, nullish, optional, record, shape, tuple, union };
|