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
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
.:truthy
- Allows additional values, i.e. truthy values to be considered valid booleans by converting them totrue
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 tofalse
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"}