View Source Vx.Map (Vx v0.1.0)
The Map type.
Summary
Functions
Checks the shape of the map.
Checks the size of the map.
Builds a new Map type that matches any map.
Builds a new Map type with a specific key and value type.
Types
@opaque t()
The map type.
Functions
Checks the shape of the map.
Examples
iex> schema = Vx.Map.shape(%{a: Vx.String.t(), b: Vx.Number.t()})
...> Vx.validate!(schema, %{a: "foo", b: 123})
:ok
iex> schema = Vx.Map.shape(%{a: Vx.String.t(), b: Vx.Number.t()})
...> Vx.validate!(schema, %{a: "foo"})
** (Vx.Error) must have key(s) :b
iex> schema = Vx.Map.shape(%{a: Vx.String.t(), b: Vx.Number.t()})
...> Vx.validate!(schema, %{a: "foo", b: "bar"})
** (Vx.Error) does not match shape
- key :b: must be a number
It is also possible to mark certain keys as optional.
iex> schema = Vx.Map.shape(%{
...> :a => Vx.String.t(),
...> Vx.Optional.t(:b) => Vx.Number.t()
...> })
...> Vx.validate!(schema, %{a: "foo"})
:ok
iex> schema = Vx.Map.shape(%{
...> :a => Vx.String.t(),
...> Vx.Optional.t(:b) => Vx.Number.t()
...> })
...> Vx.validate!(schema, %{a: "foo", b: "bar"})
** (Vx.Error) does not match shape
- key :b: must be a number
@spec size(t(), non_neg_integer()) :: t()
Checks the size of the map.
Examples
iex> Vx.Map.size(0) |> Vx.validate!(%{})
:ok
iex> Vx.Map.size(1) |> Vx.validate!(%{a: "foo", b: 123})
** (Vx.Error) must have a size of 1
@spec t() :: t()
Builds a new Map type that matches any map.
Examples
iex> Vx.Map.t() |> Vx.validate!(%{})
:ok
iex> Vx.Map.t() |> Vx.validate!(%{a: "foo", b: 123})
:ok
iex> Vx.Map.t() |> Vx.validate!("foo")
** (Vx.Error) must be a map
Builds a new Map type with a specific key and value type.
Examples
iex> Vx.Map.t(Vx.String.t(), Vx.Number.t()) |> Vx.validate!(%{})
:ok
iex> schema = Vx.Map.t(Vx.Atom.t(), Vx.Number.t())
...> Vx.validate!(schema, %{a: 123, b: 234.5})
:ok
iex> Vx.Map.t(Vx.Atom.t(), Vx.Number.t()) |> Vx.validate!("foo")
** (Vx.Error) must be a map<atom, number>
iex> Vx.Map.t(Vx.Atom.t(), Vx.Number.t()) |> Vx.validate!(%{foo: "bar"})
** (Vx.Error) must be a map<atom, number>
- value of element :foo: must be a number