ajv-errors
Custom error messages in JSON-Schema for Ajv validator
Contents
Install
npm install ajv-errorsUsage
Add the keyword errorMessages to Ajv instance:
See Options below.
Single message
Replace all errors in the current schema and subschemas with a single message:
Processed errors:
Messages for keywords
Replace errors for certain keywords in the current schema only:
Processed errors:
For keywords "required" and "dependencies" it is possible to specify different messages for different properties:
Messages for properties and items
Replace errors for properties / items (and deeper), regardless where in schema they were created:
Processed errors:
Default message
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:
Processed errors:
The message in property _ of errorMessage replaces the same errors that would have been replaced if errorMessage were a string.
Templates
Custom error messages used in errorMessage keyword can be templates using JSON-pointers or relative JSON-pointers to data being validated, in which case the value will be interpolated. Also see examples of relative JSON-pointers.
The syntax to interpolate a value is ${<pointer>}.
The values used in messages will be JSON-stringified:
to differentiate between
falseand"false", etc.to support structured values.
Example:
Options
Defaults:
keepErrors: keep original errors. Default is to remove matched errors (they will still be available in
params.errorsproperty of generated error). If an error was matched and included in the error generated byerrorMessagekeyword it will have propertyemUsed: true.singleError: create one error for all keywords used in
errorMessagekeyword (error messages defined for properties and items are not merged because they have different dataPaths). Multiple error messages are concatenated. Option values:false(default): create multiple errors, one for each messagetrue: create single error, messages are concatenated using"; "non-empty string: this string is used as a separator to concatenate messages
Supporters
License
Last updated
Was this helpful?
