Packages

Lib for elixir

Current section

Files

Jump to
gulib lib validations format.ex
Raw

lib/validations/format.ex

defmodule Gulib.Validations.Format do
# options:
# :with
# :message
def cannot_pure_number(value, options \\ []) do
Gulib.Validate.unless_skipping(value, options) do
if Gulib.String.Format.pure_number?(value) do
{:fail, :cannot_pure_number_format, Keyword.get(options, :message) || Gulib.Translator.dgettext(
"validate", "cannot have the pure number format"
)}
else
:ok
end
end
end
# options:
# :with
# :message
def must_pure_number(value, options \\ []) do
Gulib.Validate.unless_skipping(value, options) do
if Gulib.String.Format.pure_number?(value) do
:ok
else
{:fail, :must_pure_number_format, Keyword.get(options, :message) || Gulib.Translator.dgettext(
"validate", "must have the pure number format"
)}
end
end
end
# options:
# :with
# :message
def cannot_email(value, options \\ []) do
Gulib.Validate.unless_skipping(value, options) do
if Gulib.String.Format.email?(value) do
{:fail, :cannot_email_format, Keyword.get(options, :message) || Gulib.Translator.dgettext("validate", "cannot have the email format")}
else
:ok
end
end
end
# options:
# :with
# :message
def must_email(value, options \\ []) do
Gulib.Validate.unless_skipping(value, options) do
if Gulib.String.Format.email?(value) do
:ok
else
{:fail, :must_email_format, Keyword.get(options, :message) || Gulib.Translator.dgettext("validate", "must have the email format")}
end
end
end
# options:
# :with
# :message
def validate(value, options) when is_list(options) do
Gulib.Validate.unless_skipping(value, options) do
pattern = Keyword.get(options, :with)
pattern = cond do
Regex.regex?(pattern) -> pattern
true -> ~r"#{pattern}"
end
if Regex.match?(pattern, value) do
:ok
else
message = Keyword.get(options, :message) || Gulib.Translator.dgettext("validate", "must have the correct format")
{:fail, :format_mismatch, message}
end
end
end
def validate(value, pattern) do
if Regex.regex?(pattern) do
validate(value, with: pattern)
end
end
end