feat: add custom email type for validation
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
3e2140fda7
commit
859f5f4497
4 changed files with 43 additions and 18 deletions
34
lib/membership/email.ex
Normal file
34
lib/membership/email.ex
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
defmodule Mv.Membership.Email do
|
||||
@constraints [
|
||||
match: ~r/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/,
|
||||
trim?: true,
|
||||
min_length: 5,
|
||||
max_length: 254
|
||||
]
|
||||
|
||||
use Ash.Type.NewType,
|
||||
subtype_of: :string,
|
||||
constraints: @constraints
|
||||
|
||||
@impl true
|
||||
def cast_input(value, _) when is_binary(value) do
|
||||
value = if @constraints[:trim?], do: String.trim(value), else: value
|
||||
|
||||
cond do
|
||||
@constraints[:min_length] && String.length(value) < @constraints[:min_length] ->
|
||||
:error
|
||||
|
||||
@constraints[:max_length] && String.length(value) > @constraints[:max_length] ->
|
||||
:error
|
||||
|
||||
@constraints[:match] && !Regex.match?(@constraints[:match], value) ->
|
||||
:error
|
||||
|
||||
true ->
|
||||
{:ok, value}
|
||||
end
|
||||
end
|
||||
|
||||
@impl true
|
||||
def cast_input(_, _), do: :error
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue