Code Monkey home page Code Monkey logo

express-validators's Introduction

validator-for-express

Nodejs (Express js) server side Validator. This package offer very simple and easy way to validate any type of json object (request json object) for expressjs. This validator also work with bluebird promise as well as async method callback. Expressjs framework json object(request) validator package.

Install using NPM

npm i validator-for-express --save

require in your (expressjs) route file.

var Validators = require('validator-for-express')

or if you using bluebird promise

var Promise = require('bluebird');
var Validators = require('validator-for-express');
Promise.promisifyAll(Validators);

Or

var Validators = Promise.promisifyAll(require('validator-for-express'));

Usage

e.g: Json object need to be validated.

    var Obj = {
        currentDate: '2016-09-16',
        afterDate: '2016-09-17',
        beforeDateValue: '2016-09-18',
        alphaValue: 12112,
        alpha_numValue: 'test_1-2',
        arrayValue: [],
        betweenValue: 40,
        booleanValue: true,
        confirmedValue: "233232",
        confirmedValue1: "233232ss",
        dateValue: "2015-03-25ss",
        notEqualValue: 12112,
        numericValue: "12112ss",
        lengthValue: "12112ss",
        emailValue: "[email protected]",
        inValue: "23",
        ipValue: "23",
        notInValue: "23", 
        regexValue: "",
        requiredValue: "",
        requiredIfValue: "",
        stringValue: 7797,
        urlValue: 7797,
        amountValue: "7797.00ss" 
    };

You can specify the rules for each index in json object. also you can specify multiple rules for a particular index by adding "|" after first rule.

    var rules = {
        "currentDate": "after:afterDate|date",
        "beforeDateValue": "before:afterDate",
        "alphaValue": "alpha",
        "alpha_numValue": "alpha_num",
        "arrayValue": "array",
        "betweenValue": "between:10,30",
        "booleanValue": "boolean",
        "confirmedValue": "confirmed:confirmedValue1",
        "dateValue": "date",
        "notEqualValue": "notEqual:alphaValue",
        "numericValue": "numeric",
        "lengthValue": "length:10,30",
        "inValue": "in:23,34",
        "ipValue": "ip",
        "notInValue": "notIn:23,35",
        "issetValue": "isset",
        "regexValue": "regex:pattern",
        "requiredValue": "required",
        "requiredIfValue": "requiredIf:alphaValue,12112",
        "stringValue": "string",
        "urlValue": "url",
        "amountValue": "amount"
    };
    

You can define messages for each rule of an index. if don't specify the messages it will return default messages. It is not mendatory.

    var messages = {
        emailValue:{
            email: "Please enter a valid email address."
        }
    };

Call to validator meathod

    Validators.validator(Obj, rules, messages, function (err, validated) {
         
          if (validated.fails) { 
              console.log(validated.getErrors)
          }
    })

Or If using promise.

Validators.validatorAsync(Obj, rules, messages)
            .then(function (validated) {
                   if (validated.fails) { 
                      console.log(validated.getErrors)
                  }
            })
            .catch(function(err){
              console.error(err);
            })

fails index will be true if there is an error. Otherwise it will be false. getErrors index contains error field as a key and array of error messages for that field.

e.g:

{ currentDate: [ 'CurrentDate must be later date then afterDate' ],
  beforeDateValue: [ 'BeforeDateValue must be before date then afterDate' ],
  alphaValue: [ 'AlphaValue must be entirely alphabetic characters.' ],
  alpha_numValue: [ 'Alpha numValue must be alpha-numeric characters.' ],
  betweenValue: [ 'BetweenValue value must be in 10,30' ],
  confirmedValue: [ 'ConfirmedValue value must be same as ConfirmedValue1' ],
  dateValue: [ 'DateValue must be a valid date.' ],
  notEqualValue: [ 'NotEqualValue value must not be equal to AlphaValue' ],
  numericValue: [ 'NumericValue must be a numeric value.' ],
  lengthValue: [ 'LengthValue should be 10 to 30 character long.' ],
  emailValue: [ 'Please enter a valid email address.' ],
  ipValue: [ 'IpValue must be a valid ip address.' ],
  notInValue: [ 'NotInValue must not be in 23,35.' ],
  issetValue: [ 'IssetValue does not exists.' ],
  regexValue: [ 'RegexValue found to be in invalid pattern.' ],
  requiredValue: [ 'RequiredValue field is required.' ],
  requiredIfValue: [ 'RequiredIfValue field is required.' ],
  stringValue: [ 'StringValue must be a string.' ],
  urlValue: [ 'UrlValue must be a valid url.' ],
  amountValue: [ 'AmountValue must be an amount value.' ] }
  

express-validators's People

Contributors

scopdrag avatar beliga44 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.