Ecto Changeset validate map values

I am in need to use
https://hexdocs.pm/ecto/Ecto.Changeset.html#validate_inclusion/4
The issue I have is that I need to validate a value, which is stored inside a Map (used for Postgres’ JSONB column) while the docs say:

validate_inclusion(t(), atom(), Enum.t(), Keyword.t()) :: t()

meaning the function expects field to be an atom. I can of course specify appropriate column name as an atom but how does one get the value under specific key validated?

Ecto doesn’t really ship validations for the :map type. You’d likely need to use validate_change and do your own validation logic or use embeds instead of :map.

1 Like