View Source Drops.Validator.Messages.Backend behaviour (drops v0.1.0)

Messages Backends are used to generate error messages from error results.

Examples

iex> defmodule MyBackend do
...>   use Drops.Validator.Messages.Backend
...>
...>   def text(:type?, type, input) do
...>     "#{inspect(input)} received but it must be a #{type}"
...>   end
...>
...>   def text(:filled?, _input) do
...>     "cannot be empty"
...>   end
...> end
iex> defmodule UserContract do
...>   use Drops.Contract, message_backend: MyBackend
...>
...>   schema do
...>     %{
...>       required(:name) => string(:filled?),
...>       required(:email) => string(:filled?)
...>     }
...>   end
...> end
iex> UserContract.conform(%{name: "", email: 312})
{:error,
  [
    %Drops.Validator.Messages.Error.Type{
      path: [:email],
      text: "312 received but it must be a string",
      meta: %{args: [:string, 312], predicate: :type?}
    },
    %Drops.Validator.Messages.Error.Type{
      path: [:name],
      text: "cannot be empty",
      meta: %{args: [""], predicate: :filled?}
    }
  ]
}

Summary

Callbacks

@callback text(atom(), any()) :: String.t()
@callback text(atom(), any(), any()) :: String.t()