Vex.Validators.Number (Vex v0.9.2) View Source

Ensure a value is a number.

Options

At least one of the following must be provided:

  • :is: The value is a number (integer or float) or not.
  • :equal_to: The value is a number equal to this number.
  • :greater_than : The value is a number greater than this number.
  • :greater_than_or_equal_to: The value is a number greater than or equal to this number.
  • :less_than : The value is a number less than this number.
  • :less_than_or_equal_to: The value is a number less than or equal to this number.

Optional:

  • :message: A custom error message. May be in EEx format and use the fields described in Custom Error Messages.
  • :allow_nil: A boolean whether to skip this validation for nil values.
  • :allow_blank: A boolean whether to skip this validaton for blank values.

The :is option can be provided in place of the keyword list if no other options are set. When multiple options are than the validator will do an and logic between them.

Examples

Examples when using the :is option:

iex> Vex.Validators.Number.validate("not_a_number", is: true)
{:error, "must be a number"}
iex> Vex.Validators.Number.validate(3.14, is: true)
:ok

iex> Vex.Validators.Number.validate("not_a_number", is: false)
:ok
iex> Vex.Validators.Number.validate(3.14, is: false)
{:error, "must not be a number"}

Examples when using the boolean value in options for the :is option:

iex> Vex.Validators.Number.validate("not_a_number", true)
{:error, "must be a number"}
iex> Vex.Validators.Number.validate(3.14, true)
:ok

iex> Vex.Validators.Number.validate("not_a_number", false)
:ok
iex> Vex.Validators.Number.validate(3.14, false)
{:error, "must not be a number"}

Examples when using the :equal_to option:

iex> Vex.Validators.Number.validate(3.14, equal_to: 1.41)
{:error, "must be a number equal to 1.41"}
iex> Vex.Validators.Number.validate(3.14, equal_to: 3.14)
:ok
iex> Vex.Validators.Number.validate(3.14, equal_to: 6.28)
{:error, "must be a number equal to 6.28"}

Examples when using the :greater_than option:

iex> Vex.Validators.Number.validate(3.14, greater_than: 1.41)
:ok
iex> Vex.Validators.Number.validate(3.14, greater_than: 3.14)
{:error, "must be a number greater than 3.14"}
iex> Vex.Validators.Number.validate(3.14, greater_than: 6.28)
{:error, "must be a number greater than 6.28"}

Examples when using the :greater_than_or_equal_to option:

iex> Vex.Validators.Number.validate(3.14, greater_than_or_equal_to: 1.41)
:ok
iex> Vex.Validators.Number.validate(3.14, greater_than_or_equal_to: 3.14)
:ok
iex> Vex.Validators.Number.validate(3.14, greater_than_or_equal_to: 6.28)
{:error, "must be a number greater than or equal to 6.28"}

Examples when using the :less_than option:

iex> Vex.Validators.Number.validate(3.14, less_than: 1.41)
{:error, "must be a number less than 1.41"}
iex> Vex.Validators.Number.validate(3.14, less_than: 3.14)
{:error, "must be a number less than 3.14"}
iex> Vex.Validators.Number.validate(3.14, less_than: 6.28)
:ok

Examples when using the :less_than_or_equal_to option:

iex> Vex.Validators.Number.validate(3.14, less_than_or_equal_to: 1.41)
{:error, "must be a number less than or equal to 1.41"}
iex> Vex.Validators.Number.validate(3.14, less_than_or_equal_to: 3.14)
:ok
iex> Vex.Validators.Number.validate(3.14, less_than_or_equal_to: 6.28)
:ok

Examples when using the combinations of the above options:

iex> Vex.Validators.Number.validate("not_a_number", is: true, greater_than: 0, less_than_or_equal_to: 3.14)
{:error, "must be a number"}
iex> Vex.Validators.Number.validate(0, is: true, greater_than: 0, less_than_or_equal_to: 3.14)
{:error, "must be a number greater than 0"}
iex> Vex.Validators.Number.validate(1.41, is: true, greater_than: 0, less_than_or_equal_to: 3.14)
:ok
iex> Vex.Validators.Number.validate(3.14, is: true, greater_than: 0, less_than_or_equal_to: 3.14)
:ok
iex> Vex.Validators.Number.validate(6.28, is: true, greater_than: 0, less_than_or_equal_to: 3.14)
{:error, "must be a number less than or equal to 3.14"}

Custom Error Messages

Custom error messages (in EEx format), provided as :message, can use the following values:

iex> Vex.Validators.Number.__validator__(:message_fields)
[                             
  value: "Bad value",         
  is: "Is number",            
  equal_to: "Equal to number",             
  greater_than: "Greater than number",
  greater_than_or_equal_to: "Greater than or equal to number",
  less_than: "Less than number",
  less_than_or_equal_to: "Less than or equal to number"
]

An example:

iex> Vex.Validators.Number.validate(3.14, less_than: 1.41,
...>                                      message: "<%= inspect value %> should be less than <%= less_than %>")
{:error, "3.14 should be less than 1.41"}

Link to this section Summary

Functions

Callback implementation for c:Vex.Validator.Behaviour.validate/2.

Callback implementation for c:Vex.Validator.Behaviour.validate/3.

Link to this section Functions

Link to this function

validate(value, options)

View Source

Callback implementation for c:Vex.Validator.Behaviour.validate/2.

Link to this function

validate(data, context, options)

View Source

Callback implementation for c:Vex.Validator.Behaviour.validate/3.