Skip to content

Commit

Permalink
Updates to support mongoose extended properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Powell committed Jun 12, 2015
1 parent 34e7571 commit 2ca749c
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 280 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ The built in Mongoose message template variables still work as expected. You can
### option.type - optional
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)

### Extending the error properties (mongoose version >= 3.9.7)

Any additional members added to the options object will be available in the 'err.properties' field of the mongoose validation error.

```javascript
var alphaValidator = validate({
validator: 'isAlphanumeric',
passIfEmpty: true,
message: 'Name should contain alpha-numeric characters only',
httpStatus: 400
});
```
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)

## Regular Expressions

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:
Expand Down Expand Up @@ -161,4 +175,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 changes: 5 additions & 5 deletions lib/mongoose-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

var validatorjs = require('validator');
var defaultErrorMessages = require('./default-errors');
var _ = require('underscore');

var errorMessages = {};

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

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

if (validator) {
return {
return _.extend({
validator: function(val, next) {
var validatorArgs = [val].concat(args);

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

return next(validator.apply(null, validatorArgs));
},
message: message,
type: type
};
message: message
}, extend);
}

throw new Error('Validator `' + name + '` does not exist on validator.js');
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mongoose-validator",
"description": "Validators for mongoose models utilising validator.js",
"version": "1.1.1",
"version": "1.2.0",
"author": {
"name": "Lee Powell",
"email": "[email protected]"
Expand Down Expand Up @@ -30,15 +30,20 @@
{
"name": "Eric Saboia",
"url": "https://github.com/ericsaboia"
},
{
"name": "Rob Rodriguez",
"url": "https://github.com/rodriguise"
}
],
"dependencies": {
"underscore": "^1.8.3",
"validator": "^3.18.0"
},
"devDependencies": {
"mongoose": "^4.0.5",
"mocha": "^2.2.5",
"should": "^3.3.1"
"should": "^6.0.3"
},
"keywords": [
"mongoose",
Expand Down
Loading

0 comments on commit 2ca749c

Please sign in to comment.