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-request-checker

v0.2.3

Published

Express request checker extension.

Downloads

4

Readme

express-request-checker

NPM version Downloads

Create request checker middleware for Express.

with express-request-checker, checking HTTP request's querybody or url params will be more easy and readable. All the works is just require express-request-checker in router.js which belongs to an Express project and config it. So it's no need to modify any other source file.

Since validation codes are written in a config-like way, router.js file will look like an API document, I hope that the communication cost within the development team can be reduced.

  • No validation codes.
  • router.js is also an API document.
  • Easy to combine with other validation package.

Quick Example(Javascript):

// router.js

var express          = require('express');
var reqCheckerModule = require('express-request-checker');

var reqChecker = reqCheckerModule.requestChecker;
var router = express.Router();

var options = {
  strict: false,  // Allow unexpected parameter. (true|false, DEFAULT: true)
  query: {        // Check req.query. (params|query|body)
    'param1': {
      matchRegExp: /^[0-9]{1}$/
    },
    'param2': {
      isIn: [1, 2, 3],
      isOptional: true    // Optional parameter. (true|false, DEFAULT: false)
    }
  }
};
router.get('/path', reqChecker(options), handlerFunction);

module.exports = router;

Quick Example(CoffeeScript):

# router.coffee

express          = require 'express'
reqCheckerModule = require 'express-request-checker'

reqChecker = reqCheckerModule.requestChecker
router = express.Router()

options =
  strict: false  # Allow unexpected parameter. (true|false, DEFAULT: true)
  query:         # Check req.query. (params|query|body)
    'param1':
      matchRegExp: /^[0-9]{1}$/
    'param2':
      isIn: [1, 2, 3]
      isOptional: true    # Optional parameter. (true|false, DEFAULT: false)
router.get '/path', reqChecker(options), handlerFunction

module.exports = router

Play with other modules

// router.js

var express          = require('express');
var reqCheckerModule = require('express-request-checker');

var reqChecker = reqCheckerModule.requestChecker;
var router = express.Router();

var validator = require('validator');
var options = {
  params: {
    'id': {
      assertTrue: validator.isInt
    }
  },
  body: {
    'email': {
      assertTrue: validator.isEmail
    },
    'jsonData': {
      assertTrue: validator.isJSON
    }
  }
};
router.post('/user/:id', reqChecker(options), handlerFunction);

module.exports = router;

Checker Options Default Values

|Option |Default Value| |------------|-------------| |strict |true |

Parameter Options Default Values

|Option |Default Value| |--------------|-------------| |isOptional |false | |assertTrue |[] | |assertFalse |[] | |matchRegExp |[] | |isIn |[] | |notIn |[] | |isInteger |null | |isEmail |null | |isArray |null | |isIntegerArray|null | |equal |null | |greaterThan |null | |greaterEqual |null | |lessThan |null | |lessEqual |null | |allowEmpty |false | |minLength |null | |maxLangth |null |

Parameter Options

assertTrue

function, [function, function ...] or []. (DEFAULT: [] - No checker)
Using parameter in request as function(s)'s argument, if the function(s) return true,OK. Otherwise, NG.

Example:

option = {
  query: {
    param1: {
      assertTrue: [function(value) { return value > 10; }]
    }
  }
}

assertFalse

Opposite to assertTrue.

matchRegExp

RegExp, [RegExp, RegExp ...] or []. (DEFAULT: [] - Don't check)
If the RegExp(s) test result is true, OK. Otherwise, NG.

Example:

option = {
  query: {
    param1: {
      matchRegExp: [/^[012]{1}$/, /^[234]{1}$/]
    }
  }
}

isIn

[value, value, ...] or []. (DEFAULT: [] - Don't check)
Values of parameter in request which are allowed.

Example:

option = {
  query: {
    param1: {
      isIn: [1, 2, 3]
    }
  }
}

notIn

Opposite to isIn.

isInteger

true or false. (DEFALT:null - Don't care)
when true, The value of parameter in request must be an integer.
when false , The value of parameter in request must NOT be an integer.

Example:

option = {
  query: {
    param1: {
      isInteger: true
    }
  }
}

isEmail

true or false. (DEFALT:null - Don't care)
when true, The value of parameter in request must be an correct email address.
when false , The value of parameter in request must NOT be an email address.

Example:

option = {
  query: {
    param1: {
      isEmail: true
    }
  }
}

isArray

true or false. (DEFALT:null - Don't care)
when true, The value of parameter in request must be an Array or stringified Array.
when false , The value of parameter in request must NOT be an Array or stringified Array.

Example:

option = {
  query: {
    param1: {
      isArray: true
    }
  }
}

isIntegerArray

true or false. (DEFALT:null - Don't care)
when true, The value of parameter in request must be an Array or stringified Array whose elements are all integers.
when false , The value of parameter in request must NOT be an Array or stringified Array whose elements are all integers.

Example:

option = {
  query: {
    param1: {
      isIntegerArray: true
    }
  }
}

equal / greaterThan / greaterEqual / lessThan / lessEqual

integer or null. (DEFALT:null - Don't care)
The value of parameter in request must be equal/greaterThan/greaterEqual/lessThan/lessEqual to the option value.

Example:

option = {
  query: {
    param1: {
      equal: 100
    }
  }
}

allowEmpty

true or false. (DEFAULT: false)
when setted true, The value of parameter in request can be ''.
when setted true, The value of parameter in request can NOT be ''.

Example:

option = {
  query: {
    param1: {
      isEmpty: false
    }
  }
}

maxLength / minLength

integer or null. (DEFALT:null - Don't care)
Max/Min Length of the value of parameter in request.

Example:

option = {
  query: {
    param1: {
      minLength: 5,
      maxLength: 10
    }
  }
}

Install:

npm install express-request-checker

Test:

cd node_modules/express-request-checker
npm test

License

MIT