cevado

cevado

Proposal: Allow nested schemaless changeset with Ecto

Being able to build nested relations in a schemaless changeset would be helpful to deal with complex forms on Phoenix without the need to create either an intermediary embed schema to deal with complex forms or constraint the form to express only the db ecto schema.
Also it allows to use the cast/validation tools of ecto with complex data without the need to create a embed schema.

I think this could be achieved with cast_assoc/3 work with schemaless changeset for example:

defmodule Test do
  import Ecto.Changeset
  def nested(params) do
    {%{field1: %{a: 1}}, %{field1: {:map, :a}, field2: {:list, :b}}
    |> cast(params, [])
    |> cast_assoc(:field1, with: &function/2)
    |> cast_assoc(:field2, with: &function/2)
  end
  
  defp function({:a, data}, params) do
    {data, %{a: :integer}}
    |> cast(params, [:a])
  end
  
  defp function({:b, data}, params) do
    {data, %{b: :string}}
    |> cast(params, [:b])
  end
end

iex> Test.nested(%{field1: %{a: 5, b: 2}, field2: [%{a: 1, b: "thing"}]})
%{field1: %{a: 5}, field2: [%{b: "thing"}]}

Constraints to casting assoc on a schemaless changeset: it always have to include a with: option.
In this way put_assoc/4 would also require the with: option and have the same behavior of overriding whatever value that was present in original params of the parent assoc.

Where Next?

Popular in Proposals: Ideas Top

beepboop
(re-post of: Feature idea: measure and expose socket latency · Issue #1890 · phoenixframework/phoenix_live_view · GitHub) This is relate...
New
dorgan
Currently the Phoenix.LiveView.JS struct is defined as an @opaque type, meaning it’s fields are internal api not to be relied upon. I ha...
New
AHBruns
Rethinking phx-no-feedback So, I won’t go into how phx-no-feedback works in detail, but the tl;dr is that it lets you conditionally add a...
New
sevensidedmarble
All the pieces are there to execute arbitrary JS commands from the server. You can put them on a data property and call them from the cli...
New
7rans
I implemented Access behavior for a struct today. Pseudo-code… defmodule MyStruct do defstruct data: %{} @behaviour Access # ... ...
New
andreamancuso
Hey folks, This might sound niche, but I think it’s worth bringing up - especially given Phoenix’s reputation for being lightweight, por...
New
aglassman
Problem The cancel_async function is easily overlooked. Since the results of “outdated” tasks are ignored, it’s easy for developers to a...
New

Other popular topics Top

dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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