Bodychecker

Bodychecker is a express middleware for handling req.body, which is used to validate the value in the body like the React Proptypes way.
Installation
npm install --save bodychecker
Usage
Bodychecker add result "req.$bcResult" to the express RequestHandler.
example:
var express = require('express')
var bodychecker = require('bodychecker')
var app = express()
app.post('/post/new', bodychecker({
title: bodychecker.string.isRequired,
author: bodychecker.string,
time: bodychecker.number,
content: bodychecker.string.isRequired
}), function (req, res, next) {
})
API
All supported types:
-
any (optional isRequired) any type, e.g 'abc',123,undefind
-
array (optional isRequired) array type, e.g [1,2,3...]
-
object (optional isRequired) object type, e.g {a:1}
-
bool (optional isRequired) boolean type, e.g true, false
-
number (optional isRequired) number type, e.g 3.14159265
-
string (optional isRequired) string type, e.g Hello world!
-
oneof (optional isRequired) array argument required, e.g oneof(['a', 'b'])
-
oneoftype (optional isRequired) array of function argument required, e.g oneoftype([bodychecker.string,bodychecker.number])
-
arrayof (optional isRequired) function argument required, e.g arrayof(bodychecker.string)
-
objectof (optional isRequired) function argument required, e.g objectof(bodychecker.string)
-
shapeof (optional isRequired) obeject of function argument required, e.g shapeof({ title: bodychecker.string })
Object "req.$bcResult"
{
message: 'message',
type: 'invalid',
fieldpath: fieldpath,
fieldvalue: fieldvalue
}
Custom checker
Otherwise, You can also add custom checker.
function customChecker(body, fieldname) {
}
LICENSE
MIT