litmus v1.0.1 Litmus.Type.List View Source
This type validates that a value is list.
Options
:default
- Setting:default
will populate a field with the provided value, assuming that it is not present already. If a field already has a value present, it will not be altered.:min_length
- Specifies the minimum list length. Allowed values are non-negative integers.:max_length
- Specifies the maximum list length. Allowed values are non-negative integers.:length
- Specifies the exact list length. Allowed values are non-negative integers.:required
- Setting:required
totrue
will cause a validation error when a field is not present or the value isnil
. Allowed values for required aretrue
andfalse
. The default isfalse
.:type
- Specifies the data type of elements in the list. Allowed values are are atoms:atom, :boolean, :number and :string
. Default value isnil
. Ifnil
, any element type is allowed in the list.:unique
- Setting:unique
to true will validate that all values in the list are unique. The default value isfalse
.
Examples
iex> schema = %{
...> "ids" => %Litmus.Type.List{
...> min_length: 1,
...> max_length: 5,
...> type: :number
...> }
...> }
iex> Litmus.validate(%{"ids" => [1, 2]}, schema)
{:ok, %{"ids" => [1, 2]}}
iex> Litmus.validate(%{"ids" => [1, "a"]}, schema)
{:error, "ids must be a list of numbers"}
iex> schema = %{
...> "ids" => %Litmus.Type.List{
...> default: []
...> }
...> }
iex> Litmus.validate(%{}, schema)
{:ok, %{"ids" => []}}
Link to this section Summary
Link to this section Types
Specs
t() :: %Litmus.Type.List{ default: any(), length: non_neg_integer() | nil, max_length: non_neg_integer() | nil, min_length: non_neg_integer() | nil, required: boolean(), type: atom() | nil, unique: boolean() }