index.community/backend/lib/backend_web/views/changeset_view.ex

20 lines
558 B
Elixir
Raw Permalink Normal View History

2019-07-14 11:47:06 +00:00
defmodule BackendWeb.ChangesetView do
use BackendWeb, :view
@doc """
Traverses and translates changeset errors.
See `Ecto.Changeset.traverse_errors/2` and
`BackendWeb.ErrorHelpers.translate_error/1` for more details.
"""
def translate_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, &translate_error/1)
end
def render("error.json", %{changeset: changeset}) do
# When encoded, the changeset returns its errors
# as a JSON object. So we just pass it forward.
%{errors: translate_errors(changeset)}
end
end