want v1.1.1 Want.Map

Manages conversions to and from maps.

Link to this section Summary

Functions

Cast an incoming keyword list or map to an output map using the provided schema to control conversion rules and validations.

Link to this section Types

Link to this type

input()

input() :: Keyword.t() | map()
Link to this type

key()

key() :: binary() | atom()
Link to this type

opts()

opts() :: Keyword.t()
Link to this type

result()

result() :: {:ok, result :: map()} | {:error, reason :: binary()}
Link to this type

schema()

schema() :: map()

Link to this section Functions

Link to this function

cast(input, schema)

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

Cast an incoming keyword list or map to an output map using the provided schema to control conversion rules and validations.

Examples

iex> Want.Map.cast(%{"id" => 1}, %{id: [type: :integer]}) {:ok, %{id: 1}}

iex> Want.Map.cast(%{}, %{id: [type: :integer, default: 1]}) {:ok, %{id: 1}}

iex> Want.Map.cast(%{"id" => "bananas"}, %{id: [type: :integer, default: 1]}) {:ok, %{id: 1}}

iex> Want.Map.cast(%{"hello" => "world", "foo" => "bar"}, %{hello: [], foo: [type: :atom]}) {:ok, %{hello: "world", foo: :bar}}

iex> Want.Map.cast(%{"hello" => %{"foo" => "bar"}}, %{hello: %{foo: [type: :atom]}}) {:ok, %{hello: %{foo: :bar}}}

Link to this function

cast(input, key, opts)

cast(input :: any(), key :: key(), opts :: opts() | map()) ::
  {:ok, result :: any()} | {:error, reason :: binary()}