voughtdq

voughtdq

Where should per-application validations go?

I have several backend applications that use their own changesets. In the frontend web application, I want to tie the interaction with these backends together to provide a better user experience. In other words, I want to merge changeset errors that the backend applications return and display those to the user.

The idea would be to use the changeset functions for each backend concern:

  def enroll(params) do
    enrollment = Portal.Enrollment.changeset(%Portal.Enrollment{}, params) # used for rendering the form

    user_changeset = Identity.User.changeset(%Identity.User{}, params)
    contact_changeset = Accounts.Contact.changeset(%Accounts.Contact{}, params)
    contact_point_changeset = Account.ContactPoint.changeset(%Accounts.ContactPoint, params)

    enrollment_changeset =
      Enum.reduce([user_changeset, contact_changeset, contact_point_changeset],
        enrollment,
        fn changeset, acc ->
          case Ecto.Changeset.apply_action(changeset, :insert) do
            {:ok, _data} ->
              acc

            {:error, changeset} ->
              %{acc | errors: acc.errors ++ changeset.errors}
          end
        end)

    ...
  end

%Portal.Enrollment{} is an embedded ecto schema used for rendering the forms on the web frontend.

What do you think of this approach? I guess one criticism would be that I should be more defensive and that these validations would be a better fit closer to the user. However, my concern is incongruity between the backend’s validations and the frontend’s. Do you a different way of managing this?

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement