View Source Litmus.Type.Boolean (litmus v1.0.2)
This type validates and converts values to booleans. It converts truthy and
falsy values to true or false.
options
Options
:default- Setting:defaultwill 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:requiredtotruewill cause a validation error when a field is not present or the value isnil. Allowed values for required aretrueandfalse. The default isfalse.:truthy- Allows additional values, i.e. truthy values to be considered valid booleans by converting them totrueduring 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 tofalseduring validation. Allowed value is an array of strings, number or boolean values. The default is[false, "false"]
examples
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"}