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 🙏

© 2025 – Pkg Stats / Ryan Hefner

express-validate.js

v1.3.0

Published

Middleware wrapper for validate.js validation framework

Readme

#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