JSV.ErrorFormatter (jsv v0.28.1)

Copy Markdown View Source

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:

LevelMeaningExample
5The message only points at a deeper error.property 'name' did not conform to the property schema
10The 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.

level_default() deprecated

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

error_unit()

@type error_unit() :: %{
  :valid => boolean(),
  :instanceLocation => binary(),
  :evaluationPath => binary(),
  :schemaLocation => binary(),
  optional(:errors) => [keyword_error()]
}

keyword_error()

@type keyword_error() :: %{
  :kind => atom(),
  :message => String.t(),
  optional(:ctx) => map(),
  optional(:details) => [error_unit()]
}

level()

@type level() :: integer()

normalize_opt()

@type normalize_opt() ::
  {:sort, :asc | :desc}
  | {:keys, :atoms | :strings}
  | {:min_error_level, level()}
  | {:max_errors, pos_integer() | :infinity}

raw_path()

@type raw_path() :: [raw_path()] | binary() | integer() | atom()

Functions

format_data_path(rev_data_path)

@spec format_data_path(raw_path()) :: String.t()

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".

level_cause()

@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.

level_default()

This function is deprecated. Use `level_cause/0` instead..
@spec level_default() :: level()

Returns 10.

level_intermediary()

@spec level_intermediary() :: level()

Returns 5, the level given to errors whose message only points at a deeper error.

level_parent_reported()

This function is deprecated. Errors of the built-in vocabularies no longer use this level..
@spec level_parent_reported() :: level()

Returns 8.

normalize_error(e, opts \\ [])

@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 by instanceLocation. 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 in JSV.ErrorFormatter. The default value is 0, which keeps all errors defined by this library.

    Passing 10 drops 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 of anyOf, oneOf, allOf and if keeps up to that number of locations of its own. See JSV.ValidationError.prune_errors/2. The default value is :infinity.

valid_annot(subschema, vctx)

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.