LearnBoost/mongoose — Issue #718: Add multiple validators to a Schema field
Author: diogogmt
Created: 2012-02-06 03:57:39+00:00
State: open

I'm not sure if there is already way to add more than one validator to a field.

I'm working on a project and I wanted to add multiple validators to a field, so for example:

Instead of having:
'username': [func, 'error msg']

I wanted something like:
'username': [func1, 'error msg'], [func2, 'error msg2']

I came up with this solution:

```javascript
User = new Schema({
// 'id': Number,
'username': {
type: String,
validate: [validator1, 'validator1'],
},
'email': {
type: String,
validate: [
{
'func': validator1,
'error': 'validator1 error'
},
{
'func': validator2,
'error': 'validator2 error'
},
],
},
});
```

I made some changes to the validate method to accept an object of validators:
https://github.com/diogogmt/mongoose/commit/23426c838253b609ce3a16324f98bd944ad267a3
I tested the changes and apparently is working

I wasn't sure how to name the properties of the validator object, so I just gave 'func' for the function to be called when validating and 'error' for the error message