interface.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import type { VueNode } from '../_util/type';
  2. export declare type FormLabelAlign = 'left' | 'right';
  3. export declare type InternalNamePath = (string | number)[];
  4. export declare type NamePath = string | number | InternalNamePath;
  5. export declare type StoreValue = any;
  6. export interface Store {
  7. [name: string]: StoreValue;
  8. }
  9. export interface Meta {
  10. touched: boolean;
  11. validating: boolean;
  12. errors: string[];
  13. name: InternalNamePath;
  14. }
  15. export interface InternalFieldData extends Meta {
  16. value: StoreValue;
  17. }
  18. /**
  19. * Used by `setFields` config
  20. */
  21. export interface FieldData extends Partial<Omit<InternalFieldData, 'name'>> {
  22. name: NamePath;
  23. }
  24. export declare type RuleType = 'string' | 'number' | 'boolean' | 'method' | 'regexp' | 'integer' | 'float' | 'object' | 'enum' | 'date' | 'url' | 'hex' | 'email';
  25. declare type Validator = (rule: RuleObject, value: StoreValue, callback: (error?: string) => void) => Promise<void> | void;
  26. export interface ValidatorRule {
  27. warningOnly?: boolean;
  28. message?: string | VueNode;
  29. /** custom validate function (Note: callback must be called) */
  30. validator: Validator;
  31. }
  32. interface BaseRule {
  33. warningOnly?: boolean;
  34. /** validate the value from a list of possible values */
  35. enum?: StoreValue[];
  36. /** validate the exact length of a field */
  37. len?: number;
  38. /** validate the max length of a field */
  39. max?: number;
  40. /** validation error message */
  41. message?: string | VueNode;
  42. /** validate the min length of a field */
  43. min?: number;
  44. /** validate from a regular expression */
  45. pattern?: RegExp;
  46. /** indicates whether field is required */
  47. required?: boolean;
  48. /** transform a value before validation */
  49. transform?: (value: StoreValue) => StoreValue;
  50. /** built-in validation type, available options: https://github.com/yiminghe/async-validator#type */
  51. type?: RuleType;
  52. /** treat required fields that only contain whitespace as errors */
  53. whitespace?: boolean;
  54. /** Customize rule level `validateTrigger`. Must be subset of Field `validateTrigger` */
  55. validateTrigger?: string | string[];
  56. /** Check trigger timing */
  57. trigger?: 'blur' | 'change' | Array<'change' | 'blur'>;
  58. }
  59. declare type AggregationRule = BaseRule & Partial<ValidatorRule>;
  60. interface ArrayRule extends Omit<AggregationRule, 'type'> {
  61. type: 'array';
  62. defaultField?: RuleObject;
  63. }
  64. export declare type RuleObject = AggregationRule | ArrayRule;
  65. export declare type Rule = RuleObject;
  66. export interface ValidateErrorEntity<Values = any> {
  67. values: Values;
  68. errorFields: {
  69. name: InternalNamePath;
  70. errors: string[];
  71. }[];
  72. outOfDate: boolean;
  73. }
  74. export interface FieldError {
  75. name: InternalNamePath | string;
  76. errors: string[];
  77. }
  78. export interface RuleError {
  79. errors: string[];
  80. rule: RuleObject;
  81. }
  82. export interface ValidateOptions {
  83. triggerName?: string;
  84. validateMessages?: ValidateMessages;
  85. }
  86. export declare type InternalValidateFields = (nameList?: NamePath[], options?: ValidateOptions) => Promise<Store>;
  87. export declare type ValidateFields = (nameList?: NamePath[]) => Promise<Store>;
  88. interface ValueUpdateInfo {
  89. type: 'valueUpdate';
  90. source: 'internal' | 'external';
  91. }
  92. interface ValidateFinishInfo {
  93. type: 'validateFinish';
  94. }
  95. interface ResetInfo {
  96. type: 'reset';
  97. }
  98. interface SetFieldInfo {
  99. type: 'setField';
  100. data: FieldData;
  101. }
  102. interface DependenciesUpdateInfo {
  103. type: 'dependenciesUpdate';
  104. /**
  105. * Contains all the related `InternalNamePath[]`.
  106. * a <- b <- c : change `a`
  107. * relatedFields=[a, b, c]
  108. */
  109. relatedFields: InternalNamePath[];
  110. }
  111. export declare type NotifyInfo = ValueUpdateInfo | ValidateFinishInfo | ResetInfo | SetFieldInfo | DependenciesUpdateInfo;
  112. export declare type ValuedNotifyInfo = NotifyInfo & {
  113. store: Store;
  114. };
  115. export interface Callbacks<Values = any> {
  116. onValuesChange?: (changedValues: any, values: Values) => void;
  117. onFieldsChange?: (changedFields: FieldData[], allFields: FieldData[]) => void;
  118. onFinish?: (values: Values) => void;
  119. onFinishFailed?: (errorInfo: ValidateErrorEntity<Values>) => void;
  120. onValidate?: (name: string | number | string[] | number[], status: boolean, errors: string[] | null) => void;
  121. }
  122. export declare type EventArgs = any[];
  123. declare type ValidateMessage = string | (() => string);
  124. export interface ValidateMessages {
  125. default?: ValidateMessage;
  126. required?: ValidateMessage;
  127. enum?: ValidateMessage;
  128. whitespace?: ValidateMessage;
  129. date?: {
  130. format?: ValidateMessage;
  131. parse?: ValidateMessage;
  132. invalid?: ValidateMessage;
  133. };
  134. types?: {
  135. string?: ValidateMessage;
  136. method?: ValidateMessage;
  137. array?: ValidateMessage;
  138. object?: ValidateMessage;
  139. number?: ValidateMessage;
  140. date?: ValidateMessage;
  141. boolean?: ValidateMessage;
  142. integer?: ValidateMessage;
  143. float?: ValidateMessage;
  144. regexp?: ValidateMessage;
  145. email?: ValidateMessage;
  146. url?: ValidateMessage;
  147. hex?: ValidateMessage;
  148. };
  149. string?: {
  150. len?: ValidateMessage;
  151. min?: ValidateMessage;
  152. max?: ValidateMessage;
  153. range?: ValidateMessage;
  154. };
  155. number?: {
  156. len?: ValidateMessage;
  157. min?: ValidateMessage;
  158. max?: ValidateMessage;
  159. range?: ValidateMessage;
  160. };
  161. array?: {
  162. len?: ValidateMessage;
  163. min?: ValidateMessage;
  164. max?: ValidateMessage;
  165. range?: ValidateMessage;
  166. };
  167. pattern?: {
  168. mismatch?: ValidateMessage;
  169. };
  170. }
  171. export {};