Skip to content

Commit

Permalink
1.1.1 update to better document the options.type property. Also amend…
Browse files Browse the repository at this point in the history
…ed message template variable to remain similar to mongoose's built-in's
  • Loading branch information
Lee Powell committed Jun 9, 2015
1 parent 66a13e7 commit 34e7571
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var nameValidator = [
validate({
validator: 'isLength',
arguments: [3, 50],
message: 'Name should be between {args.0} and {args.1} characters' // Argument interpolation
message: 'Name should be between {ARGS[0]} and {ARGS[1]} characters'
}),
validate({
validator: 'isAlphanumeric',
Expand Down Expand Up @@ -64,15 +64,21 @@ Arguments to be passed to the validator. These can either be an array of argumen
Some of the validator.js validators require a value to check against (isEmail, isUrl etc). There may be instances where you don't have a value to check i.e. a path that is not required and as such these few validators return an false value causing validation to fail. This can now be bypassed by setting the `passIfEmpty` option.

### option.message - optional
Set the error message to be used should the validator fail. If no error message is set then mongoose-validator will attempt to use one of the built-in default messages, if it can't then a simple message of 'Error' will be returned. You can pass `{args.[argument index position]}` for crude argument interpolation. Note: Use `{args.0}` if your arguments isn't an array.
Set the error message to be used should the validator fail. If no error message is set then mongoose-validator will attempt to use one of the built-in default messages, if it can't then a simple message of 'Error' will be returned. Enhanced message templating is supported by giving the ability to use the validator arguments. You can use these like `{ARGS[argument index position]}`. Note: Use `{ARGS[0]}` if your arguments isn't an array.

```javascript
validate({
validator: 'isLength',
arguments: [3, 50],
message: 'Name should be between {args.0} and {args.1} characters'
message: 'Name should be between {ARGS[0]} and {ARGS[1]} characters'
}),

// On error produces: Name should be between 3 and 50 characters
```
The built in Mongoose message template variables still work as expected. You can find out more about those here: [http://mongoosejs.com/docs/api.html#error_messages_MongooseError-messages](http://mongoosejs.com/docs/api.html#error_messages_MongooseError-messages)

### 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)

## Regular Expressions

Expand All @@ -94,7 +100,7 @@ validate({
});
```

## <a name="custom-validators"></a>Custom validators
## Custom validators

Custom validators can also be added - these are then added to the validator.js object.
**NOTE**: Validator.js converts all values to strings internally for built-in validators - however custom validators do *not* do this. This allows you to create custom validators for checking all types such as arrays and objects.
Expand Down
5 changes: 4 additions & 1 deletion lib/mongoose-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ function validate (options) {
args = !Array.isArray(args) ? [args] : args;

// Interpolate message with argument values
message = message.replace(/{args\.(\d+)}/g, function(replace, argIndex) {return args[argIndex]});
message = message.replace(/{ARGS\[(\d+)\]}/g, function (replace, argIndex) {
var val = args[argIndex];
return val !== undefined ? val : '';
});

if (validator) {
return {
Expand Down
2 changes: 1 addition & 1 deletion 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.0",
"version": "1.1.1",
"author": {
"name": "Lee Powell",
"email": "[email protected]"
Expand Down
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe('Mongoose Validator', function() {
});

it('Should replace args on custom error message', function (done) {
schema.path('name').validate(validate({ validator: 'isLength', arguments: [5, 10], message: 'At least {args.0} and less than {args.1}' }));
schema.path('name').validate(validate({ validator: 'isLength', arguments: [5, 10], message: 'At least {ARGS[0]} and less than {ARGS[1]}' }));

should.exist(doc);

Expand Down

0 comments on commit 34e7571

Please sign in to comment.