diff --git a/lib/animina/accounts/resources/basic_user.ex b/lib/animina/accounts/resources/basic_user.ex index 228a9fd3..e60c0b75 100644 --- a/lib/animina/accounts/resources/basic_user.ex +++ b/lib/animina/accounts/resources/basic_user.ex @@ -69,7 +69,7 @@ defmodule Animina.Accounts.BasicUser do validate {Validations.Birthday, attribute: :birthday} validate {Validations.PostalCode, attribute: :zip_code} validate {Validations.Gender, attribute: :gender} - validate {Validations.PhoneNumber, attribute: :mobile_phone} + validate {Validations.MobilePhoneNumber, attribute: :mobile_phone} end identities do diff --git a/lib/animina/accounts/resources/user.ex b/lib/animina/accounts/resources/user.ex index 3ce532ac..bbbf24e3 100644 --- a/lib/animina/accounts/resources/user.ex +++ b/lib/animina/accounts/resources/user.ex @@ -79,7 +79,7 @@ defmodule Animina.Accounts.User do validate {Validations.Birthday, attribute: :birthday} validate {Validations.PostalCode, attribute: :zip_code} validate {Validations.Gender, attribute: :gender} - validate {Validations.PhoneNumber, attribute: :mobile_phone} + validate {Validations.MobilePhoneNumber, attribute: :mobile_phone} end identities do diff --git a/lib/animina/validations/mobile_phone_number.ex b/lib/animina/validations/mobile_phone_number.ex new file mode 100644 index 00000000..aafc644e --- /dev/null +++ b/lib/animina/validations/mobile_phone_number.ex @@ -0,0 +1,46 @@ +defmodule Animina.Validations.MobilePhoneNumber do + use Ash.Resource.Validation + + @moduledoc """ + This is a module for validating a mobile phone number. + """ + + @impl true + def init(opts) do + case is_atom(opts[:attribute]) do + true -> {:ok, opts} + _ -> {:error, "attribute must be an atom!"} + end + end + + @impl true + def validate(changeset, opts) do + Ash.Changeset.get_attribute(changeset, :mobile_phone) + |> extract_ex_phone_number() + |> validate_mobile_phone_number(opts) + end + + defp extract_ex_phone_number(raw_attribute) do + case ExPhoneNumber.parse(raw_attribute, "DE") do + {:ok, phone_number} -> phone_number + _ -> nil + end + end + + defp validate_mobile_phone_number(nil, opts) do + {:error, field: opts[:attribute], message: "must be a valid phone number"} + end + + defp validate_mobile_phone_number(ex_phone_number, opts) do + case ExPhoneNumber.is_valid_number?(ex_phone_number) do + false -> + {:error, field: opts[:attribute], message: "must be a valid phone number"} + + _ -> + case ExPhoneNumber.get_number_type(ex_phone_number) do + :mobile -> :ok + _ -> {:error, field: opts[:attribute], message: "must be a mobile phone number"} + end + end + end +end diff --git a/lib/animina/validations/phone_number.ex b/lib/animina/validations/phone_number.ex deleted file mode 100644 index 79509aab..00000000 --- a/lib/animina/validations/phone_number.ex +++ /dev/null @@ -1,31 +0,0 @@ -defmodule Animina.Validations.PhoneNumber do - use Ash.Resource.Validation - - @moduledoc """ - This is a module for validating a phone number. - """ - - @impl true - def init(opts) do - case is_atom(opts[:attribute]) do - true -> {:ok, opts} - _ -> {:error, "attribute must be an atom!"} - end - end - - @impl true - def validate(changeset, opts) do - raw_phone_number = Ash.Changeset.get_attribute(changeset, :mobile_phone) - - case ExPhoneNumber.parse(raw_phone_number, "DE") do - {:ok, phone_number} -> - case ExPhoneNumber.is_valid_number?(phone_number) do - false -> {:error, field: opts[:attribute], message: "must be a valid phone number"} - _ -> :ok - end - - _ -> - {:error, field: opts[:attribute], message: "must be a valid phone number"} - end - end -end