correct
Simple JavaScript validation module
Install
npm install correct
Usage
var correct = var fn = correctstring === true === false
At any time, if you want to understand why you have a failing validation, you can use
var fn = correctstring fn// will return an object with {valid: false, errors: [...]} and the array of validations that did not pass fn //will return {valid: true}
Types
For now there are only 3 base validation types available
- string - correct.string
- number - correct.number
- int - correct.int
Example:
var isInt = correctint == false == false == true
You can chain validation functions for all types (detailed below).
var intMax10 = correctint == true == false
NOTE: types are specified with simple dot access, while all other chained validations should be chained as function calls:
var validate = correctstring == true
string
var stringMin5 = correctstring == false == true
Available string tests:
- min(n: Number) - validates the value to have at least n characters in length
- max(n: Number) - validates the value to have at most n characters in length
- re(r: RegExp) - validates the value against the given regular expression
- email() - validates the value to be a valid email address (npm module isemail is used)
- fn(f: Function) - validates the value if the specified function returns truthy
- not(v: String) - validates the value if it is not strict equal to v
- numeric() - validates the value if it is a numeric string: eg - '1.34'
- required() - validates the value if it is different from empty string
Number
Validates numbers.
var between10And100 = correctnumber == true == false var even = correctnumber == true var x = 67 == false var isNumber = correctnumber == true == false
Available number tests:
- min(n: Number) - validates the value if it is >= n
- max(n: Number) - validates the value if it is <= n
- fn(f: Function) - validates the value if the specified function returns truthy
- not(v: String) - validates the value if it is not strict equal to v
Int
Inherits all the number tests, and adds some more
- odd(n: Number) - validates the value if it is odd
- even(n: Number) - validates the value if it is even
Tests
make
License
MIT