You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+10-4Lines changed: 10 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ var nameValidator = [
36
36
validate({
37
37
validator:'isLength',
38
38
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'
40
40
}),
41
41
validate({
42
42
validator:'isAlphanumeric',
@@ -64,15 +64,21 @@ Arguments to be passed to the validator. These can either be an array of argumen
64
64
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.
65
65
66
66
### 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.
68
68
69
69
```javascript
70
70
validate({
71
71
validator:'isLength',
72
72
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'
74
74
}),
75
+
76
+
// On error produces: Name should be between 3 and 50 characters
75
77
```
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)
Custom validators can also be added - these are then added to the validator.js object.
100
106
**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.
0 commit comments