yma-validator
v1.0.3
Published
Form 表单校验器
Downloads
84
Readme
YMA Validator
表单校验器,适用于对数据统一校验,不仅仅是 Input 校验,比如 Excel 导入数据时的校验
对单个表单类的数据进行校验
const YmaValidator = require('yma-validator');
const validator = new YmaValidator([
{
label: '密码', // 提示字段使用
key: 'newPwd', // 对应校验 assert 中数据的 key
rules: [
{
name: 'required',
},
],
},
{
label: '确认密码',
key: 'newPwd2',
rules: [
{
name: 'required',
},
{
name: 'equalTo',
correlative: 'newPwd', // 关联 key
},
],
},
]);
validator.valid(
{
newPwd: 1,
newPwd2: '',
},
function (messages) {
// messages is array
console.log(messages);
}
);
对 Excel 类型的数据进行校验
const YmaValidator = require('yma-validator');
const forms = [
{
username: 'x1',
password: 'P',
},
{
username: 'x1',
age: 'ac',
},
{
username: 'x',
age: '1',
},
];
const rules = [
{
label: '用户名',
key: 'username',
rules: [
'nospace',
'unique',
'required',
// {
// validator: function (value, options, callback) {
// callback && callback(value === 'x' ? true : 'xxxx 不合法');
// },
// },
// {
// validator: function (value, options) {
// callback(true);
// },
// },
],
},
{
label: '密码',
key: 'password',
rules: [
{
name: 'password',
params: [[0, 1, 2, 3]],
},
],
},
];
const formsValidator = new YmaValidator.FormsValidator(rules);
formsValidator.valid(forms).then(errors => {
console.log(errors);
});