@tldraw/validate 4.2.2 → 4.2.3
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/dist-cjs/index.d.ts +1 -37
- package/dist-cjs/index.js +1 -1
- package/dist-cjs/lib/validation.js +68 -304
- package/dist-cjs/lib/validation.js.map +3 -3
- package/dist-esm/index.d.mts +1 -37
- package/dist-esm/index.mjs +1 -1
- package/dist-esm/lib/validation.mjs +68 -304
- package/dist-esm/lib/validation.mjs.map +3 -3
- package/package.json +2 -2
- package/src/lib/validation.ts +68 -355
- package/src/test/validation.test.ts +5 -38
package/dist-cjs/index.d.ts
CHANGED
|
@@ -322,20 +322,6 @@ declare function model<T extends {
|
|
|
322
322
|
readonly id: string;
|
|
323
323
|
}>(name: string, validator: Validatable<T>): Validator<T>;
|
|
324
324
|
|
|
325
|
-
/**
|
|
326
|
-
* Validator that ensures a value is a finite, non-zero number. Allows negative numbers.
|
|
327
|
-
* Useful for scale factors that can be negative (for flipping) but not zero.
|
|
328
|
-
*
|
|
329
|
-
* @example
|
|
330
|
-
* ```ts
|
|
331
|
-
* const scale = T.nonZeroFiniteNumber.validate(-1.5) // Returns -1.5 (valid, allows negative)
|
|
332
|
-
* T.nonZeroFiniteNumber.validate(0) // Throws ValidationError: "Expected a non-zero number, got 0"
|
|
333
|
-
* T.nonZeroFiniteNumber.validate(Infinity) // Throws ValidationError
|
|
334
|
-
* ```
|
|
335
|
-
* @public
|
|
336
|
-
*/
|
|
337
|
-
declare const nonZeroFiniteNumber: Validator<number>;
|
|
338
|
-
|
|
339
325
|
/**
|
|
340
326
|
* Validator that ensures a value is a positive integer (\> 0). Rejects zero and negative integers.
|
|
341
327
|
*
|
|
@@ -627,8 +613,6 @@ declare namespace T {
|
|
|
627
613
|
number,
|
|
628
614
|
positiveNumber,
|
|
629
615
|
nonZeroNumber,
|
|
630
|
-
nonZeroFiniteNumber,
|
|
631
|
-
unitInterval,
|
|
632
616
|
integer,
|
|
633
617
|
positiveInteger,
|
|
634
618
|
nonZeroInteger,
|
|
@@ -751,22 +735,6 @@ export declare type UnionValidatorConfig<Key extends string, Config> = {
|
|
|
751
735
|
};
|
|
752
736
|
};
|
|
753
737
|
|
|
754
|
-
/**
|
|
755
|
-
* Validator that ensures a value is a number in the unit interval [0, 1].
|
|
756
|
-
* Useful for opacity, percentages expressed as decimals, and other normalized values.
|
|
757
|
-
*
|
|
758
|
-
* @example
|
|
759
|
-
* ```ts
|
|
760
|
-
* const opacity = T.unitInterval.validate(0.5) // Returns 0.5
|
|
761
|
-
* T.unitInterval.validate(0) // Returns 0 (valid)
|
|
762
|
-
* T.unitInterval.validate(1) // Returns 1 (valid)
|
|
763
|
-
* T.unitInterval.validate(1.5) // Throws ValidationError
|
|
764
|
-
* T.unitInterval.validate(-0.1) // Throws ValidationError
|
|
765
|
-
* ```
|
|
766
|
-
* @public
|
|
767
|
-
*/
|
|
768
|
-
declare const unitInterval: Validator<number>;
|
|
769
|
-
|
|
770
738
|
/**
|
|
771
739
|
* Validator that accepts any value without type checking. Useful as a starting point for
|
|
772
740
|
* building custom validations or when you need to accept truly unknown data.
|
|
@@ -886,17 +854,13 @@ declare class ValidationError extends Error {
|
|
|
886
854
|
export declare class Validator<T> implements Validatable<T> {
|
|
887
855
|
readonly validationFn: ValidatorFn<T>;
|
|
888
856
|
readonly validateUsingKnownGoodVersionFn?: undefined | ValidatorUsingKnownGoodVersionFn<T>;
|
|
889
|
-
/* Excluded from this release type: skipSameValueCheck */
|
|
890
857
|
/**
|
|
891
858
|
* Creates a new Validator instance.
|
|
892
859
|
*
|
|
893
860
|
* validationFn - Function that validates and returns a value of type T
|
|
894
861
|
* validateUsingKnownGoodVersionFn - Optional performance-optimized validation function
|
|
895
|
-
* skipSameValueCheck - Internal flag to skip dev check for validators that transform values
|
|
896
862
|
*/
|
|
897
|
-
constructor(validationFn: ValidatorFn<T>, validateUsingKnownGoodVersionFn?: undefined | ValidatorUsingKnownGoodVersionFn<T
|
|
898
|
-
/** @internal */
|
|
899
|
-
skipSameValueCheck?: boolean);
|
|
863
|
+
constructor(validationFn: ValidatorFn<T>, validateUsingKnownGoodVersionFn?: undefined | ValidatorUsingKnownGoodVersionFn<T>);
|
|
900
864
|
/**
|
|
901
865
|
* Validates an unknown value and returns it with the correct type. The returned value is
|
|
902
866
|
* guaranteed to be referentially equal to the passed value.
|
package/dist-cjs/index.js
CHANGED
|
@@ -39,7 +39,6 @@ __export(validation_exports, {
|
|
|
39
39
|
literal: () => literal,
|
|
40
40
|
literalEnum: () => literalEnum,
|
|
41
41
|
model: () => model,
|
|
42
|
-
nonZeroFiniteNumber: () => nonZeroFiniteNumber,
|
|
43
42
|
nonZeroInteger: () => nonZeroInteger,
|
|
44
43
|
nonZeroNumber: () => nonZeroNumber,
|
|
45
44
|
nullable: () => nullable,
|
|
@@ -54,13 +53,11 @@ __export(validation_exports, {
|
|
|
54
53
|
srcUrl: () => srcUrl,
|
|
55
54
|
string: () => string,
|
|
56
55
|
union: () => union,
|
|
57
|
-
unitInterval: () => unitInterval,
|
|
58
56
|
unknown: () => unknown,
|
|
59
57
|
unknownObject: () => unknownObject
|
|
60
58
|
});
|
|
61
59
|
module.exports = __toCommonJS(validation_exports);
|
|
62
60
|
var import_utils = require("@tldraw/utils");
|
|
63
|
-
const IS_DEV = process.env.NODE_ENV !== "production";
|
|
64
61
|
function formatPath(path) {
|
|
65
62
|
if (!path.length) {
|
|
66
63
|
return null;
|
|
@@ -137,12 +134,10 @@ class Validator {
|
|
|
137
134
|
*
|
|
138
135
|
* validationFn - Function that validates and returns a value of type T
|
|
139
136
|
* validateUsingKnownGoodVersionFn - Optional performance-optimized validation function
|
|
140
|
-
* skipSameValueCheck - Internal flag to skip dev check for validators that transform values
|
|
141
137
|
*/
|
|
142
|
-
constructor(validationFn, validateUsingKnownGoodVersionFn
|
|
138
|
+
constructor(validationFn, validateUsingKnownGoodVersionFn) {
|
|
143
139
|
this.validationFn = validationFn;
|
|
144
140
|
this.validateUsingKnownGoodVersionFn = validateUsingKnownGoodVersionFn;
|
|
145
|
-
this.skipSameValueCheck = skipSameValueCheck;
|
|
146
141
|
}
|
|
147
142
|
/**
|
|
148
143
|
* Validates an unknown value and returns it with the correct type. The returned value is
|
|
@@ -166,7 +161,7 @@ class Validator {
|
|
|
166
161
|
*/
|
|
167
162
|
validate(value) {
|
|
168
163
|
const validated = this.validationFn(value);
|
|
169
|
-
if (
|
|
164
|
+
if (process.env.NODE_ENV !== "production" && !Object.is(value, validated)) {
|
|
170
165
|
throw new ValidationError("Validator functions must return the same value they were passed");
|
|
171
166
|
}
|
|
172
167
|
return validated;
|
|
@@ -327,9 +322,7 @@ class Validator {
|
|
|
327
322
|
return knownGoodValue;
|
|
328
323
|
}
|
|
329
324
|
return otherValidationFn(validated);
|
|
330
|
-
}
|
|
331
|
-
true
|
|
332
|
-
// skipSameValueCheck: refine is designed to transform values
|
|
325
|
+
}
|
|
333
326
|
);
|
|
334
327
|
}
|
|
335
328
|
check(nameOrCheckFn, checkFn) {
|
|
@@ -357,25 +350,11 @@ class ArrayOfValidator extends Validator {
|
|
|
357
350
|
(value) => {
|
|
358
351
|
const arr = array.validate(value);
|
|
359
352
|
for (let i = 0; i < arr.length; i++) {
|
|
360
|
-
|
|
361
|
-
prefixError(i, () => itemValidator.validate(arr[i]));
|
|
362
|
-
} else {
|
|
363
|
-
try {
|
|
364
|
-
itemValidator.validate(arr[i]);
|
|
365
|
-
} catch (err) {
|
|
366
|
-
if (err instanceof ValidationError) {
|
|
367
|
-
throw new ValidationError(err.rawMessage, [i, ...err.path]);
|
|
368
|
-
}
|
|
369
|
-
throw new ValidationError(err.toString(), [i]);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
353
|
+
prefixError(i, () => itemValidator.validate(arr[i]));
|
|
372
354
|
}
|
|
373
355
|
return arr;
|
|
374
356
|
},
|
|
375
357
|
(knownGoodValue, newValue) => {
|
|
376
|
-
if (Object.is(knownGoodValue, newValue)) {
|
|
377
|
-
return knownGoodValue;
|
|
378
|
-
}
|
|
379
358
|
if (!itemValidator.validateUsingKnownGoodVersion) return this.validate(newValue);
|
|
380
359
|
const arr = array.validate(newValue);
|
|
381
360
|
let isDifferent = knownGoodValue.length !== arr.length;
|
|
@@ -383,46 +362,18 @@ class ArrayOfValidator extends Validator {
|
|
|
383
362
|
const item = arr[i];
|
|
384
363
|
if (i >= knownGoodValue.length) {
|
|
385
364
|
isDifferent = true;
|
|
386
|
-
|
|
387
|
-
prefixError(i, () => itemValidator.validate(item));
|
|
388
|
-
} else {
|
|
389
|
-
try {
|
|
390
|
-
itemValidator.validate(item);
|
|
391
|
-
} catch (err) {
|
|
392
|
-
if (err instanceof ValidationError) {
|
|
393
|
-
throw new ValidationError(err.rawMessage, [i, ...err.path]);
|
|
394
|
-
}
|
|
395
|
-
throw new ValidationError(err.toString(), [i]);
|
|
396
|
-
}
|
|
397
|
-
}
|
|
365
|
+
prefixError(i, () => itemValidator.validate(item));
|
|
398
366
|
continue;
|
|
399
367
|
}
|
|
400
368
|
if (Object.is(knownGoodValue[i], item)) {
|
|
401
369
|
continue;
|
|
402
370
|
}
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
isDifferent = true;
|
|
410
|
-
}
|
|
411
|
-
} else {
|
|
412
|
-
try {
|
|
413
|
-
const checkedItem = itemValidator.validateUsingKnownGoodVersion(
|
|
414
|
-
knownGoodValue[i],
|
|
415
|
-
item
|
|
416
|
-
);
|
|
417
|
-
if (!Object.is(checkedItem, knownGoodValue[i])) {
|
|
418
|
-
isDifferent = true;
|
|
419
|
-
}
|
|
420
|
-
} catch (err) {
|
|
421
|
-
if (err instanceof ValidationError) {
|
|
422
|
-
throw new ValidationError(err.rawMessage, [i, ...err.path]);
|
|
423
|
-
}
|
|
424
|
-
throw new ValidationError(err.toString(), [i]);
|
|
425
|
-
}
|
|
371
|
+
const checkedItem = prefixError(
|
|
372
|
+
i,
|
|
373
|
+
() => itemValidator.validateUsingKnownGoodVersion(knownGoodValue[i], item)
|
|
374
|
+
);
|
|
375
|
+
if (!Object.is(checkedItem, knownGoodValue[i])) {
|
|
376
|
+
isDifferent = true;
|
|
426
377
|
}
|
|
427
378
|
}
|
|
428
379
|
return isDifferent ? newValue : knownGoodValue;
|
|
@@ -482,25 +433,11 @@ class ObjectValidator extends Validator {
|
|
|
482
433
|
if (typeof object2 !== "object" || object2 === null) {
|
|
483
434
|
throw new ValidationError(`Expected object, got ${typeToString(object2)}`);
|
|
484
435
|
}
|
|
485
|
-
for (const key
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
;
|
|
491
|
-
validator.validate((0, import_utils.getOwnProperty)(object2, key));
|
|
492
|
-
});
|
|
493
|
-
} else {
|
|
494
|
-
try {
|
|
495
|
-
;
|
|
496
|
-
validator.validate((0, import_utils.getOwnProperty)(object2, key));
|
|
497
|
-
} catch (err) {
|
|
498
|
-
if (err instanceof ValidationError) {
|
|
499
|
-
throw new ValidationError(err.rawMessage, [key, ...err.path]);
|
|
500
|
-
}
|
|
501
|
-
throw new ValidationError(err.toString(), [key]);
|
|
502
|
-
}
|
|
503
|
-
}
|
|
436
|
+
for (const [key, validator] of Object.entries(config)) {
|
|
437
|
+
prefixError(key, () => {
|
|
438
|
+
;
|
|
439
|
+
validator.validate((0, import_utils.getOwnProperty)(object2, key));
|
|
440
|
+
});
|
|
504
441
|
}
|
|
505
442
|
if (!shouldAllowUnknownProperties) {
|
|
506
443
|
for (const key of Object.keys(object2)) {
|
|
@@ -512,46 +449,26 @@ class ObjectValidator extends Validator {
|
|
|
512
449
|
return object2;
|
|
513
450
|
},
|
|
514
451
|
(knownGoodValue, newValue) => {
|
|
515
|
-
if (Object.is(knownGoodValue, newValue)) {
|
|
516
|
-
return knownGoodValue;
|
|
517
|
-
}
|
|
518
452
|
if (typeof newValue !== "object" || newValue === null) {
|
|
519
453
|
throw new ValidationError(`Expected object, got ${typeToString(newValue)}`);
|
|
520
454
|
}
|
|
521
455
|
let isDifferent = false;
|
|
522
|
-
for (const key
|
|
523
|
-
if (!(0, import_utils.hasOwnProperty)(config, key)) continue;
|
|
524
|
-
const validator = config[key];
|
|
456
|
+
for (const [key, validator] of Object.entries(config)) {
|
|
525
457
|
const prev = (0, import_utils.getOwnProperty)(knownGoodValue, key);
|
|
526
458
|
const next = (0, import_utils.getOwnProperty)(newValue, key);
|
|
527
459
|
if (Object.is(prev, next)) {
|
|
528
460
|
continue;
|
|
529
461
|
}
|
|
530
|
-
|
|
531
|
-
const
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
return validatable.validate(next);
|
|
537
|
-
}
|
|
538
|
-
});
|
|
539
|
-
if (!Object.is(checked, prev)) {
|
|
540
|
-
isDifferent = true;
|
|
541
|
-
}
|
|
542
|
-
} else {
|
|
543
|
-
try {
|
|
544
|
-
const validatable = validator;
|
|
545
|
-
const checked = validatable.validateUsingKnownGoodVersion ? validatable.validateUsingKnownGoodVersion(prev, next) : validatable.validate(next);
|
|
546
|
-
if (!Object.is(checked, prev)) {
|
|
547
|
-
isDifferent = true;
|
|
548
|
-
}
|
|
549
|
-
} catch (err) {
|
|
550
|
-
if (err instanceof ValidationError) {
|
|
551
|
-
throw new ValidationError(err.rawMessage, [key, ...err.path]);
|
|
552
|
-
}
|
|
553
|
-
throw new ValidationError(err.toString(), [key]);
|
|
462
|
+
const checked = prefixError(key, () => {
|
|
463
|
+
const validatable = validator;
|
|
464
|
+
if (validatable.validateUsingKnownGoodVersion) {
|
|
465
|
+
return validatable.validateUsingKnownGoodVersion(prev, next);
|
|
466
|
+
} else {
|
|
467
|
+
return validatable.validate(next);
|
|
554
468
|
}
|
|
469
|
+
});
|
|
470
|
+
if (!Object.is(checked, prev)) {
|
|
471
|
+
isDifferent = true;
|
|
555
472
|
}
|
|
556
473
|
}
|
|
557
474
|
if (!shouldAllowUnknownProperties) {
|
|
@@ -659,13 +576,8 @@ class UnionValidator extends Validator {
|
|
|
659
576
|
throw new ValidationError(
|
|
660
577
|
`Expected a string for key "${this.key}", got ${typeToString(variant)}`
|
|
661
578
|
);
|
|
662
|
-
} else if (this.useNumberKeys) {
|
|
663
|
-
|
|
664
|
-
if (numVariant - numVariant !== 0) {
|
|
665
|
-
throw new ValidationError(
|
|
666
|
-
`Expected a number for key "${this.key}", got "${variant}"`
|
|
667
|
-
);
|
|
668
|
-
}
|
|
579
|
+
} else if (this.useNumberKeys && !Number.isFinite(Number(variant))) {
|
|
580
|
+
throw new ValidationError(`Expected a number for key "${this.key}", got "${variant}"`);
|
|
669
581
|
}
|
|
670
582
|
const matchingSchema = (0, import_utils.hasOwnProperty)(this.config, variant) ? this.config[variant] : void 0;
|
|
671
583
|
return { matchingSchema, variant };
|
|
@@ -701,24 +613,11 @@ class DictValidator extends Validator {
|
|
|
701
613
|
if (typeof object2 !== "object" || object2 === null) {
|
|
702
614
|
throw new ValidationError(`Expected object, got ${typeToString(object2)}`);
|
|
703
615
|
}
|
|
704
|
-
for (const key
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
valueValidator.validate(object2[key]);
|
|
710
|
-
});
|
|
711
|
-
} else {
|
|
712
|
-
try {
|
|
713
|
-
keyValidator.validate(key);
|
|
714
|
-
valueValidator.validate(object2[key]);
|
|
715
|
-
} catch (err) {
|
|
716
|
-
if (err instanceof ValidationError) {
|
|
717
|
-
throw new ValidationError(err.rawMessage, [key, ...err.path]);
|
|
718
|
-
}
|
|
719
|
-
throw new ValidationError(err.toString(), [key]);
|
|
720
|
-
}
|
|
721
|
-
}
|
|
616
|
+
for (const [key, value] of Object.entries(object2)) {
|
|
617
|
+
prefixError(key, () => {
|
|
618
|
+
keyValidator.validate(key);
|
|
619
|
+
valueValidator.validate(value);
|
|
620
|
+
});
|
|
722
621
|
}
|
|
723
622
|
return object2;
|
|
724
623
|
},
|
|
@@ -726,71 +625,36 @@ class DictValidator extends Validator {
|
|
|
726
625
|
if (typeof newValue !== "object" || newValue === null) {
|
|
727
626
|
throw new ValidationError(`Expected object, got ${typeToString(newValue)}`);
|
|
728
627
|
}
|
|
729
|
-
const newObj = newValue;
|
|
730
628
|
let isDifferent = false;
|
|
731
|
-
|
|
732
|
-
for (const key in newObj) {
|
|
733
|
-
if (!(0, import_utils.hasOwnProperty)(newObj, key)) continue;
|
|
734
|
-
newKeyCount++;
|
|
735
|
-
const next = newObj[key];
|
|
629
|
+
for (const [key, value] of Object.entries(newValue)) {
|
|
736
630
|
if (!(0, import_utils.hasOwnProperty)(knownGoodValue, key)) {
|
|
737
631
|
isDifferent = true;
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
});
|
|
743
|
-
} else {
|
|
744
|
-
try {
|
|
745
|
-
keyValidator.validate(key);
|
|
746
|
-
valueValidator.validate(next);
|
|
747
|
-
} catch (err) {
|
|
748
|
-
if (err instanceof ValidationError) {
|
|
749
|
-
throw new ValidationError(err.rawMessage, [key, ...err.path]);
|
|
750
|
-
}
|
|
751
|
-
throw new ValidationError(err.toString(), [key]);
|
|
752
|
-
}
|
|
753
|
-
}
|
|
632
|
+
prefixError(key, () => {
|
|
633
|
+
keyValidator.validate(key);
|
|
634
|
+
valueValidator.validate(value);
|
|
635
|
+
});
|
|
754
636
|
continue;
|
|
755
637
|
}
|
|
756
|
-
const prev = knownGoodValue
|
|
638
|
+
const prev = (0, import_utils.getOwnProperty)(knownGoodValue, key);
|
|
639
|
+
const next = value;
|
|
757
640
|
if (Object.is(prev, next)) {
|
|
758
641
|
continue;
|
|
759
642
|
}
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
return valueValidator.validate(next);
|
|
766
|
-
}
|
|
767
|
-
});
|
|
768
|
-
if (!Object.is(checked, prev)) {
|
|
769
|
-
isDifferent = true;
|
|
770
|
-
}
|
|
771
|
-
} else {
|
|
772
|
-
try {
|
|
773
|
-
const checked = valueValidator.validateUsingKnownGoodVersion ? valueValidator.validateUsingKnownGoodVersion(prev, next) : valueValidator.validate(next);
|
|
774
|
-
if (!Object.is(checked, prev)) {
|
|
775
|
-
isDifferent = true;
|
|
776
|
-
}
|
|
777
|
-
} catch (err) {
|
|
778
|
-
if (err instanceof ValidationError) {
|
|
779
|
-
throw new ValidationError(err.rawMessage, [key, ...err.path]);
|
|
780
|
-
}
|
|
781
|
-
throw new ValidationError(err.toString(), [key]);
|
|
643
|
+
const checked = prefixError(key, () => {
|
|
644
|
+
if (valueValidator.validateUsingKnownGoodVersion) {
|
|
645
|
+
return valueValidator.validateUsingKnownGoodVersion(prev, next);
|
|
646
|
+
} else {
|
|
647
|
+
return valueValidator.validate(next);
|
|
782
648
|
}
|
|
649
|
+
});
|
|
650
|
+
if (!Object.is(checked, prev)) {
|
|
651
|
+
isDifferent = true;
|
|
783
652
|
}
|
|
784
653
|
}
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
for (const key in knownGoodValue) {
|
|
788
|
-
if ((0, import_utils.hasOwnProperty)(knownGoodValue, key)) {
|
|
789
|
-
oldKeyCount++;
|
|
790
|
-
}
|
|
791
|
-
}
|
|
792
|
-
if (oldKeyCount !== newKeyCount) {
|
|
654
|
+
for (const key of Object.keys(knownGoodValue)) {
|
|
655
|
+
if (!(0, import_utils.hasOwnProperty)(newValue, key)) {
|
|
793
656
|
isDifferent = true;
|
|
657
|
+
break;
|
|
794
658
|
}
|
|
795
659
|
}
|
|
796
660
|
return isDifferent ? newValue : knownGoodValue;
|
|
@@ -811,125 +675,28 @@ function typeofValidator(type) {
|
|
|
811
675
|
const unknown = new Validator((value) => value);
|
|
812
676
|
const any = new Validator((value) => value);
|
|
813
677
|
const string = typeofValidator("string");
|
|
814
|
-
const number =
|
|
815
|
-
if (Number.
|
|
816
|
-
return value;
|
|
817
|
-
}
|
|
818
|
-
if (typeof value !== "number") {
|
|
819
|
-
throw new ValidationError(`Expected number, got ${typeToString(value)}`);
|
|
820
|
-
}
|
|
821
|
-
if (value !== value) {
|
|
678
|
+
const number = typeofValidator("number").check((number2) => {
|
|
679
|
+
if (Number.isNaN(number2)) {
|
|
822
680
|
throw new ValidationError("Expected a number, got NaN");
|
|
823
681
|
}
|
|
824
|
-
|
|
825
|
-
});
|
|
826
|
-
const positiveNumber = new Validator((value) => {
|
|
827
|
-
if (Number.isFinite(value) && value >= 0) {
|
|
828
|
-
return value;
|
|
829
|
-
}
|
|
830
|
-
if (typeof value !== "number") {
|
|
831
|
-
throw new ValidationError(`Expected number, got ${typeToString(value)}`);
|
|
832
|
-
}
|
|
833
|
-
if (value !== value) {
|
|
834
|
-
throw new ValidationError("Expected a number, got NaN");
|
|
682
|
+
if (!Number.isFinite(number2)) {
|
|
683
|
+
throw new ValidationError(`Expected a finite number, got ${number2}`);
|
|
835
684
|
}
|
|
836
|
-
if (value < 0) {
|
|
837
|
-
throw new ValidationError(`Expected a positive number, got ${value}`);
|
|
838
|
-
}
|
|
839
|
-
throw new ValidationError(`Expected a finite number, got ${value}`);
|
|
840
685
|
});
|
|
841
|
-
const
|
|
842
|
-
if (
|
|
843
|
-
return value;
|
|
844
|
-
}
|
|
845
|
-
if (typeof value !== "number") {
|
|
846
|
-
throw new ValidationError(`Expected number, got ${typeToString(value)}`);
|
|
847
|
-
}
|
|
848
|
-
if (value !== value) {
|
|
849
|
-
throw new ValidationError("Expected a number, got NaN");
|
|
850
|
-
}
|
|
851
|
-
if (value <= 0) {
|
|
852
|
-
throw new ValidationError(`Expected a non-zero positive number, got ${value}`);
|
|
853
|
-
}
|
|
854
|
-
throw new ValidationError(`Expected a finite number, got ${value}`);
|
|
855
|
-
});
|
|
856
|
-
const nonZeroFiniteNumber = new Validator((value) => {
|
|
857
|
-
if (Number.isFinite(value) && value !== 0) {
|
|
858
|
-
return value;
|
|
859
|
-
}
|
|
860
|
-
if (typeof value !== "number") {
|
|
861
|
-
throw new ValidationError(`Expected number, got ${typeToString(value)}`);
|
|
862
|
-
}
|
|
863
|
-
if (value !== value) {
|
|
864
|
-
throw new ValidationError("Expected a number, got NaN");
|
|
865
|
-
}
|
|
866
|
-
if (value === 0) {
|
|
867
|
-
throw new ValidationError(`Expected a non-zero number, got 0`);
|
|
868
|
-
}
|
|
869
|
-
throw new ValidationError(`Expected a finite number, got ${value}`);
|
|
686
|
+
const positiveNumber = number.check((value) => {
|
|
687
|
+
if (value < 0) throw new ValidationError(`Expected a positive number, got ${value}`);
|
|
870
688
|
});
|
|
871
|
-
const
|
|
872
|
-
if (
|
|
873
|
-
return value;
|
|
874
|
-
}
|
|
875
|
-
if (typeof value !== "number") {
|
|
876
|
-
throw new ValidationError(`Expected number, got ${typeToString(value)}`);
|
|
877
|
-
}
|
|
878
|
-
if (value !== value) {
|
|
879
|
-
throw new ValidationError("Expected a number, got NaN");
|
|
880
|
-
}
|
|
881
|
-
throw new ValidationError(`Expected a number between 0 and 1, got ${value}`);
|
|
689
|
+
const nonZeroNumber = number.check((value) => {
|
|
690
|
+
if (value <= 0) throw new ValidationError(`Expected a non-zero positive number, got ${value}`);
|
|
882
691
|
});
|
|
883
|
-
const integer =
|
|
884
|
-
if (Number.isInteger(value)) {
|
|
885
|
-
return value;
|
|
886
|
-
}
|
|
887
|
-
if (typeof value !== "number") {
|
|
888
|
-
throw new ValidationError(`Expected number, got ${typeToString(value)}`);
|
|
889
|
-
}
|
|
890
|
-
if (value !== value) {
|
|
891
|
-
throw new ValidationError("Expected a number, got NaN");
|
|
892
|
-
}
|
|
893
|
-
if (value - value !== 0) {
|
|
894
|
-
throw new ValidationError(`Expected a finite number, got ${value}`);
|
|
895
|
-
}
|
|
896
|
-
throw new ValidationError(`Expected an integer, got ${value}`);
|
|
692
|
+
const integer = number.check((value) => {
|
|
693
|
+
if (!Number.isInteger(value)) throw new ValidationError(`Expected an integer, got ${value}`);
|
|
897
694
|
});
|
|
898
|
-
const positiveInteger =
|
|
899
|
-
if (
|
|
900
|
-
return value;
|
|
901
|
-
}
|
|
902
|
-
if (typeof value !== "number") {
|
|
903
|
-
throw new ValidationError(`Expected number, got ${typeToString(value)}`);
|
|
904
|
-
}
|
|
905
|
-
if (value !== value) {
|
|
906
|
-
throw new ValidationError("Expected a number, got NaN");
|
|
907
|
-
}
|
|
908
|
-
if (value - value !== 0) {
|
|
909
|
-
throw new ValidationError(`Expected a finite number, got ${value}`);
|
|
910
|
-
}
|
|
911
|
-
if (value < 0) {
|
|
912
|
-
throw new ValidationError(`Expected a positive integer, got ${value}`);
|
|
913
|
-
}
|
|
914
|
-
throw new ValidationError(`Expected an integer, got ${value}`);
|
|
695
|
+
const positiveInteger = integer.check((value) => {
|
|
696
|
+
if (value < 0) throw new ValidationError(`Expected a positive integer, got ${value}`);
|
|
915
697
|
});
|
|
916
|
-
const nonZeroInteger =
|
|
917
|
-
if (
|
|
918
|
-
return value;
|
|
919
|
-
}
|
|
920
|
-
if (typeof value !== "number") {
|
|
921
|
-
throw new ValidationError(`Expected number, got ${typeToString(value)}`);
|
|
922
|
-
}
|
|
923
|
-
if (value !== value) {
|
|
924
|
-
throw new ValidationError("Expected a number, got NaN");
|
|
925
|
-
}
|
|
926
|
-
if (value - value !== 0) {
|
|
927
|
-
throw new ValidationError(`Expected a finite number, got ${value}`);
|
|
928
|
-
}
|
|
929
|
-
if (value <= 0) {
|
|
930
|
-
throw new ValidationError(`Expected a non-zero positive integer, got ${value}`);
|
|
931
|
-
}
|
|
932
|
-
throw new ValidationError(`Expected an integer, got ${value}`);
|
|
698
|
+
const nonZeroInteger = integer.check((value) => {
|
|
699
|
+
if (value <= 0) throw new ValidationError(`Expected a non-zero positive integer, got ${value}`);
|
|
933
700
|
});
|
|
934
701
|
const boolean = typeofValidator("boolean");
|
|
935
702
|
const bigint = typeofValidator("bigint");
|
|
@@ -1095,14 +862,13 @@ function optional(validator) {
|
|
|
1095
862
|
return validator.validate(value);
|
|
1096
863
|
},
|
|
1097
864
|
(knownGoodValue, newValue) => {
|
|
865
|
+
if (knownGoodValue === void 0 && newValue === void 0) return void 0;
|
|
1098
866
|
if (newValue === void 0) return void 0;
|
|
1099
867
|
if (validator.validateUsingKnownGoodVersion && knownGoodValue !== void 0) {
|
|
1100
868
|
return validator.validateUsingKnownGoodVersion(knownGoodValue, newValue);
|
|
1101
869
|
}
|
|
1102
870
|
return validator.validate(newValue);
|
|
1103
|
-
}
|
|
1104
|
-
// Propagate skipSameValueCheck from inner validator to allow refine wrappers
|
|
1105
|
-
validator instanceof Validator && validator.skipSameValueCheck
|
|
871
|
+
}
|
|
1106
872
|
);
|
|
1107
873
|
}
|
|
1108
874
|
function nullable(validator) {
|
|
@@ -1117,9 +883,7 @@ function nullable(validator) {
|
|
|
1117
883
|
return validator.validateUsingKnownGoodVersion(knownGoodValue, newValue);
|
|
1118
884
|
}
|
|
1119
885
|
return validator.validate(newValue);
|
|
1120
|
-
}
|
|
1121
|
-
// Propagate skipSameValueCheck from inner validator to allow refine wrappers
|
|
1122
|
-
validator instanceof Validator && validator.skipSameValueCheck
|
|
886
|
+
}
|
|
1123
887
|
);
|
|
1124
888
|
}
|
|
1125
889
|
function literalEnum(...values) {
|