Dry (dry v0.1.9)
Allow for creating structs, with fields of certain type, default value, custom function and more.
Example:
defmodule Sibling do
use Dry
alias Dry.Types
schema do
attribute(:name, Types.String)
end
end
defmodule User do
use Dry
alias Dry.Types
schema do
attribute(:name, Types.String)
attribute(:age, Types.Integer.options(optional: true))
attribute(:height)
attribute(:country, Types.String.options(default: "UK"))
attribute(:siblings, Types.Array.options(type: Sibling))
attribute(:favourite_colours, Types.Array.options(type: Types.String, default: ["blue", "green"]))
attribute :is_adult do
Map.get(entity, :age, 0) >= 18
end
attribute :tall do
Map.get(entity, :height, 0) >= 180
end
end
end
user = User.new!(%{name: "Rob", age: 18, height: 169, country: "BG", siblings: [%{name: "John"}]})
user == %User{
age: 18,
country: "BG",
height: 169,
is_adult: true,
name: "Rob",
tall: false,
siblings: [%Sibling{name: "John"}],
favourite_colours: ["blue", "green"]
}
{:ok, _user} = User.new(%{name: "Rob", age: 18, height: 169, country: "BG", siblings: [%{name: "John"}]}) ```
Link to this section Summary
Functions
Macro to define attribute withing a Dry schema Examples
Link to this section Functions
Macro to define attribute withing a Dry schema Examples:
attribute(:age, :integer, optional: true)
Valid types can be - :atom, :string, :integer, :float, :bool, :map, :atom or a struct
Link to this function