Validatex v1.0.1 Validatex.Validators View Source

This module provides a few functions for validating data.

All functions return Result. It means, if an input value is correct (i.e. the value satisfies to given validation), function returns a tuple {:ok, val}. If not then {:error, "msg"}.

Usage:

Let say that you have a few input forms on your page, for instance: name, surname, password etc. Now you want to validate whether the filled data are correct for each field. So you create somewhere inside your project a module Validators which will be containing any of functions like bellow.

Note:

Almost each function has a default error message. This message can be rewritten according to your needs.

Link to this section Summary

Functions

Validates if the input value is greater or equal to required value.

Validates if the input value is less or equal to required value.

Validates if the input value is equal to required value.

Validates if the input value is equal to another input value. For example password input form and confirm_password form.

Guard for verifying if error msg is a string.

Validates if the input value is float.

In case of validating complex input data you can use regex.

Validates if the input value is greater than required value.

Validates if the input value is inside required list.

Validates if the input value is between two numbers (integer or float) (mathematically speaking it's closed interval).

Validates if the input value is integer.

Validates if the input value is less than required value.

Validates if the input value is empty or not.

Guard for verifying if raw is a string.

Validates if the input value is true or false.

Link to this section Types

Link to this section Functions

Link to this function

at_least(value, req_val, msg \\ "The value has to be greater or equal to required value!")

View Source
at_least(raw(), number(), error_msg()) :: Result.t(error_msg(), number())

Validates if the input value is greater or equal to required value.

Link to this function

at_most(value, req_val, msg \\ "The value has to be less or equal to required value!")

View Source
at_most(raw(), number(), error_msg()) :: Result.t(error_msg(), number())

Validates if the input value is less or equal to required value.

Example:

@spec total_count(String.t()) :: Result.t(String.t(), number())
def total_count(value) do
  Validators.at_most(value, 10, "The value has to be less or equal to 10!")
end
Link to this function

equal?(value, req_val, msg \\ "The value has to be equal to required value!")

View Source
equal?(raw(), number() | String.t(), error_msg()) ::
  Result.t(error_msg(), number() | String.t())

Validates if the input value is equal to required value.

Example:

@spec captcha(String.t()) :: Result.t(String.t(), number())
def captcha(value) do
  Validators.equal?(value, 10, "The summation has to be equal to 10!")
end
Link to this function

equal_to?(value, field, msg \\ "Fields do not match.")

View Source
equal_to?(a, Validatex.Validation.field(any(), a), error_msg()) ::
  Result.t(error_msg(), a)
when a: var

Validates if the input value is equal to another input value. For example password input form and confirm_password form.

Example:

@spec conf_password(Validation.field(any(), a)) :: (a -> Result.t(String.t(), a)) when a: var
def conf_password(pass) do
  &Validators.equal_to?(&1, pass, "The passwords don't match!")
end
Link to this macro

error_msg?(msg)

View Source (macro)

Guard for verifying if error msg is a string.

Link to this function

float(value, msg \\ "The value has to be a float!")

View Source
float(raw(), error_msg()) :: Result.t(error_msg(), float())

Validates if the input value is float.

Example:

@spec temperature(String.t()) :: Result.t(String.t(), float())
def temperature(value) do
  Validators.float(value, "The temperature has to be float!")
end
Link to this function

format(value, regex, msg)

View Source
format(raw(), Regex.t(), error_msg()) :: Result.t(error_msg(), raw())

In case of validating complex input data you can use regex.

Example:

@spec date(String.t()) :: Result.t(String.t(), Date.t())
def date(value) do
  Validators.format(
  value,
  ~r/^\d{1,2}\.\d{1,2}\.(\d{4})?$/,
  "Correct date is e.g. in format 11.12.1918 or 03.08.2008."
)
end

@spec email(String.t()) :: Result.t(String.t(), String.t())
def email(value) do
  Validators.format(
    value,
    ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
    "It's required valid email!"
  )
end
Link to this function

greater_than(value, req_val, msg \\ "The value has to be greater than required value!")

View Source
greater_than(raw(), number(), error_msg()) :: Result.t(error_msg(), number())

Validates if the input value is greater than required value.

Link to this function

in_list(value, list, msg \\ "The value has to be in list!")

View Source
in_list(a, [a], error_msg()) :: Result.t(error_msg(), a) when a: var

Validates if the input value is inside required list.

Link to this function

in_range(value, min, max, msg)

View Source
in_range(raw(), number(), number(), error_msg()) ::
  Result.t(error_msg(), number())

Validates if the input value is between two numbers (integer or float) (mathematically speaking it's closed interval).

Example:

@spec password(String.t()) :: Result.t(String.t(), String.t())
def password(pass) do
  min = 6
  max = 12

  [
    Validators.not_empty(value, "Passsword is required!"),
    pass
    |> String.length()
    |> Kernel.to_string()
    |> Validators.in_range(min, max,
      "Password has to be at most #{min} and at least #{max} lenght!"
      )
  ]
  |> Result.product()
  |> Result.map(&hd/1)
end
Link to this function

integer(value, msg \\ "The value has to be an integer!")

View Source
integer(raw(), error_msg()) :: Result.t(error_msg(), integer())

Validates if the input value is integer.

Example:

@spec score(String.t()) :: Result.t(String.t(), integer())
def score(value) do
  Validators.integer(value, "The score has to be integer!")
end
Link to this function

less_than(value, req_val, msg \\ "The value has to be less than required value!")

View Source
less_than(raw(), number(), error_msg()) :: Result.t(error_msg(), number())

Validates if the input value is less than required value.

Example:

@spec count(String.t()) :: Result.t(String.t(), number())
def count(value) do
  Validators.less_than(value, 10, "The value has to be less than 10!")
end
Link to this function

not_empty(value, msg \\ "The value must not be an empty!")

View Source
not_empty(raw(), error_msg()) :: Result.t(error_msg(), raw())

Validates if the input value is empty or not.

Example:

# Validators.ex

@spec name(String.t()) :: Result.t(String.t(), String.t())
def name(value) do
  Validators.not_empty(value, "Name is required!")
end

Guard for verifying if raw is a string.

Validates if the input value is true or false.