Skip to content

Commit 2ca749c

Browse files
author
Lee Powell
committed
Updates to support mongoose extended properties
1 parent 34e7571 commit 2ca749c

File tree

4 files changed

+272
-280
lines changed

4 files changed

+272
-280
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ The built in Mongoose message template variables still work as expected. You can
8080
### option.type - optional
8181
Set the type of validator type. If this is not defined, Mongoose will set this for you. Read more about this here: [http://mongoosejs.com/docs/api.html#schematype_SchemaType-validate](http://mongoosejs.com/docs/api.html#schematype_SchemaType-validate)
8282

83+
### Extending the error properties (mongoose version >= 3.9.7)
84+
85+
Any additional members added to the options object will be available in the 'err.properties' field of the mongoose validation error.
86+
87+
```javascript
88+
var alphaValidator = validate({
89+
validator: 'isAlphanumeric',
90+
passIfEmpty: true,
91+
message: 'Name should contain alpha-numeric characters only',
92+
httpStatus: 400
93+
});
94+
```
95+
In this example the error object returned by mongoose will have its 'properties' extended with httpStatus should validation fail. More details can be found about this here: [http://thecodebarbarian.com/2014/12/19/mongoose-397](http://thecodebarbarian.com/2014/12/19/mongoose-397)
96+
8397
## Regular Expressions
8498

8599
Mongoose Validator can use the validator.js `matches` method, however, it's worth noting that the regex can be passed in 2 ways - as per the validator.js documentation, firstly they can be passed as a literal:
@@ -161,4 +175,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
161175
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
162176
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
163177
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
164-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
178+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

lib/mongoose-validator.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
var validatorjs = require('validator');
99
var defaultErrorMessages = require('./default-errors');
10+
var _ = require('underscore');
1011

1112
var errorMessages = {};
1213

@@ -31,8 +32,8 @@ function validate (options) {
3132
var args = options.arguments || [];
3233
var passIfEmpty = options.passIfEmpty !== undefined ? options.passIfEmpty : false;
3334
var message = options.message || errorMessages[name] || defaultErrorMessages[name] || 'Error';
34-
var type = options.type;
3535
var validator = (name instanceof Function) ? name : validatorjs[name];
36+
var extend = _.omit(options, 'passIfEmpty', 'message', 'validator', 'arguments');
3637

3738
// Coerse args into an array
3839
args = !Array.isArray(args) ? [args] : args;
@@ -44,7 +45,7 @@ function validate (options) {
4445
});
4546

4647
if (validator) {
47-
return {
48+
return _.extend({
4849
validator: function(val, next) {
4950
var validatorArgs = [val].concat(args);
5051

@@ -54,9 +55,8 @@ function validate (options) {
5455

5556
return next(validator.apply(null, validatorArgs));
5657
},
57-
message: message,
58-
type: type
59-
};
58+
message: message
59+
}, extend);
6060
}
6161

6262
throw new Error('Validator `' + name + '` does not exist on validator.js');

package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mongoose-validator",
33
"description": "Validators for mongoose models utilising validator.js",
4-
"version": "1.1.1",
4+
"version": "1.2.0",
55
"author": {
66
"name": "Lee Powell",
77
"email": "[email protected]"
@@ -30,15 +30,20 @@
3030
{
3131
"name": "Eric Saboia",
3232
"url": "https://github.com/ericsaboia"
33+
},
34+
{
35+
"name": "Rob Rodriguez",
36+
"url": "https://github.com/rodriguise"
3337
}
3438
],
3539
"dependencies": {
40+
"underscore": "^1.8.3",
3641
"validator": "^3.18.0"
3742
},
3843
"devDependencies": {
3944
"mongoose": "^4.0.5",
4045
"mocha": "^2.2.5",
41-
"should": "^3.3.1"
46+
"should": "^6.0.3"
4247
},
4348
"keywords": [
4449
"mongoose",

0 commit comments

Comments
 (0)