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