litmus v1.0.1 Litmus.Type.Boolean View Source

This type validates and converts values to booleans. It converts truthy and falsy values to true or false.

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.

  • :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.

  • :truthy - Allows additional values, i.e. truthy values to be considered valid booleans by converting them to true during validation. Allowed value is an array of strings, numbers, or booleans. The default is [true, "true"]

  • :falsy - Allows additional values, i.e. falsy values to be considered valid booleans by converting them to false during validation. Allowed value is an array of strings, number or boolean values. The default is [false, "false"]

Examples

iex> schema = %{
...>   "new_user" => %Litmus.Type.Boolean{
...>     truthy: ["1"],
...>     falsy: ["0"]
...>   }
...> }
iex> params = %{"new_user" => "1"}
iex> Litmus.validate(params, schema)
{:ok, %{"new_user" => true}}

iex> schema = %{
...>   "new_user" => %Litmus.Type.Boolean{
...>     default: false
...>   }
...> }
iex> Litmus.validate(%{}, schema)
{:ok, %{"new_user" => false}}

iex> schema = %{"new_user" => %Litmus.Type.Boolean{}}
iex> params = %{"new_user" => 0}
iex> Litmus.validate(params, schema)
{:error, "new_user must be a boolean"}

Link to this section Summary

Link to this section Types

Specs

t() :: %Litmus.Type.Boolean{
  default: any(),
  falsy: [term()],
  required: boolean(),
  truthy: [term()]
}

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