/** * 允许的参数类型枚举 * @description 定义了验证器支持的所有数据类型 */ export type AllowedTypes = 'string' | 'number' | 'boolean' | 'object' | 'array' | 'null' | 'undefined'; /** * 验证规则配置接口 * * @param {boolean} required - 是否为必填字段 * @param {AllowedTypes | AllowedTypes[]} type - 字段的数据类型 * @param {Rule | Record} items - 嵌套验证规则 * @param {RegExp | string} pattern - 正则表达式校验规则 * @param {string | Function} message - 自定义错误消息 */ export type Rule = { required?: boolean; type?: AllowedTypes | AllowedTypes[]; items?: Rule | Record; pattern?: RegExp | string; message?: string | Function; }; /** * 包含验证的详细结果信息 * * @param {boolean} valid - 是否验证通过 * @param {string[]} errors - 所有验证错误信息列表 * @param {string} tip - 第一条错误信息(用于快速提示) */ export type ValidationResult = { valid: boolean; errors: string[]; tip: string; }; /** * 验证器响应接口 * * @param {boolean} isValid - 当前验证是否通过 * @param {string} error - 验证失败时的错误信息 */ export interface ValidateResponse { isValid: boolean; error?: string; } /** * 错误消息模板接口 * * @param {string} typeError - 类型错误消息模板 * @param {string} requiredError - 必填错误消息模板 * @param {string} patternError - 格式错误消息模板 */ export interface ErrorMessages { typeError: string; requiredError: string; patternError: string; } /** * 验证器上下文接口 * * @param {unknown} param - 待验证的参数值 * @param {Rule} constraint - 验证规则 * @param {Record} payload - 验证相关的负载数据 * @param {ErrorMessages} errorMessages - 错误消息模板 * @param {Function} formatErrorMessage - 错误消息格式化函数 * @param {Function} validate - 递归验证函数 */ export interface ValidatorContext { param: unknown; constraint: Rule; payload: Record; errorMessages: ErrorMessages; formatErrorMessage: (template: string, params: Record) => string; validate: (params: unknown, rules?: Rule | Record) => ValidationResult; } /** * 验证器函数类型 */ export type ValidatorFn = (context: ValidatorContext) => { enable: () => boolean; apply: () => ValidateResponse; };