Skip to content

Commit 411cfee

Browse files
committed
docs: default message when errorMessage is an object, closes #3
1 parent 95bc585 commit 411cfee

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Custom error messages in JSON-Schema for Ajv validator
1414
- [Single message](#single-message)
1515
- [Messages for keywords](#messages-for-keywords)
1616
- [Messages for properties and items](#messages-for-properties-and-items)
17+
- [Default message](#default-message)
1718
- [Templates](templates)
1819
- [Options](options)
1920
- [License](license)
@@ -207,6 +208,58 @@ Processed errors:
207208
```
208209

209210

211+
### Default message
212+
213+
When the value of keyword `errorMessage` is an object you can specify a message that will be used if any error appears that is not specified by keywords/properties/items:
214+
215+
```javascript
216+
var schema = {
217+
type: 'object',
218+
required: ['foo', 'bar'],
219+
allOf: [{
220+
properties: {
221+
foo: { type: 'integer', minimum: 2 },
222+
bar: { type: 'string', minLength: 2 }
223+
},
224+
additionalProperties: false
225+
}],
226+
errorMessage: {
227+
type: 'data should be an object',
228+
properties: {
229+
foo: 'data.foo should be integer >= 2',
230+
bar: 'data.bar should be string with length >= 2'
231+
},
232+
_: 'data should have properties "foo" and "bar" only'
233+
}
234+
};
235+
236+
var validate = ajv.compile(schema);
237+
console.log(validate({})); // false
238+
console.log(validate.errors); // processed errors
239+
```
240+
241+
Processed errors:
242+
243+
```javascript
244+
[
245+
{
246+
keyword: 'errorMessage',
247+
message: 'data should be an object with properties "foo" and "bar" only',
248+
dataPath: '',
249+
// ...
250+
params: {
251+
errors: [
252+
{ keyword: 'required' /* , ... */ },
253+
{ keyword: 'required' /* , ... */ }
254+
]
255+
},
256+
}
257+
]
258+
```
259+
260+
The message in property `_` of `errorMessage` replaces the same errors that would have been replaced if `errorMessage` were a string.
261+
262+
210263
## Templates
211264

212265
Custom error messages used in `errorMessage` keyword can be templates using [JSON-pointers](https://tools.ietf.org/html/rfc6901) or [relative JSON-pointers](http://tools.ietf.org/html/draft-luff-relative-json-pointer-00) to data being validated, in which case the value will be interpolated. Also see [examples](https://gist.github.com/geraintluff/5911303) of relative JSON-pointers.

0 commit comments

Comments
 (0)