he-validation
@licence GPL v3
Install
# npm install he-validation --save
quick How to use
require the Core
const ValidatorCore = require('he-validator');
create new instance from Core
const Validator = new ValidatorCore;
after that you are ready to make your validation like so
first prepare your Inputs Object which contains all inputs that you need to validate
it can be req.body
if you use Express js or any Object
const Inputs = req.body;
// OR
const Inputs = {
"input": "value"
}
then you need to prepare the Roles that will contains all your validation roles
const Roles = {
// any Role you need with it's options
"input": ["required", "min:3", "max:100", "numeric"]
}
finally run the Validator
const validation = Validator.make(Inputs, Roles);
the Validator by Default returns Promise
validation.then(() => {
// the Validator success with NO Errors at All
}, (errors) => {
// the Validator failed and all Errors passed here
});
Go Deep Into Rabbit Hole
if you need to add custom Roles or Extend the Validator
first method => you can create Object contains your Custom Roles then pass it to the constractor.
const RolesObject = {
"new_role": (options) => {
return new Promise((resolve, reject) => {
if(validationPassed === true){
return resolve();
}
return reject("$s failed with error message");
});
},
"another_awesome_role": ...
};
// create new instance from Core
const Validator = new ValidatorCore(RolesObject);
second method => you can add or register custom Role after Intiating the Validator by using register
method
Validator.register("customRoleName", (options) => {});
all registerd roles must have Handler that accept options
parameter that contains value
, params
, inputs
and roles
-
value
=> the value that User passed into Inputs Object. -
params
=> any additional params passed to the role -
inputs
=> all user inputs in it's original shap in case you need to access another value. -
roles
=> the main RolesObject in case you need to interact with any Other Role.
Notice: any error message should contain $s
that will replaced with the input name
Overwrite
if there is a case when you need to replace exist role with new one with the same name .. in other words you need to Overwrite exist Role you can do this by add :force
to the name of the Role.
but Notice that this is NOT Recommended because there is some Roles Depend on other Roles so be careful when you do this Overwriting thing.
Validator.register("required:force", (options) => {});
this will force the Validator to Unregister the Old required
Role and add Yours.
Availabile Roles
Name | Parameters | Description |
---|---|---|
accepted | the field must be one of these values 'yes, on, 1, or true', usefull on forms confirmation. | |
required | the field must be presend and not Null | |
require_if | anotherField, value | 1- the field will be required if field 'anotherField' is present. 2- the field will be required if field 'anotherField' is equal to 'value' |
string | the field must be string | |
number | the field must be number | |
array | the field must be array | |
min | value | the field must be at least equal to 'value' AND if the field 1- string => then we check the number of chars 2- number => then we check the value 3- array => then we check the length |
max | value | the field must be at most equal to 'value' AND if the field 1- string => then we check the number of chars 2- number => then we check the value 3- array => then we check the length |
length | value | the field must be equal to 'value' AND if the field 1- string => then we check the number of chars 2- number => then we check the value 3- array => then we check the length |
date | the field must be a valid date, with any format | |
date_format | value | the field must be a valid date with 'value' format |
date_after | value | the field must be a valid date and must be grater than 'value' |
date_before | value | the field must be a valid date and must be less than 'value' |
in | value1, value2, ..etc | the field value must be equal one of 'value1, value2, ..etc' |
not_in | value1, value2, ..etc | the field value must be NOT one of 'value1, value2, ..etc' |
alpha | the field must be string of alphabets only | |
alphanum | the field must be string of alphabets and numbers only | |
between | value1, value2 | if the field is number then it's value must be between 'value1, value2' |
boolean | the field must be true or false OR 'true' or 'false' as strings | |
match | value | the field must be equal 'value' |
the field must be a valid email | ||
url | the field must be a valid URL |
License
This project is licensed under the GPL v3 License - see the LICENSE.md file for details