mitgliederverwaltung/lib/membership/email.ex
Moritz 156cdb24d0
Some checks failed
continuous-integration/drone/push Build is failing
WIP: validate required fields
2025-06-02 22:41:04 +02:00

40 lines
813 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("", _), do: {:ok, nil}
@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