Skip to content

Easily validate input data with rules and get customised error messages on errors.

License

Notifications You must be signed in to change notification settings

sabyasachibiswal/validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sabyasachibiswal/validator

@sabyasachibiswal/validator npm badge @sabyasachibiswal/validator npm downloads badge

Node.js validators - Data validator engine for validating input data / form data, with customisable error messages for each field.

Getting started

Install via npm

npm install --save @sabyasachibiswal/validator

Import the module

var Validator = require("@sabyasachibiswal/validator");

Usage

exports.Signup = function(req , res) {    
    
    // Lets assume we have this inputData
    var inputData = {
        name : "Sabyaschi Biswal",
        email : "info@sabyasachibiswal.com",
        password : "my-secret"
    };
    
    // Rules for validations
    var validationRules = {
        name : "required|string",
        email : "required|email",
        password : "required|min:6",
        captcha : "required"
    };
    
    // Optional - Custom messages
    var customMessages = {
        name : {
            required : "Name is required",
            string : "Name must contain only alphabets",
        },
        email : {
            required : "Email is required",
            email : "Email must be a valid email address"
        },
        password : {
            required : "Password is required",
            min : "Password must be minimum 6 characters"
        },
        captcha : {
            required : "Captcha is required"
        }
    };
    
    // Validate
    var validator = new Validator(inputData, validationRules, customMessages).validate();
    
    // Check if validation failed
    if(validator.valid === false){
        // Error
        return res.json({status : "error", errors : validator.getAllErrorsMessages()});
    }else{
        // Create user
        //...
        
        // Return success response
        return res.json({status : "success", message : "Signup successful"});
    }

};