Skip to content

A validation package written in es6 inspired by laravel validation

Notifications You must be signed in to change notification settings

xx19941215/validation-es6

Repository files navigation

validation-es6

使用ES6编写的一个类似laravel的js验证模块.

安装

$ npm install validation-es6

基本用法

import {
    Validator
} from './node_modules/validation-es6/validator.js';

let rules = {
  username: 'required|min:5',
  password: 'required|confirmed|min:6|max:16',
}

let data = {
  username: 'test',
  password: '123456',
}

let v = new Validator(data, rules);

if (v.fails()) {
    console.log(v.messages());
}

定义验证规则

  • "|" 分隔的字符串形式
let rules = {
  username: 'required|min:5',
  password: 'required|confirmed|min:6|max:16',
  email: 'email'
}
  • 数组形式
let rules = {
  username: ['required', 'min:5'],
  password: ['required', ['confirmed'], ['min:6'], ['max:16'],
  email: ['required', 'email']
}

其他

  • 自定义错误消息、属性别名、验证函数

    在消息字符串用使用:attribute 作为属性名占位符。

    //错误消息
    let customMessages = {
        accepted: ":attribute 必须接受",
    };
    
    //属性别名
    let attribute = {
        "item": "条款"
    }
    
    //使用回调函数定义错误消息
    let replacers = {
        accepted: function (message, attribute, rule,parameters) {
            return message;
        }
    },
    
    let v = new Validator(data, rules, customMessages, attribute, replacers);
    
    //注册自定义验证函数
    v.register('upper', (attribute, value, parameters) => {
        return value.toUpperCase() == "NAME";
    }, ":attribute = :value 不对");
    
    if (v.fails()) {
        console.log(v.messages());
    }

License

MIT

About

A validation package written in es6 inspired by laravel validation

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published