express-validate.js

1.3.0 • Public • Published

#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

Package Sidebar

Install

npm i express-validate.js

Weekly Downloads

1

Version

1.3.0

License

MIT

Last publish

Collaborators

  • janpotoms