interface.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. export type RuleType = 'string' | 'number' | 'boolean' | 'method' | 'regexp' | 'integer' | 'float' | 'array' | 'object' | 'enum' | 'date' | 'url' | 'hex' | 'email' | 'pattern' | 'any';
  2. export 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. }
  12. export type SyncErrorType = Error | string;
  13. export type SyncValidateResult = boolean | SyncErrorType | SyncErrorType[];
  14. export type ValidateResult = void | Promise<void> | SyncValidateResult;
  15. export interface RuleItem {
  16. type?: RuleType;
  17. required?: boolean;
  18. pattern?: RegExp | string;
  19. min?: number;
  20. max?: number;
  21. len?: number;
  22. enum?: (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. }
  32. export type Rule = RuleItem | RuleItem[];
  33. export 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. */
  46. export 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. */
  57. export type ExecuteValidator = (rule: InternalRuleItem, value: Value, callback: (error?: string[]) => void, source: Values, options: ValidateOption) => void;
  58. type ValidateMessage<T extends any[] = unknown[]> = string | ((...args: T) => string);
  59. type FullField = string | undefined;
  60. type EnumString = string | undefined;
  61. type Pattern = string | RegExp | undefined;
  62. type Range = number | undefined;
  63. type Type = string | undefined;
  64. export 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. }
  111. export interface InternalValidateMessages extends ValidateMessages {
  112. clone: () => InternalValidateMessages;
  113. }
  114. export type Value = any;
  115. export type Values = Record<string, Value>;
  116. export interface ValidateError {
  117. message?: string;
  118. fieldValue?: Value;
  119. field?: string;
  120. }
  121. export type ValidateFieldsError = Record<string, ValidateError[]>;
  122. export type ValidateCallback = (errors: ValidateError[] | null, fields: ValidateFieldsError | Values) => void;
  123. export interface RuleValuePackage {
  124. rule: InternalRuleItem;
  125. value: Value;
  126. source: Values;
  127. field: string;
  128. }
  129. export interface InternalRuleItem extends Omit<RuleItem, 'validator'> {
  130. field?: string;
  131. fullField?: string;
  132. fullFields?: string[];
  133. validator?: RuleItem['validator'] | ExecuteValidator;
  134. }
  135. export {};