express-validator
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -32,32 +32,20 @@ /* | ||
req.mixinParams = function() { | ||
this.params = this.params || {}; | ||
// this.params is an array, we want an object instead | ||
if (Array.isArray(this.params)) { | ||
var params = {}; | ||
for (var c in this.params) { | ||
params[c] = this.params[c]; | ||
} | ||
this.params = params; | ||
req.updateParam = function(name, value) { | ||
// route params like /user/:id | ||
if (this.params && this.params.hasOwnProperty(name) && undefined !== this.params[name]) { | ||
return this.params[name] = value; | ||
} | ||
this.query = this.query || {}; | ||
this.body = this.body || {}; | ||
// Merge params from the query string | ||
for (var i in this.query) { | ||
if (typeof this.params[i] === 'undefined') { | ||
this.params[i] = this.query[i]; | ||
} | ||
// query string params | ||
if (undefined !== this.query[name]) { | ||
return this.query[name] = value; | ||
} | ||
// Merge params from the request body | ||
for (var j in this.body) { | ||
if (typeof this.params[j] === 'undefined') { | ||
this.params[j] = this.body[j]; | ||
} | ||
// request body params via connect.bodyParser | ||
if (this.body && undefined !== this.body[name]) { | ||
return this.body[name] = value; | ||
} | ||
return false; | ||
}; | ||
req.check = function(param, fail_msg) { | ||
return validator.check(this.params[param], fail_msg); | ||
return validator.check(this.param(param), fail_msg); | ||
}; | ||
@@ -84,5 +72,5 @@ | ||
this.str = str; | ||
self.params[param] = str; // Replace the param with the filtered version | ||
self.updateParam(param, str); // Replace the param with the filtered version | ||
}; | ||
return filter.sanitize(this.params[param]); | ||
return filter.sanitize(this.param(param)); | ||
}; | ||
@@ -89,0 +77,0 @@ |
@@ -8,3 +8,3 @@ { | ||
], | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"homepage": "https://github.com/ctavan/express-validator", | ||
@@ -23,3 +23,12 @@ "repository": { | ||
"devDependencies": { | ||
} | ||
}, | ||
"keywords": [ | ||
"express", | ||
"validator", | ||
"validation", | ||
"validate", | ||
"sanitize", | ||
"sanitization", | ||
"xss" | ||
] | ||
} |
@@ -9,2 +9,8 @@ # express-validator | ||
## Installation | ||
``` | ||
npm install express-validator | ||
``` | ||
## Usage | ||
@@ -20,3 +26,3 @@ | ||
app.post('/:foo', function(req, res) { | ||
app.post('/:urlparam', function(req, res) { | ||
var errors = []; | ||
@@ -28,7 +34,5 @@ req.onValidationError(function(msg) { | ||
req.mixinParams(); | ||
req.assert('postparam', 'Invalid postparam').isInt(); | ||
req.assert('getparam', 'Invalid getparam').isInt(); | ||
req.assert('foo', 'Invalid foo').isAlpha(); | ||
req.assert('urlparam', 'Invalid urlparam').isAlpha(); | ||
@@ -41,3 +45,7 @@ req.sanitize('postparam').toBoolean(); | ||
} | ||
res.json(req.params); | ||
res.json({ | ||
urlparam: req.param('urlparam'), | ||
getparam: req.param('getparam'), | ||
postparam: req.param('postparam') | ||
}); | ||
}); | ||
@@ -52,6 +60,6 @@ | ||
$ curl -d 'postparam=1' http://localhost:8888/test?getparam=1 | ||
{"foo":"test","getparam":"1","postparam":true} | ||
{"urlparam":"test","getparam":"1","postparam":true} | ||
$ curl -d 'postparam=1' http://localhost:8888/t1est?getparam=1 | ||
There have been validation errors: Invalid foo | ||
There have been validation errors: Invalid urlparam | ||
@@ -62,2 +70,12 @@ $ curl -d 'postparam=1' http://localhost:8888/t1est?getparam=1ab | ||
## Changelog | ||
### v0.1.1 | ||
- Use req.param() method to get parameter values instead of accessing | ||
req.params directly. | ||
- Remove req.mixinParams() method. | ||
### v0.1.0 | ||
- Initial release | ||
## Contributors | ||
@@ -67,5 +85,5 @@ | ||
## Licence | ||
## License | ||
Copyright (c) 2010 Chris O'Hara <cohara87@gmail.com>, MIT License | ||
var express = require('express'), | ||
expressValidator = require('express-validator'), | ||
expressValidator = require('../index.js'), | ||
app = express.createServer(); | ||
@@ -8,3 +8,3 @@ | ||
app.post('/:foo', function(req, res) { | ||
app.post('/:urlparam', function(req, res) { | ||
var errors = []; | ||
@@ -15,9 +15,6 @@ req.onValidationError(function(msg) { | ||
}); | ||
console.log(req.params.hasOwnProperty('foo')); | ||
req.mixinParams(); | ||
req.assert('postparam', 'Invalid postparam').isInt(); | ||
req.assert('getparam', 'Invalid getparam').isInt(); | ||
req.assert('foo', 'Invalid foo').isAlpha(); | ||
req.assert('urlparam', 'Invalid urlparam').isAlpha(); | ||
@@ -30,5 +27,9 @@ req.sanitize('postparam').toBoolean(); | ||
} | ||
res.json(req.params); | ||
res.json({ | ||
urlparam: req.param('urlparam'), | ||
getparam: req.param('getparam'), | ||
postparam: req.param('postparam') | ||
}); | ||
}); | ||
app.listen(8888); |
6110
83
99