dat-middleware
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -218,2 +218,13 @@ var error = require('./error'); | ||
/** | ||
* @method | ||
* @param {regexp} re - keys sh. | ||
*/ | ||
RequestData.prototype.matches = function (re) { | ||
if (!re) { | ||
throw new Error('match requires a regexp'); | ||
} | ||
this.validate(validations.matches(re)); | ||
return this; | ||
}; | ||
/** | ||
* assert keys exist | ||
@@ -288,3 +299,3 @@ * @method function | ||
if (typeof data[key] === 'string') { | ||
data[key] = keypather.get(req, data[key]) || data[key]; | ||
data[key] = keypather.get(req, data[key]) || data[key]; | ||
} | ||
@@ -291,0 +302,0 @@ }); |
@@ -33,3 +33,12 @@ var utils = require('map-utils'); | ||
}; | ||
}, | ||
matches: function (re) { | ||
return function (val) { | ||
if (!re.test(val)) { | ||
var err = error('should match '+re.toString()); | ||
err.val = val; | ||
return err; | ||
} | ||
}; | ||
} | ||
}; |
{ | ||
"name": "dat-middleware", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "common request, response, body, query, and param validation, transformation, and flow control middleware", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -124,2 +124,16 @@ # dat-middleware [![Build Status](https://travis-ci.org/tjmehta/dat-middleware.png?branch=master)](https://travis-ci.org/tjmehta/dat-middleware) | ||
## matches(regexp) | ||
requires the keys are an instance of the specified class (if they exist), and nexts a 400 error if one is not | ||
```js | ||
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) | ||
@@ -126,0 +140,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57310
23
1476
316