这是通过定义规则,自动完成实体的校验(边界检查的工具类)
npm i @ticatec/entity-validator
import validator from "@ticatec/entity-validator";
import {BaseValidator, StringValidator, NumberValidator, DateValidator, EnumValidator, ObjectValidator, ArrayValidator} from "@ticatec/entity-validator";
let rules: Array<BaseValidator> = [
]
let data = {}
let result = validator.validate(data, rules);
interface StringValidatorOptions {
required?: boolean,
minLen?: number, //最小长度
maxLen?: number, //最大长度
format?: {
regex: RegExp, //正则表达式
message: string //匹配失败信息
},
check?: CustomCheck, //自定义的校验函数
}
new StringValidator(field, name, options);
interface NumberValidatorOptions {
required?: boolean,
minValue?: number, //最小值
maxValue?: number, //最大值
check?: CustomCheck, //自定义的校验函数
}
new NumberValidator(field, name, options);
interface DateValidatorOptions {
required?: boolean,
from?: Date, //最早日期
to?: Date, //最迟日期
maxDaysBefore?: number, //最早开始的天数
maxDaysAfter?: number, //最后开始的天数
check?: CustomCheck, //自定义的校验函数
}
new DateValidator(field, name, options);
interface EnumValidatorOptions {
required?: boolean,
values: Array<any>; //枚举值
check?: CustomCheck, //自定义的校验函数
}
new EnumValidator(field, name, options);
interface BooleanValidatorOptions {
required?: boolean,
check?: CustomCheck, //自定义的校验函数
}
new BooleanValidator(field, name, options);
interface ObjectValidatorOptions {
required?: boolean,
rules: Array<BaseValidator>;
check?: CustomCheck, //自定义的校验函数
}
new ObjectValidator(field, name, options);
interface ArrayValidatorOptions {
required?: boolean,
rules: Array<BaseValidator>;
minLen?: number;
maxLen?: number;
check?: CustomCheck, //自定义的校验函数
}
new ArrayValidator(field, name, options);