Initial commit on Forgejo
Fresh repository history for elektrine/tarakan hosted at https://git.elektrine.com/elektrine/tarakan.
This commit is contained in:
commit
af6077b9c3
455 changed files with 78366 additions and 0 deletions
15
lib/tarakan_web/changeset_errors.ex
Normal file
15
lib/tarakan_web/changeset_errors.ex
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
defmodule TarakanWeb.ChangesetErrors do
|
||||
@moduledoc """
|
||||
Renders an Ecto changeset's errors into a plain, JSON-encodable map for API
|
||||
responses (field => list of interpolated messages).
|
||||
"""
|
||||
|
||||
@doc "Traverses `changeset` errors, interpolating `%{count}`-style placeholders."
|
||||
def to_map(changeset) do
|
||||
Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
|
||||
Regex.replace(~r"%{(\w+)}", message, fn _match, key ->
|
||||
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
|
||||
end)
|
||||
end)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue