@ticatec/entity-validator
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

Node实体校验工具

这是通过定义规则,自动完成实体的校验(边界检查的工具类)

使用方法

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);

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
0.0.10latest

Version History

VersionDownloads (Last 7 Days)Published
0.0.10

Package Sidebar

Install

npm i @ticatec/entity-validator

Weekly Downloads

0

Version

0.0.1

License

MIT

Unpacked Size

29.1 kB

Total Files

27

Last publish

Collaborators

  • henry.feng