Minimalistic yet powerful and highly customizable validation.
npm install mcheck
const Check = require('mcheck')
const check = new Check()
check.val(6, 'rating').optional().int({ min: 1, max: 5 })
const body = { email: 'no mail' }
check.obj(email, 'email').required().string({ max: 50 }).email()
check.ok // false
check.errors[0] // { name: 'rating', code: 'int_rng', min: 1, max: 5 }
check.errors[1] // { name: 'email', code: 'email' }
Use Check.add()
to add new validators
const Check = require('mcheck')
// register new validator
const cool = (ctx) => {
ctx.ensure(val => val === 'cool', 'cool_err_code')
}
Check.add({ cool })
// use new validator
const check = new Check()
check.val('hot', 'my field').string().cool()
check.ok // false
check.errors[0] // { name: 'my field', code: 'cool_err_code' }
required() - check if value is not null
or undefined
check.val(null, 'f').required() // err: { name: 'f', code: 'req' }
optional() - if value is null
or undefined
result is valid even if other checks not
check.val(null, 'f').optional().int() // ok: true
bool() - check if value is boolean
check.val(5, 'f').bool() // err: { name: 'f', code: 'boolean' }
int({ min, max }) - check if value is integer
-
min
- minimal value (included) -
max
- maximal value (included)
check.val('str', 'f').int() // err: { name: 'f', code: 'int' }
check.val(2, 'f').int({ min: 3 }) // err: { name: 'f', code: 'int_min', min: 3 }
check.val(2, 'f').int({ max: 1 }) // err: { name: 'f', code: 'int_max', max: 1 }
check.val(2, 'f').int({ min: 3, max: 5 }) // err: { name: 'f', code: 'int_rng', min: 3, max: 5 }
number({ min, max }) - check if value is number
-
min
- minimal value (included) -
max
- maximal value (included)
check.val('str', 'f').number() // err: { name: 'f', code: 'num' }
check.val(2, 'f').number({ min: 3 }) // err: { name: 'f', code: 'num_min', min: 3 }
check.val(2, 'f').number({ max: 1 }) // err: { name: 'f', code: 'num_max', max: 1 }
check.val(2, 'f').number({ min: 3, max: 5 }) // err: { name: 'f', code: 'num_rng', min: 3, max: 5 }
string({ lng, min, max }) - check if value is string
-
lng
- string length -
min
- minimal string length (included) -
max
- maximal string length (included) -
empty
- allows string to be empty (default true)
check.val(1, 'f').sting() // err: { name: 'f', code: 'str' }
check.val('str', 'f').string({ lng: 2 }) // err: { name: 'f', code: 'str_lng', lng: 2 }
check.val('str', 'f').string({ min: 5 }) // err: { name: 'f', code: 'str_min', min: 5 }
check.val('str', 'f').string({ max: 1 }) // err: { name: 'f', code: 'str_max', max: 1 }
check.val('str', 'f').string({ min: 5, max: 10 }) // err: { name: 'f', code: 'str_rng', min: 5, max: 10 }
check.val(' ', 'f').string() // err: { name: f, code: str_empty }
check.val(' ', 'f').string({ empty: true }) // ok: true
email() - check if value is email
check.val('no mail', 'f').email() // err: { name: 'f', code: 'email' }
object() - check if value is object
check.val(1, 'f').object() // err: { name: 'f', code: 'obj' }
date({ min, max }) - check if value is date
-
min
- minimal date -
max
- maximal date
const y05 = new Date(2005, 1, 1)
const y10 = new Date(2010, 1, 1)
const y15 = new Date(2015, 1, 1)
check.val(1, 'f').date() // err: { name: 'f', code: 'date' }
check.val(y05, 'f').date({ min: y10 }) // err: { name: 'f', code: 'date_min', min: 2010-01-31.. }
check.val(y10, 'f').date({ max: y05 }) // err: { name: 'f', code: 'date_max', max: 2005-01-31.. }
check.val(y15, 'f').date({ min: y05, max: y10 }) // err: { name: 'f', code: 'date_rng', min: 2005-01-31.., max: 2010-01-31.. }
func() - check if value is func
check.val(1, 'f').func() // err: { name: 'f', code: 'func' }
array({ lng, min, max }) - check if value is array
-
lng
- array length -
min
- minimal array length -
max
- maximal array length
check.val(1, 'f').array() // err: { name: 'f', code: 'array' }
check.val([], 'f').array({ lng: 1 }) // err: { name: 'f', code: 'array_lng', lng: 1 }
check.val([], 'f').array({ min: 1 }) // err: { name: 'f', code: 'array_min', min: 1 }
check.val([1,2], 'f').array({ max: 1 }) // err: { name: 'f', code: 'array_max', max: 1 }
check.val([], 'f').array({ min: 1, max: 2 }) // err: { name: 'f', code: 'array_rng', min: 1, max: 2 }
MIT