-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from animina-dating/Virtual-branch-2
Validate that mobile_phone is indeed a mobile phone number. closes #88
- Loading branch information
Showing
4 changed files
with
48 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.