28 lines
546 B
Elixir
28 lines
546 B
Elixir
defmodule Mv.Membership.PhoneNumber do
|
|
@match_pattern ~S/^\+?\d{5,16}$/
|
|
@match_regex Regex.compile!(@match_pattern)
|
|
|
|
use Ash.Type.NewType,
|
|
subtype_of: :string,
|
|
constraints: [
|
|
match: @match_pattern,
|
|
trim?: true
|
|
]
|
|
|
|
@impl true
|
|
def cast_input("", _), do: {:ok, nil}
|
|
|
|
@impl true
|
|
def cast_input(value, _) when is_binary(value) do
|
|
value = String.trim(value)
|
|
|
|
if Regex.match?(@match_regex, value) do
|
|
{:ok, value}
|
|
else
|
|
:error
|
|
end
|
|
end
|
|
|
|
@impl true
|
|
def cast_input(_, _), do: :error
|
|
end
|