Socket
Socket
Sign inDemoInstall

body-checker

Package Overview
Dependencies
1
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    body-checker

A simple tool to protect your API against bad request parameters


Version published
Weekly downloads
3
decreased by-72.73%
Maintainers
1
Install size
131 kB
Created
Weekly downloads
 

Readme

Source

Body Checker

A simple tool to protect your API against bad request parameters

NPM

Build Status

Installation

npm install body-checker

Usage

var check = require('body-checker');

check([body to validate], [configuration options], [callback]);
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 = require('body-checker');

module.exports = function(req, res, next) {

	check(req.body, {
		name: {
			type: 'string',
			default: 'public',
			required: true
		},
		id: {
			type: 'integer',
			required: true
		}
	}, function(err, body) {

		if(err) {

			// Log detailed error message on server
			console.log(err.message);

			// Send generic error to client
			res.status(400).send({
				message: 'Bad Request'
			});

		} else {

			// do stuff with safe parameters
			// and eventually...

			res.status(200).send(body);
		}

	});

}

Tests

npm test

Keywords

FAQs

Last updated on 07 Mar 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc