Josh
Josh

Reputation: 3120

How do I combine changeset errors?

I'm trying to combine changeset errors.

I have a Institution schema which belongs to a User schema. Some fields are required on each but the error response looks like:

{
    "errors": {
        "user": {
            "password_confirmation": [
                "The password confirmation does not match the password."
            ],
            "password": [
                "This field is required."
            ],
            "name": [
                "This field is required."
            ]
        },
        "institution": {
            "web_address": [
                "is required."
            ]
        },

    }
}

How do I combine these error objects into one?

My insert looks like:

 user_changeset =
      User.normal_user_changeset(%User{}, %{
        :email => Map.get(attrs, "email"),
        :password => Map.get(attrs, "password"),
        :password_confirmation => Map.get(attrs, "password_confirmation"),
        :name => Map.get(attrs, "name"),
        :postcode => Map.get(attrs, "postcode"),
        :dob => Map.get(attrs, "dob")
      })

    institution =
      %Institution{}
      |> Institution.changeset(%{
        :web_address => Map.get(attrs, "web_address"),
        :person_responsible => Map.get(attrs, "person_responsible"),
        :annual_turnover => Map.get(attrs, "annual_turnover")
      })
      |> Ecto.Changeset.put_assoc(:user, user_changeset)
      |> Repo.insert()

I'd like the error response to be:

 {
        "errors": {
                "password_confirmation": [
                    "The password confirmation does not match the password."
                ],
                "password": [
                    "This field is required."
                ],
                "name": [
                    "This field is required."
                ]
                "web_address": [
                    "is required."
                ]
        }
    }

I have this function in my fallback controller (here by default):

  def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
    conn
    |> put_status(:unprocessable_entity)
    |> render(SfiWeb.ChangesetView, "error.json", changeset: changeset)
  end

Upvotes: 2

Views: 2501

Answers (2)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

errors is just one of the values in %Ecto.Changeset{} struct.

That said, one always might modify %Ecto.Changeset{} to provide custom errors:

def fix_errors(%Ecto.Changeset{errors: errors} = ch) do
  %Ecto.Changeset{ch | errors: errors ++ [:my_custom_error]}
end

What you need is just to remap your input using the function like the one above and embed the result into your changeset chain:

institution =
  %Institution{}
  |> ...
  |> Ecto.Changeset.put_assoc(:user, user_changeset)
  |> fix_errors() # see above 
  |> Repo.insert()

Whether you have provided the valid Elixir terms for errors before and after, I could show how to perform the transformation itself. E.g.:

errors = %{
  institution: %{web_address: ["is required"]},
  user: %{
    name: ["is required."],
    password: ["is required."],
    password_confirmation: ["no match"]
  }
}

Might be flattened as:

input
|> Enum.map(fn {_, v} -> v end)
|> Enum.reduce(&Map.merge/2)
#⇒ %{
#   name: ["is required."],
#   password: ["is required."],
#   password_confirmation: ["no match"],
#   web_address: ["is required"]
# }

Upvotes: 1

Dogbert
Dogbert

Reputation: 222198

You can get the values of the errors field and merge them all using Enum.reduce/3:

map = Jason.decode! """
{
    "errors": {
        "user": {
            "password_confirmation": [
                "The password confirmation does not match the password."
            ],
            "password": [
                "This field is required."
            ],
            "name": [
                "This field is required."
            ]
        },
        "institution": {
            "web_address": [
                "is required."
            ]
        }
    }
}
"""

Map.update!(map, "errors", fn errors ->
  errors |> Map.values() |> Enum.reduce(%{}, &Map.merge/2)
end)
|> IO.inspect

Output:

%{
  "errors" => %{
    "name" => ["This field is required."],
    "password" => ["This field is required."],
    "password_confirmation" => ["The password confirmation does not match the password."],
    "web_address" => ["is required."]
  }
}

Upvotes: 3

Related Questions