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