justus 0.0.6 → 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/NOTICE.md +1 -1
- package/README.md +13 -6
- package/dist/dts-generator.js +67 -14
- package/dist/dts-generator.js.map +2 -2
- package/dist/dts-generator.mjs +69 -15
- package/dist/dts-generator.mjs.map +2 -2
- package/dist/index.js +169 -151
- package/dist/index.js.map +3 -3
- package/dist/index.mjs +163 -150
- package/dist/index.mjs.map +3 -3
- package/dts-generator.d.ts +3 -3
- package/index.d.ts +75 -134
- package/package.json +14 -14
- package/src/dts-generator.ts +44 -18
- package/src/index.ts +3 -1
- package/src/schema.ts +1 -72
- package/src/types.ts +53 -140
- package/src/utilities.ts +5 -11
- package/src/validators/any.ts +2 -2
- package/src/validators/array.ts +7 -15
- package/src/validators/boolean.ts +2 -2
- package/src/validators/constant.ts +2 -2
- package/src/validators/date.ts +5 -11
- package/src/validators/never.ts +18 -0
- package/src/validators/number.ts +8 -12
- package/src/validators/object.ts +15 -48
- package/src/validators/optional.ts +30 -0
- package/src/validators/string.ts +8 -13
- package/src/validators/tuple.ts +2 -12
- package/src/validators/union.ts +3 -2
- package/src/validators/url.ts +5 -9
package/dist/index.js
CHANGED
|
@@ -1,37 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
-
var __spreadValues = (a, b) => {
|
|
11
|
-
for (var prop in b || (b = {}))
|
|
12
|
-
if (__hasOwnProp.call(b, prop))
|
|
13
|
-
__defNormalProp(a, prop, b[prop]);
|
|
14
|
-
if (__getOwnPropSymbols)
|
|
15
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
-
if (__propIsEnum.call(b, prop))
|
|
17
|
-
__defNormalProp(a, prop, b[prop]);
|
|
18
|
-
}
|
|
19
|
-
return a;
|
|
20
|
-
};
|
|
21
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
22
|
-
var __restKey = (key) => typeof key === "symbol" ? key : key + "";
|
|
23
|
-
var __objRest = (source, exclude) => {
|
|
24
|
-
var target = {};
|
|
25
|
-
for (var prop in source)
|
|
26
|
-
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
27
|
-
target[prop] = source[prop];
|
|
28
|
-
if (source != null && __getOwnPropSymbols)
|
|
29
|
-
for (var prop of __getOwnPropSymbols(source)) {
|
|
30
|
-
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
31
|
-
target[prop] = source[prop];
|
|
32
|
-
}
|
|
33
|
-
return target;
|
|
34
|
-
};
|
|
35
6
|
var __export = (target, all) => {
|
|
36
7
|
for (var name in all)
|
|
37
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
@@ -49,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
49
20
|
// src/index.ts
|
|
50
21
|
var src_exports = {};
|
|
51
22
|
__export(src_exports, {
|
|
23
|
+
AbstractValidator: () => AbstractValidator,
|
|
52
24
|
AllOfValidator: () => AllOfValidator,
|
|
53
25
|
AnyArrayValidator: () => AnyArrayValidator,
|
|
54
26
|
AnyNumberValidator: () => AnyNumberValidator,
|
|
@@ -59,15 +31,16 @@ __export(src_exports, {
|
|
|
59
31
|
BooleanValidator: () => BooleanValidator,
|
|
60
32
|
ConstantValidator: () => ConstantValidator,
|
|
61
33
|
DateValidator: () => DateValidator,
|
|
34
|
+
NeverValidator: () => NeverValidator,
|
|
62
35
|
NumberValidator: () => NumberValidator,
|
|
63
36
|
ObjectValidator: () => ObjectValidator,
|
|
64
37
|
OneOfValidator: () => OneOfValidator,
|
|
38
|
+
OptionalValidator: () => OptionalValidator,
|
|
65
39
|
StringValidator: () => StringValidator,
|
|
66
40
|
TupleValidator: () => TupleValidator,
|
|
67
41
|
URLValidator: () => URLValidator,
|
|
68
42
|
ValidationError: () => ValidationError,
|
|
69
43
|
ValidationErrorBuilder: () => ValidationErrorBuilder,
|
|
70
|
-
Validator: () => Validator,
|
|
71
44
|
_allowAdditionalProperties: () => _allowAdditionalProperties,
|
|
72
45
|
_array: () => _array,
|
|
73
46
|
_date: () => _date,
|
|
@@ -87,14 +60,13 @@ __export(src_exports, {
|
|
|
87
60
|
constant: () => constant,
|
|
88
61
|
date: () => date,
|
|
89
62
|
getValidator: () => getValidator,
|
|
90
|
-
|
|
91
|
-
|
|
63
|
+
isValidator: () => isValidator,
|
|
64
|
+
makeValidatorFactory: () => makeValidatorFactory,
|
|
92
65
|
never: () => never,
|
|
93
66
|
number: () => number,
|
|
94
67
|
object: () => object,
|
|
95
68
|
oneOf: () => oneOf,
|
|
96
69
|
optional: () => optional,
|
|
97
|
-
readonly: () => readonly,
|
|
98
70
|
restValidator: () => restValidator,
|
|
99
71
|
schemaValidator: () => schemaValidator,
|
|
100
72
|
string: () => string,
|
|
@@ -114,7 +86,9 @@ function pathToString(path) {
|
|
|
114
86
|
}, "");
|
|
115
87
|
}
|
|
116
88
|
var ValidationError = class extends Error {
|
|
89
|
+
/** An `Array` of validation errors encountered while validating */
|
|
117
90
|
errors;
|
|
91
|
+
/** Our stack, always present as we enforce it in the constructor */
|
|
118
92
|
stack;
|
|
119
93
|
constructor(builderOrCause, constructorOrPath, maybeConstructor) {
|
|
120
94
|
let constructor;
|
|
@@ -140,7 +114,16 @@ var ValidationError = class extends Error {
|
|
|
140
114
|
}
|
|
141
115
|
};
|
|
142
116
|
var ValidationErrorBuilder = class {
|
|
117
|
+
/** The current list of validation errors */
|
|
143
118
|
errors = [];
|
|
119
|
+
/**
|
|
120
|
+
* Record a validation error associated with the specified key.
|
|
121
|
+
*
|
|
122
|
+
* @param error - The error (normally a `string` or a `ValidationError`)
|
|
123
|
+
* to record and associate with the given key
|
|
124
|
+
* @param key - The key in an object, or index in an array where the
|
|
125
|
+
* vaildation error was encountered
|
|
126
|
+
*/
|
|
144
127
|
record(error, ...key) {
|
|
145
128
|
const path = [...key];
|
|
146
129
|
if (error instanceof ValidationError) {
|
|
@@ -152,6 +135,12 @@ var ValidationErrorBuilder = class {
|
|
|
152
135
|
}
|
|
153
136
|
return this;
|
|
154
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* Assert there are no validation errors and return the specified value, or
|
|
140
|
+
* throw a `ValidationError` combining all errors
|
|
141
|
+
*
|
|
142
|
+
* @param value - The value to return if no errors have been recorded
|
|
143
|
+
*/
|
|
155
144
|
assert(value) {
|
|
156
145
|
if (this.errors.length > 0)
|
|
157
146
|
throw new ValidationError(this);
|
|
@@ -168,19 +157,29 @@ function assertSchema(what, message) {
|
|
|
168
157
|
}
|
|
169
158
|
|
|
170
159
|
// src/types.ts
|
|
160
|
+
var isValidator = Symbol.for("justus.isValidator");
|
|
171
161
|
var restValidator = Symbol.for("justus.restValidator");
|
|
172
162
|
var schemaValidator = Symbol.for("justus.schemaValidator");
|
|
173
|
-
var modifierValidator = Symbol.for("justus.modifierValidator");
|
|
174
163
|
var additionalValidator = Symbol.for("justus.additionalValidator");
|
|
175
|
-
|
|
176
|
-
|
|
164
|
+
function makeValidatorFactory(validator, factory) {
|
|
165
|
+
return Object.assign(factory, {
|
|
166
|
+
optional: validator.optional,
|
|
167
|
+
validate: validator.validate.bind(validator),
|
|
168
|
+
[Symbol.iterator]: validator[Symbol.iterator].bind(validator),
|
|
169
|
+
[isValidator]: true
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
var AbstractValidator = class {
|
|
173
|
+
[isValidator] = true;
|
|
174
|
+
optional = void 0;
|
|
175
|
+
/** Allow any `Validator` to be used as a rest parameter in `Tuple`s */
|
|
177
176
|
*[Symbol.iterator]() {
|
|
178
177
|
yield { [restValidator]: this };
|
|
179
178
|
}
|
|
180
179
|
};
|
|
181
180
|
|
|
182
181
|
// src/validators/any.ts
|
|
183
|
-
var AnyValidator = class extends
|
|
182
|
+
var AnyValidator = class extends AbstractValidator {
|
|
184
183
|
validate(value) {
|
|
185
184
|
return value;
|
|
186
185
|
}
|
|
@@ -188,7 +187,7 @@ var AnyValidator = class extends Validator {
|
|
|
188
187
|
var any = new AnyValidator();
|
|
189
188
|
|
|
190
189
|
// src/validators/constant.ts
|
|
191
|
-
var ConstantValidator = class extends
|
|
190
|
+
var ConstantValidator = class extends AbstractValidator {
|
|
192
191
|
constant;
|
|
193
192
|
constructor(constant2) {
|
|
194
193
|
super();
|
|
@@ -205,7 +204,7 @@ function constant(constant2) {
|
|
|
205
204
|
var nullValidator = new ConstantValidator(null);
|
|
206
205
|
|
|
207
206
|
// src/validators/tuple.ts
|
|
208
|
-
var TupleValidator = class extends
|
|
207
|
+
var TupleValidator = class extends AbstractValidator {
|
|
209
208
|
members;
|
|
210
209
|
tuple;
|
|
211
210
|
constructor(tuple2) {
|
|
@@ -264,65 +263,38 @@ var TupleValidator = class extends Validator {
|
|
|
264
263
|
function tuple(tuple2) {
|
|
265
264
|
return new TupleValidator(tuple2);
|
|
266
265
|
}
|
|
267
|
-
function makeTupleRestIterable(create) {
|
|
268
|
-
const validator = create();
|
|
269
|
-
create[Symbol.iterator] = function* () {
|
|
270
|
-
yield { [restValidator]: validator };
|
|
271
|
-
};
|
|
272
|
-
return create;
|
|
273
|
-
}
|
|
274
266
|
|
|
275
267
|
// src/validators/object.ts
|
|
276
|
-
var AnyObjectValidator = class extends
|
|
268
|
+
var AnyObjectValidator = class extends AbstractValidator {
|
|
277
269
|
validate(value) {
|
|
278
270
|
assertValidation(typeof value == "object", 'Value is not an "object"');
|
|
279
271
|
assertValidation(value !== null, 'Value is "null"');
|
|
280
272
|
return value;
|
|
281
273
|
}
|
|
282
274
|
};
|
|
283
|
-
var ObjectValidator = class extends
|
|
275
|
+
var ObjectValidator = class extends AbstractValidator {
|
|
284
276
|
schema;
|
|
285
|
-
|
|
277
|
+
validators = /* @__PURE__ */ new Map();
|
|
286
278
|
additionalProperties;
|
|
287
279
|
constructor(schema) {
|
|
288
280
|
super();
|
|
289
|
-
|
|
290
|
-
const _b = schema, { [_a = additionalValidator]: additional } = _b, properties = __objRest(_b, [__restKey(_a)]);
|
|
281
|
+
const { [additionalValidator]: additional, ...properties } = schema;
|
|
291
282
|
if (additional)
|
|
292
283
|
this.additionalProperties = getValidator(additional);
|
|
293
284
|
for (const key of Object.keys(properties)) {
|
|
294
|
-
|
|
295
|
-
if (definition === never) {
|
|
296
|
-
this.properties.set(key, void 0);
|
|
297
|
-
} else if (isModifier(definition)) {
|
|
298
|
-
this.properties.set(key, {
|
|
299
|
-
validator: definition[modifierValidator],
|
|
300
|
-
readonly: definition.readonly,
|
|
301
|
-
optional: definition.optional
|
|
302
|
-
});
|
|
303
|
-
} else {
|
|
304
|
-
this.properties.set(key, { validator: getValidator(definition) });
|
|
305
|
-
}
|
|
285
|
+
this.validators.set(key, getValidator(properties[key]));
|
|
306
286
|
}
|
|
307
287
|
this.schema = schema;
|
|
308
288
|
}
|
|
309
289
|
validate(value, options) {
|
|
310
290
|
assertValidation(typeof value === "object", 'Value is not an "object"');
|
|
311
291
|
assertValidation(value !== null, 'Value is "null"');
|
|
312
|
-
const { stripAdditionalProperties,
|
|
292
|
+
const { stripAdditionalProperties, stripOptionalNulls } = options;
|
|
313
293
|
const record = value;
|
|
314
294
|
const builder = new ValidationErrorBuilder();
|
|
315
295
|
const clone = {};
|
|
316
|
-
for (const [key,
|
|
317
|
-
const
|
|
318
|
-
if (!validator) {
|
|
319
|
-
if (record[key] === void 0)
|
|
320
|
-
continue;
|
|
321
|
-
if (stripForbiddenProperties)
|
|
322
|
-
continue;
|
|
323
|
-
builder.record("Forbidden property", key);
|
|
324
|
-
continue;
|
|
325
|
-
}
|
|
296
|
+
for (const [key, validator] of this.validators.entries()) {
|
|
297
|
+
const optional2 = !!validator.optional;
|
|
326
298
|
if (record[key] === void 0) {
|
|
327
299
|
if (!optional2)
|
|
328
300
|
builder.record("Required property missing", key);
|
|
@@ -332,12 +304,14 @@ var ObjectValidator = class extends Validator {
|
|
|
332
304
|
continue;
|
|
333
305
|
}
|
|
334
306
|
try {
|
|
335
|
-
|
|
307
|
+
const value2 = validator.validate(record[key], options);
|
|
308
|
+
if (!(optional2 && value2 == void 0))
|
|
309
|
+
clone[key] = value2;
|
|
336
310
|
} catch (error) {
|
|
337
311
|
builder.record(error, key);
|
|
338
312
|
}
|
|
339
313
|
}
|
|
340
|
-
const additionalKeys = Object.keys(record).filter((k) => !this.
|
|
314
|
+
const additionalKeys = Object.keys(record).filter((k) => !this.validators.has(k));
|
|
341
315
|
const additional = this.additionalProperties;
|
|
342
316
|
if (additional) {
|
|
343
317
|
additionalKeys.forEach((key) => {
|
|
@@ -358,10 +332,7 @@ var ObjectValidator = class extends Validator {
|
|
|
358
332
|
return builder.assert(clone);
|
|
359
333
|
}
|
|
360
334
|
};
|
|
361
|
-
var anyObjectValidator = new AnyObjectValidator();
|
|
362
335
|
function _object(schema) {
|
|
363
|
-
if (!schema)
|
|
364
|
-
return anyObjectValidator;
|
|
365
336
|
const validator = new ObjectValidator(schema);
|
|
366
337
|
function* iterator() {
|
|
367
338
|
yield { [restValidator]: validator };
|
|
@@ -371,23 +342,19 @@ function _object(schema) {
|
|
|
371
342
|
[Symbol.iterator]: { value: iterator, enumerable: false }
|
|
372
343
|
});
|
|
373
344
|
}
|
|
374
|
-
var object =
|
|
345
|
+
var object = makeValidatorFactory(new AnyObjectValidator(), _object);
|
|
375
346
|
|
|
376
347
|
// src/utilities.ts
|
|
377
348
|
function getValidator(validation) {
|
|
378
|
-
if (validation === void 0)
|
|
379
|
-
return any;
|
|
380
349
|
if (validation === null)
|
|
381
350
|
return nullValidator;
|
|
382
|
-
if (validation
|
|
351
|
+
if (validation[isValidator] === true)
|
|
383
352
|
return validation;
|
|
384
353
|
switch (typeof validation) {
|
|
385
354
|
case "boolean":
|
|
386
355
|
case "string":
|
|
387
356
|
case "number":
|
|
388
357
|
return new ConstantValidator(validation);
|
|
389
|
-
case "function":
|
|
390
|
-
return validation();
|
|
391
358
|
case "object":
|
|
392
359
|
if (schemaValidator in validation)
|
|
393
360
|
return validation[schemaValidator];
|
|
@@ -405,26 +372,13 @@ function _allowAdditionalProperties(options) {
|
|
|
405
372
|
return { [additionalValidator]: false };
|
|
406
373
|
if (options === true)
|
|
407
374
|
return { [additionalValidator]: any };
|
|
408
|
-
return { [additionalValidator]: getValidator(options) };
|
|
375
|
+
return { [additionalValidator]: options ? getValidator(options) : any };
|
|
409
376
|
}
|
|
410
377
|
var allowAdditionalProperties = _allowAdditionalProperties;
|
|
411
378
|
allowAdditionalProperties[additionalValidator] = any;
|
|
412
|
-
function isModifier(what) {
|
|
413
|
-
return what && typeof what === "object" && modifierValidator in what;
|
|
414
|
-
}
|
|
415
|
-
function readonly(options) {
|
|
416
|
-
const { [modifierValidator]: validation = any, optional: optional2 = false } = isModifier(options) ? options : { [modifierValidator]: options };
|
|
417
|
-
const validator = getValidator(validation);
|
|
418
|
-
return optional2 ? { [modifierValidator]: validator, optional: optional2, readonly: true } : { [modifierValidator]: validator, readonly: true };
|
|
419
|
-
}
|
|
420
|
-
function optional(options) {
|
|
421
|
-
const { [modifierValidator]: validation = any, readonly: readonly2 = false } = isModifier(options) ? options : { [modifierValidator]: options };
|
|
422
|
-
const validator = getValidator(validation);
|
|
423
|
-
return readonly2 ? { [modifierValidator]: validator, readonly: readonly2, optional: true } : { [modifierValidator]: validator, optional: true };
|
|
424
|
-
}
|
|
425
379
|
|
|
426
380
|
// src/validators/union.ts
|
|
427
|
-
var OneOfValidator = class extends
|
|
381
|
+
var OneOfValidator = class extends AbstractValidator {
|
|
428
382
|
validators;
|
|
429
383
|
constructor(args) {
|
|
430
384
|
super();
|
|
@@ -445,7 +399,7 @@ var OneOfValidator = class extends Validator {
|
|
|
445
399
|
function oneOf(...args) {
|
|
446
400
|
return new OneOfValidator(args);
|
|
447
401
|
}
|
|
448
|
-
var AllOfValidator = class extends
|
|
402
|
+
var AllOfValidator = class extends AbstractValidator {
|
|
449
403
|
validators;
|
|
450
404
|
constructor(args) {
|
|
451
405
|
super();
|
|
@@ -463,13 +417,13 @@ function allOf(...args) {
|
|
|
463
417
|
}
|
|
464
418
|
|
|
465
419
|
// src/validators/array.ts
|
|
466
|
-
var AnyArrayValidator = class extends
|
|
420
|
+
var AnyArrayValidator = class extends AbstractValidator {
|
|
467
421
|
validate(value, options) {
|
|
468
422
|
assertValidation(Array.isArray(value), 'Value is not an "array"');
|
|
469
423
|
return [...value];
|
|
470
424
|
}
|
|
471
425
|
};
|
|
472
|
-
var ArrayValidator = class extends
|
|
426
|
+
var ArrayValidator = class extends AbstractValidator {
|
|
473
427
|
maxItems;
|
|
474
428
|
minItems;
|
|
475
429
|
uniqueItems;
|
|
@@ -492,8 +446,14 @@ var ArrayValidator = class extends Validator {
|
|
|
492
446
|
}
|
|
493
447
|
validate(value, options) {
|
|
494
448
|
assertValidation(Array.isArray(value), 'Value is not an "array"');
|
|
495
|
-
assertValidation(
|
|
496
|
-
|
|
449
|
+
assertValidation(
|
|
450
|
+
value.length >= this.minItems,
|
|
451
|
+
`Array must have a minimum length of ${this.minItems}`
|
|
452
|
+
);
|
|
453
|
+
assertValidation(
|
|
454
|
+
value.length <= this.maxItems,
|
|
455
|
+
`Array must have a maximum length of ${this.maxItems}`
|
|
456
|
+
);
|
|
497
457
|
const builder = new ValidationErrorBuilder();
|
|
498
458
|
const clone = new Array(value.length);
|
|
499
459
|
value.forEach((item, i) => {
|
|
@@ -513,20 +473,17 @@ var ArrayValidator = class extends Validator {
|
|
|
513
473
|
return builder.assert(clone);
|
|
514
474
|
}
|
|
515
475
|
};
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
return anyArrayValidator;
|
|
520
|
-
const items = getValidator(options.items);
|
|
521
|
-
return new ArrayValidator(__spreadProps(__spreadValues({}, options), { items }));
|
|
476
|
+
function _array(constraints) {
|
|
477
|
+
const items = constraints.items ? getValidator(constraints.items) : any;
|
|
478
|
+
return new ArrayValidator({ ...constraints, items });
|
|
522
479
|
}
|
|
523
|
-
var array =
|
|
480
|
+
var array = makeValidatorFactory(new AnyArrayValidator(), _array);
|
|
524
481
|
function arrayOf(validation) {
|
|
525
482
|
return new ArrayValidator({ items: getValidator(validation) });
|
|
526
483
|
}
|
|
527
484
|
|
|
528
485
|
// src/validators/boolean.ts
|
|
529
|
-
var BooleanValidator = class extends
|
|
486
|
+
var BooleanValidator = class extends AbstractValidator {
|
|
530
487
|
validate(value) {
|
|
531
488
|
assertValidation(typeof value === "boolean", 'Value is not a "boolean"');
|
|
532
489
|
return value;
|
|
@@ -536,7 +493,7 @@ var boolean = new BooleanValidator();
|
|
|
536
493
|
|
|
537
494
|
// src/validators/date.ts
|
|
538
495
|
var ISO_8601_REGEX = /^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|(?:(?:\+|-)\d{2}:\d{2}))?)?$/;
|
|
539
|
-
var DateValidator = class extends
|
|
496
|
+
var DateValidator = class extends AbstractValidator {
|
|
540
497
|
format;
|
|
541
498
|
from;
|
|
542
499
|
until;
|
|
@@ -544,7 +501,10 @@ var DateValidator = class extends Validator {
|
|
|
544
501
|
super();
|
|
545
502
|
const { format, from, until } = constraints;
|
|
546
503
|
if (from != void 0 && until !== void 0) {
|
|
547
|
-
assertSchema(
|
|
504
|
+
assertSchema(
|
|
505
|
+
until.getTime() >= from.getTime(),
|
|
506
|
+
`Constraint "until" (${until.toISOString()}) must not be before "from" (${from.toISOString()})`
|
|
507
|
+
);
|
|
548
508
|
}
|
|
549
509
|
this.format = format;
|
|
550
510
|
this.from = from;
|
|
@@ -574,11 +534,22 @@ var DateValidator = class extends Validator {
|
|
|
574
534
|
return date2;
|
|
575
535
|
}
|
|
576
536
|
};
|
|
577
|
-
var anyDateValidator = new DateValidator();
|
|
578
537
|
function _date(constraints) {
|
|
579
|
-
return
|
|
538
|
+
return new DateValidator(constraints);
|
|
580
539
|
}
|
|
581
|
-
var date =
|
|
540
|
+
var date = makeValidatorFactory(new DateValidator(), _date);
|
|
541
|
+
|
|
542
|
+
// src/validators/never.ts
|
|
543
|
+
var NeverValidator = class extends AbstractValidator {
|
|
544
|
+
optional = true;
|
|
545
|
+
validate(value, options) {
|
|
546
|
+
const { stripForbiddenProperties } = options;
|
|
547
|
+
if (stripForbiddenProperties || value === void 0)
|
|
548
|
+
return;
|
|
549
|
+
throw new ValidationError("Forbidden property");
|
|
550
|
+
}
|
|
551
|
+
};
|
|
552
|
+
var never = new NeverValidator();
|
|
582
553
|
|
|
583
554
|
// src/validators/number.ts
|
|
584
555
|
var PRECISION = 6;
|
|
@@ -590,14 +561,14 @@ function countDecimals(n) {
|
|
|
590
561
|
const digits = (match[1] || ".").length - 1 - parseInt(match[2]);
|
|
591
562
|
return digits < 0 ? 0 : digits;
|
|
592
563
|
}
|
|
593
|
-
var AnyNumberValidator = class extends
|
|
564
|
+
var AnyNumberValidator = class extends AbstractValidator {
|
|
594
565
|
validate(value) {
|
|
595
566
|
assertValidation(typeof value == "number", 'Value is not a "number"');
|
|
596
567
|
assertValidation(!isNaN(value), 'Number is "NaN"');
|
|
597
568
|
return value;
|
|
598
569
|
}
|
|
599
570
|
};
|
|
600
|
-
var NumberValidator = class extends
|
|
571
|
+
var NumberValidator = class extends AbstractValidator {
|
|
601
572
|
#isMultipleOf;
|
|
602
573
|
allowNaN;
|
|
603
574
|
exclusiveMaximum;
|
|
@@ -620,13 +591,22 @@ var NumberValidator = class extends Validator {
|
|
|
620
591
|
this.brand = constraints.brand;
|
|
621
592
|
assertSchema(maximum >= minimum, `Constraint "minimum" (${minimum}) is greater than "maximum" (${maximum})`);
|
|
622
593
|
if (exclusiveMaximum !== void 0) {
|
|
623
|
-
assertSchema(
|
|
594
|
+
assertSchema(
|
|
595
|
+
exclusiveMaximum > minimum,
|
|
596
|
+
`Constraint "exclusiveMaximum" (${exclusiveMaximum}) must be greater than "minimum" (${minimum})`
|
|
597
|
+
);
|
|
624
598
|
}
|
|
625
599
|
if (exclusiveMinimum !== void 0) {
|
|
626
|
-
assertSchema(
|
|
600
|
+
assertSchema(
|
|
601
|
+
maximum > exclusiveMinimum,
|
|
602
|
+
`Constraint "maximum" (${maximum}) must be greater than "exclusiveMinimum" (${exclusiveMinimum})`
|
|
603
|
+
);
|
|
627
604
|
}
|
|
628
605
|
if (exclusiveMinimum != void 0 && exclusiveMaximum !== void 0) {
|
|
629
|
-
assertSchema(
|
|
606
|
+
assertSchema(
|
|
607
|
+
exclusiveMaximum > exclusiveMinimum,
|
|
608
|
+
`Constraint "exclusiveMaximum" (${exclusiveMaximum}) must be greater than "exclusiveMinimum" (${exclusiveMinimum})`
|
|
609
|
+
);
|
|
630
610
|
}
|
|
631
611
|
if (multipleOf !== void 0) {
|
|
632
612
|
assertSchema(multipleOf > 0, `Constraint "multipleOf" (${multipleOf}) must be greater than zero`);
|
|
@@ -662,26 +642,53 @@ var NumberValidator = class extends Validator {
|
|
|
662
642
|
}
|
|
663
643
|
assertValidation(value >= this.minimum, `Number is less than ${this.minimum}`);
|
|
664
644
|
assertValidation(value <= this.maximum, `Number is greater than ${this.maximum}`);
|
|
665
|
-
assertValidation(
|
|
666
|
-
|
|
667
|
-
|
|
645
|
+
assertValidation(
|
|
646
|
+
this.exclusiveMinimum == void 0 || value > this.exclusiveMinimum,
|
|
647
|
+
`Number is less than or equal to ${this.exclusiveMinimum}`
|
|
648
|
+
);
|
|
649
|
+
assertValidation(
|
|
650
|
+
this.exclusiveMaximum == void 0 || value < this.exclusiveMaximum,
|
|
651
|
+
`Number is greater than or equal to ${this.exclusiveMaximum}`
|
|
652
|
+
);
|
|
653
|
+
assertValidation(
|
|
654
|
+
this.#isMultipleOf ? this.#isMultipleOf(value) : true,
|
|
655
|
+
`Number is not a multiple of ${this.multipleOf}`
|
|
656
|
+
);
|
|
668
657
|
return value;
|
|
669
658
|
}
|
|
670
659
|
};
|
|
671
|
-
var anyNumberValidator = new AnyNumberValidator();
|
|
672
660
|
function _number(constraints) {
|
|
673
|
-
return
|
|
661
|
+
return new NumberValidator(constraints);
|
|
662
|
+
}
|
|
663
|
+
var number = makeValidatorFactory(new AnyNumberValidator(), _number);
|
|
664
|
+
|
|
665
|
+
// src/validators/optional.ts
|
|
666
|
+
var OptionalValidator = class extends AbstractValidator {
|
|
667
|
+
validator;
|
|
668
|
+
optional = true;
|
|
669
|
+
constructor(validator) {
|
|
670
|
+
super();
|
|
671
|
+
this.validator = validator;
|
|
672
|
+
}
|
|
673
|
+
validate(value, options) {
|
|
674
|
+
if (value === void 0)
|
|
675
|
+
return value;
|
|
676
|
+
return this.validator.validate(value, options);
|
|
677
|
+
}
|
|
678
|
+
};
|
|
679
|
+
function optional(validation) {
|
|
680
|
+
const validator = getValidator(validation);
|
|
681
|
+
return new OptionalValidator(validator);
|
|
674
682
|
}
|
|
675
|
-
var number = makeTupleRestIterable(_number);
|
|
676
683
|
|
|
677
684
|
// src/validators/string.ts
|
|
678
|
-
var AnyStringValidator = class extends
|
|
685
|
+
var AnyStringValidator = class extends AbstractValidator {
|
|
679
686
|
validate(value) {
|
|
680
687
|
assertValidation(typeof value == "string", 'Value is not a "string"');
|
|
681
688
|
return value;
|
|
682
689
|
}
|
|
683
690
|
};
|
|
684
|
-
var StringValidator = class extends
|
|
691
|
+
var StringValidator = class extends AbstractValidator {
|
|
685
692
|
maxLength;
|
|
686
693
|
minLength;
|
|
687
694
|
pattern;
|
|
@@ -704,17 +711,25 @@ var StringValidator = class extends Validator {
|
|
|
704
711
|
}
|
|
705
712
|
validate(value) {
|
|
706
713
|
assertValidation(typeof value == "string", 'Value is not a "string"');
|
|
707
|
-
assertValidation(
|
|
708
|
-
|
|
709
|
-
|
|
714
|
+
assertValidation(
|
|
715
|
+
value.length >= this.minLength,
|
|
716
|
+
`String must have a minimum length of ${this.minLength}`
|
|
717
|
+
);
|
|
718
|
+
assertValidation(
|
|
719
|
+
value.length <= this.maxLength,
|
|
720
|
+
`String must have a maximum length of ${this.maxLength}`
|
|
721
|
+
);
|
|
722
|
+
assertValidation(
|
|
723
|
+
this.pattern ? this.pattern.test(value) : true,
|
|
724
|
+
`String does not match required pattern ${this.pattern}`
|
|
725
|
+
);
|
|
710
726
|
return value;
|
|
711
727
|
}
|
|
712
728
|
};
|
|
713
|
-
var anyStringValidator = new AnyStringValidator();
|
|
714
729
|
function _string(constraints) {
|
|
715
|
-
return
|
|
730
|
+
return new StringValidator(constraints);
|
|
716
731
|
}
|
|
717
|
-
var string =
|
|
732
|
+
var string = makeValidatorFactory(new AnyStringValidator(), _string);
|
|
718
733
|
|
|
719
734
|
// src/validators/url.ts
|
|
720
735
|
var KEYS = [
|
|
@@ -735,7 +750,7 @@ var OPTIONS = {
|
|
|
735
750
|
stripForbiddenProperties: false,
|
|
736
751
|
stripOptionalNulls: false
|
|
737
752
|
};
|
|
738
|
-
var URLValidator = class extends
|
|
753
|
+
var URLValidator = class extends AbstractValidator {
|
|
739
754
|
href;
|
|
740
755
|
origin;
|
|
741
756
|
protocol;
|
|
@@ -796,29 +811,32 @@ var URLValidator = class extends Validator {
|
|
|
796
811
|
};
|
|
797
812
|
var anyURLValidator = new URLValidator();
|
|
798
813
|
function _url(constraints) {
|
|
799
|
-
return
|
|
814
|
+
return new URLValidator(constraints);
|
|
800
815
|
}
|
|
801
|
-
var url =
|
|
816
|
+
var url = makeValidatorFactory(anyURLValidator, _url);
|
|
802
817
|
|
|
803
818
|
// src/index.ts
|
|
804
819
|
function validate(validation, value, options = {}) {
|
|
805
|
-
const opts =
|
|
820
|
+
const opts = {
|
|
806
821
|
stripAdditionalProperties: false,
|
|
807
822
|
stripForbiddenProperties: false,
|
|
808
|
-
stripOptionalNulls: false
|
|
809
|
-
|
|
823
|
+
stripOptionalNulls: false,
|
|
824
|
+
...options
|
|
825
|
+
};
|
|
810
826
|
return getValidator(validation).validate(value, opts);
|
|
811
827
|
}
|
|
812
828
|
function strip(validation, value, options = {}) {
|
|
813
|
-
const opts =
|
|
829
|
+
const opts = {
|
|
814
830
|
stripAdditionalProperties: true,
|
|
815
831
|
stripForbiddenProperties: false,
|
|
816
|
-
stripOptionalNulls: true
|
|
817
|
-
|
|
832
|
+
stripOptionalNulls: true,
|
|
833
|
+
...options
|
|
834
|
+
};
|
|
818
835
|
return getValidator(validation).validate(value, opts);
|
|
819
836
|
}
|
|
820
837
|
// Annotate the CommonJS export names for ESM import in node:
|
|
821
838
|
0 && (module.exports = {
|
|
839
|
+
AbstractValidator,
|
|
822
840
|
AllOfValidator,
|
|
823
841
|
AnyArrayValidator,
|
|
824
842
|
AnyNumberValidator,
|
|
@@ -829,15 +847,16 @@ function strip(validation, value, options = {}) {
|
|
|
829
847
|
BooleanValidator,
|
|
830
848
|
ConstantValidator,
|
|
831
849
|
DateValidator,
|
|
850
|
+
NeverValidator,
|
|
832
851
|
NumberValidator,
|
|
833
852
|
ObjectValidator,
|
|
834
853
|
OneOfValidator,
|
|
854
|
+
OptionalValidator,
|
|
835
855
|
StringValidator,
|
|
836
856
|
TupleValidator,
|
|
837
857
|
URLValidator,
|
|
838
858
|
ValidationError,
|
|
839
859
|
ValidationErrorBuilder,
|
|
840
|
-
Validator,
|
|
841
860
|
_allowAdditionalProperties,
|
|
842
861
|
_array,
|
|
843
862
|
_date,
|
|
@@ -857,14 +876,13 @@ function strip(validation, value, options = {}) {
|
|
|
857
876
|
constant,
|
|
858
877
|
date,
|
|
859
878
|
getValidator,
|
|
860
|
-
|
|
861
|
-
|
|
879
|
+
isValidator,
|
|
880
|
+
makeValidatorFactory,
|
|
862
881
|
never,
|
|
863
882
|
number,
|
|
864
883
|
object,
|
|
865
884
|
oneOf,
|
|
866
885
|
optional,
|
|
867
|
-
readonly,
|
|
868
886
|
restValidator,
|
|
869
887
|
schemaValidator,
|
|
870
888
|
string,
|