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 to true will cause a validation error when a field is not present or the value is nil. Allowed values for required are true and false. The default is false.

  • :type - Specifies the data type of elements in the list. Allowed values are are atoms :atom, :boolean, :number and :string. Default value is nil. If nil, 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 is false.

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

Link to this section Functions

Link to this function

validate_field(type, field, data)

View Source

Specs

validate_field(t(), term(), map()) :: {:ok, map()} | {:error, String.t()}