litmus v0.5.0 Litmus.Type.Number View Source

This type validates that values are numbers, and converts them to numbers if possible. It converts “stringified” numerical values to numbers.

Options

  • :min - Specifies the minimum value of the field.

  • :max - Specifies the maximum value of the field.

  • :integer - Specifies that the number must be an integer (no floating point). Allowed values are true and false. The default is false.

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

Examples

iex> schema = %{
...> "id" => %Litmus.Type.Number{
...>   integer: true
...> },
...> "gpa" => %Litmus.Type.Number{
...>   min: 0,
...>   max: 4
...>  }
...> }
iex> params = %{"id" => "123", "gpa" => 3.8}
iex> Litmus.validate(params, schema)
{:ok, %{"id" => 123, "gpa" => 3.8}}
iex> params = %{"id" => "123.456", "gpa" => 3.8}
iex> Litmus.validate(params, schema)
{:error, "id must be an integer"}

Link to this section Summary

Link to this section Types

Link to this type t() View Source
t() :: %Litmus.Type.Number{
  integer: boolean(),
  max: number() | nil,
  min: number() | nil,
  required: boolean()
}

Link to this section Functions

Link to this function validate_field(type, field, data) View Source
validate_field(t(), binary(), map()) :: {:ok, map()} | {:error, binary()}