View Source JsonXema.ValidationError exception (json_xema v0.6.2)

Raised when a validation fails.

Example

iex> schema = JsonXema.new(%{"type" => "string"})
iex> {:error, error} = JsonXema.validate(schema, 6)
iex> Exception.message(error)
~s|Expected "string", got 6.|

Summary

Functions

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

Traverse the error tree and invokes the given function.

Types

@type opts() :: [] | [{:path, path()}]
@type path() :: [atom() | integer() | String.t()]

Functions

@spec format_error({:error, map()} | map()) :: String.t()

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

Example

iex> schema = JsonXema.new(%{"type" => "integer"})
iex> schema
...>   |> JsonXema.validate(1.1)
...>   |> JsonXema.ValidationError.format_error()
~s|Expected "integer", got 1.1.|
Link to this function

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

View Source
@spec 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 = JsonXema.new(%{
...>   "properties" => %{
...>     "int" => %{"type" => "integer"},
...>     "names" => %{
...>       "type" => "array",
...>       "items" => %{"type" => "string"}
...>     },
...>     "num" => %{"anyOf" => [
...>       %{"type" => "integer"},
...>       %{"type" => "number"}
...>     ]}
...>   }
...> })
iex>
iex> data = %{"int" => "x", "names" => [1, "x", 5], "num" => :foo}
iex>
iex> schema
...>   |> JsonXema.validate(data)
...>   |> JsonXema.ValidationError.travers_errors([], fun)
[
  ~s|Error at ["num"]|,
  ~s|Error at ["names", 2]|,
  ~s|Error at ["names", 0]|,
  ~s|Error at ["names"]|,
  ~s|Error at ["int"]|,
  ~s|Error at []|
]