dat-middleware
Common request, response, body, query, and param validation, transformation, and flow control middleware
Installation
npm install dat-middleware
Examples
Validations:
require()
requires the keys specified, and nexts a 400 error if one does not exist
mwbodykeysmwmw
var mw = ;var app = ; // requires that req.body.key1 and req.body.key2 are not undefinedapp;// example error:// 400 { message: body parameter "key1" is required }
string()
requires the keys are strings (if they exist), and nexts a 400 error if one is not
mwbodykeysmwmw
number()
requires the keys are numbers (if they exist), and nexts a 400 error if one is not
mwbodykeysmwmw
object()
requires the keys are objects (if they exist), and nexts a 400 error if one is not
mwbodykeysobjectmwobjectmwobject
function()
requires the keys are functions (if they exist), and nexts a 400 error if one is not
mwbodykeysmwmw
boolean()
requires the keys are booleans (if they exist), and nexts a 400 error if one is not
mwbodykeysmwmw
array()
requires the keys are arrays (if they exist), and nexts a 400 error if one is not
mwbodykeysmwmw
var mw = ;var app = ; // requires that req.body.key1 and req.body.key2 arrays *if they exist*app;// example error:// 400 { message: body parameter "key1" must be an array }
mwbodykeysmwmw
instanceOf(Class)
requires the keys are an instance of the specified class (if they exist), and nexts a 400 error if one is not
var mw = ;var app = ; // requires that req.body.key1 and req.body.key2 arrays *if they exist*app;// example error:// 400 { message: body parameter "key1" must be an instance of Class }
matches(regexp)
requires the keys are an instance of the specified class (if they exist), and nexts a 400 error if one is not
var mw = ;var app = ; // requires that req.body.key1 and req.body.key2 match the regexp *if they exist*app;// example error:// 400 { message: body parameter "key1" must match /^hello/ }
validate(validation)
requires the keys pass the validation (if they exist), and nexts a 400 error if one is not dat-middleware uses spumko/boom for http errors (exported as mw.Boom)
var mw = ;var app = ; { return vallength !== 24 ? mwBoom: null; // pass} // requires that req.body.key1 and req.body.key2 arrays *if they exist*app;// example error:// 400 { message: body parameter "key1" is not 24 characters }
Transformations:
mapValues(transformation)
transforms the values of the keys specified using the transformation function
var mw = ;var app = ; { return ;} // transforms the req.body.key1 and req.body.key2 to integersapp;
transform(transformation)
transforms the entire dataType object using the transformation function
var mw = ;var app = ; { Object;} // transforms the req.body.key1 and req.body.key2 to integersapp;
pick()
picks the keys specified and ignores the rest. a way of filtering data values by key.
var mw = ;var app = ; // a body of { key1: true, key2: true, key3:true } becomes { key1: true }app;
set()
sets the keys and values on the data type.
var mw = ;var app = ; // a body of { foo: 1 } becomes { foo: 1, key: 'value' }app; // a body of { key1: true, key2: true, key3:true } becomes extended by objapp; // a body of { key1: 2 } becomes { key1: 2, key2: 1.4142135623730951 }app
unset()
deletes the keys on the data type.
var mw = ;var app = ; // a body of { key1: true, key2: true, key3:true } becomes { key3:true }app;
Flow Control
each()
iterates through a series of middlewares (in parallel) for each item in an array
var mw = ;var app = ; // a body of { arr: [1,2,3] }app;
Conditionals (flow control):
for more flow control checkout middleware-flow
[validation chain].then(middlewares...).else(middlewares)
var mw = ;var app = ; // requires that req.body.key1 and req.body.key2 exist and are 24 charactersapp;
Chaining: chained methods will run in order
note: conditionals do not chain before validations and transformations
var mw = ;var app = ; { return vallength !== 3 ? mwBoom: null; // pass} { return ;} // requires that req.body.key1 and req.body.key2 exist and are 24 charactersapp;
License
MIT