1.0.1 • Published 7 years ago

@laomu/validator v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

字段校验工具

安装

npm install @laomu/validator

usage

let Validate = require('@laomu/validator')
let rules = {
    username: [
        {type: 'string', required: true, message: '请输入用户名'},
        {min: 6, max: 10, message: '请输入6到10位字符'},
    ],
    password: [
        {type: 'string', required: true, message: '请输入密码'},
        {min: 6, max: 10, message: '请输入6到10位字符'},
    ],
    email: [
        {type: 'email', message: '邮箱格式非法'}
    ]
};

let validator = new Validate(rules);

validator
    .validate({username: '123', password: '123'})
    .catch(err => {
        console.log(err);
    });
/*** 将输出错误
[ { message: '请输入6到10位字符', field: 'username' },
  { message: '请输入6到10位字符', field: 'password' } ]
***/

api

  • new Validate(rules) 生成新的校验程序实例
  • instance.validate(data) 校验数据,返回一个promise
  • instance.validExist(data) 校验数据中存在的属性(可枚举并在rules中定义过)
  • instance.validAttr(data, attr) 校验一个属性, 例如validAttrs(data, 'username')
  • instance.validAttrs(data, attrs) 校验属性列表,例如validAttrs(data, 'username', 'password')

其他

rule配置规则请参考async-validator

1.0.1

7 years ago

1.0.0

7 years ago