A fullly tested, 100% covered validator for node --WIP
npm install --save sourcegr/SRE-Validator#master -- WIP
Create a validator with:
const fields_to_test = {
id: 3,
age: 24,
name: 'John doe'
}
const the_rules = [
{id: 'numeric'},
{age: 'numeric'},
{age: {
between: [1, 30]
}},
{name: 'required'},
];
const v = Validator.create(fields_to_test, the_rules);
const result = v.validate();
if result
is true, congratulations. Otherwise, you should get the error with
v.getErrors();
The rules should be an array of objects.
The key of each object, should be the name of the input you want to check against.
The value can be a string, an array of strings, or an object. Typically, strings and array of strings are used when there is no need for parameters.
const rules = [
{id: 'required'},
{name: ['alphanum', 'required']}
];
Object rules are required when you want to pass parameters
const rules = [
{lastname: {minLength: 2}}, // name should be at least 2 chars
{age: { between: [18, 99]}} // age should be between 19 and 99
];
Currently, the following rules exist
Generic Rules
'required'
- Check if the field exists
Number Rules
isNumber
- checks for a number.
WARNING ON NUMBER RULES BELLOW : The lib does no checks whatsoever if the input is a number before running any of the checks bellow. If you want to make a check, please include the isNumber
check along with any other checks.
-
{between: [min, max, include_limits=true]}
Between the range defined bymin
,max
. To exclude the min/max values, setinclude_limits
tofalse
-
{notBetween: [min, max, include_limits=true]}
The opposite ofbetween
-
'positive'
/{positive:true}
Positive number / Positive number but also allow zero -
'negative'
/{negative:true}
Negative number / Negative number but also allow zero -
'nonZero'
Number and not zero. -
{lessThan:number}
/{lessThan:[number, true]}
/
Less thannumber
/ less than or equal tonumber
-
{moreThan:number}
/{moreThan:[number, true]}
/
More thannumber
/ more than or equal tonumber
-
{lessThanOrEqual:number}
Less than or equal tonumber
-
{moreThanOrEqual:number}
More than or equal tonumber
-
integer
- WIP
String Rules
-
{minLength: number}
/{minLength:[number, true]}
length should be more / more than or equal tonumber
-
{maxLength: number}
/{maxLength:[number, true]}
length should be less / less than or equal tonumber
-
exactLength
- WIP -
startsWith
- WIP -
endsWith
- WIP -
contains
- WIP -
alpha
- WIP -
alphanum
- WIP -
alphanumdash
- WIP -
slug
- WIP -
url
- WIP -
email
- WIP -
regexp
- WIP
The rules are added on demand. If you want a specific rule please add it yourself and help this lib grow.
Just clone the repo and fire away!
MIT