npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-validators

v1.0.4

Published

Express framework json object(req) validator.

Downloads

779

Readme

express-validators

npm npm npm npm

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 install express-validators --save

require in your (expressjs) route file.

var Validators = require('express-validators')

or if you using bluebird promise

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

Or


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

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.


    var rules = {
        "currentDate": "after:afterDate",
        "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.' ] }