@modulify/validator 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/dist/assert.cjs +101 -7
- package/dist/assert.mjs +95 -1
- package/dist/assertions.cjs +309 -32
- package/dist/assertions.mjs +279 -2
- package/dist/checkers.cjs +23 -0
- package/dist/checkers.mjs +16 -0
- package/dist/combinators.cjs +24 -22
- package/dist/combinators.mjs +3 -1
- package/dist/constraints.cjs +23 -0
- package/dist/constraints.mjs +21 -0
- package/dist/extractors.cjs +6 -0
- package/dist/extractors.mjs +5 -0
- package/dist/index.cjs +14 -90
- package/dist/index.mjs +5 -81
- package/dist/json-schema.cjs +2 -2
- package/dist/json-schema.mjs +1 -1
- package/dist/metadata.cjs +98 -6
- package/dist/metadata.mjs +93 -1
- package/dist/violations.cjs +81 -0
- package/dist/violations.mjs +80 -0
- package/package.json +1 -1
- package/dist/assert-BDOtHdLx.cjs +0 -289
- package/dist/assert-CzMg5aO7.mjs +0 -206
- package/dist/assertions-DPYCHREw.mjs +0 -297
- package/dist/assertions-nfOKqcjs.cjs +0 -476
package/dist/assertions.mjs
CHANGED
|
@@ -1,3 +1,280 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { assert, createRefinement, refine } from "./assert.mjs";
|
|
2
|
+
import { endsWith as endsWith$1, inRange, isEqual, isGte, isLte, isMultipleOf, matchesPattern, startsWith as startsWith$1 } from "./checkers.mjs";
|
|
3
|
+
import { length, size } from "./extractors.mjs";
|
|
4
|
+
import { isArray, isBigInt as isBigInt$1, isBlob as isBlob$1, isBoolean as isBoolean$1, isDate as isDate$1, isEmail as isEmail$1, isError as isError$1, isFile as isFile$1, isFiniteNumber as isFiniteNumber$1, isFunction as isFunction$1, isInteger as isInteger$1, isMap as isMap$1, isNaN as isNaN$1, isNull as isNull$1, isNumber as isNumber$1, isPromiseLike as isPromiseLike$1, isRegExp as isRegExp$1, isSafeInteger as isSafeInteger$1, isSet as isSet$1, isString as isString$1, isSymbol as isSymbol$1, isValidDate as isValidDate$1 } from "./predicates.mjs";
|
|
5
|
+
//#region src/assertions.ts
|
|
6
|
+
var buildLengthConstraints = (options) => {
|
|
7
|
+
const { exact = null, max = null, min = null, range = null } = options;
|
|
8
|
+
const constraints = [];
|
|
9
|
+
if (exact !== null) constraints.push([
|
|
10
|
+
length,
|
|
11
|
+
isEqual,
|
|
12
|
+
"length.exact",
|
|
13
|
+
exact
|
|
14
|
+
]);
|
|
15
|
+
if (max !== null) constraints.push([
|
|
16
|
+
length,
|
|
17
|
+
isLte,
|
|
18
|
+
"length.max",
|
|
19
|
+
max
|
|
20
|
+
]);
|
|
21
|
+
if (min !== null) constraints.push([
|
|
22
|
+
length,
|
|
23
|
+
isGte,
|
|
24
|
+
"length.min",
|
|
25
|
+
min
|
|
26
|
+
]);
|
|
27
|
+
if (range !== null) constraints.push([
|
|
28
|
+
length,
|
|
29
|
+
inRange,
|
|
30
|
+
"length.range",
|
|
31
|
+
range
|
|
32
|
+
]);
|
|
33
|
+
return constraints;
|
|
34
|
+
};
|
|
35
|
+
var buildSizeConstraints = (options) => {
|
|
36
|
+
const { exact = null, max = null, min = null, range = null } = options;
|
|
37
|
+
const constraints = [];
|
|
38
|
+
if (exact !== null) constraints.push([
|
|
39
|
+
size,
|
|
40
|
+
isEqual,
|
|
41
|
+
"size.exact",
|
|
42
|
+
exact
|
|
43
|
+
]);
|
|
44
|
+
if (max !== null) constraints.push([
|
|
45
|
+
size,
|
|
46
|
+
isLte,
|
|
47
|
+
"size.max",
|
|
48
|
+
max
|
|
49
|
+
]);
|
|
50
|
+
if (min !== null) constraints.push([
|
|
51
|
+
size,
|
|
52
|
+
isGte,
|
|
53
|
+
"size.min",
|
|
54
|
+
min
|
|
55
|
+
]);
|
|
56
|
+
if (range !== null) constraints.push([
|
|
57
|
+
size,
|
|
58
|
+
inRange,
|
|
59
|
+
"size.range",
|
|
60
|
+
range
|
|
61
|
+
]);
|
|
62
|
+
return constraints;
|
|
63
|
+
};
|
|
64
|
+
var buildValueConstraints = (options) => {
|
|
65
|
+
const { exact = null, max = null, min = null, range = null } = options;
|
|
66
|
+
const constraints = [];
|
|
67
|
+
if (exact !== null) constraints.push([
|
|
68
|
+
(value) => value,
|
|
69
|
+
isEqual,
|
|
70
|
+
"number.exact",
|
|
71
|
+
exact
|
|
72
|
+
]);
|
|
73
|
+
if (max !== null) constraints.push([
|
|
74
|
+
(value) => value,
|
|
75
|
+
isLte,
|
|
76
|
+
"number.max",
|
|
77
|
+
max
|
|
78
|
+
]);
|
|
79
|
+
if (min !== null) constraints.push([
|
|
80
|
+
(value) => value,
|
|
81
|
+
isGte,
|
|
82
|
+
"number.min",
|
|
83
|
+
min
|
|
84
|
+
]);
|
|
85
|
+
if (range !== null) constraints.push([
|
|
86
|
+
(value) => value,
|
|
87
|
+
inRange,
|
|
88
|
+
"number.range",
|
|
89
|
+
range
|
|
90
|
+
]);
|
|
91
|
+
return constraints;
|
|
92
|
+
};
|
|
93
|
+
var isBoolean = /* @__PURE__ */ assert(isBoolean$1, {
|
|
94
|
+
name: "isBoolean",
|
|
95
|
+
bail: true,
|
|
96
|
+
code: "type.boolean"
|
|
97
|
+
});
|
|
98
|
+
var isBigInt = /* @__PURE__ */ assert(isBigInt$1, {
|
|
99
|
+
name: "isBigInt",
|
|
100
|
+
bail: true,
|
|
101
|
+
code: "type.bigint"
|
|
102
|
+
});
|
|
103
|
+
var isBlob = /* @__PURE__ */ assert(isBlob$1, {
|
|
104
|
+
name: "isBlob",
|
|
105
|
+
bail: true,
|
|
106
|
+
code: "type.blob"
|
|
107
|
+
});
|
|
108
|
+
var isDate = /* @__PURE__ */ assert(isDate$1, {
|
|
109
|
+
name: "isDate",
|
|
110
|
+
bail: true,
|
|
111
|
+
code: "type.date"
|
|
112
|
+
});
|
|
113
|
+
var isDefined = /* @__PURE__ */ assert((value) => value !== void 0, {
|
|
114
|
+
name: "isDefined",
|
|
115
|
+
bail: true,
|
|
116
|
+
code: "value.defined"
|
|
117
|
+
});
|
|
118
|
+
var isEmail = /* @__PURE__ */ assert(isEmail$1, {
|
|
119
|
+
name: "isEmail",
|
|
120
|
+
bail: true,
|
|
121
|
+
code: "string.email"
|
|
122
|
+
});
|
|
123
|
+
var isError = /* @__PURE__ */ assert(isError$1, {
|
|
124
|
+
name: "isError",
|
|
125
|
+
bail: true,
|
|
126
|
+
code: "type.error"
|
|
127
|
+
});
|
|
128
|
+
var isFiniteNumber = /* @__PURE__ */ assert(isFiniteNumber$1, {
|
|
129
|
+
name: "isFiniteNumber",
|
|
130
|
+
bail: true,
|
|
131
|
+
code: "number.finite"
|
|
132
|
+
});
|
|
133
|
+
var isFile = /* @__PURE__ */ assert(isFile$1, {
|
|
134
|
+
name: "isFile",
|
|
135
|
+
bail: true,
|
|
136
|
+
code: "type.file"
|
|
137
|
+
});
|
|
138
|
+
var isFunction = /* @__PURE__ */ assert(isFunction$1, {
|
|
139
|
+
name: "isFunction",
|
|
140
|
+
bail: true,
|
|
141
|
+
code: "type.function"
|
|
142
|
+
});
|
|
143
|
+
var isInteger = /* @__PURE__ */ assert(isInteger$1, {
|
|
144
|
+
name: "isInteger",
|
|
145
|
+
bail: true,
|
|
146
|
+
code: "number.integer"
|
|
147
|
+
});
|
|
148
|
+
var isMap = /* @__PURE__ */ assert(isMap$1, {
|
|
149
|
+
name: "isMap",
|
|
150
|
+
bail: true,
|
|
151
|
+
code: "type.map"
|
|
152
|
+
});
|
|
153
|
+
var isNaN = /* @__PURE__ */ assert(isNaN$1, {
|
|
154
|
+
name: "isNaN",
|
|
155
|
+
bail: true,
|
|
156
|
+
code: "number.nan"
|
|
157
|
+
});
|
|
158
|
+
var isNull = /* @__PURE__ */ assert(isNull$1, {
|
|
159
|
+
name: "isNull",
|
|
160
|
+
bail: true,
|
|
161
|
+
code: "type.null"
|
|
162
|
+
});
|
|
163
|
+
var isNumber = /* @__PURE__ */ assert(isNumber$1, {
|
|
164
|
+
name: "isNumber",
|
|
165
|
+
bail: true,
|
|
166
|
+
code: "type.number"
|
|
167
|
+
});
|
|
168
|
+
var isPromiseLike = /* @__PURE__ */ assert(isPromiseLike$1, {
|
|
169
|
+
name: "isPromiseLike",
|
|
170
|
+
bail: true,
|
|
171
|
+
code: "type.promise-like"
|
|
172
|
+
});
|
|
173
|
+
var isRegExp = /* @__PURE__ */ assert(isRegExp$1, {
|
|
174
|
+
name: "isRegExp",
|
|
175
|
+
bail: true,
|
|
176
|
+
code: "type.regexp"
|
|
177
|
+
});
|
|
178
|
+
var isSafeInteger = /* @__PURE__ */ assert(isSafeInteger$1, {
|
|
179
|
+
name: "isSafeInteger",
|
|
180
|
+
bail: true,
|
|
181
|
+
code: "number.safe-integer"
|
|
182
|
+
});
|
|
183
|
+
var isSet = /* @__PURE__ */ assert(isSet$1, {
|
|
184
|
+
name: "isSet",
|
|
185
|
+
bail: true,
|
|
186
|
+
code: "type.set"
|
|
187
|
+
});
|
|
188
|
+
var isString = /* @__PURE__ */ assert(isString$1, {
|
|
189
|
+
name: "isString",
|
|
190
|
+
bail: true,
|
|
191
|
+
code: "type.string"
|
|
192
|
+
});
|
|
193
|
+
var isSymbol = /* @__PURE__ */ assert(isSymbol$1, {
|
|
194
|
+
name: "isSymbol",
|
|
195
|
+
bail: true,
|
|
196
|
+
code: "type.symbol"
|
|
197
|
+
});
|
|
198
|
+
var isValidDate = /* @__PURE__ */ assert(isValidDate$1, {
|
|
199
|
+
name: "isValidDate",
|
|
200
|
+
bail: true,
|
|
201
|
+
code: "date.valid"
|
|
202
|
+
});
|
|
203
|
+
var hasLength = (options = {}) => {
|
|
204
|
+
const { bail = false } = options;
|
|
205
|
+
const constraints = buildLengthConstraints(options);
|
|
206
|
+
return /* @__PURE__ */ createRefinement({
|
|
207
|
+
name: "hasLength",
|
|
208
|
+
bail,
|
|
209
|
+
code: "length.unsupported-type"
|
|
210
|
+
}, constraints);
|
|
211
|
+
};
|
|
212
|
+
var hasSize = (options = {}) => {
|
|
213
|
+
const { bail = false } = options;
|
|
214
|
+
const constraints = buildSizeConstraints(options);
|
|
215
|
+
return /* @__PURE__ */ createRefinement({
|
|
216
|
+
name: "hasSize",
|
|
217
|
+
bail,
|
|
218
|
+
code: "size.unsupported-type"
|
|
219
|
+
}, constraints);
|
|
220
|
+
};
|
|
221
|
+
var hasPattern = (pattern, { bail = false } = {}) => /* @__PURE__ */ createRefinement({
|
|
222
|
+
name: "hasPattern",
|
|
223
|
+
bail,
|
|
224
|
+
code: "string.unsupported-type"
|
|
225
|
+
}, [[
|
|
226
|
+
(value) => value,
|
|
227
|
+
matchesPattern,
|
|
228
|
+
"string.pattern",
|
|
229
|
+
pattern
|
|
230
|
+
]]);
|
|
231
|
+
var startsWith = (prefix, { bail = false } = {}) => /* @__PURE__ */ createRefinement({
|
|
232
|
+
name: "startsWith",
|
|
233
|
+
bail,
|
|
234
|
+
code: "string.unsupported-type"
|
|
235
|
+
}, [[
|
|
236
|
+
(value) => value,
|
|
237
|
+
startsWith$1,
|
|
238
|
+
"string.starts-with",
|
|
239
|
+
prefix
|
|
240
|
+
]]);
|
|
241
|
+
var endsWith = (suffix, { bail = false } = {}) => /* @__PURE__ */ createRefinement({
|
|
242
|
+
name: "endsWith",
|
|
243
|
+
bail,
|
|
244
|
+
code: "string.unsupported-type"
|
|
245
|
+
}, [[
|
|
246
|
+
(value) => value,
|
|
247
|
+
endsWith$1,
|
|
248
|
+
"string.ends-with",
|
|
249
|
+
suffix
|
|
250
|
+
]]);
|
|
251
|
+
var hasValue = (options = {}) => {
|
|
252
|
+
const { bail = false } = options;
|
|
253
|
+
const constraints = buildValueConstraints(options);
|
|
254
|
+
return /* @__PURE__ */ createRefinement({
|
|
255
|
+
name: "hasValue",
|
|
256
|
+
bail,
|
|
257
|
+
code: "number.unsupported-type"
|
|
258
|
+
}, constraints);
|
|
259
|
+
};
|
|
260
|
+
var multipleOf = (step, { bail = false } = {}) => /* @__PURE__ */ createRefinement({
|
|
261
|
+
name: "multipleOf",
|
|
262
|
+
bail,
|
|
263
|
+
code: "number.unsupported-type"
|
|
264
|
+
}, [[
|
|
265
|
+
(value) => value,
|
|
266
|
+
isMultipleOf,
|
|
267
|
+
"number.multiple-of",
|
|
268
|
+
step
|
|
269
|
+
]]);
|
|
270
|
+
var oneOf = (values, { equalTo = (a, b) => a === b, bail = false } = {}) => {
|
|
271
|
+
const haystack = isArray(values) ? values : Object.values(values);
|
|
272
|
+
return /* @__PURE__ */ assert((value) => haystack.some((item) => equalTo(item, value)), {
|
|
273
|
+
name: "oneOf",
|
|
274
|
+
bail,
|
|
275
|
+
code: "value.one-of",
|
|
276
|
+
args: [haystack]
|
|
277
|
+
});
|
|
278
|
+
};
|
|
279
|
+
//#endregion
|
|
3
280
|
export { assert, endsWith, hasLength, hasPattern, hasSize, hasValue, isBigInt, isBlob, isBoolean, isDate, isDefined, isEmail, isError, isFile, isFiniteNumber, isFunction, isInteger, isMap, isNaN, isNull, isNumber, isPromiseLike, isRegExp, isSafeInteger, isSet, isString, isSymbol, isValidDate, multipleOf, oneOf, refine, startsWith };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/checkers.ts
|
|
2
|
+
var isEqual = (value, exact) => value === exact;
|
|
3
|
+
var isGte = (value, min) => value >= min;
|
|
4
|
+
var isLte = (value, max) => value <= max;
|
|
5
|
+
var inRange = (value, [min, max]) => value >= min && value <= max;
|
|
6
|
+
var startsWith = (value, prefix) => value.startsWith(prefix);
|
|
7
|
+
var endsWith = (value, suffix) => value.endsWith(suffix);
|
|
8
|
+
var matchesPattern = (value, pattern) => new RegExp(pattern.source, pattern.flags).test(value);
|
|
9
|
+
var isMultipleOf = (value, step) => {
|
|
10
|
+
if (step === 0) return false;
|
|
11
|
+
const quotient = value / step;
|
|
12
|
+
const delta = Math.abs(quotient - Math.round(quotient));
|
|
13
|
+
return Number.isFinite(quotient) && delta <= Number.EPSILON * Math.max(1, Math.abs(quotient)) * 16;
|
|
14
|
+
};
|
|
15
|
+
//#endregion
|
|
16
|
+
exports.endsWith = endsWith;
|
|
17
|
+
exports.inRange = inRange;
|
|
18
|
+
exports.isEqual = isEqual;
|
|
19
|
+
exports.isGte = isGte;
|
|
20
|
+
exports.isLte = isLte;
|
|
21
|
+
exports.isMultipleOf = isMultipleOf;
|
|
22
|
+
exports.matchesPattern = matchesPattern;
|
|
23
|
+
exports.startsWith = startsWith;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
//#region src/checkers.ts
|
|
2
|
+
var isEqual = (value, exact) => value === exact;
|
|
3
|
+
var isGte = (value, min) => value >= min;
|
|
4
|
+
var isLte = (value, max) => value <= max;
|
|
5
|
+
var inRange = (value, [min, max]) => value >= min && value <= max;
|
|
6
|
+
var startsWith = (value, prefix) => value.startsWith(prefix);
|
|
7
|
+
var endsWith = (value, suffix) => value.endsWith(suffix);
|
|
8
|
+
var matchesPattern = (value, pattern) => new RegExp(pattern.source, pattern.flags).test(value);
|
|
9
|
+
var isMultipleOf = (value, step) => {
|
|
10
|
+
if (step === 0) return false;
|
|
11
|
+
const quotient = value / step;
|
|
12
|
+
const delta = Math.abs(quotient - Math.round(quotient));
|
|
13
|
+
return Number.isFinite(quotient) && delta <= Number.EPSILON * Math.max(1, Math.abs(quotient)) * 16;
|
|
14
|
+
};
|
|
15
|
+
//#endregion
|
|
16
|
+
export { endsWith, inRange, isEqual, isGte, isLte, isMultipleOf, matchesPattern, startsWith };
|
package/dist/combinators.cjs
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_constraints = require("./constraints.cjs");
|
|
3
|
+
const require_metadata = require("./metadata.cjs");
|
|
4
|
+
const require_assert = require("./assert.cjs");
|
|
3
5
|
const require_predicates = require("./predicates.cjs");
|
|
4
6
|
//#region src/combinators.ts
|
|
5
7
|
var describeConstraintMap = (constraints) => {
|
|
6
8
|
const described = {};
|
|
7
9
|
Reflect.ownKeys(constraints).forEach((key) => {
|
|
8
|
-
described[key] =
|
|
10
|
+
described[key] = require_metadata.describeConstraints(constraints[key]);
|
|
9
11
|
});
|
|
10
12
|
return described;
|
|
11
13
|
};
|
|
@@ -20,16 +22,16 @@ var normalizeAsyncRuleDescriptor = (descriptor) => ({
|
|
|
20
22
|
...normalizeRuleDescriptor(descriptor),
|
|
21
23
|
async: true
|
|
22
24
|
});
|
|
23
|
-
var passthrough = (kind, accepts, constraints) => /* @__PURE__ */
|
|
25
|
+
var passthrough = (kind, accepts, constraints) => /* @__PURE__ */ require_metadata.attachConstraintDescriptor({
|
|
24
26
|
check(value) {
|
|
25
|
-
return accepts(value) ||
|
|
27
|
+
return accepts(value) || require_constraints.matchesConstraints(value, constraints);
|
|
26
28
|
},
|
|
27
29
|
run(validate, value, path) {
|
|
28
30
|
return accepts(value) ? [] : [validate(value, constraints, path)];
|
|
29
31
|
}
|
|
30
32
|
}, () => ({
|
|
31
33
|
kind,
|
|
32
|
-
child:
|
|
34
|
+
child: require_metadata.describeConstraints(constraints)
|
|
33
35
|
}));
|
|
34
36
|
var keysOf = (value) => Object.keys(value);
|
|
35
37
|
var hasOwn = (value, key) => Object.prototype.hasOwnProperty.call(value, key);
|
|
@@ -76,7 +78,7 @@ var getPathValue = (value, path) => path.reduce((current, key) => {
|
|
|
76
78
|
var toPath = (selector) => typeof selector === "object" ? [...selector] : [selector];
|
|
77
79
|
var toShapeRefinementViolations = (value, path, result) => {
|
|
78
80
|
if (result === null || result === void 0) return [];
|
|
79
|
-
return
|
|
81
|
+
return require_constraints.arrayify(result).filter((issue) => issue !== null && issue !== void 0).map((issue) => {
|
|
80
82
|
const issuePath = issue.path ? [...issue.path] : [];
|
|
81
83
|
return {
|
|
82
84
|
value: issue.value ?? getPathValue(value, issuePath),
|
|
@@ -106,11 +108,11 @@ var createObjectShape = (descriptor, unknownKeys, refinements, rules) => {
|
|
|
106
108
|
const keys = keysOf(descriptor);
|
|
107
109
|
const addAsyncRefinement = (refinement, ruleDescriptor = { kind: "refine" }) => createObjectShape(descriptor, unknownKeys, [...refinements, refinement], [...rules, normalizeAsyncRuleDescriptor(ruleDescriptor)]);
|
|
108
110
|
const addSyncRefinement = (refinement, ruleDescriptor = { kind: "refine" }) => createObjectShape(descriptor, unknownKeys, [...refinements, refinement], [...rules, normalizeRuleDescriptor(ruleDescriptor)]);
|
|
109
|
-
return /* @__PURE__ */
|
|
111
|
+
return /* @__PURE__ */ require_metadata.attachConstraintDescriptor({
|
|
110
112
|
descriptor,
|
|
111
113
|
unknownKeys,
|
|
112
114
|
check(value) {
|
|
113
|
-
return require_predicates.isRecord(value) && keys.every((key) =>
|
|
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;
|
|
114
116
|
},
|
|
115
117
|
run(validate, value, path) {
|
|
116
118
|
if (!require_predicates.isRecord(value)) return [[{
|
|
@@ -197,9 +199,9 @@ var collectUnionViolations = (branches, validate, value, path) => {
|
|
|
197
199
|
});
|
|
198
200
|
return toUnionFailure(branches, value, path, branchResults);
|
|
199
201
|
};
|
|
200
|
-
var each = (constraints) => /* @__PURE__ */
|
|
202
|
+
var each = (constraints) => /* @__PURE__ */ require_metadata.attachConstraintDescriptor({
|
|
201
203
|
check(value) {
|
|
202
|
-
return require_predicates.isArray(value) && value.every((item) =>
|
|
204
|
+
return require_predicates.isArray(value) && value.every((item) => require_constraints.matchesConstraints(item, constraints));
|
|
203
205
|
},
|
|
204
206
|
run(validate, value, path) {
|
|
205
207
|
return require_predicates.isArray(value) ? value.map((item, index) => validate(item, constraints, [...path, index])) : [[{
|
|
@@ -215,11 +217,11 @@ var each = (constraints) => /* @__PURE__ */ require_assert.attachConstraintDescr
|
|
|
215
217
|
}
|
|
216
218
|
}, () => ({
|
|
217
219
|
kind: "each",
|
|
218
|
-
item:
|
|
220
|
+
item: require_metadata.describeConstraints(constraints)
|
|
219
221
|
}));
|
|
220
|
-
var tuple = (constraints) => /* @__PURE__ */
|
|
222
|
+
var tuple = (constraints) => /* @__PURE__ */ require_metadata.attachConstraintDescriptor({
|
|
221
223
|
check(value) {
|
|
222
|
-
return require_predicates.isArray(value) && value.length === constraints.length && constraints.every((constraint, index) =>
|
|
224
|
+
return require_predicates.isArray(value) && value.length === constraints.length && constraints.every((constraint, index) => require_constraints.matchesConstraints(value[index], constraint));
|
|
223
225
|
},
|
|
224
226
|
run(validate, value, path) {
|
|
225
227
|
if (!require_predicates.isArray(value)) return [[{
|
|
@@ -246,24 +248,24 @@ var tuple = (constraints) => /* @__PURE__ */ require_assert.attachConstraintDesc
|
|
|
246
248
|
}
|
|
247
249
|
}, () => ({
|
|
248
250
|
kind: "tuple",
|
|
249
|
-
items: constraints.map((constraint) =>
|
|
251
|
+
items: constraints.map((constraint) => require_metadata.describeConstraints(constraint))
|
|
250
252
|
}));
|
|
251
|
-
var union = (constraints) => /* @__PURE__ */
|
|
253
|
+
var union = (constraints) => /* @__PURE__ */ require_metadata.attachConstraintDescriptor({
|
|
252
254
|
check(value) {
|
|
253
|
-
return constraints.some((constraint) =>
|
|
255
|
+
return constraints.some((constraint) => require_constraints.matchesConstraints(value, constraint));
|
|
254
256
|
},
|
|
255
257
|
run(validate, value, path) {
|
|
256
258
|
return [collectUnionViolations(constraints, validate, value, path)];
|
|
257
259
|
}
|
|
258
260
|
}, () => ({
|
|
259
261
|
kind: "union",
|
|
260
|
-
branches: constraints.map((constraint) =>
|
|
262
|
+
branches: constraints.map((constraint) => require_metadata.describeConstraints(constraint))
|
|
261
263
|
}));
|
|
262
|
-
var discriminatedUnion = (key, variants) => /* @__PURE__ */
|
|
264
|
+
var discriminatedUnion = (key, variants) => /* @__PURE__ */ require_metadata.attachConstraintDescriptor({
|
|
263
265
|
check(value) {
|
|
264
266
|
if (!require_predicates.isRecord(value)) return false;
|
|
265
267
|
const discriminator = value[key];
|
|
266
|
-
return Object.prototype.hasOwnProperty.call(variants, discriminator) &&
|
|
268
|
+
return Object.prototype.hasOwnProperty.call(variants, discriminator) && require_constraints.matchesConstraints(value, variants[discriminator]);
|
|
267
269
|
},
|
|
268
270
|
run(validate, value, path) {
|
|
269
271
|
if (!require_predicates.isRecord(value)) return [[{
|
|
@@ -294,9 +296,9 @@ var discriminatedUnion = (key, variants) => /* @__PURE__ */ require_assert.attac
|
|
|
294
296
|
key,
|
|
295
297
|
variants: describeConstraintMap(variants)
|
|
296
298
|
}));
|
|
297
|
-
var record = (constraints) => /* @__PURE__ */
|
|
299
|
+
var record = (constraints) => /* @__PURE__ */ require_metadata.attachConstraintDescriptor({
|
|
298
300
|
check(value) {
|
|
299
|
-
return require_predicates.isRecord(value) && Object.values(value).every((item) =>
|
|
301
|
+
return require_predicates.isRecord(value) && Object.values(value).every((item) => require_constraints.matchesConstraints(item, constraints));
|
|
300
302
|
},
|
|
301
303
|
run(validate, value, path) {
|
|
302
304
|
if (!require_predicates.isRecord(value)) return [[{
|
|
@@ -313,7 +315,7 @@ var record = (constraints) => /* @__PURE__ */ require_assert.attachConstraintDes
|
|
|
313
315
|
}
|
|
314
316
|
}, () => ({
|
|
315
317
|
kind: "record",
|
|
316
|
-
values:
|
|
318
|
+
values: require_metadata.describeConstraints(constraints)
|
|
317
319
|
}));
|
|
318
320
|
var shape = (descriptor) => {
|
|
319
321
|
return /* @__PURE__ */ createObjectShape(descriptor, "passthrough", [], []);
|
package/dist/combinators.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { arrayify, matchesConstraints } from "./constraints.mjs";
|
|
2
|
+
import { attachConstraintDescriptor, describeConstraints } from "./metadata.mjs";
|
|
3
|
+
import { assert } from "./assert.mjs";
|
|
2
4
|
import { isArray, isExact, isNull, isRecord, isUndefined } from "./predicates.mjs";
|
|
3
5
|
//#region src/combinators.ts
|
|
4
6
|
var describeConstraintMap = (constraints) => {
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const require_assert = require("./assert.cjs");
|
|
2
|
+
//#region src/constraints.ts
|
|
3
|
+
var isValidator = (constraint) => "run" in constraint;
|
|
4
|
+
function arrayify(value) {
|
|
5
|
+
return Array.isArray(value) ? [...value] : [value];
|
|
6
|
+
}
|
|
7
|
+
function matchesConstraints(value, constraints) {
|
|
8
|
+
let establishedDomain = false;
|
|
9
|
+
return arrayify(constraints).every((constraint) => {
|
|
10
|
+
if (isValidator(constraint)) {
|
|
11
|
+
establishedDomain = false;
|
|
12
|
+
return constraint.check(value);
|
|
13
|
+
}
|
|
14
|
+
if (!establishedDomain && require_assert.isRefinementAssertion(constraint)) return false;
|
|
15
|
+
const matched = establishedDomain && require_assert.isRefinementAssertion(constraint) ? require_assert.checkRefinementAssertion(constraint, value) : constraint.check(value);
|
|
16
|
+
if (matched) establishedDomain = true;
|
|
17
|
+
return matched;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
exports.arrayify = arrayify;
|
|
22
|
+
exports.isValidator = isValidator;
|
|
23
|
+
exports.matchesConstraints = matchesConstraints;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { checkRefinementAssertion, isRefinementAssertion } from "./assert.mjs";
|
|
2
|
+
//#region src/constraints.ts
|
|
3
|
+
var isValidator = (constraint) => "run" in constraint;
|
|
4
|
+
function arrayify(value) {
|
|
5
|
+
return Array.isArray(value) ? [...value] : [value];
|
|
6
|
+
}
|
|
7
|
+
function matchesConstraints(value, constraints) {
|
|
8
|
+
let establishedDomain = false;
|
|
9
|
+
return arrayify(constraints).every((constraint) => {
|
|
10
|
+
if (isValidator(constraint)) {
|
|
11
|
+
establishedDomain = false;
|
|
12
|
+
return constraint.check(value);
|
|
13
|
+
}
|
|
14
|
+
if (!establishedDomain && isRefinementAssertion(constraint)) return false;
|
|
15
|
+
const matched = establishedDomain && isRefinementAssertion(constraint) ? checkRefinementAssertion(constraint, value) : constraint.check(value);
|
|
16
|
+
if (matched) establishedDomain = true;
|
|
17
|
+
return matched;
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
export { arrayify, isValidator, matchesConstraints };
|
package/dist/index.cjs
CHANGED
|
@@ -1,92 +1,16 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
3
|
-
const
|
|
2
|
+
const require_constraints = require("./constraints.cjs");
|
|
3
|
+
const require_metadata = require("./metadata.cjs");
|
|
4
|
+
const require_assert = require("./assert.cjs");
|
|
5
|
+
const require_assertions = require("./assertions.cjs");
|
|
4
6
|
const require_combinators = require("./combinators.cjs");
|
|
5
|
-
|
|
6
|
-
var ROOT_PATH = Object.freeze([]);
|
|
7
|
-
var normalizePath = (path) => path ?? ROOT_PATH;
|
|
8
|
-
var isSamePath = (left, right = ROOT_PATH) => {
|
|
9
|
-
const normalizedLeft = normalizePath(left);
|
|
10
|
-
if (normalizedLeft.length !== right.length) return false;
|
|
11
|
-
return normalizedLeft.every((part, index) => Object.is(part, right[index]));
|
|
12
|
-
};
|
|
13
|
-
var isPathPrefix = (path, prefix) => {
|
|
14
|
-
if (prefix.length > path.length) return false;
|
|
15
|
-
return prefix.every((part, index) => Object.is(part, path[index]));
|
|
16
|
-
};
|
|
17
|
-
var createTreeState = (path) => ({
|
|
18
|
-
path: Object.freeze([...path]),
|
|
19
|
-
self: [],
|
|
20
|
-
subtree: [],
|
|
21
|
-
children: /* @__PURE__ */ new Map()
|
|
22
|
-
});
|
|
23
|
-
var TreeNode = class TreeNode {
|
|
24
|
-
constructor(state) {
|
|
25
|
-
this.path = state.path;
|
|
26
|
-
this.self = new ViolationCollection(state.self);
|
|
27
|
-
this.subtree = new ViolationCollection(state.subtree);
|
|
28
|
-
this.children = new Map([...state.children.entries()].map(([key, child]) => [key, new TreeNode(child)]));
|
|
29
|
-
}
|
|
30
|
-
at(path) {
|
|
31
|
-
if (!isPathPrefix(path, this.path)) return;
|
|
32
|
-
if (path.length === this.path.length) return this;
|
|
33
|
-
return this.children.get(path[this.path.length])?.at(path);
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
var buildTree = (violations) => {
|
|
37
|
-
const root = createTreeState(ROOT_PATH);
|
|
38
|
-
violations.forEach((violation) => {
|
|
39
|
-
const path = normalizePath(violation.path);
|
|
40
|
-
root.subtree.push(violation);
|
|
41
|
-
if (path.length === 0) {
|
|
42
|
-
root.self.push(violation);
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
let current = root;
|
|
46
|
-
const currentPath = [];
|
|
47
|
-
path.forEach((segment, index) => {
|
|
48
|
-
currentPath.push(segment);
|
|
49
|
-
let child = current.children.get(segment);
|
|
50
|
-
if (!child) {
|
|
51
|
-
child = createTreeState(currentPath);
|
|
52
|
-
current.children.set(segment, child);
|
|
53
|
-
}
|
|
54
|
-
child.subtree.push(violation);
|
|
55
|
-
if (index === path.length - 1) child.self.push(violation);
|
|
56
|
-
current = child;
|
|
57
|
-
});
|
|
58
|
-
});
|
|
59
|
-
return new TreeNode(root);
|
|
60
|
-
};
|
|
61
|
-
var ViolationCollection = class ViolationCollection {
|
|
62
|
-
constructor(violations) {
|
|
63
|
-
this.violations = [...violations];
|
|
64
|
-
this.size = this.violations.length;
|
|
65
|
-
}
|
|
66
|
-
[Symbol.iterator]() {
|
|
67
|
-
return this.violations[Symbol.iterator]();
|
|
68
|
-
}
|
|
69
|
-
forEach(callback) {
|
|
70
|
-
this.violations.forEach((violation, index) => callback(violation, index, this));
|
|
71
|
-
}
|
|
72
|
-
map(callback) {
|
|
73
|
-
return this.violations.map((violation, index) => callback(violation, index, this));
|
|
74
|
-
}
|
|
75
|
-
at(path) {
|
|
76
|
-
return new ViolationCollection(this.violations.filter((violation) => isSamePath(violation.path, path)));
|
|
77
|
-
}
|
|
78
|
-
tree() {
|
|
79
|
-
return buildTree(this.violations);
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
var collection = (violations) => new ViolationCollection(violations);
|
|
83
|
-
//#endregion
|
|
7
|
+
const require_violations = require("./violations.cjs");
|
|
84
8
|
//#region src/index.ts
|
|
85
9
|
var collectViolations = async (value, constraints, path = []) => {
|
|
86
10
|
const validations = [];
|
|
87
11
|
let establishedAssertionDomain = false;
|
|
88
|
-
for (const c of
|
|
89
|
-
if (
|
|
12
|
+
for (const c of require_constraints.arrayify(constraints)) {
|
|
13
|
+
if (require_constraints.isValidator(c)) {
|
|
90
14
|
establishedAssertionDomain = false;
|
|
91
15
|
validations.push(...c.run(collectViolations, value, path).map((v) => v instanceof Promise ? v : Promise.resolve(v)));
|
|
92
16
|
continue;
|
|
@@ -114,8 +38,8 @@ collectViolations.sync = false;
|
|
|
114
38
|
var collectViolationsSync = (value, constraints, path = []) => {
|
|
115
39
|
const violations = [];
|
|
116
40
|
let establishedAssertionDomain = false;
|
|
117
|
-
for (const c of
|
|
118
|
-
if (
|
|
41
|
+
for (const c of require_constraints.arrayify(constraints)) {
|
|
42
|
+
if (require_constraints.isValidator(c)) {
|
|
119
43
|
establishedAssertionDomain = false;
|
|
120
44
|
violations.push(...c.run(collectViolationsSync, value, path));
|
|
121
45
|
continue;
|
|
@@ -176,11 +100,11 @@ var validate = /* @__PURE__ */ Object.assign(async (value, constraints) => {
|
|
|
176
100
|
return toResult(value, collectViolationsSync(value, constraints));
|
|
177
101
|
} });
|
|
178
102
|
//#endregion
|
|
179
|
-
exports.ViolationCollection = ViolationCollection;
|
|
103
|
+
exports.ViolationCollection = require_violations.ViolationCollection;
|
|
180
104
|
exports.assert = require_assert.assert;
|
|
181
|
-
exports.collection = collection;
|
|
182
|
-
exports.custom =
|
|
183
|
-
exports.describe =
|
|
105
|
+
exports.collection = require_violations.collection;
|
|
106
|
+
exports.custom = require_metadata.custom;
|
|
107
|
+
exports.describe = require_metadata.describe;
|
|
184
108
|
exports.discriminatedUnion = require_combinators.discriminatedUnion;
|
|
185
109
|
exports.each = require_combinators.each;
|
|
186
110
|
exports.endsWith = require_assertions.endsWith;
|
|
@@ -212,7 +136,7 @@ exports.isString = require_assertions.isString;
|
|
|
212
136
|
exports.isSymbol = require_assertions.isSymbol;
|
|
213
137
|
exports.isValidDate = require_assertions.isValidDate;
|
|
214
138
|
exports.matches = matches;
|
|
215
|
-
exports.meta =
|
|
139
|
+
exports.meta = require_metadata.meta;
|
|
216
140
|
exports.multipleOf = require_assertions.multipleOf;
|
|
217
141
|
exports.nullable = require_combinators.nullable;
|
|
218
142
|
exports.nullish = require_combinators.nullish;
|