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
+53Lines changed: 53 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ Custom error messages in JSON-Schema for Ajv validator
14
14
-[Single message](#single-message)
15
15
-[Messages for keywords](#messages-for-keywords)
16
16
-[Messages for properties and items](#messages-for-properties-and-items)
17
+
-[Default message](#default-message)
17
18
-[Templates](templates)
18
19
-[Options](options)
19
20
-[License](license)
@@ -207,6 +208,58 @@ Processed errors:
207
208
```
208
209
209
210
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
+
210
263
## Templates
211
264
212
265
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