Skip to content

Commit

Permalink
Add more validation methods to Mayo.Any, Mayo.Number and Mayo.String
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Jul 3, 2016
1 parent 29cd7eb commit 235e4f2
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 11 deletions.
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ defmodule MyModule do
require Mayo

def call do
Mayo.validate %{
username: "John",
password: "123456"
}, %{
username: Mayo.Any.string |> Mayo.String.min(4),
password: Mayo.Any.string |> Mayo.String.min(6)
}
# %{username: "John", password: "123456"}

Mayo.validate %{
username: "John",
password: "1234"
Expand Down
28 changes: 28 additions & 0 deletions lib/mayo/any.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,32 @@ defmodule Mayo.Any do
type: "any.map"
}}
end

@doc """
Forbids any value except `nil`.
iex> Mayo.Any.forbidden(nil)
nil
iex> Mayo.Any.forbidden("test")
{:error, %Mayo.Error{type: "any.forbidden"}}
"""
def forbidden(value) when is_nil(value), do: value

def forbidden(_) do
{:error, %Mayo.Error{
type: "any.forbidden"
}}
end

@doc """
Sets the value to `nil`. Used to sanitize output.
iex> Mayo.Any.strip("test")
nil
iex> Mayo.Any.strip(nil)
nil
"""
def strip(_), do: nil
end
78 changes: 77 additions & 1 deletion lib/mayo/number.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ defmodule Mayo.Number do
iex> Mayo.Number.odd(2)
{:error, %Mayo.Error{type: "number.odd"}}
"""
def odd(value) when is_number(value) and rem(value, 2) != 0, do: value
def odd(value) when is_number(value) and rem(value, 2) == 1, do: value

def odd(value) when is_number(value) do
{:error, %Mayo.Error{
Expand Down Expand Up @@ -112,4 +112,80 @@ defmodule Mayo.Number do
end

def even(value), do: value

@doc """
Check if the number is an integer.
iex> Mayo.Number.integer(2)
2
iex> Mayo.Number.integer(3.14)
{:error, %Mayo.Error{type: "number.integer"}}
"""
def integer(value) when is_number(value) and is_float(value) do
{:error, %Mayo.Error{
type: "number.integer"
}}
end

def integer(value) when is_number(value), do: value

def integer(value), do: value

@doc """
Check if the number is a float.
iex> Mayo.Number.float(3.14)
3.14
iex> Mayo.Number.float(3)
{:error, %Mayo.Error{type: "number.float"}}
"""
def float(value) when is_number(value) and is_float(value), do: value

def float(value) when is_number(value) do
{:error, %Mayo.Error{
type: "number.float"
}}
end

def float(value), do: value

@doc """
Checks if the number is greater than the limit.
iex> Mayo.Number.greater(4, 3)
4
iex> Mayo.Number.greater(3, 3)
{:error, %Mayo.Error{type: "number.greater"}}
"""
def greater(value, limit) when is_number(value) and value > limit, do: value

def greater(value, _) when is_number(value) do
{:error, %Mayo.Error{
type: "number.greater"
}}
end

def greater(value, _), do: value

@doc """
Checks if the number is less than the limit.
iex> Mayo.Number.less(2, 3)
2
iex> Mayo.Number.less(3, 3)
{:error, %Mayo.Error{type: "number.less"}}
"""
def less(value, limit) when is_number(value) and value < limit, do: value

def less(value, _) when is_number(value) do
{:error, %Mayo.Error{
type: "number.less"
}}
end

def less(value, _), do: value
end
50 changes: 49 additions & 1 deletion lib/mayo/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ defmodule Mayo.String do
# http://stackoverflow.com/a/13653180
@uuid_pattern ~r/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/

# https://github.com/hapijs/joi/blob/master/lib/string.js#L312
# https://github.com/hapijs/joi/blob/v8.4.2/lib/string.js#L375
@hex_pattern ~r/^[a-f0-9]+$/i

# https://github.com/hapijs/joi/blob/v8.4.2/lib/string.js#L170
@alphanum_pattern ~r/^[a-zA-Z0-9]+$/

# https://github.com/hapijs/joi/blob/v8.4.2/lib/string.js#L170
@token_pattern ~r/^\w+$/

@doc """
Checks the minimum length of a string.
Expand Down Expand Up @@ -174,4 +180,46 @@ defmodule Mayo.String do
end

def credit_card(value), do: value

@doc """
Checks if the string only contains a-z, A-Z, and 0-9.
iex> Mayo.String.alphanum("Aa0")
"Aa0"
iex> Mayo.String.alphanum("!$!")
{:error, %Mayo.Error{type: "string.alphanum"}}
"""
def alphanum(value) when is_binary(value) do
if String.match?(value, @alphanum_pattern) do
value
else
{:error, %Mayo.Error{
type: "string.alphanum"
}}
end
end

def alphanum(value), do: value

@doc """
Checks if the string only contains a-z, A-Z, 0-9 and underscore _.
iex> Mayo.String.token("t_z")
"t_z"
iex> Mayo.String.token("t!z")
{:error, %Mayo.Error{type: "string.token"}}
"""
def token(value) when is_binary(value) do
if String.match?(value, @token_pattern) do
value
else
{:error, %Mayo.Error{
type: "string.token"
}}
end
end

def token(value), do: value
end

0 comments on commit 235e4f2

Please sign in to comment.