UNPKG

5.74 kBTypeScriptView Raw
1export declare type RuleType = 'string' | 'number' | 'boolean' | 'method' | 'regexp' | 'integer' | 'float' | 'array' | 'object' | 'enum' | 'date' | 'url' | 'hex' | 'email' | 'pattern' | 'any';
2export interface ValidateOption {
3 suppressWarning?: boolean;
4 suppressValidatorError?: boolean;
5 first?: boolean;
6 firstFields?: boolean | string[];
7 messages?: Partial<ValidateMessages>;
8 /** The name of rules need to be trigger. Will validate all rules if leave empty */
9 keys?: string[];
10 error?: (rule: InternalRuleItem, message: string) => ValidateError;
11}
12export declare type SyncErrorType = Error | string;
13export declare type SyncValidateResult = boolean | SyncErrorType | SyncErrorType[];
14export declare type ValidateResult = void | Promise<void> | SyncValidateResult;
15export interface RuleItem {
16 type?: RuleType;
17 required?: boolean;
18 pattern?: RegExp | string;
19 min?: number;
20 max?: number;
21 len?: number;
22 enum?: Array<string | number | boolean | null | undefined>;
23 whitespace?: boolean;
24 fields?: Record<string, Rule>;
25 options?: ValidateOption;
26 defaultField?: Rule;
27 transform?: (value: Value) => Value;
28 message?: string | ((a?: string) => string);
29 asyncValidator?: (rule: InternalRuleItem, value: Value, callback: (error?: string | Error) => void, source: Values, options: ValidateOption) => void | Promise<void>;
30 validator?: (rule: InternalRuleItem, value: Value, callback: (error?: string | Error) => void, source: Values, options: ValidateOption) => SyncValidateResult | void;
31}
32export declare type Rule = RuleItem | RuleItem[];
33export declare type Rules = Record<string, Rule>;
34/**
35 * Rule for validating a value exists in an enumerable list.
36 *
37 * @param rule The validation rule.
38 * @param value The value of the field on the source object.
39 * @param source The source object being validated.
40 * @param errors An array of errors that this rule may add
41 * validation errors to.
42 * @param options The validation options.
43 * @param options.messages The validation messages.
44 * @param type Rule type
45 */
46export declare type ExecuteRule = (rule: InternalRuleItem, value: Value, source: Values, errors: string[], options: ValidateOption, type?: string) => void;
47/**
48 * Performs validation for any type.
49 *
50 * @param rule The validation rule.
51 * @param value The value of the field on the source object.
52 * @param callback The callback function.
53 * @param source The source object being validated.
54 * @param options The validation options.
55 * @param options.messages The validation messages.
56 */
57export declare type ExecuteValidator = (rule: InternalRuleItem, value: Value, callback: (error?: string[]) => void, source: Values, options: ValidateOption) => void;
58declare type ValidateMessage<T extends any[] = unknown[]> = string | ((...args: T) => string);
59declare type FullField = string | undefined;
60declare type EnumString = string | undefined;
61declare type Pattern = string | RegExp | undefined;
62declare type Range = number | undefined;
63declare type Type = string | undefined;
64export interface ValidateMessages {
65 default?: ValidateMessage;
66 required?: ValidateMessage<[FullField]>;
67 enum?: ValidateMessage<[FullField, EnumString]>;
68 whitespace?: ValidateMessage<[FullField]>;
69 date?: {
70 format?: ValidateMessage;
71 parse?: ValidateMessage;
72 invalid?: ValidateMessage;
73 };
74 types?: {
75 string?: ValidateMessage<[FullField, Type]>;
76 method?: ValidateMessage<[FullField, Type]>;
77 array?: ValidateMessage<[FullField, Type]>;
78 object?: ValidateMessage<[FullField, Type]>;
79 number?: ValidateMessage<[FullField, Type]>;
80 date?: ValidateMessage<[FullField, Type]>;
81 boolean?: ValidateMessage<[FullField, Type]>;
82 integer?: ValidateMessage<[FullField, Type]>;
83 float?: ValidateMessage<[FullField, Type]>;
84 regexp?: ValidateMessage<[FullField, Type]>;
85 email?: ValidateMessage<[FullField, Type]>;
86 url?: ValidateMessage<[FullField, Type]>;
87 hex?: ValidateMessage<[FullField, Type]>;
88 };
89 string?: {
90 len?: ValidateMessage<[FullField, Range]>;
91 min?: ValidateMessage<[FullField, Range]>;
92 max?: ValidateMessage<[FullField, Range]>;
93 range?: ValidateMessage<[FullField, Range, Range]>;
94 };
95 number?: {
96 len?: ValidateMessage<[FullField, Range]>;
97 min?: ValidateMessage<[FullField, Range]>;
98 max?: ValidateMessage<[FullField, Range]>;
99 range?: ValidateMessage<[FullField, Range, Range]>;
100 };
101 array?: {
102 len?: ValidateMessage<[FullField, Range]>;
103 min?: ValidateMessage<[FullField, Range]>;
104 max?: ValidateMessage<[FullField, Range]>;
105 range?: ValidateMessage<[FullField, Range, Range]>;
106 };
107 pattern?: {
108 mismatch?: ValidateMessage<[FullField, Value, Pattern]>;
109 };
110}
111export interface InternalValidateMessages extends ValidateMessages {
112 clone: () => InternalValidateMessages;
113}
114export declare type Value = any;
115export declare type Values = Record<string, Value>;
116export interface ValidateError {
117 message?: string;
118 fieldValue?: Value;
119 field?: string;
120}
121export declare type ValidateFieldsError = Record<string, ValidateError[]>;
122export declare type ValidateCallback = (errors: ValidateError[] | null, fields: ValidateFieldsError | Values) => void;
123export interface RuleValuePackage {
124 rule: InternalRuleItem;
125 value: Value;
126 source: Values;
127 field: string;
128}
129export interface InternalRuleItem extends Omit<RuleItem, 'validator'> {
130 field?: string;
131 fullField?: string;
132 fullFields?: string[];
133 validator?: RuleItem['validator'] | ExecuteValidator;
134}
135export {};