1.10.4 • Published 8 years ago

dat-middleware v1.10.4

Weekly downloads
9
License
MIT
Repository
github
Last release
8 years ago

dat-middleware

Build Status dependency status dev dependency status

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

mw.body(keys..).require()
mw.query(keys..).require()
mw.params(keys..).require()
var mw = require('dat-middleware');
var app = require('express')();

// requires that req.body.key1 and req.body.key2 are not undefined
app.use(mw.body('key1, key2').require());
// 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

mw.body(keys..).string()
mw.query(keys..).string()
mw.params(keys..).string()

number()

requires the keys are numbers (if they exist), and nexts a 400 error if one is not

mw.body(keys..).number()
mw.query(keys..).number()
mw.params(keys..).number()

object()

requires the keys are objects (if they exist), and nexts a 400 error if one is not

mw.body(keys..).object()
mw.query(keys..).object()
mw.params(keys..).object()

function()

requires the keys are functions (if they exist), and nexts a 400 error if one is not

mw.body(keys..).function()
mw.query(keys..).function()
mw.params(keys..).function()

boolean()

requires the keys are booleans (if they exist), and nexts a 400 error if one is not

mw.body(keys..).boolean()
mw.query(keys..).boolean()
mw.params(keys..).boolean()

array()

requires the keys are arrays (if they exist), and nexts a 400 error if one is not

mw.body(keys..).array()
mw.query(keys..).array()
mw.params(keys..).array()
var mw = require('dat-middleware');
var app = require('express')();

// requires that req.body.key1 and req.body.key2 arrays *if they exist*
app.use(mw.body('key1, key2').array());
// example error:
// 400 { message: body parameter "key1" must be an array }
mw.body(keys..).instanceOf(Class)
mw.query(keys..).instanceOf(Class)
mw.params(keys..).instanceOf(Class)

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 = require('dat-middleware');
var app = require('express')();

// requires that req.body.key1 and req.body.key2 arrays *if they exist*
app.use(mw.body('key1, key2').instanceOf(Class));
// 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 = require('dat-middleware');
var app = require('express')();

// requires that req.body.key1 and req.body.key2 match the regexp *if they exist*
app.use(mw.body('key1, key2').matches(/^hello/));
// 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 = require('dat-middleware');
var app = require('express')();
function is24Chars (val) {
  return (val.length !== 24) ?
    mw.Boom.badRequest('is not 24 characters'):
    null; // pass
}

// requires that req.body.key1 and req.body.key2 arrays *if they exist*
app.use(mw.body('key1, key2').validate(is24Chars));
// 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 = require('dat-middleware');
var app = require('express')();
function toInt (v) {
  return parseInt(v);
}

// transforms the req.body.key1 and req.body.key2 to integers
app.use(mw.body('key1, key2').mapValues(toInt));

transform(transformation)

transforms the entire dataType object using the transformation function

var mw = require('dat-middleware');
var app = require('express')();
function valuesToInt (body) {
  Object.keys(body).forEach(function (key) {
    body[key] = parseInt(body[key]);
  });
}

// transforms the req.body.key1 and req.body.key2 to integers
app.use(mw.body().transform(valuesToInt));

pick()

picks the keys specified and ignores the rest. a way of filtering data values by key.

var mw = require('dat-middleware');
var app = require('express')();

// a body of { key1: true, key2: true, key3:true } becomes { key1: true }
app.use(mw.body('key1').pick());

set()

sets the keys and values on the data type.

var mw = require('dat-middleware');
var app = require('express')();

// a body of { foo: 1 } becomes { foo: 1, key: 'value' }
app.use(mw.body().set('key', 'value'));

// a body of { key1: true, key2: true, key3:true } becomes extended by obj
app.use(mw.body().set(obj));

// a body of { key1: 2 } becomes { key1: 2, key2: 1.4142135623730951 }
app.use(mw.body().set('key2', 'key1', function (val) { return Math.sqrt(val); }))

unset()

deletes the keys on the data type.

var mw = require('dat-middleware');
var app = require('express')();

// a body of { key1: true, key2: true, key3:true } becomes { key3:true }
app.use(mw.body().unset('key1', 'key2'));

Flow Control

each()

iterates through a series of middlewares (in parallel) for each item in an array

var mw = require('dat-middleware');
var app = require('express')();

// a body of { arr: [1,2,3] }
app.use(mw.body('arr').each(
  function (eachReq, res, next) {
    // eachReq prototypically inherits from the original req
    // (get gets from req, set only sets to eachReq)
  },
  function (item, req, eachReq, res, next) {
    // if five arguments are accepted the current item and the origin req are passed
  }
));

Conditionals (flow control):

for more flow control checkout middleware-flow

validation chain.then(middlewares...).else(middlewares)

var mw = require('dat-middleware');
var app = require('express')();

// requires that req.body.key1 and req.body.key2 exist and are 24 characters
app.use(mw.body('key1, key2').require()
  .then(mw1) // executes mw1 if key1 and key2 exist
  .else(mw2) // executes mw1 if key1 and key2 if they dont
             // if mw2 accept an error it will recieve the validation error, else it will be ignored
);

Chaining: chained methods will run in order

note: conditionals do not chain before validations and transformations

var mw = require('dat-middleware');
var app = require('express')();
function hasLengthOf3 (val) {
  return (val.length !== 3) ?
    mw.Boom.badRequest('is not 3 characters'):
    null; // pass
}
function toInt (v) {
  return parseInt(v);
}

// requires that req.body.key1 and req.body.key2 exist and are 24 characters
app.use(mw.body('key1, key2').require().validate(hasLengthOf3).transform(toInt));

License

MIT

1.10.4

8 years ago

1.10.3

9 years ago

1.10.2

9 years ago

1.10.1

9 years ago

1.10.0

9 years ago

1.9.4

9 years ago

1.9.3

9 years ago

1.9.2

10 years ago

1.9.1

10 years ago

1.9.0

10 years ago

1.8.1

10 years ago

1.8.0

10 years ago

1.7.0

10 years ago

1.6.1

10 years ago

1.6.0

10 years ago

1.5.13

10 years ago

1.5.12

10 years ago

1.5.11

10 years ago

1.5.10

10 years ago

1.5.9

10 years ago

1.5.8

10 years ago

1.5.7

10 years ago

1.5.6

10 years ago

1.5.5

10 years ago

1.5.4

10 years ago

1.5.3

10 years ago

1.5.2

10 years ago

1.5.1

10 years ago

1.5.0

10 years ago

1.4.0

10 years ago

1.3.1

10 years ago

1.3.0

10 years ago

1.2.0

10 years ago

1.1.3

10 years ago

1.1.2

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago