Body Checker
A simple tool to protect your API against bad request parameters
Installation
npm install body-checker
Usage
var check = ; ;
Body to validate
This is the request object (req.body
in express) that you want to validate.
Currently we only support shallow objects, but if there is an overwhelming need for deep objects, let us know in the issues and we will implement deep validation.
Configuration options
This is an object that outlines your allowed request parameters. It takes the following form:
paramKey: type: 'string' // String: Required required: false // Boolean: Optional, defaults to false default: 'default value' // String: Optional nextParamKey: ...
Allowed Types
Type is a required parameter. If you don't care what type it is, you can set type to any
.
- string: validates a string
- number: validates a number
- integer: validates a non floating point number
- array: validates an array
- object: validates an object
- null: expects value to be null
- assigned: expects value to be assigned
- any: bypasses type checking
Callback
Callback is a traditional callback(err, data) function. It will pass back detailed errors for debugging or the final req.body
object. This allows you to send your own generic error to the client to prevent phishing attacks. See example below.
Examples
Express request handler
var check = ; module { ; }
Tests
npm test