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