jj-validator
v1.0.20
Published
like laravel style of validator
Downloads
53
Readme
jj-validator
like laravel style of validator
return first invalid param info by default
return Chinese invalid message by default
support custom rules and invalid messages
install:
npm install jj-validator
basic usage:
const V = require('jj-validator')
let params = {
name: 'jarone',
age: '18'
}
let schema = {
mobile: 'required|numberic',
name : 'max:10|min:1',
age : 'numberic'
}
let invalidMsg = V(params, schema)
console.log(invalidMsg);
/*
[
{
paramName: 'mobile',
value: undefined,
invalid: '必须传递mobile'
}
]
*/
English invalid message usage:
const V = require('jj-validator')
let params = {
name: 'jarone',
age: '18'
}
let schema = {
mobile: 'required|numberic',
name : 'max:10|min:1',
age : 'numberic'
}
let invalidMsg = V(params, schema, 'en')
console.log(invalidMsg);
/*
[
{
paramName: 'mobile',
value: undefined,
invalid: 'mobile is required.'
}
]
*/
get all invalid params info usage:
const V = require('jj-validator')
let params = {
name: 'jarone',
age: '18'
}
let schema = {
mobile: 'required|numberic',
name : 'max:5|min:1',
age : 'numberic'
}
let invalidMsg = V(params, schema, 'en', true)
console.log(invalidMsg);
/*
[
{
paramName: 'mobile',
value: undefined,
invalid: 'mobile is required.'
},
{
paramName: 'name',
value: 'jarone',
invalid: 'name can not gt 5. jarone given.'
}
]
*/
custom rules and invalid messages
const V = require('jj-validator')
let params = {name: 'luy'}
let schema = {name: 'isJarone'}
let extRules = {isJarone: (val) => val === 'jarone'}
let extInvalidMsg = {
isJarone: (paramName, val) => `${paramName} is not jarone, ${val} given.`
}
let invalidMsg = V(params, schema, 'en', true, extRules, extInvalidMsg)
console.log(invalidMsg);
/*
[
{
paramName: 'name',
value: 'luy',
invalid: 'name is not jarone, luy given.'
}
]
*/
Validation rules:
- required
- bool
- numberic
- array
- max
- min