|
| 1 | +import { DateSchemaConfig } from "../types/DateSchema"; |
| 2 | +import { |
| 3 | + DateSchemaError, |
| 4 | + DateSchemaErrorEnum as msg, |
| 5 | +} from "../types/DateError"; |
| 6 | +import * as check from "./allChecks"; |
| 7 | + |
| 8 | +const checkRunner = ( |
| 9 | + x: any, |
| 10 | + config: DateSchemaConfig, |
| 11 | + key: string |
| 12 | +): { value: Date; errors: DateSchemaError[] } => { |
| 13 | + const { |
| 14 | + greaterThan, |
| 15 | + greaterThanOrEqualTo, |
| 16 | + lessThan, |
| 17 | + lessThanOrEqualTo, |
| 18 | + equalTo, |
| 19 | + } = config; |
| 20 | + const errors: DateSchemaError[] = []; |
| 21 | + |
| 22 | + // type check |
| 23 | + if (!check.type(x)) { |
| 24 | + errors.push({ |
| 25 | + error: `${key} must be a Date object`, |
| 26 | + errorType: msg.Type, |
| 27 | + }); |
| 28 | + return { value: x, errors }; |
| 29 | + } |
| 30 | + |
| 31 | + // value check |
| 32 | + if (greaterThan && !check.greaterThan(x, greaterThan)) { |
| 33 | + errors.push({ |
| 34 | + error: `${key} must be greater than ${greaterThan}`, |
| 35 | + errorType: msg.Value, |
| 36 | + }); |
| 37 | + } |
| 38 | + if ( |
| 39 | + greaterThanOrEqualTo && |
| 40 | + !check.greaterThanOrEqual(x, greaterThanOrEqualTo) |
| 41 | + ) { |
| 42 | + errors.push({ |
| 43 | + error: `${key} must be greater than or equal to ${greaterThanOrEqualTo}`, |
| 44 | + errorType: msg.Value, |
| 45 | + }); |
| 46 | + } |
| 47 | + if (lessThan && !check.lessThan(x, lessThan)) { |
| 48 | + errors.push({ |
| 49 | + error: `${key} must be less than ${lessThan}`, |
| 50 | + errorType: msg.Value, |
| 51 | + }); |
| 52 | + } |
| 53 | + if (lessThanOrEqualTo && !check.lessThanOrEqual(x, lessThanOrEqualTo)) { |
| 54 | + errors.push({ |
| 55 | + error: `${key} must be less than or equal to ${lessThanOrEqualTo}`, |
| 56 | + errorType: msg.Value, |
| 57 | + }); |
| 58 | + } |
| 59 | + if (equalTo && !check.equalTo(x, equalTo)) { |
| 60 | + errors.push({ |
| 61 | + error: `${key} must be equal to ${equalTo}`, |
| 62 | + errorType: msg.Value, |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + return { value: x, errors }; |
| 67 | +}; |
| 68 | + |
| 69 | +export default checkRunner; |
0 commit comments