Ecto.Changeset.traverse_errors
You're seeing just the function
traverse_errors
, go back to Ecto.Changeset module for more information.
Specs
traverse_errors( t(), (error() -> String.t()) | (t(), atom(), error() -> String.t()) ) :: %{required(atom()) => [String.t() | map()]}
Traverses changeset errors and applies the given function to error messages.
This function is particularly useful when associations and embeds are cast in the changeset as it will traverse all associations and embeds and place all errors in a series of nested maps.
A changeset is supplied along with a function to apply to each
error message as the changeset is traversed. The error message
function receives an error tuple {msg, opts}
, for example:
{"should be at least %{count} characters", [count: 3, validation: :length, min: 3]}
Examples
iex> traverse_errors(changeset, fn {msg, opts} ->
...> Enum.reduce(opts, msg, fn {key, value}, acc ->
...> String.replace(acc, "%{#{key}}", to_string(value))
...> end)
...> end)
%{title: ["should be at least 3 characters"]}
Optionally function can accept three arguments: changeset
, field
and error tuple {msg, opts}
. It is useful whenever you want to extract
validations rules from changeset.validations
to build detailed error
description.