2.1.0 • Published 6 years ago

he-validation v2.1.0

Weekly downloads
2
License
GPL
Repository
github
Last release
6 years ago

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

  1. value => the value that User passed into Inputs Object.
  2. params => any additional params passed to the role
  3. inputs => all user inputs in it's original shap in case you need to access another value.
  4. 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

License

This project is licensed under the GPL v3 License - see the LICENSE.md file for details

I'm Welcoming with any comment or advise or you can open new issue on github

2.1.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago