@modulify/validator 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/README.md +100 -136
- package/dist/assertions/Assert.d.ts +2 -0
- package/dist/assertions/HasLength.d.ts +7 -0
- package/dist/assertions/check.d.ts +3 -0
- package/dist/assertions/index.d.ts +16 -0
- package/dist/assertions.cjs +109 -0
- package/dist/assertions.d.ts +2 -0
- package/dist/assertions.mjs +109 -0
- package/dist/index.cjs +110 -317
- package/dist/index.d.ts +6 -0
- package/dist/index.mjs +110 -308
- package/dist/predicates.cjs +81 -0
- package/dist/predicates.d.ts +34 -0
- package/dist/predicates.mjs +81 -0
- package/dist/runners/Each.d.ts +3 -0
- package/dist/runners/HasProperties.d.ts +6 -0
- package/dist/runners/index.d.ts +2 -0
- package/dist/runners.cjs +32 -0
- package/dist/runners.d.ts +2 -0
- package/dist/runners.mjs +32 -0
- package/logo.png +0 -0
- package/package.json +62 -22
- package/types/index.d.ts +53 -118
- package/dist/index.cjs.map +0 -1
- package/dist/index.mjs.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,326 +1,119 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
11
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
12
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
13
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
14
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
15
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
16
|
-
***************************************************************************** */
|
|
17
|
-
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
21
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
22
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
23
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
24
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
25
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
26
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
31
|
-
var e = new Error(message);
|
|
32
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
class Collection {
|
|
36
|
-
constructor(constraints) {
|
|
37
|
-
this.name = '@modulify/validator/Collection';
|
|
38
|
-
this.constraints = constraints;
|
|
39
|
-
}
|
|
40
|
-
reduce(reducer, initial) {
|
|
41
|
-
return Object.keys(this.constraints).reduce((accumulator, key) => {
|
|
42
|
-
return reducer(accumulator, this.constraints[key], key);
|
|
43
|
-
}, initial);
|
|
44
|
-
}
|
|
45
|
-
toViolation(value, path, reason) {
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const assertions = require("./assertions.cjs");
|
|
4
|
+
const runners = require("./runners.cjs");
|
|
5
|
+
const check = (assert, value, path = []) => {
|
|
6
|
+
if (assert(value)) {
|
|
7
|
+
for (const a of assert.also) {
|
|
8
|
+
const violation = check(a, value, path);
|
|
9
|
+
if (violation) {
|
|
46
10
|
return {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
path,
|
|
50
|
-
reason,
|
|
11
|
+
...violation,
|
|
12
|
+
violates: assert.fqn
|
|
51
13
|
};
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
: [element]));
|
|
64
|
-
});
|
|
65
|
-
return flattened;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
value,
|
|
20
|
+
path,
|
|
21
|
+
violates: assert.fqn,
|
|
22
|
+
..."reason" in assert ? { reason: assert.reason } : {},
|
|
23
|
+
..."meta" in assert ? { meta: assert.meta } : {}
|
|
24
|
+
};
|
|
66
25
|
};
|
|
67
|
-
const
|
|
68
|
-
|
|
26
|
+
const isBatch = (c) => {
|
|
27
|
+
return "run" in c;
|
|
69
28
|
};
|
|
70
|
-
const
|
|
71
|
-
|
|
29
|
+
const _validate = async (value, constraints, path = []) => {
|
|
30
|
+
const validations = [];
|
|
31
|
+
for (const c of arraify(constraints)) {
|
|
32
|
+
if (isBatch(c)) {
|
|
33
|
+
validations.push(...c.run(_validate, value, path).map((v2) => v2 instanceof Promise ? v2 : Promise.resolve(v2)));
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const v = "That" in c ? check(c, value, [...path]) : c(value, [...path]);
|
|
37
|
+
if (v instanceof Promise) {
|
|
38
|
+
if (c.bail) {
|
|
39
|
+
const awaited = await v;
|
|
40
|
+
if (awaited) {
|
|
41
|
+
validations.push(Promise.resolve([awaited]));
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
validations.push(v.then((v2) => v2 ? [v2] : []));
|
|
46
|
+
}
|
|
47
|
+
} else if (v) {
|
|
48
|
+
validations.push(Promise.resolve([v]));
|
|
49
|
+
if (c.bail) {
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return settle(value, path, validations);
|
|
72
55
|
};
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
path,
|
|
98
|
-
reason: 'undefined',
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
class Length {
|
|
104
|
-
constructor(options) {
|
|
105
|
-
var _a, _b, _c;
|
|
106
|
-
this.name = '@modulify/validator/Length';
|
|
107
|
-
this.exact = (_a = options.exact) !== null && _a !== void 0 ? _a : null;
|
|
108
|
-
this.max = (_b = options.max) !== null && _b !== void 0 ? _b : null;
|
|
109
|
-
this.min = (_c = options.min) !== null && _c !== void 0 ? _c : null;
|
|
110
|
-
}
|
|
111
|
-
toViolation(value, path, reason) {
|
|
112
|
-
return {
|
|
113
|
-
by: this.name,
|
|
114
|
-
value,
|
|
115
|
-
path,
|
|
116
|
-
reason,
|
|
117
|
-
meta: {
|
|
118
|
-
exact: this.exact,
|
|
119
|
-
max: this.max,
|
|
120
|
-
min: this.min,
|
|
121
|
-
}[reason],
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
class OneOf {
|
|
127
|
-
constructor(values, equalTo = (a, b) => a === b) {
|
|
128
|
-
this.name = '@modulify/validator/OneOf';
|
|
129
|
-
this.values = Array.isArray(values) ? values : Object.values(values);
|
|
130
|
-
this.equalTo = equalTo;
|
|
131
|
-
}
|
|
132
|
-
toViolation(value, path) {
|
|
133
|
-
return {
|
|
134
|
-
by: this.name,
|
|
135
|
-
value,
|
|
136
|
-
path,
|
|
137
|
-
meta: this.values,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
class LengthValidator {
|
|
143
|
-
constructor(constraint) {
|
|
144
|
-
this.constraint = constraint;
|
|
145
|
-
}
|
|
146
|
-
validate(value, path = []) {
|
|
147
|
-
const constraint = this.constraint;
|
|
148
|
-
const { exact, max, min } = constraint;
|
|
149
|
-
if (!(typeof value === 'string' || Array.isArray(value))) {
|
|
150
|
-
return constraint.toViolation(value, path, 'unsupported');
|
|
151
|
-
}
|
|
152
|
-
if (exact !== null && exact !== value.length) {
|
|
153
|
-
return constraint.toViolation(value, path, 'exact');
|
|
154
|
-
}
|
|
155
|
-
if (max !== null && value.length > max) {
|
|
156
|
-
return constraint.toViolation(value, path, 'max');
|
|
157
|
-
}
|
|
158
|
-
if (min !== null && value.length < min) {
|
|
159
|
-
return constraint.toViolation(value, path, 'min');
|
|
160
|
-
}
|
|
161
|
-
return null;
|
|
162
|
-
}
|
|
56
|
+
const _sync = (value, constraints, path = []) => {
|
|
57
|
+
const violations = [];
|
|
58
|
+
for (const c of arraify(constraints)) {
|
|
59
|
+
if (isBatch(c)) {
|
|
60
|
+
violations.push(...c.run(_sync, value, path));
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const v = "That" in c ? check(c, value, [...path]) : c(value, [...path]);
|
|
64
|
+
if (v instanceof Promise) {
|
|
65
|
+
throw new Error("Found asynchronous constraint validator " + String(c.fqn));
|
|
66
|
+
} else if (v) {
|
|
67
|
+
violations.push(v);
|
|
68
|
+
if (c.bail) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return flatten(violations);
|
|
74
|
+
};
|
|
75
|
+
const validate = Object.assign(_validate, {
|
|
76
|
+
sync: _sync
|
|
77
|
+
});
|
|
78
|
+
function arraify(value) {
|
|
79
|
+
return Array.isArray(value) ? [...value] : [value];
|
|
163
80
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const equalTo = this.constraint.equalTo;
|
|
171
|
-
if (!this.constraint.values.some(allowed => equalTo(allowed, value))) {
|
|
172
|
-
return this.constraint.toViolation(value, path);
|
|
173
|
-
}
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
81
|
+
function flatten(recursive) {
|
|
82
|
+
const flattened = [];
|
|
83
|
+
recursive.forEach((element) => {
|
|
84
|
+
flattened.push(...Array.isArray(element) ? flatten(element) : [element]);
|
|
85
|
+
});
|
|
86
|
+
return flattened;
|
|
176
87
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
override(provider) {
|
|
195
|
-
return new ProviderChain(provider, this);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
const validateAsynchronously = (provider, value, constraints, path = []) => __awaiter(void 0, void 0, void 0, function* () {
|
|
200
|
-
const validations = [];
|
|
201
|
-
for (const c of arraify(constraints)) {
|
|
202
|
-
if (c instanceof Collection) {
|
|
203
|
-
if (isRecord(value)) {
|
|
204
|
-
validations.push(...c.reduce((validations, constraints, key) => {
|
|
205
|
-
return [...validations, validateAsynchronously(provider, value[key], constraints, [...path, key])];
|
|
206
|
-
}, []));
|
|
207
|
-
}
|
|
208
|
-
else {
|
|
209
|
-
validations.push(Promise.resolve([c.toViolation(value, path, 'unsupported')]));
|
|
210
|
-
}
|
|
211
|
-
continue;
|
|
212
|
-
}
|
|
213
|
-
if (c instanceof Each) {
|
|
214
|
-
if (Array.isArray(value)) {
|
|
215
|
-
value.forEach((value, index) => {
|
|
216
|
-
validations.push(validateAsynchronously(provider, value, c.constraints, [...path, index]));
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
else {
|
|
220
|
-
validations.push(validateAsynchronously(provider, value, c.constraints, [...path]));
|
|
221
|
-
}
|
|
222
|
-
continue;
|
|
223
|
-
}
|
|
224
|
-
if (c instanceof Exists) {
|
|
225
|
-
if (typeof value === 'undefined') {
|
|
226
|
-
validations.push(Promise.resolve([c.toViolation(value, [...path])]));
|
|
227
|
-
break;
|
|
228
|
-
}
|
|
229
|
-
continue;
|
|
230
|
-
}
|
|
231
|
-
const validator = provider.get(c);
|
|
232
|
-
if (!validator) {
|
|
233
|
-
throw new Error('No validator for constraint ' + c.name);
|
|
234
|
-
}
|
|
235
|
-
const v = validator.validate(value, [...path]);
|
|
236
|
-
if (v) {
|
|
237
|
-
if (v instanceof Promise) {
|
|
238
|
-
validations.push(v.then(v => v ? [v] : []));
|
|
239
|
-
}
|
|
240
|
-
else {
|
|
241
|
-
validations.push(Promise.resolve([v]));
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
const results = yield Promise.allSettled(validations);
|
|
246
|
-
const violations = [];
|
|
247
|
-
results.forEach(result => {
|
|
248
|
-
if (result.status === 'fulfilled') {
|
|
249
|
-
violations.push(...result.value);
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
|
-
return violations;
|
|
253
|
-
});
|
|
254
|
-
const validateSynchronously = (provider, value, constraints, path = []) => {
|
|
255
|
-
const violations = [];
|
|
256
|
-
for (const c of arraify(constraints)) {
|
|
257
|
-
if (c instanceof Collection) {
|
|
258
|
-
if (isRecord(value)) {
|
|
259
|
-
violations.push(c.reduce((violations, constraints, key) => {
|
|
260
|
-
return [...violations, ...validateSynchronously(provider, value[key], constraints, [...path, key])];
|
|
261
|
-
}, []));
|
|
262
|
-
}
|
|
263
|
-
else {
|
|
264
|
-
violations.push(c.toViolation(value, path, 'unsupported'));
|
|
265
|
-
}
|
|
266
|
-
continue;
|
|
267
|
-
}
|
|
268
|
-
if (c instanceof Each) {
|
|
269
|
-
if (Array.isArray(value)) {
|
|
270
|
-
value.forEach((value, index) => {
|
|
271
|
-
violations.push(...validateSynchronously(provider, value, c.constraints, [...path, index]));
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
else {
|
|
275
|
-
violations.push(...validateSynchronously(provider, value, c.constraints, [...path]));
|
|
276
|
-
}
|
|
277
|
-
continue;
|
|
278
|
-
}
|
|
279
|
-
if (c instanceof Exists) {
|
|
280
|
-
if (typeof value === 'undefined') {
|
|
281
|
-
violations.push(c.toViolation(value, [...path]));
|
|
282
|
-
break;
|
|
283
|
-
}
|
|
284
|
-
continue;
|
|
285
|
-
}
|
|
286
|
-
const validator = provider.get(c);
|
|
287
|
-
if (!validator) {
|
|
288
|
-
throw new Error('No validator for constraint ' + c.name);
|
|
289
|
-
}
|
|
290
|
-
const v = validator.validate(value, [...path]);
|
|
291
|
-
if (v) {
|
|
292
|
-
if (v instanceof Promise) {
|
|
293
|
-
throw new Error('Found asynchronous validator for constraint ' + c.name);
|
|
294
|
-
}
|
|
295
|
-
violations.push(v);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
return flatten(violations);
|
|
299
|
-
};
|
|
300
|
-
const validate = (provider, value, constraints, path = [], asynchronously = true) => {
|
|
301
|
-
return asynchronously
|
|
302
|
-
? validateAsynchronously(provider, value, constraints, path)
|
|
303
|
-
: validateSynchronously(provider, value, constraints, path);
|
|
304
|
-
};
|
|
305
|
-
class V {
|
|
306
|
-
constructor(provider = null) {
|
|
307
|
-
this._provider = provider !== null && provider !== void 0 ? provider : new ProviderChain();
|
|
308
|
-
}
|
|
309
|
-
override(provider) {
|
|
310
|
-
return new V(this._provider.override(provider));
|
|
311
|
-
}
|
|
312
|
-
validate(value, constraints, asynchronously = true) {
|
|
313
|
-
return validate(this._provider, value, constraints, [], asynchronously);
|
|
314
|
-
}
|
|
88
|
+
async function settle(value, path, validations) {
|
|
89
|
+
const violations = [];
|
|
90
|
+
const settled = await Promise.allSettled(validations);
|
|
91
|
+
settled.forEach((result) => {
|
|
92
|
+
if (result.status === "fulfilled") {
|
|
93
|
+
violations.push(...result.value);
|
|
94
|
+
} else {
|
|
95
|
+
violations.push({
|
|
96
|
+
value,
|
|
97
|
+
path,
|
|
98
|
+
violates: "@modulify/validator",
|
|
99
|
+
reason: "reject",
|
|
100
|
+
meta: result.reason
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
return violations;
|
|
315
105
|
}
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
exports.
|
|
319
|
-
exports.
|
|
320
|
-
exports.
|
|
321
|
-
exports.
|
|
322
|
-
exports.
|
|
323
|
-
exports.
|
|
324
|
-
exports.
|
|
106
|
+
exports.Assert = assertions.Assert;
|
|
107
|
+
exports.HasLength = assertions.HasLength;
|
|
108
|
+
exports.IsBoolean = assertions.IsBoolean;
|
|
109
|
+
exports.IsDate = assertions.IsDate;
|
|
110
|
+
exports.IsDefined = assertions.IsDefined;
|
|
111
|
+
exports.IsEmail = assertions.IsEmail;
|
|
112
|
+
exports.IsNull = assertions.IsNull;
|
|
113
|
+
exports.IsNumber = assertions.IsNumber;
|
|
114
|
+
exports.IsString = assertions.IsString;
|
|
115
|
+
exports.IsSymbol = assertions.IsSymbol;
|
|
116
|
+
exports.OneOf = assertions.OneOf;
|
|
117
|
+
exports.Each = runners.Each;
|
|
118
|
+
exports.HasProperties = runners.HasProperties;
|
|
325
119
|
exports.validate = validate;
|
|
326
|
-
//# sourceMappingURL=index.cjs.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Constraint, MaybeMany, Violation } from '../types';
|
|
2
|
+
export * from './assertions';
|
|
3
|
+
export * from './runners';
|
|
4
|
+
export declare const validate: (<T>(value: T, constraints: MaybeMany<Constraint>, path?: PropertyKey[]) => Promise<Violation[]>) & {
|
|
5
|
+
sync: <T>(value: T, constraints: MaybeMany<Constraint>, path?: PropertyKey[]) => Violation[];
|
|
6
|
+
};
|