bvobart

bvobart

Ecto.Changeset - How do I make validation errors in embedded schemas show up on the parent?

Hi, I’m using Ecto embedded schemas for input validation of a JSON API. Basically, I have a Plug.Router implementation that accepts JSON and casts the input data into an Elixir struct with Ecto:

  ...
  post "/validate" do
    payload = conn.body_params

    changeset = MyApp.Document.changeset(%MyApp.Document{}, payload)
    if changeset.valid? do
      document = Ecto.Changeset.apply_changes(changeset)
      IO.inspect(document)
      send_resp(conn, 200, "Payload: #{inspect(document)}\n")
    else
      IO.inspect(changeset.errors)
      send_resp(conn, 400, "Error 400: Bad Request: #{inspect(changeset.errors)}\n")
    end
  end
  ...

My current implementation of the Ecto schema for MyApp.Document is shown below.
As you can see, a Document embeds a bunch of children, which for simplicity’s sake is now just Text elements.

defmodule MyApp.Document do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key false
  embedded_schema do
    field :id, Ecto.UUID
    field :type, :string
    embeds_many :children, MyApp.Text
  end

  def changeset(document, attrs) do
    document
    |> cast(attrs, [:id, :type])
    |> cast_embed(:children)
    |> validate_required([:id, :type])
    |> validate_inclusion(:type, ["https://spec.nldoc.nl/Resource/Document"])
  end
end

defmodule MyApp.Text do
  use Ecto.Schema
  import Ecto.Changeset

  @primary_key false
  embedded_schema do
    field :id, Ecto.UUID
    field :type, :string
    field :text, :string
  end

  def changeset(text, attrs) do
    text
    |> cast(attrs, [:id, :type, :text])
    |> validate_required([:id, :type, :text])
    |> validate_inclusion(:type, ["https://spec.nldoc.nl/Resource/Text"])
  end
end

The casting in my router works in the sense that if I supply a document with an invalid id or where the type is incorrect, then changeset.errors in my router contains an error message and validation fails as expected (so 400 is returned).

However, if I have a similar problem in the any of the embedded objects, then the validation does indeed fail as expected, but changeset.errors in my router is empty.

How do I ensure that any errors originating from casting / validating embedded schemas (MyApp.Text), end up on the changeset returned by the parent (MyApp.Document)? Or how else do I ensure that changeset.errors in my router contains all validation errors, even from nested embeds?

Marked As Solved

LostKobrakai

LostKobrakai

You generally don’t do that. The errors of children are not meant to go on the key of the parent changeset.

They’re in parent_changeset.changes.children[*].errors. The children under changes are also changeset structs, where each has their own errors.

It’s not just that. It also traverses all the nested changesets within the parent and allows you to build a nested structure of all their errors. The result is likely what you want to return instead of changeset.errors.

Also Liked

bvobart

bvobart

Hi, thanks for your quick reply! Yes, I did find PolymorphicEmbed.traverse_errors/2 and I tried it (I even examined its implementation), but it can’t iterate over errors in a child changeset that doesn’t exist :sweat_smile:

I’ve created a minimal working example in a GitHub repo here just to illustrate the point a bit better:

https://github.com/bvobart/polymorphic-embed-errors-mwe

See lib/schema.ex and test/schema_test.exs to see what I mean. I’ve added some tests that show the current behaviour and the behaviour that would be desirable (and that would be more akin to what Ecto does for embeds_many).

Do you want to continue this conversation here, or should I make an issue on the PolymorphicEmbed repo and continue there?

Last Post!

bvobart

bvobart

Hi, thanks for your quick reply! Yes, I did find PolymorphicEmbed.traverse_errors/2 and I tried it (I even examined its implementation), but it can’t iterate over errors in a child changeset that doesn’t exist :sweat_smile:

I’ve created a minimal working example in a GitHub repo here just to illustrate the point a bit better:

https://github.com/bvobart/polymorphic-embed-errors-mwe

See lib/schema.ex and test/schema_test.exs to see what I mean. I’ve added some tests that show the current behaviour and the behaviour that would be desirable (and that would be more akin to what Ecto does for embeds_many).

Do you want to continue this conversation here, or should I make an issue on the PolymorphicEmbed repo and continue there?

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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

We're in Beta

About us Mission Statement