@sabyasachibiswal/validator

1.0.5 • Public • Published

sabyasachibiswal/validator

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"});
    }

};

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i @sabyasachibiswal/validator

    Weekly Downloads

    0

    Version

    1.0.5

    License

    MIT

    Unpacked Size

    19.6 kB

    Total Files

    5

    Last publish

    Collaborators

    • sabyasachibiswal