View Source Flint.Types.Union (Flint v0.4.0)
Union type for Ecto. Allows the field to be any of the specified types.
This is particularly useful if you want to allow one of multiple types and preserve the type of the input, so long as it adheres to the type (rather than forcing a cast).
For example, suppose you want to allow an input to be a float, or an integer, but you want to
preserve whichever type is passed (keep floats as floats and keep integers as integers), you could
use a Union
.
Options
:oneof
- Allowed types. Can be any validEcto
type (including custom types). Required.:eager
- Whether to eagerly cast the field value into the:oneof
in the order that they appear in the list. Iffalse
,Flint
will attempt to determine if the value maps to a validEcto
type before casting and will prefer that type (if it appears in:oneof
). Defaults tofalse
.
Example
defmodule Character do
use Flint
embedded_schema do
field! :name, :string
field! :strength, Union, oneof: [:integer, :float]
end
end
Character.new!(%{name: "Elf", strength: 10})
%Character{name: "Elf", strength: 10}
Character.new!(%{name: "Elf", strength: 10.0})
%Character{name: "Elf", strength: 10.0}