express-validator
Advanced tools
Comparing version 2.4.0 to 2.5.0
@@ -141,2 +141,14 @@ /* | ||
methods['optional'] = function() { | ||
if (value !== undefined) { | ||
return methods; | ||
} | ||
var dummyMethods = []; | ||
for (var methodName in methods) { | ||
dummyMethods[methodName] = function() { return dummyMethods; }; | ||
} | ||
return dummyMethods; | ||
}; | ||
return methods; | ||
@@ -143,0 +155,0 @@ } |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"homepage": "https://github.com/ctavan/express-validator", | ||
@@ -14,0 +14,0 @@ "repository": { |
@@ -105,2 +105,28 @@ # express-validator | ||
####`customValidators` | ||
_{ "validatorName": function(value, [additional arguments]), ... }_ | ||
The `customValidators` option can be used to add additional validation methods as needed. This option should be an `Object` defining the validator names and associated validation functions. | ||
Define your custom validators: | ||
```javascript | ||
app.use(expressValidator({ | ||
customValidators: { | ||
isArray: function(value) { | ||
return Array.isArray(value); | ||
}, | ||
gte: function(param, num) { | ||
return param >= num; | ||
} | ||
} | ||
})); | ||
``` | ||
Use them with their validator name: | ||
```javascript | ||
req.checkBody('users', 'Users must be an array').isArray(); | ||
req.checkQuery('time', 'Time must be an integer great than or equal to 5').isInt().gte(5) | ||
``` | ||
### Validation errors | ||
@@ -146,2 +172,11 @@ | ||
### Optional input | ||
You can use the `optional()` method to check an input only when the input exists. | ||
```javascript | ||
req.checkBody('email').optional().isEmail(); | ||
//if there is no error, req.body.email is either undefined or a valid mail. | ||
``` | ||
### Nested input data | ||
@@ -197,10 +232,4 @@ | ||
You can add your own validators using `expressValidator.validator.extend(name, fn)` | ||
You can add your own validators using the `customValidators` option. See [Middleware Options](#middleware-options) for usage details. | ||
```javascript | ||
expressValidator.validator.extend('isFinite', function (str) { | ||
return isFinite(str); | ||
}); | ||
``` | ||
## Changelog | ||
@@ -207,0 +236,0 @@ |
31602
18
676
286
8