|
1 | 1 | import type { alphanumeric } from './lib/isAlphanumeric' |
| 2 | +import type { ArrayValidator } from './validators/arrays' |
| 3 | +import type { NumberValidator } from './validators/numbers' |
| 4 | +import type { StringValidator } from './validators/strings' |
2 | 5 |
|
3 | | -export interface ValidationConfig { |
4 | | - verbose: boolean |
5 | | - strictMode?: boolean |
6 | | - cacheResults?: boolean |
7 | | - errorMessages?: Record<string, string> |
| 6 | +export interface ValidationError { |
| 7 | + message: string |
| 8 | + value: any |
8 | 9 | } |
9 | 10 |
|
10 | | -export type ValidationOptions = Partial<ValidationConfig> |
11 | | - |
12 | 11 | export interface ValidationResult { |
13 | 12 | valid: boolean |
14 | 13 | errors: ValidationError[] |
15 | 14 | } |
16 | 15 |
|
17 | | -export interface ValidationError { |
18 | | - field: string |
19 | | - message: string |
20 | | - value?: any |
21 | | - rule?: string |
22 | | -} |
23 | | - |
24 | | -export interface ValidationRule<T = any> { |
25 | | - validate: (value: T, options?: any) => boolean |
26 | | - message: string | ((field: string, value: T, options?: any) => string) |
27 | | - options?: any |
28 | | -} |
29 | | - |
30 | | -export type ValidationSchema = Record<string, ValidationRule[]> |
31 | | - |
32 | | -export interface BaseValidator<T = any> { |
33 | | - validate: (value: T) => ValidationResult |
| 16 | +export interface ValidationRule<T> { |
| 17 | + name: string |
34 | 18 | test: (value: T) => boolean |
35 | | - required: () => Validator<T> |
36 | | - optional: () => Validator<T> |
37 | | - rules: Array<{ test: (val: T) => boolean, message: string, options?: any }> |
38 | | -} |
39 | | - |
40 | | -export type Validator<T = any> = Omit<BaseValidator<T>, 'rules'> |
41 | | - |
42 | | -export type StringValidator = Validator<string> & { |
43 | | - min: (length: number) => StringValidator |
44 | | - max: (length: number) => StringValidator |
45 | | - length: (length: number) => StringValidator |
46 | | - email: () => StringValidator |
47 | | - url: () => StringValidator |
48 | | - matches: (pattern: RegExp) => StringValidator |
49 | | - alphanumeric: () => StringValidator |
50 | | - alpha: () => StringValidator |
51 | | - numeric: () => StringValidator |
52 | | -} |
53 | | - |
54 | | -export type NumberValidator = Validator<number> & { |
55 | | - min: (min: number) => NumberValidator |
56 | | - max: (max: number) => NumberValidator |
57 | | - positive: () => NumberValidator |
58 | | - negative: () => NumberValidator |
59 | | - integer: () => NumberValidator |
60 | | - timestamp: () => NumberValidator |
61 | | -} |
62 | | - |
63 | | -export type BooleanValidator = Validator<boolean> |
64 | | - |
65 | | -export type ArrayValidator<T = any> = Validator<T[]> & { |
66 | | - min: (length: number) => ArrayValidator<T> |
67 | | - max: (length: number) => ArrayValidator<T> |
68 | | - length: (length: number) => ArrayValidator<T> |
69 | | - each: (validator: Validator<T>) => ArrayValidator<T> |
70 | | -} |
71 | | - |
72 | | -export type ObjectValidator<T = Record<string, any>> = Validator<T> & { |
73 | | - shape: (schema: Record<string, Validator>) => ObjectValidator<T> |
74 | | - strict: (strict?: boolean) => ObjectValidator<T> |
75 | | -} |
76 | | - |
77 | | -export type EnumValidator<T = string | number> = Validator<T> & { |
78 | | - values: (values: T[]) => EnumValidator<T> |
79 | | -} |
80 | | - |
81 | | -export type DateValidator = Validator<Date> & { |
82 | | - min: (date: Date | string | number) => DateValidator |
83 | | - max: (date: Date | string | number) => DateValidator |
84 | | - format: (format: string) => DateValidator |
85 | | - isValid: () => DateValidator |
86 | | -} |
87 | | - |
88 | | -export interface ValidationInstance { |
89 | | - string: () => StringValidator |
90 | | - number: () => NumberValidator |
91 | | - boolean: () => BooleanValidator |
92 | | - array: <T = any>() => ArrayValidator<T> |
93 | | - object: <T = Record<string, any>>() => ObjectValidator<T> |
94 | | - custom: <T = any>(validator: (value: T) => boolean, message: string) => Validator<T> |
95 | | - enum: <T = string | number>() => EnumValidator<T> |
96 | | - date: () => DateValidator |
97 | | - clearCache: () => void |
| 19 | + message: string |
| 20 | + params?: Record<string, any> |
98 | 21 | } |
99 | 22 |
|
100 | 23 | export interface ContainsOptions { |
@@ -182,12 +105,11 @@ export interface IsEmptyOptions { |
182 | 105 | } |
183 | 106 |
|
184 | 107 | export interface IsFloatOptions { |
185 | | - locale?: boolean | string |
186 | | - hasOwnProperty?: boolean | string |
| 108 | + locale?: string |
187 | 109 | min?: number |
188 | 110 | max?: number |
189 | | - lt?: boolean | string |
190 | | - gt?: boolean | string |
| 111 | + lt?: number |
| 112 | + gt?: number |
191 | 113 | } |
192 | 114 |
|
193 | 115 | export interface IsFQDNOptions { |
@@ -302,3 +224,10 @@ export interface NormalizeEmailOptions { |
302 | 224 | yandex_lowercase?: boolean | string |
303 | 225 | yandex_convert_yandexru?: boolean | string |
304 | 226 | } |
| 227 | + |
| 228 | +export interface Validator<T> { |
| 229 | + test: (value: T) => boolean |
| 230 | + validate: (value: T) => ValidationResult |
| 231 | + required: () => Validator<T> |
| 232 | + optional: () => Validator<T> |
| 233 | +} |
0 commit comments