express-validator
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -49,3 +49,35 @@ /* | ||
req.check = function(param, fail_msg) { | ||
return validator.check(this.param(param), fail_msg); | ||
var value; | ||
// get value and param name for nested inputs like foo[bar][baz] | ||
if (Array.isArray(param)) { | ||
param.map(function(item) { | ||
if(value === undefined) { | ||
value = req.param(item); | ||
} else { | ||
value = value[item]; | ||
} | ||
}); | ||
param = param.join('_'); | ||
} else { | ||
value = this.param(param); | ||
} | ||
validator.error = function(msg) { | ||
var error = { | ||
param: param, | ||
msg: msg, | ||
value: value | ||
}; | ||
if (req._validationErrors === undefined) { | ||
req._validationErrors = []; | ||
} | ||
req._validationErrors.push(error); | ||
if (req.onErrorCallback) { | ||
req.onErrorCallback(msg); | ||
} | ||
} | ||
return validator.check(value, fail_msg); | ||
}; | ||
@@ -64,5 +96,19 @@ | ||
req.onValidationError = function(errback) { | ||
validator.error = errback; | ||
req.onErrorCallback = errback; | ||
}; | ||
req.validationErrors = function(mapped) { | ||
if (req._validationErrors === undefined) { | ||
return false; | ||
} | ||
if (mapped) { | ||
var errors = {}; | ||
req._validationErrors.forEach(function(err) { | ||
errors[err.param] = err; | ||
}); | ||
return errors; | ||
} | ||
return req._validationErrors; | ||
} | ||
req.filter = function(param) { | ||
@@ -69,0 +115,0 @@ var self = this; |
@@ -6,5 +6,6 @@ { | ||
"contributors": [ | ||
"Chris O'Hara <cohara87@gmail.com>" | ||
"Chris O'Hara <cohara87@gmail.com>", | ||
"@orfaust" | ||
], | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/ctavan/express-validator", | ||
@@ -16,2 +17,5 @@ "repository": { | ||
"main": "./index.js", | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"engines": { | ||
@@ -21,5 +25,8 @@ "node": "*" | ||
"dependencies": { | ||
"validator": ">=0.2.8" | ||
"validator": "~0.4.5" | ||
}, | ||
"devDependencies": { | ||
"async": "~0.1.18", | ||
"express": "~2.5.9", | ||
"request": "~2.9.153" | ||
}, | ||
@@ -26,0 +33,0 @@ "keywords": [ |
# express-validator | ||
[![Build Status](https://secure.travis-ci.org/ctavan/express-validator.png)](http://travis-ci.org/ctavan/express-validator) | ||
An [express.js]( https://github.com/visionmedia/express ) middleware for | ||
@@ -18,4 +20,5 @@ [node-validator]( https://github.com/chriso/node-validator ). | ||
```javascript | ||
var express = require('express'), | ||
expressValidator = require('express-validator'), | ||
var util = require('util'), | ||
express = require('express'), | ||
expressValidator = require('../../index'), | ||
app = express.createServer(); | ||
@@ -27,8 +30,2 @@ | ||
app.post('/:urlparam', function(req, res) { | ||
var errors = []; | ||
req.onValidationError(function(msg) { | ||
console.log('Validation error: ' + msg); | ||
errors.push(msg); | ||
return this; | ||
}); | ||
@@ -41,4 +38,5 @@ req.assert('postparam', 'Invalid postparam').notEmpty().isInt(); | ||
if (errors.length) { | ||
res.send('There have been validation errors: ' + errors.join(', '), 500); | ||
var errors = req.validationErrors(); | ||
if (errors) { | ||
res.send('There have been validation errors: ' + util.inspect(errors), 500); | ||
return; | ||
@@ -63,6 +61,9 @@ } | ||
$ curl -d 'postparam=1' http://localhost:8888/t1est?getparam=1 | ||
There have been validation errors: Invalid urlparam | ||
There have been validation errors: [ | ||
{ param: 'urlparam', msg: 'Invalid urlparam', value: 't1est' } ] | ||
$ curl -d 'postparam=1' http://localhost:8888/t1est?getparam=1ab | ||
There have been validation errors: Invalid getparam, Invalid foo | ||
There have been validation errors: [ | ||
{ param: 'getparam', msg: 'Invalid getparam', value: '1ab' }, | ||
{ param: 'urlparam', msg: 'Invalid urlparam', value: 't1est' } ] | ||
``` | ||
@@ -82,5 +83,78 @@ | ||
### Validation errors | ||
You have two choices on how to get the validation errors: | ||
```javascript | ||
req.assert('email', 'required').notEmpty(); | ||
req.assert('email', 'valid email required').isEmail(); | ||
req.assert('password', '6 to 20 characters required').len(6, 20); | ||
var errors = req.validationErrors(); | ||
var mappedErrors = req.validationErrors(true); | ||
``` | ||
errors: | ||
```javascript | ||
[ | ||
{param: "email", msg: "required", value: "<received input>"}, | ||
{param: "email", msg: "valid email required", value: "<received input>"}, | ||
{param: "password", msg: "6 to 20 characters required", value: "<received input>"} | ||
] | ||
``` | ||
mappedErrors: | ||
```javascript | ||
{ | ||
email: { | ||
param: "email", | ||
msg: "valid email required", | ||
value: "<received input>" | ||
}, | ||
password: { | ||
param: "password", | ||
msg: "6 to 20 characters required", | ||
value: "<received input>" | ||
} | ||
} | ||
``` | ||
### Nested input data | ||
Example: | ||
```html | ||
<input name="user[fields][email]" /> | ||
``` | ||
Provide an array instead of a string: | ||
```javascript | ||
req.assert(['user', 'fields', 'email'], 'valid email required').isEmail(); | ||
var errors = req.validationErrors(); | ||
console.log(errors); | ||
``` | ||
Output: | ||
```javascript | ||
[ | ||
{ | ||
param: "user_fields_email", | ||
msg: "valid email required", | ||
value: "<received input>" | ||
} | ||
] | ||
``` | ||
## Changelog | ||
### v0.2.0 | ||
- Added `validationErrors()` method (by @orfaust) | ||
- Added support for nested form fields (by @orfaust) | ||
- Added test cases | ||
### v0.1.3 | ||
@@ -103,2 +177,3 @@ - Readme update | ||
- Christoph Tavan <dev@tavan.de> - Wrap the gist in an npm package | ||
- @orfaust - Add `validationErrors()` and nested field support | ||
@@ -105,0 +180,0 @@ ## License |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
15423
12
339
178
3
3
1
+ Addedvalidator@0.4.28(transitive)
- Removedvalidator@13.12.0(transitive)
Updatedvalidator@~0.4.5