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-req-validate

v1.1.0

Published

Mongoose-inspired req.query and req.body validator for express/connect servers.

Downloads

8

Readme

express-req-validate

Mongoose-inspired req.query and req.body validator for express/connect servers.

npm build status

Installation

npm install --save express-req-validate

Load the validator middleware after the body parser.

var bodyParser = require('body-parser');
var queryParser = require('express-query-int');
var reqValidate = require('express-req-validate');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(queryParser());
app.use(reqValidate());

Validating req.body

By default the module will only attempt to validate req.query. To validate req.body:

req.validate(req.body, {
  // your model here...
});

To validate both req.query and req.body:

req.validate([req.query, req.body], {
  // your model here...
});

Model

model.type

Checks if request parameter is created by the specified constructor.

req.validate({
  a: {
    type: String
  }
});
// ?a=b     => true
// ?a[0]=b  => false

Note: By default, numbers in query parameters will be strings. To use { type: Number } you will need a parser such as express-query-int.

model.required

Checks if request parameter is present.

req.validate({
  a: {
    required: true
  }
});
// ?a    => true
// ?a=0  => true
// ?a=b  => true

model.min

If parameter is a Number, checks that it is greater than or equal to min.

req.validate({
  a: {
    type: Number,
    min: 10
  }
});
// ?a=10  => true
// ?a=11  => true
// ?a=9   => false

If parameter is a String, checks that the string is longer or equal length to min.

req.validate({
  a: {
    type: String,
    min: 3
  }
});
// ?a=abc   => true
// ?a=abcd  => true
// ?a=ab    => false

If parameter is an Array, checks that the array.length is greater than or equal to min.

req.validate({
  a: {
    type: Array,
    min: 2
  }
});
// ?a[0]=b&a[1]=c         => true
// ?a[0]=b&a[1]=c&a[2]=d  => true
// ?a[0]=b                => false

If no type is specified, validator will do its best to guess. To ensure that you get the expected result, it is recommended to provide a type.

model.max

If parameter is a Number, checks that it is less than or equal to max.

req.validate({
  a: {
    type: Number,
    max: 10
  }
});
// ?a=10  => true
// ?a=9   => true
// ?a=11  => false

If parameter is a String, checks that the string is shorter or equal length to max.

req.validate({
  a: {
    type: String,
    max: 3
  }
});
// ?a=abc   => true
// ?a=ab    => true
// ?a=abcd  => false

If parameter is an Array, checks that the array.length is less than or equal to max.

req.validate({
  a: {
    type: Array,
    max: 2
  }
});
// ?a[0]=b&a[1]=c         => true
// ?a[0]=b                => true
// ?a[0]=b&a[1]=c&a[2]=d  => false

If no type is specified, validator will do its best to guess. To ensure that you get the expected result, it is recommended to provide a type.

model.each

Checks that each value of the Object/Array meets condition. Return:

  • true - passes validation
  • false - fails validation
  • new Error('msg') - fails validation with custom message
req.validate({
  a: {
    each: function(value) {
      return value > 3;
    }
  }
});
// ?a[0]=b&a[4]=c  => true
// ?a[0]=b&a[2]=c  => false

model.validator

Checks that value passes custom function validation. Return:

  • true - passes validation
  • false - fails validation
  • new Error('msg') - fails validation with custom message
req.validate({
  a: {
    validate: function(value) {
      return new Error('You shall not pass!');
    }
  }
});
// ?a  => false

Note: You could use a custom validation library like Validator.js for each and validate.

var validator = require('validator');
req.validate({
  a: {
    validate: validator.isCreditCard
  }
});
// ?a  => false

Errors

The validator will not throw an error, instead it will set req.validateError to an error object. If there are no validation errors, req.validateError will be null.

req.validate(...)

if (req.validateError) {
  return yourOwnErrorHandler(res, req.validateError);
}

Custom Error Format

You can set a custom error format when initializing the module. See lib/error.js for the default constructor.

function ErrorGenerator(message, options) {
  this.message = message;
}

ErrorGenerator.prototype = Error.prototype;

app.use(reqValidate({
  ErrorGenerator: ErrorGenerator
}))

License

Copyright (c) 2015 Marius Craciunoiu. Licensed under the MIT license.