Socket
Socket
Sign inDemoInstall

express-validate.js

Package Overview
Dependencies
1
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    express-validate.js

Middleware wrapper for validate.js validation framework


Version published
Weekly downloads
12
increased by500%
Maintainers
1
Install size
878 kB
Created
Weekly downloads
 

Readme

Source

#Express validate.js Build Status Coverage Status Dependency Status

Middleware wrapper for validate.js validation framework

##Installation

$ npm install express-validate.js

##Example

var validate = require('express-validate.js'),
    express  = require('express');

express()
  .get('/user/:userId/:page?', validate({
    userId: {
      scope: 'route',
      presence: true,
      format: {
        pattern: /\d{5}/,
        message: 'must be a five digit number'
      }
    },
    page: {
      scope: ['route', 'query'],
      numericality: {
        onlyInteger: true,
        greaterThanOrEqualTo: 0,
      }
    }
  }), function (req, res) {
    var userId = req.valid.userId,
        page   = req.valid.page || 0;
    res.send(200, 'User ' + userId + ' is on page ' + page);
  })
  .listen(3000);

Following requests validate:

curl http://localhost:3000/user/12345
=> 200: User 12345 is on page 0

curl http://localhost:3000/user/12345?page=1
=> 200: User 12345 is on page 1

curl http://localhost:3000/user/12345/14
=> 200: User 12345 is on page 14

Following requests are rejected:

curl http://localhost:3000/user/1234
=> 400: {
  "userId": [
    "User id must be a five digit number"
  ]
}

curl http://localhost:3000/user/abcde/-1
=> 400: {
  "userId": [
    "User id must be a five digit number"
  ],
  "page": [
    "Page must be greater than or equal to 0"
  ]
}

##How it works

This middleware is configured the same way as validate.js with an additional scope constraint. This can either be a string or an array containing one of following values:

  • route: Check for values in the route parameters of the request (default)
  • body: Check for parameters in the request body, requires express.bodyParser
  • query: Check for parameters in the querystring of the request
  • cookies: Check for parameters in the cookies, requires cookieParser

If scope is an array, the first scope that has a corresponding value wins. If no scope is provided, route scope is used to evaluate parameters.

In case of invalid values, the validator responds with a 400 response containing the result of the validation. Otherwise the validated parameters are attached to the request in the valid object.

In the same way as can be done to validate.js, custom validators can be attached to validate.validators

the response content in case of an error can be customized. To do this add a customResponse function to validate. The function will receive the validate.js result object and it's return value is used as the response for invalid parameters:

validate.customResponse = function (errors) {
  return 'Nothing to see here';
};

validate.js supports async validators. express-validate.js evaluates constraints asynchronously if a supported Promise library is detected. Analog to validate.js a compatible promise library can be set through validate.Promise.

##License

MIT

Keywords

FAQs

Last updated on 18 Dec 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc