1.0.4 • Published 6 years ago

simplest-param-validator v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

simplest-param-validator

Extremely simple, lightweight and superfast express middleware for strictly verifying the presence of various parameters

Prerequisites

This middleware uses ES6 features. Make sure your Node.js version fully supports ES6

Quick start

First, run npm install simplest-param-validator --save for your app. Then, in an Express app, you may use it by calling the checkParams function. It checks the req.query,req.body and req.params for the presence of desired inputs and strictly checks their value for null, undefined and empty strings.:

const express = require('express')
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

const paramValidator = require('simplest-param-validator')

/* The code of this route strictly requires title, author, email in body parameters (req.body).
 So you can use this middleware to verify that all these parameters are available 
 in express route and their values are not null, undefined or empty string
*/
app.post('/',paramValidator.checkParams("body","title","author","email"),(req,res)=>{

    if(req.paramsMismatch!=true){
        //execute the code of your function
    }
    else{
        // send a response that there is a mismatch in the parameters required by the API. 
        res.send({"expected":req.paramsExpected,"received":req.paramsReceived})
    }

   
});

The function simply takes first input as the type of the parameter you are trying to verify for which the possible values are - route, query and body and then you are supposed to pass the names of the parameters as comma separated values. Since its a middleware, the error can be simply captured by checking the request variable paramsMismatch. If its true, that means your route didn't get the parameters that you were looking for. You may also use req.paramsExpected and req.paramsReceived which are also set by this middleware to show the users what parameters they missed.

For example, If you want to ensure that your route is getting query parameters named page and skip an

/* The code of this route strictly requires page and skip in query parameters (req.query).
*/
app.get('/',paramValidator.checkParams("query","page","skip"),(req,res)=>{

     if(req.paramsMismatch!=true){
        //execute the code of your function
    }
    else{
        // send a response that there is a mismatch in the parameters required by the API. 
        res.send({"expected":req.paramsExpected,"received":req.paramsReceived})
    }

});

/* The code of this route strictly requires id in route parameters (req.params).
*/
app.get('/',paramValidator.checkParams("route","id"),(req,res)=>{

     if(req.paramsMismatch!=true){
        //execute the code of your function
    }
    else{
        // send a response that there is a mismatch in the parameters required by the API. 
        res.send({"expected":req.paramsExpected,"received":req.paramsReceived})
    }

});


// ...

Use with Restify

You may use it with Restify as well. Since Restify categorises all kinds of input in req.params only, you will have to pass the type as route and that will do the trick for you.

Important note

You may validate any number of parameters but please make sure that the first argument to the function is always the type of parameter you are trying to validate. The possible values are route, query and body only!

Limitation

While using with ExpressJS, you can use this middeware to validate only one type of parameter at a time.Chaining this middleware to detect multiple types in a single route may give wrong results in some cases. Version 2.x of this middleware will not have this limitation.

This limitation is not there while using with Restify as all the parameters are there in req.params only.

License

This project is licensed under the MIT License - see the LICENSE file for details

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago