Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | 17x 5x 5x 3x 5x 5x 3x 5x 5x 3x 4x 4x 2x 20x 20x 18x 4x 4x 2x | import { Validator } from '..';
import { isNullOrUndefined } from '../utils/is-defined';
/**
* Passes if the value is defined.
* @param thing the thing to validate
* @returns why the validator failed
*/
export function isDefined(thing: any): string | void {
if (isNullOrUndefined(thing)) return 'is not defined';
}
/**
* Passes if the array or string is equal to the length.
* @param length the length to compare to
* @returns a validator function matching the criteria
*/
export function isLength(length: number): Validator.Fn<any[] | string | null | undefined> {
return (value: any[] | string | null | undefined): string | void => {
if (isNullOrUndefined(value)) return;
if (value.length !== length) return `has length not equal to "${length}"`;
};
}
/**
* Passes if the array or string is not equal to the length.
* @param length the length to compare to
* @returns a validator function matching the criteria
*/
export function isNotLength(length: number): Validator.Fn<any[] | string | null | undefined> {
return (value: any[] | string | null | undefined): string | void => {
if (isNullOrUndefined(value)) return;
if (value.length === length) return `has length equal to "${length}"`;
};
}
/**
* Passes if the array or string is equal to or less than the maximum length.
* @param length the maximum length (inclusive)
* @returns a validator function matching the criteria
*/
export function isWithinLength(length: number): Validator.Fn<any[] | string | null | undefined> {
return (value: any[] | string | null | undefined): string | void => {
if (isNullOrUndefined(value)) return;
if (value.length > length) return `exceeds max length of "${length}"`;
};
}
/**
* Passes if the value matches the expected value.
* @param expectedValue the value to compare to
* @returns a validator function matching the criteria
*/
export function isEqualTo<T>(expectedValue: T): Validator.Fn<T | null | undefined> {
return (value: T | null | undefined): string | void => {
if (isNullOrUndefined(value)) return;
if (value !== expectedValue) return `is not equal to "${expectedValue}"`;
};
}
/**
* Passes if the value does not match the expected value.
* @param expectedValue the value to compare to
* @returns a validator function matching the criteria
*/
export function isNotEqualTo<T>(expectedValue: T): Validator.Fn<T | null | undefined> {
return (value: T | null | undefined): string | void => {
if (isNullOrUndefined(value)) return;
if (value === expectedValue) return `is equal to "${expectedValue}"`;
};
}
/**
* Passes if the value matches any of the expected values.
* @param expectedValues the values to compare to
* @returns a validator function matching the criteria
*/
export function isAny<T>(...expectedValues: T[]): Validator.Fn<T | null | undefined> {
return (value: T | null | undefined): string | void => {
if (isNullOrUndefined(value)) return;
if (!expectedValues.includes(value)) return `is not equal to "${expectedValues.join(', ')}"`;
};
}
export * from './number';
export * from './string';
export * from './helpers';
|