@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.
Files changed (84) hide show
  1. package/CHANGELOG.md +27 -1
  2. package/README.md +12 -2
  3. package/dist/assert-BDOtHdLx.cjs +289 -0
  4. package/dist/assert-CzMg5aO7.mjs +206 -0
  5. package/dist/assert.cjs +7 -65
  6. package/dist/assert.d.cts +37 -0
  7. package/dist/assert.d.mts +37 -0
  8. package/dist/assert.d.ts +24 -3
  9. package/dist/assert.mjs +2 -66
  10. package/dist/assertions-DPYCHREw.mjs +297 -0
  11. package/dist/assertions-nfOKqcjs.cjs +476 -0
  12. package/dist/assertions.cjs +34 -206
  13. package/dist/assertions.d.cts +56 -0
  14. package/dist/assertions.d.mts +56 -0
  15. package/dist/assertions.d.ts +43 -45
  16. package/dist/assertions.mjs +3 -207
  17. package/dist/checkers.d.cts +8 -0
  18. package/dist/checkers.d.mts +8 -0
  19. package/dist/checkers.d.ts +1 -1
  20. package/dist/combinators.cjs +303 -304
  21. package/dist/combinators.d.cts +16 -0
  22. package/dist/combinators.d.mts +16 -0
  23. package/dist/combinators.d.ts +15 -16
  24. package/dist/combinators.mjs +304 -315
  25. package/dist/constraints.d.cts +4 -0
  26. package/dist/constraints.d.mts +4 -0
  27. package/dist/constraints.d.ts +2 -2
  28. package/dist/extractors.d.cts +2 -0
  29. package/dist/extractors.d.mts +2 -0
  30. package/dist/index.cjs +210 -213
  31. package/dist/index.d.cts +12 -0
  32. package/dist/index.d.mts +12 -0
  33. package/dist/index.d.ts +9 -9
  34. package/dist/index.mjs +165 -218
  35. package/dist/json-schema.cjs +364 -489
  36. package/dist/json-schema.d.cts +14 -0
  37. package/dist/json-schema.d.mts +14 -0
  38. package/dist/json-schema.d.ts +3 -3
  39. package/dist/json-schema.mjs +365 -492
  40. package/dist/metadata.cjs +6 -7
  41. package/dist/metadata.d.cts +8 -0
  42. package/dist/metadata.d.mts +8 -0
  43. package/dist/metadata.d.ts +2 -2
  44. package/dist/metadata.mjs +2 -8
  45. package/dist/predicates.cjs +98 -41
  46. package/dist/predicates.d.cts +77 -0
  47. package/dist/predicates.d.mts +77 -0
  48. package/dist/predicates.d.ts +25 -4
  49. package/dist/predicates.mjs +92 -66
  50. package/dist/types/index.d.cts +984 -0
  51. package/dist/types/index.d.mts +984 -0
  52. package/dist/types/index.d.ts +984 -0
  53. package/dist/types/json-schema.d.cts +75 -0
  54. package/dist/types/json-schema.d.mts +75 -0
  55. package/dist/types/json-schema.d.ts +75 -0
  56. package/dist/violations.d.cts +29 -0
  57. package/dist/violations.d.mts +29 -0
  58. package/dist/violations.d.ts +1 -1
  59. package/docs/RELEASING.md +66 -0
  60. package/docs/en/00-index.md +2 -0
  61. package/docs/en/01-shape-api.md +35 -10
  62. package/docs/en/02-metadata-and-introspection.md +2 -1
  63. package/docs/en/03-violations.md +1 -1
  64. package/docs/en/04-json-schema-export.md +5 -0
  65. package/docs/en/05-public-api.md +35 -3
  66. package/docs/en/06-common-recipes.md +2 -1
  67. package/docs/en/07-ai-reference.md +5 -2
  68. package/docs/en/08-violation-code-types.md +28 -2
  69. package/docs/en/09-migration.md +75 -0
  70. package/docs/ru/00-index.md +2 -0
  71. package/docs/ru/01-shape-api.md +35 -10
  72. package/docs/ru/02-metadata-and-introspection.md +2 -1
  73. package/docs/ru/03-violations.md +1 -1
  74. package/docs/ru/04-json-schema-export.md +5 -0
  75. package/docs/ru/05-public-api.md +35 -3
  76. package/docs/ru/06-common-recipes.md +2 -1
  77. package/docs/ru/07-ai-reference.md +5 -2
  78. package/docs/ru/08-violation-code-types.md +28 -2
  79. package/docs/ru/09-migration.md +76 -0
  80. package/docs/ru/README.md +10 -2
  81. package/package.json +63 -37
  82. package/types/index.d.ts +275 -115
  83. package/dist/metadata.cjs.js +0 -130
  84. package/dist/metadata.es.js +0 -131
@@ -1,341 +1,330 @@
1
- import { assert } from "./assert.mjs";
2
- import { e as attachConstraintDescriptor, f as matchesConstraints, d as describeConstraints, a as arrayify } from "./metadata.es.js";
3
- import { isRecord, isArray, isExact, isUndefined, isNull } from "./predicates.mjs";
4
- const describeConstraintMap = (constraints) => {
5
- const described = {};
6
- Reflect.ownKeys(constraints).forEach((key) => {
7
- described[key] = describeConstraints(constraints[key]);
8
- });
9
- return described;
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
- const normalizeRuleDescriptor = (descriptor) => {
12
- if (!descriptor.metadata) {
13
- return descriptor;
14
- }
15
- return {
16
- ...descriptor,
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
- const passthrough = (kind, accepts, constraints) => attachConstraintDescriptor({
21
- check(value) {
22
- return accepts(value) || matchesConstraints(value, constraints);
23
- },
24
- run(validate, value, path) {
25
- return accepts(value) ? [] : [validate(value, constraints, path)];
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
- kind,
29
- child: describeConstraints(constraints)
30
+ kind,
31
+ child: describeConstraints(constraints)
30
32
  }));
31
- const keysOf = (value) => Object.keys(value);
32
- const hasOwn = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
33
- const hasUnknownKeys = (value, descriptor) => Object.keys(value).some((key) => !hasOwn(descriptor, key));
34
- const collectUnknownKeyViolations = (value, path, descriptor) => Object.keys(value).filter((key) => !hasOwn(descriptor, key)).map((key) => [{
35
- value: value[key],
36
- path: [...path, key],
37
- violates: { kind: "validator", name: "shape", code: "shape.unknown-key", args: [] }
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
- const pickDescriptor = (descriptor, keys) => {
40
- const picked = {};
41
- keys.forEach((key) => {
42
- if (hasOwn(descriptor, key)) {
43
- picked[key] = descriptor[key];
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
- const omitDescriptor = (descriptor, keys) => {
49
- const omitted = { ...descriptor };
50
- keys.forEach((key) => {
51
- delete omitted[key];
52
- });
53
- return omitted;
53
+ var omitDescriptor = (descriptor, keys) => {
54
+ const omitted = { ...descriptor };
55
+ keys.forEach((key) => {
56
+ delete omitted[key];
57
+ });
58
+ return omitted;
54
59
  };
55
- const extendDescriptor = (descriptor, extension) => ({
56
- ...descriptor,
57
- ...extension
60
+ var extendDescriptor = (descriptor, extension) => ({
61
+ ...descriptor,
62
+ ...extension
58
63
  });
59
- const partialDescriptor = (descriptor) => {
60
- const partial = {};
61
- keysOf(descriptor).forEach((key) => {
62
- partial[key] = optional(descriptor[key]);
63
- });
64
- return partial;
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
- const getPathValue = (value, path) => path.reduce((current, key) => {
67
- if (current === null || current === void 0) {
68
- return void 0;
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
- const toPath = (selector) => Array.isArray(selector) ? [...selector] : [selector];
73
- const collectShapeRefinementViolations = (value, path, refinements) => refinements.flatMap((refinement) => {
74
- const result = refinement(value);
75
- if (result === null || result === void 0) {
76
- return [];
77
- }
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
- });
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
- const createObjectShape = (descriptor, unknownKeys, refinements, rules) => {
93
- const keys = keysOf(descriptor);
94
- return attachConstraintDescriptor({
95
- descriptor,
96
- unknownKeys,
97
- check(value) {
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
- const toUnionFailure = (branches, value, path, branchResults) => {
182
- const matched = branchResults.find((result) => result.length === 0);
183
- if (matched) {
184
- return matched;
185
- }
186
- return [{
187
- value,
188
- path,
189
- violates: { kind: "validator", name: "union", code: "union.no-match", args: [branches.length] }
190
- }, ...branchResults.flat()];
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
- const collectUnionViolations = (branches, validate, value, path) => {
193
- const branchResults = branches.map((branch) => validate(value, branch, path));
194
- if (branchResults.some((result) => result instanceof Promise)) {
195
- return Promise.all(branchResults.map((result) => Promise.resolve(result))).then((results) => {
196
- return toUnionFailure(branches, value, path, results);
197
- });
198
- }
199
- return toUnionFailure(branches, value, path, branchResults);
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
- const each = (constraints) => 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: { kind: "validator", name: "each", code: "type.array", args: [] }
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
- kind: "each",
214
- item: describeConstraints(constraints)
216
+ kind: "each",
217
+ item: describeConstraints(constraints)
215
218
  }));
216
- const tuple = (constraints) => attachConstraintDescriptor({
217
- check(value) {
218
- return isArray(value) && value.length === constraints.length && constraints.every((constraint, index) => matchesConstraints(value[index], constraint));
219
- },
220
- run(validate, value, path) {
221
- if (!isArray(value)) {
222
- return [[{
223
- value,
224
- path,
225
- violates: { kind: "validator", name: "tuple", code: "type.array", args: [] }
226
- }]];
227
- }
228
- if (value.length !== constraints.length) {
229
- return [[{
230
- value,
231
- path,
232
- violates: { kind: "validator", name: "tuple", code: "tuple.length", args: [constraints.length] }
233
- }]];
234
- }
235
- return constraints.map((constraint, index) => validate(value[index], constraint, [...path, index]));
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
- kind: "tuple",
239
- items: constraints.map((constraint) => describeConstraints(constraint))
247
+ kind: "tuple",
248
+ items: constraints.map((constraint) => describeConstraints(constraint))
240
249
  }));
241
- const union = (constraints) => attachConstraintDescriptor({
242
- check(value) {
243
- return constraints.some((constraint) => matchesConstraints(value, constraint));
244
- },
245
- run(validate, value, path) {
246
- return [collectUnionViolations(constraints, validate, value, path)];
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
- kind: "union",
250
- branches: constraints.map((constraint) => describeConstraints(constraint))
258
+ kind: "union",
259
+ branches: constraints.map((constraint) => describeConstraints(constraint))
251
260
  }));
252
- const discriminatedUnion = (key, variants) => attachConstraintDescriptor({
253
- check(value) {
254
- if (!isRecord(value)) {
255
- return false;
256
- }
257
- const discriminator = value[key];
258
- return Object.prototype.hasOwnProperty.call(variants, discriminator) && matchesConstraints(value, variants[discriminator]);
259
- },
260
- run(validate, value, path) {
261
- if (!isRecord(value)) {
262
- return [[{
263
- value,
264
- path,
265
- violates: { kind: "validator", name: "discriminatedUnion", code: "type.record", args: [] }
266
- }]];
267
- }
268
- const discriminator = value[key];
269
- if (!Object.prototype.hasOwnProperty.call(variants, discriminator)) {
270
- return [[{
271
- value: discriminator,
272
- path: [...path, key],
273
- violates: {
274
- kind: "validator",
275
- name: "discriminatedUnion",
276
- code: "union.invalid-discriminator",
277
- args: [Reflect.ownKeys(variants)]
278
- }
279
- }]];
280
- }
281
- return [validate(value, variants[discriminator], path)];
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
- kind: "discriminatedUnion",
285
- key,
286
- variants: describeConstraintMap(variants)
292
+ kind: "discriminatedUnion",
293
+ key,
294
+ variants: describeConstraintMap(variants)
287
295
  }));
288
- const record = (constraints) => attachConstraintDescriptor({
289
- check(value) {
290
- return isRecord(value) && Object.values(value).every((item) => matchesConstraints(item, constraints));
291
- },
292
- run(validate, value, path) {
293
- if (!isRecord(value)) {
294
- return [[{
295
- value,
296
- path,
297
- violates: { kind: "validator", name: "record", code: "type.record", args: [] }
298
- }]];
299
- }
300
- return Object.keys(value).map((key) => validate(value[key], constraints, [...path, key]));
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
- kind: "record",
304
- values: describeConstraints(constraints)
314
+ kind: "record",
315
+ values: describeConstraints(constraints)
305
316
  }));
306
- const shape = (descriptor) => {
307
- return createObjectShape(descriptor, "passthrough", [], []);
317
+ var shape = (descriptor) => {
318
+ return /* @__PURE__ */ createObjectShape(descriptor, "passthrough", [], []);
308
319
  };
309
- const exact = (value) => assert(isExact(value), {
310
- name: "exact",
311
- bail: true,
312
- code: "value.exact",
313
- args: [value]
320
+ var exact = (value) => /* @__PURE__ */ assert(isExact(value), {
321
+ name: "exact",
322
+ bail: true,
323
+ code: "value.exact",
324
+ args: [value]
314
325
  });
315
- const optional = (constraints) => passthrough(
316
- "optional",
317
- isUndefined,
318
- constraints
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>;