Packages
zoi
0.9.1
0.18.7
0.18.6
0.18.5
0.18.4
0.18.3
0.18.2
0.18.1
0.18.0
0.17.4
0.17.3
0.17.2
0.17.1
0.17.0
0.16.1
0.16.0
0.15.0
0.14.1
0.14.0
0.13.1
0.13.0
0.12.1
0.12.0
0.11.1
0.11.0
0.10.7
0.10.6
0.10.5
0.10.4
0.10.3
0.10.2
0.10.1
0.10.0
0.9.1
0.9.0
0.9.0-rc.1
0.9.0-rc.0
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.0
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.3
0.2.2
0.2.1
0.2.0
0.1.3
0.1.2
0.1.1
0.1.0
Zoi is a schema validation library for Elixir, designed to provide a simple and flexible way to define and validate data.
Current section
Files
Jump to
Current section
Files
lib/zoi/regexes.ex
defmodule Zoi.Regexes do
@moduledoc false
@doc """
Regex pattern to match a valid email address.
"""
def email() do
~r/^(?!\.)(?!.*\.\.)([a-z0-9_'+\-\.]*)[a-z0-9_+\-]@([a-z0-9][a-z0-9\-]*\.)+[a-z]{2,}$/i
end
@doc """
Regex pattern based on on https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email
"""
def html5_email() do
~r/^[\w.!#$%&'*+\/=?^`{|}~-]+@[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?(?:\.[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?)*$/i
end
@doc """
Regex pattern based on RFC 5322 official standard
"""
def rfc5322_email() do
~r/^(?:"[^"]+"|[!#-'*+\/-9=?A-Z^_`a-z{|}~]+)@(?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+[A-Za-z]{2,63}$/
end
@doc """
Regex pattern based on phoenix implementation: https://github.com/phoenixframework/phoenix/blob/main/priv/templates/phx.gen.auth/schema.ex#L38C34-L38C59
"""
def simple_email() do
~r/^[^@,;\s]+@[^@,;\s]+$/
end
@doc """
Regex pattern to match only uppercase letters.
"""
def upcase() do
~r/^[^a-z]*$/
end
@doc """
Regex pattern to match only lowercase letters.
"""
def downcase() do
~r/^[^A-Z]*$/
end
@doc """
Regex pattern to match a valid UUID.
"""
def uuid(opts \\ []) do
uuid_versions = ["v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"]
version = opts[:version]
cond do
version == nil ->
~r/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$/
version in uuid_versions ->
~r/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[#{version}][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$/
true ->
raise ArgumentError, "Invalid UUID version: #{version}"
end
end
@doc """
Regex pattern to match a valid IPv4 address.
from https://stackoverflow.com/questions/5284147/validating-ipv4-addresses-with-regexp
"""
def ipv4() do
~r/^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/
end
@doc """
Regex pattern to match a valid IPv6 address.
from https://stackoverflow.com/questions/53497/regular-expression-that-matches-valid-ipv6-addresses
"""
def ipv6() do
~r/(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))/
end
@doc """
Regex pattern to match hexadecimal
"""
def hex() do
~r/^[0-9a-fA-F]*$/
end
end