`:map` type in Schemaless `Changeset.cast/3` only accepts single type
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 42/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- elixir
- Domain
- backend-api-design
Research direction
The issue points to lib/ecto/type.ex around Ecto.Type.cast_fun/1; start there and trace how Changeset.cast/3 handles {:map, type}. Compare the existing uniform-map behavior with the requested per-key types, then verify the supplied examples and invalid nested values behave as expected.
Written by the indexing model from the issue text.
Description
Elixir version
1.14.4
Database and Version
n/a
Ecto Versions
3.9.5
Database Adapter and Versions (postgrex, myxql, etc)
n/a
Current behavior
This works, and it means that all keys in the map must be strings.
types = %{foo: {:map, :string}}
cs = cast({%{}, types}, %{foo: %{bar: "hello", baz: "world"}}, Map.keys(types))
IO.inspect(cs.valid?)IO.inspect(cs.valid?)
# true
This also works to detect non-string fields within the nested map.
types = %{foo: {:map, :string}}
cs = cast({%{}, types}, %{foo: %{bar: 3}}, Map.keys(types))
IO.inspect(cs.valid?)
# false
IO.inspect(cs.errors)
# errors: [foo: {"is invalid", [type: {:map, :string}, validation: :cast]}]
However, when we try to define a specific type of the nested key, an error is raised.
types = %{foo: {:map, %{bar: :string}}}
Ecto.Changeset.cast({%{}, types}, %{foo: %{bar: "hello}}, Map.keys(types))
# ** (FunctionClauseError) no function clause matching in Ecto.Type.cast_fun/1
# The following arguments were given to Ecto.Type.cast_fun/1:
# # 1
# %{bar: :string}
It seems we are limited to uniform types exclusively.
Expected behavior
We should be able to define custom types for each key in the map. e.g:
types = %{foo: {:map, %{bar: :string, baz: :integer}}}
Ecto.Changeset.cast({%{}, types}, %{foo: %{bar: "hello", baz: 1}}, Map.keys(types))
IO.inspect(cs.valid?)
# true
Thank you
- Dominant language
- Elixir
- Stars
- 6.5k
- Forks
- 1.5k
- Avg merge
- 8h 38m
- Merged PRs (30d)
- 8
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from elixir-ecto/ecto
-
Kind:Bug
Difficulty 5/5 Over a week Newbie friendliness 25/100
elixir-ecto/ecto#4698 · 2 comments ·
Maintainers usually reply within 1 day
All issues in elixir-ecto/ecto
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
intellij-elixir/intellij-elixir#4782 ·
Maintainers usually reply within 1 day
-
level 2 wocs
Difficulty 1/5 Under an hour Newbie friendliness 90/100
phravins/QuantumBilling#51 ·
-
fix(ocr): compile the Tesseract-free pipeline with Tokio runtimePossibly taken @Goldziher claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
carverauto/serviceradar#6898 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
elixir-ecto/myxql#221 ·