ajv-errors

Custom error messages in JSON-Schema for Ajv validator

Build Status npm version Coverage Status Gitter

Contents

Install

npm install ajv-errors

Usage

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 false and "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.errors property of generated error). If an error was matched and included in the error generated by errorMessage keyword it will have property emUsed: true.

  • singleError: create one error for all keywords used in errorMessage keyword (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 message

    • true: create single error, messages are concatenated using "; "

    • non-empty string: this string is used as a separator to concatenate messages

Supporters

Roger Kepler

License

MIT

Last updated

Was this helpful?