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