Want.Keyword (want v1.14.0)
Manages conversions to and from keyword lists.
Link to this section Summary
Functions
Cast an incoming keyword list or map to an output keyword list using the provided schema to control conversion rules and validations.
Link to this section Types
enumerable()
Specs
enumerable() :: Want.enumerable()
input()
Specs
input() :: Want.enumerable()
key()
Specs
opts()
Specs
opts() :: Keyword.t()
result()
Specs
schema()
Specs
schema() :: map()
Link to this section Functions
cast(input, schema)
Specs
Cast an incoming keyword list or map to an output keyword list using the provided schema to control conversion rules and validations.
Examples
iex> Want.Keyword.cast(%{"id" => 1}, %{id: [type: :integer]})
iex> Want.Keyword.cast(%{"archived" => "true"}, %{archived: [type: :boolean, default: false]})
iex> Want.Keyword.cast(%{"archived" => "false"}, %{archived: [type: :boolean, default: false]})
iex> Want.Keyword.cast(%{}, %{archived: [type: :boolean, default: false]})
iex> Want.Keyword.cast(%{}, %{id: [type: :integer, default: 1]})
iex> Want.Keyword.cast(%{"id" => "bananas"}, %{id: [type: :integer, default: 1]})
iex> Want.Keyword.cast(%{"hello" => "world", "foo" => "bar"}, %{hello: [], foo: [type: :atom]})
iex> Want.Keyword.cast(%{"hello" => "world"}, %{hello: [], foo: [required: true]})
iex> Want.Map.cast(%{"datetime" => DateTime.from_unix!(0)}, %{datetime: [type: :datetime]}) {:ok, %{datetime: DateTime.from_unix!(0)}}
iex> Want.Map.cast(%{"datetime" => "1970-01-01T00:00:00Z"}, %{datetime: [type: :datetime]}) {:ok, %{datetime: DateTime.from_unix!(0)}}
iex> Want.Keyword.cast(%{"hello" => "world"}, %{hello: [], foo: []})
iex> Want.Keyword.cast(%{"hello" => "world"}, %{hello: [type: :enum, valid: [:world]]})
iex> Want.Keyword.cast(%{"hello" => %{"foo" => "bar"}}, %{hello: %{foo: [type: :atom]}})
iex> Want.Keyword.cast(%{"id" => "bananas"}, %{id: [type: :integer, default: 1]}, merge: [id: 2])
iex> Want.Keyword.cast(%{"id" => "bananas"}, %{id: [type: :any]})
iex> Want.Keyword.cast(%{"a" => %{"b" => %{"c" => 100}}}, %{id: [type: :integer, from: {"a", "b", "c"}]})