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
at_least(value, req_val, msg \\ "The value has to be greater or equal to required value!")
View SourceValidates if the input value is greater or equal to required value.
at_most(value, req_val, msg \\ "The value has to be less or equal to required value!")
View SourceValidates 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
equal?(value, req_val, msg \\ "The value has to be equal to required value!")
View SourceValidates 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
equal_to?(value, field, msg \\ "Fields do not match.")
View Sourceequal_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
Guard for verifying if error msg is a string.
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
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
greater_than(value, req_val, msg \\ "The value has to be greater than required value!")
View SourceValidates 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).
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
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
less_than(value, req_val, msg \\ "The value has to be less than required value!")
View SourceValidates 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
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.