|
1 | 1 | import { traverse } from './traverse'; |
2 | | -import { isPlainObject, ObjectKeys, isNumber, isString, isBool } from './utils'; |
3 | | -import { Validator } from './validatorTypes'; |
| 2 | +import { isPlainObject, isNumber, isString, isBool } from './utils'; |
| 3 | +import { Schema, Validator } from './validatorTypes'; |
4 | 4 | import { TYPEERR } from './constants'; |
5 | 5 |
|
6 | 6 | function shouldSkipValidation(value: unknown, validator: Validator) { |
7 | 7 | return value == null && Boolean(validator.optional); |
8 | 8 | } |
9 | 9 |
|
10 | | -export function createErrors<T>(schema: { [K in keyof T]: Validator }, data: T, eager = false) { |
11 | | - if (!isPlainObject(data)) { |
12 | | - return (ObjectKeys(schema) as (keyof T)[]).reduce((errors, schemaKey) => { |
13 | | - const validator = schema[schemaKey]; |
14 | | - if (!validator.optional) { |
15 | | - errors[schemaKey] = TYPEERR as string; |
16 | | - } |
17 | | - return errors; |
18 | | - }, {} as Record<keyof T, any>); |
19 | | - } else { |
20 | | - return traverse( |
21 | | - schema, |
22 | | - { |
23 | | - string(__, _, validator, value) { |
24 | | - if (shouldSkipValidation(value, validator)) return null; |
25 | | - if (!isString(value)) return TYPEERR; |
| 10 | +export function createErrors<T extends Schema>(schema: T, data: any, eager = false) { |
| 11 | + return traverse( |
| 12 | + schema, |
| 13 | + { |
| 14 | + string: ({ validator, value }) => { |
| 15 | + if (shouldSkipValidation(value, validator)) return null; |
| 16 | + if (!isString(value)) return TYPEERR; |
26 | 17 |
|
27 | | - const [minLength, minLengthErrMsg] = validator.minLength ? validator.minLength : []; |
28 | | - if (minLength && minLengthErrMsg && isNumber(minLength) && value.length < minLength) |
29 | | - return minLengthErrMsg; |
| 18 | + const [minLength, minLengthErrMsg] = validator.minLength ? validator.minLength : []; |
| 19 | + if (minLength && minLengthErrMsg && isNumber(minLength) && value.length < minLength) |
| 20 | + return minLengthErrMsg; |
30 | 21 |
|
31 | | - const [maxLength, maxLengthErrMsg] = validator.maxLength ? validator.maxLength : []; |
32 | | - if (maxLength && maxLengthErrMsg && isNumber(maxLength) && value.length > maxLength) |
33 | | - return maxLengthErrMsg; |
| 22 | + const [maxLength, maxLengthErrMsg] = validator.maxLength ? validator.maxLength : []; |
| 23 | + if (maxLength && maxLengthErrMsg && isNumber(maxLength) && value.length > maxLength) |
| 24 | + return maxLengthErrMsg; |
34 | 25 |
|
35 | | - const [pattern, patterErrMsg] = validator.pattern ? validator.pattern : []; |
36 | | - if (pattern && patterErrMsg && pattern.test(value) == false) return patterErrMsg; |
| 26 | + const [pattern, patterErrMsg] = validator.pattern ? validator.pattern : []; |
| 27 | + if (pattern && patterErrMsg && pattern.test(value) == false) return patterErrMsg; |
37 | 28 |
|
38 | | - return null; |
39 | | - }, |
40 | | - number(__, _, validator, value) { |
41 | | - if (shouldSkipValidation(value, validator)) return null; |
| 29 | + return null; |
| 30 | + }, |
| 31 | + number: ({ validator, value }) => { |
| 32 | + if (shouldSkipValidation(value, validator)) return null; |
42 | 33 |
|
43 | | - if (!isNumber(value)) return TYPEERR; |
| 34 | + if (!isNumber(value)) return TYPEERR; |
44 | 35 |
|
45 | | - const [min, minErrMsg] = validator.min ? validator.min : []; |
46 | | - if (isNumber(min) && value < min && minErrMsg) return minErrMsg; |
| 36 | + const [min, minErrMsg] = validator.min ? validator.min : []; |
| 37 | + if (isNumber(min) && value < min && minErrMsg) return minErrMsg; |
47 | 38 |
|
48 | | - const [max, maxErrMsg] = validator.max ? validator.max : []; |
49 | | - if (isNumber(max) && value > max && maxErrMsg) return maxErrMsg; |
| 39 | + const [max, maxErrMsg] = validator.max ? validator.max : []; |
| 40 | + if (isNumber(max) && value > max && maxErrMsg) return maxErrMsg; |
50 | 41 |
|
51 | | - const [is, isErrMsg] = validator.is ? validator.is : []; |
52 | | - if (isString(is) && isErrMsg) { |
53 | | - const isInt = Number.isInteger(value); |
54 | | - if ((isInt && is == 'float') || (!isInt && is == 'integer')) return isErrMsg; |
55 | | - } |
| 42 | + const [is, isErrMsg] = validator.is ? validator.is : []; |
| 43 | + if (isString(is) && isErrMsg) { |
| 44 | + const isInt = Number.isInteger(value); |
| 45 | + if ((isInt && is == 'float') || (!isInt && is == 'integer')) return isErrMsg; |
| 46 | + } |
56 | 47 |
|
57 | | - return null; |
58 | | - }, |
59 | | - boolean(__, _, validator, value) { |
60 | | - if (shouldSkipValidation(value, validator)) return null; |
61 | | - if (!isBool(value)) return TYPEERR; |
62 | | - return null; |
63 | | - }, |
64 | | - list(__, _, validator, value) { |
65 | | - if (shouldSkipValidation(value, validator)) return null; |
66 | | - if (!Array.isArray(value)) return TYPEERR; |
67 | | - return null; |
68 | | - }, |
69 | | - listof(__, _, validator, value) { |
70 | | - if (shouldSkipValidation(value, validator)) return null; |
71 | | - if (!Array.isArray(value)) return TYPEERR; |
72 | | - return null; |
73 | | - }, |
74 | | - record(__, _, validator, value) { |
75 | | - if (shouldSkipValidation(value, validator)) return null; |
76 | | - if (!isPlainObject(value)) return TYPEERR; |
77 | | - return null; |
78 | | - }, |
79 | | - recordof(__, _, validator, value) { |
80 | | - if (shouldSkipValidation(value, validator)) return null; |
81 | | - if (!isPlainObject(value)) return TYPEERR; |
82 | | - return null; |
83 | | - }, |
| 48 | + return null; |
| 49 | + }, |
| 50 | + boolean: ({ validator, value }) => { |
| 51 | + if (shouldSkipValidation(value, validator)) return null; |
| 52 | + if (!isBool(value)) return TYPEERR; |
| 53 | + return null; |
| 54 | + }, |
| 55 | + list: ({ validator, value }) => { |
| 56 | + if (shouldSkipValidation(value, validator)) return null; |
| 57 | + if (!Array.isArray(value)) return TYPEERR; |
| 58 | + return null; |
| 59 | + }, |
| 60 | + listof: ({ validator, value }) => { |
| 61 | + if (shouldSkipValidation(value, validator)) return null; |
| 62 | + if (!Array.isArray(value)) return TYPEERR; |
| 63 | + return null; |
| 64 | + }, |
| 65 | + record: ({ validator, value }) => { |
| 66 | + if (shouldSkipValidation(value, validator)) return null; |
| 67 | + if (!isPlainObject(value)) return TYPEERR; |
| 68 | + return null; |
| 69 | + }, |
| 70 | + recordof: ({ validator, value }) => { |
| 71 | + if (shouldSkipValidation(value, validator)) return null; |
| 72 | + if (!isPlainObject(value)) return TYPEERR; |
| 73 | + return null; |
84 | 74 | }, |
85 | | - data, |
86 | | - eager |
87 | | - ); |
88 | | - } |
| 75 | + }, |
| 76 | + data, |
| 77 | + eager |
| 78 | + ); |
89 | 79 | } |
0 commit comments