File size: 942 Bytes
8da2481 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | defmodule Plausible.ChangesetHelpers do
@moduledoc "Helper function for working with Ecto changesets"
def traverse_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
Regex.replace(~r"%{(\w+)}", msg, fn _, key ->
opts
|> Keyword.get(String.to_existing_atom(key), key)
|> to_string()
end)
end)
end
@doc """
iex> serialize_first_error([{"name", {"should be at most %{count} byte(s)", [count: 255]}}])
"name should be at most 255 byte(s)"
"""
def serialize_first_error(errors) do
{field, {message, opts}} = List.first(errors)
formatted_message =
Enum.reduce(opts, message, fn {key, value}, acc ->
placeholder = "%{#{key}}"
if String.contains?(acc, placeholder) do
String.replace(acc, placeholder, to_string(value))
else
acc
end
end)
"#{field} #{formatted_message}"
end
end
|