View Source Vx.List (Vx v0.4.0)

The List type.

Summary

Types

t()

The list type.

Functions

Requires the list to have a maximum size.

Requires the list to have a minimum size.

Requires the list to be non-empty.

Requires the list to match the given shape.

t()

Builds a new List type.

Builds a new List type with the given inner type.

Types

@opaque t()

The list type.

Functions

Link to this function

max_size(schema \\ t(), size)

View Source
@spec max_size(t(), non_neg_integer()) :: t()

Requires the list to have a maximum size.

Link to this function

min_size(schema \\ t(), size)

View Source
@spec min_size(t(), non_neg_integer()) :: t()

Requires the list to have a minimum size.

Link to this function

non_empty(schema \\ t())

View Source
@spec non_empty(t()) :: t()

Requires the list to be non-empty.

Examples

iex> Vx.List.non_empty() |> Vx.validate!([1, 2, 3])
:ok

iex> Vx.List.non_empty() |> Vx.validate!([])
** (Vx.Error) must not be empty
Link to this function

shape(schema \\ t(), shape)

View Source
@spec shape(t(), [Vx.schema()]) :: t()

Requires the list to match the given shape.

Examples

iex> Vx.List.shape([Vx.Number.t(), Vx.String.t()]) |> Vx.validate!([123, "foo"])
:ok

iex> Vx.List.shape([Vx.Number.t(), Vx.String.t()]) |> Vx.validate!([123])
** (Vx.Error) must match [number, string]
- element 1 is missing

iex> Vx.List.shape([Vx.Number.t(), Vx.String.t()]) |> Vx.validate!([123, :foo])
** (Vx.Error) must match [number, string]
- element 1: must be a string
Link to this function

size(schema \\ t(), size)

View Source
@spec size(t(), non_neg_integer()) :: t()
@spec t() :: t()

Builds a new List type.

Examples

iex> Vx.List.t() |> Vx.validate!([1, 2, 3])
:ok

iex> Vx.List.t() |> Vx.validate!("foo")
** (Vx.Error) must be a list
@spec t(Vx.schema()) :: t()

Builds a new List type with the given inner type.

Examples

iex> Vx.List.t(Vx.Number.t()) |> Vx.validate!([1, 2, 3])
:ok

iex> Vx.List.t(Vx.String.t()) |> Vx.validate!("foo")
** (Vx.Error) must be a list

iex> Vx.List.t(Vx.String.t()) |> Vx.validate!(["foo", 2, "bar"])
** (Vx.Error) must be a list<string>
- element 1: must be a string