checkv
javascript idiomatic validator
Purpose
A readable way to validate data
Install
Note: this module is on development
$ npm i checkv --save
Quick start
Use
check(input, validators): true | false
const checkv = require('checkv')
checkv(0, { isNumber: true })
checkv('Alice', { isNumber: false })
checkv(2, { isGreater: 1, isLessOrEqual: 3 })
checkv('simone@braceslab.com', { isEmail: true })
checkv('Frank', { isIn: ['Frank', 'Paul', 'Alex', 'Maurice'] })
Validators
You can combine validators each others.
Some validators implicitly perform others, for example isPositive
also perform isNumber
.
types
- isSet:
bool
check if input is set - it's ok if is not null
or undefined
checkv(null, { isSet: true })
checkv(undefined, { isSet: true })
checkv(0, { isSet: true })
checkv('', { isSet: true })
- isNumber:
bool
check if input is a number
checkv(0, { isNumber: true })
checkv(1, { isNumber: false })
checkv('Alice', { isNumber: true })
checkv('Alice', { isNumber: false })
- isString:
bool
check if input is a string
checkv('Alice', { isString: true })
- isObject:
bool
check if input is an object
checkv({}, { isObject: true })
checkv('Alice', { isObject: false })
- isArray:
bool
check if input is an array
checkv([], { isArray: true })
checkv('Alice', { isArray: false })
- isFunction:
bool
check if input is an object
checkv(f(){}, { isFunction: true })
number
- isInteger:
bool
check if input is an integer
checkv(1, { isInteger: true })
checkv(1.1, { isInteger: true })
- isPositive:
bool
check if input is positive
checkv(1, { isPositive: true })
checkv(-1, { isPositive: true })
- isEqual:
number
check if input is equal to number
Note: will be extend to other types
checkv(1, { isEqual: 1 })
- isGreater:
number
check if input is greater than number
checkv(1, { isGreater: 0 })
- isLess:
number
check if input is less than number
checkv(1, { isLess: 0 })
- isGreaterOrEqual:
number
check if input is greater than number
checkv(1, { isGreaterOrEqual: 1 })
- isLessOrEqual:
number
check if input is greater than number
checkv(1, { isLessOrEqual: 2 })
string
- isEmail:
bool
check if input is an email
checkv('simone@braceslab.com', { isEmail: true })
- isShorter:
number
check if input is shorter than number
checkv('simone@braceslab.com', { isShorter: 3 })
- isLonger:
number
check if input is shorter than number
checkv('simone@braceslab.com', { isLonger: 3 })
- isIn:
array|string
check if input contains value
checkv('Frank', { isIn: ['Frank', 'Paul', 'Alex', 'Maurice'] })
object
- hasProperties:
array
check if input, as object, has all properties in array
checkv({a: 1}, { hasProperties: ['a'] })
- isEnum:
object
check if input is en element of the `object`` as enum
const enum = { A: 0, B: 1 }
let a = enum.A
checkv(a, { isEnum: enum })
any
- isExactly:
*
check if input is exactly *
- like deep strict equal
checkv(1, { isExactly: 1)
checkv({a: 1}, { isExactly: {a: 1 }})
checkv(['a', 1], { isExactly: ['a', 1] })
License
The MIT License (MIT)
Copyright (c) 2017, braces lab
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.