diff options
Diffstat (limited to 'web/views/changeset_view.ex')
-rw-r--r-- | web/views/changeset_view.ex | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/web/views/changeset_view.ex b/web/views/changeset_view.ex new file mode 100644 index 0000000..767756e --- /dev/null +++ b/web/views/changeset_view.ex @@ -0,0 +1,19 @@ +defmodule TheTranscriberBackend.ChangesetView do + use TheTranscriberBackend.Web, :view + + @doc """ + Traverses and translates changeset errors. + + See `Ecto.Changeset.traverse_errors/2` and + `TheTranscriberBackend.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 |