Want.Keyword (want v1.10.1)

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

Link to this type

enumerable()

Specs

enumerable() :: Want.enumerable()

Specs

input() :: Want.enumerable()

Specs

key() :: binary() | atom()

Specs

opts() :: Keyword.t()

Specs

result() :: {:ok, result :: Keyword.t()} | {:error, reason :: binary()}

Specs

schema() :: map()

Link to this section Functions

Link to this function

cast(input, schema)

Specs

cast(value :: input(), schema :: schema()) :: result()

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]})

Link to this function

cast(input, schema, opts)

Specs

cast(value :: input(), schema :: schema(), opts :: Keyword.t()) :: result()
cast(input :: any(), key :: key(), opts :: opts() | map()) ::
  {:ok, result :: any()} | {:error, reason :: binary()}