rules
Built-in rules
Before use it
import Validator from 'validator-core'
const validator = new Validator()
required
validator.test('', 'required') // => false
validator.test('x', 'required') // => true
validator.test('test@gmail.com', 'email') // => true
validator.test('i@html.love', 'email') // => true
validator.test('i@html', 'email') // => false
url
validator.test('ws://xxx', 'url') // => false
validator.test('https://github.com', 'url') // => true
integer
validator.test(10, 'integer') // => true
validator.test(-10, 'integer') // => true
validator.test(1.01, 'integer') // => false
id_card
validator.test('610724197303236577', 'id_card') // => true
validator.test('61072419730323657X', 'id_card') // => true
after:Date
validator.test(null, 'after:2017/11/07') // => false
validator.test('2017/11/08', 'after:2017/11/07') // => true
validator.test('2017/11/08', 'after:2017/11/7') // => true
validator.test('2017-11-08', 'after:2017-11-07') // => true
validator.test('2017-11-08', 'after:2017-11-09') // => false
validator.test('2017-11-08', 'after:2017-11-08') // => false
validator.test('2017.11.08', 'after:2017.11.08') // => false
validator.test(new Date('2017-11-09'), 'after:2017-11-08') // => true
after_equal:Date
validator.test('2017-11-08', 'after_equal:2017-11-07') // => true
validator.test('2017-11-08', 'after_equal:2017-11-08') // => true
before:Date
validator.test(null, 'before:2017/11/07') // => false
validator.test('2017/11/06', 'before:2017/11/07') // => true
validator.test('2017/11/06', 'before:2017/11/7') // => true
validator.test('2017-11-06', 'before:2017-11-07') // => true
validator.test('2017-11-06', 'before:2017-11-05') // => false
validator.test('2017-11-06', 'before:2017-11-06') // => false
validator.test('2017.11.06', 'before:2017.11.06') // => false
validator.test(new Date('2017-11-09'), 'before:2017-11-10') // => true
before_equal:Date
validator.test('2017-11-06', 'before_equal:2017-11-07') // => true
validator.test('2017-11-06', 'before_equal:2017-11-05') // => false
validator.test('2017-11-06', 'before_equal:2017-11-06') // => true
size
validator.test('1234', 'size:4') // => true
validator.test('12345', 'size:4') // => false
validator.test('1234', 'size:2-4') // => true
validator.test('1', 'size:2-4') // => false
validator.test(null, 'size:2-4') // => false
validator.test(123, 'size:2-4') // => false
lt(less then)
validator.test('200', 'lt:400') // => false
validator.test(200, 'lt:400') // => true
validator.test(400, 'lt:400') // => false
validator.test(401, 'lt:400') // => false
lte(less and equal then)
validator.test('200', 'lte:400') // => false
validator.test(200, 'lte:400') // => true
validator.test(401, 'lte:400') // => false
validator.test(400, 'lte:400') // => true
validator.test(400.12, 'lte:400.12') // => true
gt(greater then)
validator.test(401, 'gt:400') // => true
validator.test(400, 'gt:400') // => false
gte(greater and equal then)
validator.test(401, 'gte:400') // => true
validator.test(400, 'gte:400') // => true
validator.test(400.12, 'gte:400.12') // => true
equal
validator.test('500', 'equal:500') // => false
validator.test(500, 'equal:500') // => true
validator.test(500.233, 'equal:500.233') // => true
validator.test(399, 'equal:500') // => false
in
validator.test(true, 'in:true') // => true
validator.test(false, 'in:true') // => false
validator.test('500', 'in:500') // => true
validator.test(500, 'in:500,600') // => true
validator.test('leo', 'in:aim,leo,ttt') // => true
validator.test('le', 'in:aim,leo') // => false
validator.test('', 'in:aim,leo') // => false
validator.test(null, 'in:aim,leo') // => false
not_in
validator.test(true, 'not_in:true') // => false
validator.test(false, 'not_in:true') // => true
validator.test('400', 'not_in:500') // => true
validator.test(500.1, 'not_in:500') // => true
validator.test('tttt', 'not_in:aim,leo,ttt') // => true
validator.test('le', 'not_in:aim,leo') // => true
validator.test('', 'not_in:aim,leo') // => true
validator.test('leo', 'not_in:aim,leo') // => false
validator.test(null, 'not_in:aim,leo') // => false
Register custom rules
you can add custom rules to validator by registerRules(rules)
method.
The rules
must be an Object like
{
'rule1': RegExp,
/**
* while using function you can pass params
* the first argment of function is `value` which should be tested
* the second argment is an array, It is pass by rule2:params1, params2, params3
* In this case the second argment should be [params1, params2, params3]
**/
'rule2': Function
}
- example
const validator = new Validator()
const customRules = {
'password': /^[^\s\u4E00-\u9FA5]{8,16}$/,
'is_prime': function isPrimeNum (num, params) {
if (params) console.log(params)
if (typeof num !== 'number' || !Number.isInteger(num)) return false
if (num === 2) {
return true
} else if (num % 2 === 0) {
return false
}
const squareRoot = Math.sqrt(num)
for (var i = 3; i <= squareRoot; i += 2) {
if (num % i === 0) return false
}
return true
},
'contain': function (value, params) {
if (!params) return false
if (typeof value !== 'string') return false
for (let i = 0; i < params.length; i++) {
const item = params[i]
if (value.indexOf(item) > -1) return true
}
return false
}
}
validator.registerRules(customRules)
validator.test('abcd123', 'password') // => false
validator.test('abcd1234', 'password') // => true
validator.test(13, 'is_prime:just_test,hei')
// log: ['just_test', 'hei']
// => true
validator.test(24, 'is_prime') // => false
Custom RegExp
validator.test('=3=', /=3=/) // => true
validator.test('=3=o', /=3=/) // => true
validator.test('=3=o', /=3=$/) // => false
Custom Function
function isPrimeNum (num) {
if (typeof num !== 'number' || !Number.isInteger(num)) return false
if (num === 2) {
return true
} else if (num % 2 === 0) {
return false
}
const squareRoot = Math.sqrt(num)
for (var i = 3; i <= squareRoot; i += 2) {
if (num % i === 0) return false
}
return true
}
validator.test(2, isPrimeNum) // => true
validator.test(991, isPrimeNum) // => true
validator.test(8, isPrimeNum) // => false