Files
firezone/elixir/apps/api/lib/api/controllers/changeset_json.ex
Brian Manifold 79c815fbbc feat(portal): Add REST API (#5579)
Why:

* In order to manage a large number of Firezone Sites, Resources,
Policies, etc... a REST API is needed as clicking through the UI is too
time consuming, as well as prone to error. By providing a REST API
Firezone customers will be able to manage things within their Firezone
accounts with code.
2024-07-20 04:20:43 +00:00

17 lines
454 B
Elixir

defmodule API.ChangesetJSON do
def error(%{status: status, changeset: changeset}) do
%{
error: %{
reason: Plug.Conn.Status.reason_phrase(status),
validation_errors: Ecto.Changeset.traverse_errors(changeset, &translate_error/1)
}
}
end
defp translate_error({msg, opts}) do
Enum.reduce(opts, msg, fn {key, value}, acc ->
String.replace(acc, "%{#{key}}", fn _ -> to_string(value) end)
end)
end
end