Skip to content

Commit 34e7571

Browse files
author
Lee Powell
committed
1.1.1 update to better document the options.type property. Also amended message template variable to remain similar to mongoose's built-in's
1 parent 66a13e7 commit 34e7571

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var nameValidator = [
3636
validate({
3737
validator: 'isLength',
3838
arguments: [3, 50],
39-
message: 'Name should be between {args.0} and {args.1} characters' // Argument interpolation
39+
message: 'Name should be between {ARGS[0]} and {ARGS[1]} characters'
4040
}),
4141
validate({
4242
validator: 'isAlphanumeric',
@@ -64,15 +64,21 @@ Arguments to be passed to the validator. These can either be an array of argumen
6464
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.
6565

6666
### option.message - optional
67-
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.
67+
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.
6868

6969
```javascript
7070
validate({
7171
validator: 'isLength',
7272
arguments: [3, 50],
73-
message: 'Name should be between {args.0} and {args.1} characters'
73+
message: 'Name should be between {ARGS[0]} and {ARGS[1]} characters'
7474
}),
75+
76+
// On error produces: Name should be between 3 and 50 characters
7577
```
78+
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)
79+
80+
### option.type - optional
81+
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)
7682

7783
## Regular Expressions
7884

@@ -94,7 +100,7 @@ validate({
94100
});
95101
```
96102

97-
## <a name="custom-validators"></a>Custom validators
103+
## Custom validators
98104

99105
Custom validators can also be added - these are then added to the validator.js object.
100106
**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.

lib/mongoose-validator.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ function validate (options) {
3838
args = !Array.isArray(args) ? [args] : args;
3939

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

4346
if (validator) {
4447
return {

package.json

Lines changed: 1 addition & 1 deletion
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.0",
4+
"version": "1.1.1",
55
"author": {
66
"name": "Lee Powell",
77
"email": "[email protected]"

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ describe('Mongoose Validator', function() {
158158
});
159159

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

163163
should.exist(doc);
164164

0 commit comments

Comments
 (0)