Error formatting helpers.
Errors are grouped by:
- Instance location: the bit of data that was invalidated
- Schema location: the part of the schema that invalidated it
- Evaluation path: the path followed from the root to this schema location
Error levels
Each error carries a level, an integer describing how much information the
error message adds on its own. Consumers that only want actionable messages
can pass :min_error_level to normalize_error/2 instead of blacklisting
keywords.
The levels used by the vocabularies shipped with this library are:
| Level | Meaning | Example |
|---|---|---|
5 | The message only points at a deeper error. | property 'name' did not conform to the property schema |
10 | The error states why the data was rejected. | value is not of type string |
Vocabularies set the level of an error with an integer :level in the error
args. Errors are assigned 10 otherwise. :min_error_level is
first applied to that level, then to the :level key that
JSV.Vocabulary.format_error/3 can return, so both levels must reach it for
the error to be kept.
Custom vocabularies can use any integer, including values between or below the
ones listed above.
Summary
Functions
Formats a data path into a JSON pointer string prefixed with #.
Returns 10, the level given to errors that state why the data
was rejected.
Returns 10.
Returns 5, the level given to errors whose message only
points at a deeper error.
Returns 8.
Returns a JSON-able version of the errors contained in the ValidationError.
Returns an output unit with valid: true for the given
JSV.Validator. This can be substitued to an Error struct in the
nested details of an error. Mostly used to show multiple validated schemas
with :oneOf.
Types
@type error_unit() :: %{ :valid => boolean(), :instanceLocation => binary(), :evaluationPath => binary(), :schemaLocation => binary(), optional(:errors) => [keyword_error()] }
@type keyword_error() :: %{ :kind => atom(), :message => String.t(), optional(:ctx) => map(), optional(:details) => [error_unit()] }
@type level() :: integer()
@type normalize_opt() :: {:sort, :asc | :desc} | {:keys, :atoms | :strings} | {:min_error_level, level()} | {:max_errors, pos_integer() | :infinity}
Functions
Formats a data path into a JSON pointer string prefixed with #.
The path is given in reverse order, as stored in errors and validation
contexts. For instance, a path pointing to the first element of a "users"
array is given as [0, "users"] and formatted as "#/users/0".
@spec level_cause() :: level()
Returns 10, the level given to errors that state why the data
was rejected.
Errors are assigned this level when JSV.Vocabulary.format_error/3 does not
return a :level key.
@spec level_default() :: level()
Returns 10.
@spec level_intermediary() :: level()
Returns 5, the level given to errors whose message only
points at a deeper error.
@spec level_parent_reported() :: level()
Returns 8.
@spec normalize_error(JSV.ValidationError.t(), keyword()) :: map()
Returns a JSON-able version of the errors contained in the ValidationError.
This is generatlly useful to generate HTTP API responses or message broker responses.
Options
:sort(:asc | :desc) - Controls the sort direction. Errors are sorted byinstanceLocation. The default value is:desc.:keys(:atoms | :strings) - Define the type of the keys in the normalized errors maps.While truly "normalized" JSON data should not have atom keys, this option defaults to :atoms for backward compatibility reasons.
:min_error_level(integer) - Drops the errors whose level is below the given value, as well as the error units left without any error. See the "Error levels" section inJSV.ErrorFormatter. The default value is0, which keeps all errors defined by this library.Passing
10drops the errors that only point at a deeper error.:max_errors(positive integer or:infinity) - Only formats the errors of the first invalid locations of the data. Each branch ofanyOf,oneOf,allOfandifkeeps up to that number of locations of its own. SeeJSV.ValidationError.prune_errors/2. The default value is:infinity.
@spec valid_annot(JSV.Validator.validator(), JSV.Validator.context()) :: error_unit()
Returns an output unit with valid: true for the given
JSV.Validator. This can be substitued to an Error struct in the
nested details of an error. Mostly used to show multiple validated schemas
with :oneOf.