@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,334 +1,335 @@
1
- "use strict";
2
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const assert = require("./assert.cjs");
4
- const metadata = require("./metadata.cjs.js");
5
- const predicates = require("./predicates.cjs");
6
- const describeConstraintMap = (constraints) => {
7
- const described = {};
8
- Reflect.ownKeys(constraints).forEach((key) => {
9
- described[key] = metadata.describeConstraints(constraints[key]);
10
- });
11
- return described;
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
- const normalizeRuleDescriptor = (descriptor) => {
14
- if (!descriptor.metadata) {
15
- return descriptor;
16
- }
17
- return {
18
- ...descriptor,
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
- const passthrough = (kind, accepts, constraints) => metadata.attachConstraintDescriptor({
23
- check(value) {
24
- return accepts(value) || metadata.matchesConstraints(value, constraints);
25
- },
26
- run(validate, value, path) {
27
- return accepts(value) ? [] : [validate(value, constraints, path)];
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
- kind,
31
- child: metadata.describeConstraints(constraints)
33
+ kind,
34
+ child: require_metadata.describeConstraints(constraints)
32
35
  }));
33
- const keysOf = (value) => Object.keys(value);
34
- const hasOwn = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
35
- const hasUnknownKeys = (value, descriptor) => Object.keys(value).some((key) => !hasOwn(descriptor, key));
36
- const collectUnknownKeyViolations = (value, path, descriptor) => Object.keys(value).filter((key) => !hasOwn(descriptor, key)).map((key) => [{
37
- value: value[key],
38
- path: [...path, key],
39
- violates: { kind: "validator", name: "shape", code: "shape.unknown-key", args: [] }
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
- const pickDescriptor = (descriptor, keys) => {
42
- const picked = {};
43
- keys.forEach((key) => {
44
- if (hasOwn(descriptor, key)) {
45
- picked[key] = descriptor[key];
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
- const omitDescriptor = (descriptor, keys) => {
51
- const omitted = { ...descriptor };
52
- keys.forEach((key) => {
53
- delete omitted[key];
54
- });
55
- return omitted;
56
+ var omitDescriptor = (descriptor, keys) => {
57
+ const omitted = { ...descriptor };
58
+ keys.forEach((key) => {
59
+ delete omitted[key];
60
+ });
61
+ return omitted;
56
62
  };
57
- const extendDescriptor = (descriptor, extension) => ({
58
- ...descriptor,
59
- ...extension
63
+ var extendDescriptor = (descriptor, extension) => ({
64
+ ...descriptor,
65
+ ...extension
60
66
  });
61
- const partialDescriptor = (descriptor) => {
62
- const partial = {};
63
- keysOf(descriptor).forEach((key) => {
64
- partial[key] = optional(descriptor[key]);
65
- });
66
- return partial;
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
- const getPathValue = (value, path) => path.reduce((current, key) => {
69
- if (current === null || current === void 0) {
70
- return void 0;
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
- const toPath = (selector) => Array.isArray(selector) ? [...selector] : [selector];
75
- const collectShapeRefinementViolations = (value, path, refinements) => refinements.flatMap((refinement) => {
76
- const result = refinement(value);
77
- if (result === null || result === void 0) {
78
- return [];
79
- }
80
- return metadata.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
- });
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
- const createObjectShape = (descriptor, unknownKeys, refinements, rules) => {
95
- const keys = keysOf(descriptor);
96
- return metadata.attachConstraintDescriptor({
97
- descriptor,
98
- unknownKeys,
99
- check(value) {
100
- return predicates.isRecord(value) && keys.every((key) => metadata.matchesConstraints(value[key], descriptor[key])) && (unknownKeys === "passthrough" || !hasUnknownKeys(value, descriptor)) && collectShapeRefinementViolations(value, [], refinements).length === 0;
101
- },
102
- run(validate, value, path) {
103
- if (!predicates.isRecord(value)) {
104
- return [[{
105
- value,
106
- path,
107
- violates: { kind: "validator", name: "shape", code: "type.record", args: [] }
108
- }]];
109
- }
110
- const validations = keys.reduce((all, key) => [
111
- ...all,
112
- validate(value[key], descriptor[key], [...path, key])
113
- ], []);
114
- if (unknownKeys === "strict") {
115
- validations.push(...collectUnknownKeyViolations(value, path, descriptor));
116
- }
117
- if (validations.some((validation) => validation instanceof Promise)) {
118
- return [Promise.all(validations.map((validation) => Promise.resolve(validation))).then((results) => {
119
- const structuralViolations2 = results.flat();
120
- return structuralViolations2.length > 0 ? structuralViolations2 : collectShapeRefinementViolations(value, path, refinements);
121
- })];
122
- }
123
- const structuralViolations = validations.flat();
124
- return structuralViolations.length > 0 ? validations : [collectShapeRefinementViolations(value, path, refinements)];
125
- },
126
- refine(refinement, ruleDescriptor = { kind: "refine" }) {
127
- return createObjectShape(
128
- descriptor,
129
- unknownKeys,
130
- [...refinements, refinement],
131
- [...rules, normalizeRuleDescriptor(ruleDescriptor)]
132
- );
133
- },
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(
143
- descriptor,
144
- unknownKeys,
145
- [...refinements, (value) => {
146
- return getPathValue(value, leftPath) === getPathValue(value, rightPath) ? [] : [{
147
- path: rightPath,
148
- code: "shape.fields.mismatch",
149
- args: [selectedKeys]
150
- }];
151
- }],
152
- [...rules, ruleDescriptor]
153
- );
154
- },
155
- strict() {
156
- return createObjectShape(descriptor, "strict", refinements, rules);
157
- },
158
- passthrough() {
159
- return createObjectShape(descriptor, "passthrough", refinements, rules);
160
- },
161
- pick(selectedKeys) {
162
- return createObjectShape(pickDescriptor(descriptor, selectedKeys), unknownKeys, [], []);
163
- },
164
- omit(selectedKeys) {
165
- return createObjectShape(omitDescriptor(descriptor, selectedKeys), unknownKeys, [], []);
166
- },
167
- partial() {
168
- return createObjectShape(partialDescriptor(descriptor), unknownKeys, [], []);
169
- },
170
- extend(extension) {
171
- return createObjectShape(extendDescriptor(descriptor, extension), unknownKeys, [], []);
172
- },
173
- merge(shape2) {
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
- const toUnionFailure = (branches, value, path, branchResults) => {
184
- const matched = branchResults.find((result) => result.length === 0);
185
- if (matched) {
186
- return matched;
187
- }
188
- return [{
189
- value,
190
- path,
191
- violates: { kind: "validator", name: "union", code: "union.no-match", args: [branches.length] }
192
- }, ...branchResults.flat()];
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
- const collectUnionViolations = (branches, validate, value, path) => {
195
- const branchResults = branches.map((branch) => validate(value, branch, path));
196
- if (branchResults.some((result) => result instanceof Promise)) {
197
- return Promise.all(branchResults.map((result) => Promise.resolve(result))).then((results) => {
198
- return toUnionFailure(branches, value, path, results);
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
- const each = (constraints) => metadata.attachConstraintDescriptor({
204
- check(value) {
205
- return predicates.isArray(value) && value.every((item) => metadata.matchesConstraints(item, constraints));
206
- },
207
- run(validate, value, path) {
208
- return predicates.isArray(value) ? value.map((item, index) => validate(item, constraints, [...path, index])) : [[{
209
- value,
210
- path,
211
- violates: { kind: "validator", name: "each", code: "type.array", args: [] }
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
- kind: "each",
216
- item: metadata.describeConstraints(constraints)
219
+ kind: "each",
220
+ item: require_metadata.describeConstraints(constraints)
217
221
  }));
218
- const tuple = (constraints) => metadata.attachConstraintDescriptor({
219
- check(value) {
220
- return predicates.isArray(value) && value.length === constraints.length && constraints.every((constraint, index) => metadata.matchesConstraints(value[index], constraint));
221
- },
222
- run(validate, value, path) {
223
- if (!predicates.isArray(value)) {
224
- return [[{
225
- value,
226
- path,
227
- violates: { kind: "validator", name: "tuple", code: "type.array", args: [] }
228
- }]];
229
- }
230
- if (value.length !== constraints.length) {
231
- return [[{
232
- value,
233
- path,
234
- violates: { kind: "validator", name: "tuple", code: "tuple.length", args: [constraints.length] }
235
- }]];
236
- }
237
- return constraints.map((constraint, index) => validate(value[index], constraint, [...path, index]));
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
- kind: "tuple",
241
- items: constraints.map((constraint) => metadata.describeConstraints(constraint))
250
+ kind: "tuple",
251
+ items: constraints.map((constraint) => require_metadata.describeConstraints(constraint))
242
252
  }));
243
- const union = (constraints) => metadata.attachConstraintDescriptor({
244
- check(value) {
245
- return constraints.some((constraint) => metadata.matchesConstraints(value, constraint));
246
- },
247
- run(validate, value, path) {
248
- return [collectUnionViolations(constraints, validate, value, path)];
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
- kind: "union",
252
- branches: constraints.map((constraint) => metadata.describeConstraints(constraint))
261
+ kind: "union",
262
+ branches: constraints.map((constraint) => require_metadata.describeConstraints(constraint))
253
263
  }));
254
- const discriminatedUnion = (key, variants) => metadata.attachConstraintDescriptor({
255
- check(value) {
256
- if (!predicates.isRecord(value)) {
257
- return false;
258
- }
259
- const discriminator = value[key];
260
- return Object.prototype.hasOwnProperty.call(variants, discriminator) && metadata.matchesConstraints(value, variants[discriminator]);
261
- },
262
- run(validate, value, path) {
263
- if (!predicates.isRecord(value)) {
264
- return [[{
265
- value,
266
- path,
267
- violates: { kind: "validator", name: "discriminatedUnion", code: "type.record", args: [] }
268
- }]];
269
- }
270
- const discriminator = value[key];
271
- if (!Object.prototype.hasOwnProperty.call(variants, discriminator)) {
272
- return [[{
273
- value: discriminator,
274
- path: [...path, key],
275
- violates: {
276
- kind: "validator",
277
- name: "discriminatedUnion",
278
- code: "union.invalid-discriminator",
279
- args: [Reflect.ownKeys(variants)]
280
- }
281
- }]];
282
- }
283
- return [validate(value, variants[discriminator], path)];
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
- kind: "discriminatedUnion",
287
- key,
288
- variants: describeConstraintMap(variants)
295
+ kind: "discriminatedUnion",
296
+ key,
297
+ variants: describeConstraintMap(variants)
289
298
  }));
290
- const record = (constraints) => metadata.attachConstraintDescriptor({
291
- check(value) {
292
- return predicates.isRecord(value) && Object.values(value).every((item) => metadata.matchesConstraints(item, constraints));
293
- },
294
- run(validate, value, path) {
295
- if (!predicates.isRecord(value)) {
296
- return [[{
297
- value,
298
- path,
299
- violates: { kind: "validator", name: "record", code: "type.record", args: [] }
300
- }]];
301
- }
302
- return Object.keys(value).map((key) => validate(value[key], constraints, [...path, key]));
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
- kind: "record",
306
- values: metadata.describeConstraints(constraints)
317
+ kind: "record",
318
+ values: require_metadata.describeConstraints(constraints)
307
319
  }));
308
- const shape = (descriptor) => {
309
- return createObjectShape(descriptor, "passthrough", [], []);
320
+ var shape = (descriptor) => {
321
+ return /* @__PURE__ */ createObjectShape(descriptor, "passthrough", [], []);
310
322
  };
311
- const exact = (value) => assert.assert(predicates.isExact(value), {
312
- name: "exact",
313
- bail: true,
314
- code: "value.exact",
315
- args: [value]
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
- const optional = (constraints) => passthrough(
318
- "optional",
319
- predicates.isUndefined,
320
- constraints
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;