xema v0.11.0 Xema.ValidationError exception View Source

Raised when a validation fails.

Link to this section Summary

Functions

This function returns an error message for an error or error tuple.

Traverse the error tree and invokes the given function.

Link to this section Types

Link to this type

opts()

View Source
opts() :: [] | [{:path, path()}]
Link to this type

t()

View Source
t() :: %Xema.ValidationError{
  __exception__: term(),
  message: String.t() | nil,
  reason: map()
}

Link to this section Functions

Link to this function

format_error(error)

View Source
format_error({:error, map()} | map()) :: String.t()

This function returns an error message for an error or error tuple.

Example

iex> schema = Xema.new(:integer)
iex> schema
...>   |> Xema.Validator.validate(1.1)
...>   |> Xema.ValidationError.format_error()
"Expected :integer, got 1.1."
Link to this function

travers_errors(error, acc, fun, opts \\ [])

View Source
travers_errors(
  {:error, map()} | map(),
  acc,
  (map(), path(), acc -> acc),
  opts()
) :: acc
when acc: any()

Traverse the error tree and invokes the given function.

Example

iex> fun = fn _error, path, acc ->
...>   ["Error at " <> inspect(path) | acc]
...> end
iex>
iex> schema = Xema.new(
...>   properties: %{
...>     int: :integer,
...>     names: {:list, items: :string},
...>     num: [any_of: [:integer, :float]]
...>   }
...> )
iex>
iex> data = %{int: "x", names: [1, "x", 5], num: :foo}
iex>
iex> schema
...>   |> Xema.Validator.validate(data)
...>   |> Xema.ValidationError.travers_errors([], fun)
[
  "Error at [:num]",
  "Error at [:names, 2]",
  "Error at [:names, 0]",
  "Error at [:names]",
  "Error at [:int]",
  "Error at []"
]