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.






















