UNPKG

@ticatec/web-bean-validator

Version:

A TypeScript/JavaScript library for rule-based entity validation with boundary checking for strings, numbers, dates, enums, objects, and arrays.

66 lines (65 loc) 1.72 kB
/** * 自定义检查 */ import ValidationResult from "./ValidationResult"; export declare type CustomCheck = (value: any, data: any) => any; /** * 忽略条件 */ export declare type IgnoreWhen = (data: any) => boolean; /** * 根据条件检查是否为必须 */ export declare type RequiredCheck = (data: any) => boolean; export interface ValidatorOptions { name?: string; required?: boolean | RequiredCheck | null; check?: CustomCheck | null; ignoreWhen?: IgnoreWhen; } export default abstract class BaseValidator { protected field: string; protected checkFun: CustomCheck; protected name: string; protected required: boolean | RequiredCheck; protected ignoreWhen: IgnoreWhen; /** * 构建基础校验器 * @param field * @param options * @protected */ protected constructor(field: string, options: ValidatorOptions); validate(data: any, result: ValidationResult, obj: any): void; /** * 检查字段的值 * @param value * @param result * @param data * @protected */ protected checkField(value: any, result: ValidationResult, data: any): boolean; /** * 检查字段的类型并转换成对应的值 * @param value * @protected */ protected checkType(value: any): any; /** * 获取字段的名称 * @protected */ protected getFieldLabel(): string; /** * 检查是否为空 * @protected */ protected checkNullValue(value: string): boolean; /** * 从数据中提前字段值 * @param data * @private */ private extractFieldValue; protected formatErrorMessage(message: string, params?: any): string; }