37 lines
759 B
Elixir
37 lines
759 B
Elixir
defmodule Mv.Membership.Email do
|
|
@match_pattern ~S/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/
|
|
@match_regex Regex.compile!(@match_pattern)
|
|
@min_length 5
|
|
@max_length 254
|
|
|
|
use Ash.Type.NewType,
|
|
subtype_of: :string,
|
|
constraints: [
|
|
match: @match_pattern,
|
|
trim?: true,
|
|
min_length: @min_length,
|
|
max_length: @max_length
|
|
]
|
|
|
|
@impl true
|
|
def cast_input(value, _) when is_binary(value) do
|
|
value = String.trim(value)
|
|
|
|
cond do
|
|
String.length(value) < @min_length ->
|
|
:error
|
|
|
|
String.length(value) > @max_length ->
|
|
:error
|
|
|
|
!Regex.match?(@match_regex, value) ->
|
|
:error
|
|
|
|
true ->
|
|
{:ok, value}
|
|
end
|
|
end
|
|
|
|
@impl true
|
|
def cast_input(_, _), do: :error
|
|
end
|