Zoi.Context (Zoi v0.5.5)

View Source

The Context provides the parsing information such as the input data, parsed data and errors.

The context is passed around during the parsing process to keep track of the current state of parsing. It contains the schema being parsed, the input data, the parsed data, the path of the current error and any errors that have occurred during parsing.

Summary

Functions

Add a error to the context.

Types

error()

@type error() :: Zoi.Error.t() | binary() | [Zoi.Error.t()]

t()

@type t() :: %Zoi.Context{
  errors: [Zoi.Error.t()],
  input: Zoi.input(),
  parsed: Zoi.input(),
  path: Zoi.Error.path(),
  schema: Zoi.Type.t()
}

Functions

add_error(context, error)

@spec add_error(t(), error()) :: t()

Add a error to the context.

Example

iex> schema = Zoi.string() |> Zoi.refine(fn input, ctx ->
...>   if String.length(input) > 5 do
...>     :ok
...>   else
...>     Zoi.Context.add_error(ctx, %Zoi.Error{message: "Input too long"})
...>   end
...> end)
...> Zoi.parse(schema, "s")
{:error, [%Zoi.Error{message: "Input too long"}]}