Skip to content

Commit

Permalink
Add more validation methods to Mayo.String
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Jul 3, 2016
1 parent 1aa49a0 commit b10883b
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 5 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@ defmodule MyModule do
require Mayo

def call do
data = %{
username: "john",
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 data, %{
Mayo.validate %{
username: "John",
password: "1234"
}, %{
username: Mayo.Any.string |> Mayo.String.min(4),
password: Mayo.Any.string |> Mayo.String.min(6) |> Mayo.String.max(50)
password: Mayo.Any.string |> Mayo.String.min(6)
}
# {:error, %Mayo.Error{type: "string.min", path: "password"}}
end
end
```
Expand Down
103 changes: 103 additions & 0 deletions lib/mayo/string.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
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
@hex_pattern ~r/^[a-f0-9]+$/i

@doc """
Checks the minimum length of a string.
Expand Down Expand Up @@ -55,4 +61,101 @@ defmodule Mayo.String do
}}
end
end

@doc """
Checks if the string is a valid UUID.
iex> Mayo.String.uuid("127862fc-3f59-47c4-9ccf-5d3bfbbe1271")
"127862fc-3f59-47c4-9ccf-5d3bfbbe1271"
iex> Mayo.String.uuid("test")
{:error, %Mayo.Error{type: "string.uuid"}}
"""
def uuid(value) do
if String.match?(value, @uuid_pattern) do
value
else
{:error, %Mayo.Error{
type: "string.uuid"
}}
end
end

@doc """
Checks if the string only contains hex.
iex> Mayo.String.hex("a1b2")
"a1b2"
iex> Mayo.String.hex("lol")
{:error, %Mayo.Error{type: "string.hex"}}
"""
def hex(value) do
if String.match?(value, @hex_pattern) do
value
else
{:error, %Mayo.Error{
type: "string.hex"
}}
end
end

@doc """
Checks if the string is a valid IPv4 or IPv6 address.
iex> Mayo.String.ip("210.59.230.150")
"210.59.230.150"
iex> Mayo.String.ip("2001:0DB8:02de:0000:0000:0000:0000:0e13")
"2001:0DB8:02de:0000:0000:0000:0000:0e13"
iex> Mayo.String.ip("test")
{:error, %Mayo.Error{type: "string.ip"}}
"""
def ip(value) do
case value |> String.to_charlist |> :inet.parse_address do
{:ok, _} -> value
_ -> {:error, %Mayo.Error{
type: "string.ip"
}}
end
end

@doc """
Checks if the string matches the pattern.
iex> Mayo.String.match("abbbbbc", ~r/^ab+c$/)
"abbbbbc"
iex> Mayo.String.match("ac", ~r/^ab+c$/)
{:error, %Mayo.Error{type: "string.match"}}
"""
def match(value, pattern) do
if String.match?(value, pattern) do
value
else
{:error, %Mayo.Error{
type: "string.match"
}}
end
end

@doc """
Checks if the string is a valid Luhn number. (Use [luhn_ex](https://github.com/ma2gedev/luhn_ex) library)
iex> Mayo.String.credit_card("378282246310005")
"378282246310005"
iex> Mayo.String.credit_card("123456789123456")
{:error, %Mayo.Error{type: "string.credit_card"}}
"""
def credit_card(value) do
if Luhn.valid?(value) do
value
else
{:error, %Mayo.Error{
type: "string.credit_card"
}}
end
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ defmodule Mayo.Mixfile do
end

defp deps do
[{:ex_doc, "~> 0.12", only: :docs},
[{:luhn, "~> 0.3"},
{:ex_doc, "~> 0.12", only: :docs},
{:excoveralls, "~> 0.5", only: :test},
{:inch_ex, "~> 0.5", only: :docs}]
end
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"idna": {:hex, :idna, "1.2.0", "ac62ee99da068f43c50dc69acf700e03a62a348360126260e87f2b54eced86b2", [:rebar3], []},
"inch_ex": {:hex, :inch_ex, "0.5.3", "39f11e96181ab7edc9c508a836b33b5d9a8ec0859f56886852db3d5708889ae7", [:mix], [{:poison, "~> 1.5 or ~> 2.0", [hex: :poison, optional: false]}]},
"jsx": {:hex, :jsx, "2.6.2", "213721e058da0587a4bce3cc8a00ff6684ced229c8f9223245c6ff2c88fbaa5a", [:mix, :rebar], []},
"luhn": {:hex, :luhn, "0.3.1", "c8c38ee131378cff925fc9dad825fc85ecd9593fcec10dbba550d26bb1744af5", [:mix], []},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], []},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], []},
"poison": {:hex, :poison, "2.2.0", "4763b69a8a77bd77d26f477d196428b741261a761257ff1cf92753a0d4d24a63", [:mix], []},
Expand Down

0 comments on commit b10883b

Please sign in to comment.