diff --git a/src/interface.ts b/src/interface.ts index 362eff54..f3e3af9e 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -1,11 +1,16 @@ import type { ReactElement } from 'react'; +import type { + Value as StoreValue, + Values as Store, + RuleType, + ValidateMessages, +} from 'async-validator'; import type { ReducerAction } from './useForm'; export type InternalNamePath = (string | number)[]; export type NamePath = string | number | InternalNamePath; -export type StoreValue = any; -export type Store = Record; +export type { RuleType, StoreValue, Store, ValidateMessages }; export interface Meta { touched: boolean; @@ -26,25 +31,10 @@ export interface FieldData extends Partial> { name: NamePath; } -export type RuleType = - | 'string' - | 'number' - | 'boolean' - | 'method' - | 'regexp' - | 'integer' - | 'float' - | 'object' - | 'enum' - | 'date' - | 'url' - | 'hex' - | 'email'; - type Validator = ( - rule: RuleObject, + rule: InternalRule, value: StoreValue, - callback: (error?: string) => void, + callback: (error?: string | Error) => void, ) => Promise | void; export type RuleRender = (form: FormInstance) => RuleObject; @@ -67,22 +57,23 @@ interface BaseRule { transform?: (value: StoreValue) => StoreValue; type?: RuleType; whitespace?: boolean; + defaultField?: RuleObject; + fields?: Record; /** Customize rule level `validateTrigger`. Must be subset of Field `validateTrigger` */ validateTrigger?: string | string[]; } -type AggregationRule = BaseRule & Partial; - -interface ArrayRule extends Omit { - type: 'array'; - defaultField?: RuleObject; -} - -export type RuleObject = AggregationRule | ArrayRule; +export type RuleObject = BaseRule & Partial; export type Rule = RuleObject | RuleRender; +export interface InternalRule extends RuleObject { + field?: string; + fullField?: string; + fullFields?: string[]; +} + export interface ValidateErrorEntity { values: Values; errorFields: { name: InternalNamePath; errors: string[] }[]; @@ -265,52 +256,3 @@ export type InternalFormInstance = Omit & { // eslint-disable-next-line @typescript-eslint/no-explicit-any export type EventArgs = any[]; - -type ValidateMessage = string | (() => string); -export interface ValidateMessages { - default?: ValidateMessage; - required?: ValidateMessage; - enum?: ValidateMessage; - whitespace?: ValidateMessage; - date?: { - format?: ValidateMessage; - parse?: ValidateMessage; - invalid?: ValidateMessage; - }; - types?: { - string?: ValidateMessage; - method?: ValidateMessage; - array?: ValidateMessage; - object?: ValidateMessage; - number?: ValidateMessage; - date?: ValidateMessage; - boolean?: ValidateMessage; - integer?: ValidateMessage; - float?: ValidateMessage; - regexp?: ValidateMessage; - email?: ValidateMessage; - url?: ValidateMessage; - hex?: ValidateMessage; - }; - string?: { - len?: ValidateMessage; - min?: ValidateMessage; - max?: ValidateMessage; - range?: ValidateMessage; - }; - number?: { - len?: ValidateMessage; - min?: ValidateMessage; - max?: ValidateMessage; - range?: ValidateMessage; - }; - array?: { - len?: ValidateMessage; - min?: ValidateMessage; - max?: ValidateMessage; - range?: ValidateMessage; - }; - pattern?: { - mismatch?: ValidateMessage; - }; -} diff --git a/src/utils/validateUtil.ts b/src/utils/validateUtil.ts index ee6011d2..2f735990 100644 --- a/src/utils/validateUtil.ts +++ b/src/utils/validateUtil.ts @@ -124,11 +124,7 @@ export function validateRules( // Replace validator if needed if (originValidatorFunc) { - cloneRule.validator = ( - rule: RuleObject, - val: StoreValue, - callback: (error?: string) => void, - ) => { + cloneRule.validator = (rule, val, callback) => { let hasPromise = false; // Wrap callback only accept when promise not provided