-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (40 loc) · 1.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Dmitry Petrov http://corpix.ru
* http://dream-web.ru
*/
var Validator = require('validator').Validator;
var v = new Validator();
v.error = function(msg) {
var e = new Error(msg);
e.type = 'validationError';
throw e;
}
module.exports = function(req, res, next) {
req.getParam = function(param) {
return this.params[param] || (this.query && this.query[param]) || (this.body ? this.body[param] : new Error(['express-validator: Param', param, 'not found'].join(' ')));
}
req.onValidationError = function(message) {
throw new Error(message)
return false
}
req.check = function(param, message, noSearch) {
if(!noSearch) {
param = this.getParam(param)
if(param instanceof Error) throw param
}
var result = v.check(param, message)
if(!result) {
req.onValidationError(['express-validator:', (noSearch ? false : param) || message]);
return false
}
return result
}
req.filter = req.sanitize = function(param, noSearch) { // TODO: req.filter('var').xss() instead of req.params.var = req.filter('var').xss()
if(!noSearch) {
param = this.getParam(param)
if(param instanceof Error) throw param
}
return v.sanitize(param)
}
return next()
}