mitgliederverwaltung/lib/membership/email.ex
Moritz aa62920c0d
All checks were successful
continuous-integration/drone/push Build is passing
chore: fix deprication warnings
2025-06-02 14:42:48 +02:00

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