diff --git a/lib/membership/phone_number.ex b/lib/membership/phone_number.ex new file mode 100644 index 0000000..4613c43 --- /dev/null +++ b/lib/membership/phone_number.ex @@ -0,0 +1,28 @@ +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 diff --git a/lib/membership/property.ex b/lib/membership/property.ex index 0bd5eab..6728da2 100644 --- a/lib/membership/property.ex +++ b/lib/membership/property.ex @@ -24,7 +24,8 @@ defmodule Mv.Membership.Property do date: [type: :date], integer: [type: :integer], string: [type: :string], - email: [type: Mv.Membership.Email] + email: [type: Mv.Membership.Email], + phone: [type: Mv.Membership.PhoneNumber] ] ] end diff --git a/lib/membership/property_type.ex b/lib/membership/property_type.ex index 8e42fa6..18215b4 100644 --- a/lib/membership/property_type.ex +++ b/lib/membership/property_type.ex @@ -19,7 +19,7 @@ defmodule Mv.Membership.PropertyType do attribute :name, :string, allow_nil?: false, public?: true attribute :value_type, :atom, - constraints: [one_of: [:string, :integer, :boolean, :date, :email]], + constraints: [one_of: [:string, :integer, :boolean, :date, :email, :phone]], allow_nil?: false, description: "Definies the datatype `Property.value` is interpreted as"