EctoMorph.deep_filter_by_schema_fields
deep_filter_by_schema_fields
, go back to EctoMorph module for more information.
Deep filters the data to only include those fields that appear in the given schema and in any of the given schema's relations.
If the schema has_one(:foo, Foo)
and data has :foo
as an key, then the value under :foo
in data will be filtered by the fields in Foo. This will happen for all casts, embeds, and virtual
fields. There is no way to determine via reflection which schema a through relation points to,
so by default they are filtered by their own schema if they are a map.
This is useful for converting a struct into a map, eliminating internal Ecto fields in the process.
Options
When filtering you can optionally choose to nillify Ecto.Association.NotLoaded structs. By default they are passed through as is, but you can nillify them like this:
deep_filter_by_schema_fields(data, MySchema, filter_not_loaded: true)
Examples
iex> deep_filter_by_schema_fields(%{a: "c", ignored: true, stuff: "nope"}, A)
%{a: "c"}
iex> data = %{relation: %Ecto.Association.NotLoaded{}}
...> deep_filter_by_schema_fields(data, A, filter_not_loaded: true)
%{relation: nil}