-
Notifications
You must be signed in to change notification settings - Fork 12
/
phone_number.ex
38 lines (29 loc) 路 800 Bytes
/
phone_number.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
defmodule Fields.PhoneNumber do
@moduledoc """
An Ecto Type for plaintext phone numbers.
Useful for publicly available numbers such as customer support.
See `Fields.PhoneNumberEncrypted` for storing numbers that are Personally Identifiable Information.
## Example
schema "retailers" do
field(:phone_number, Fields.PhoneNumber)
end
"""
alias Fields.Validate
use Ecto.Type
def type, do: :string
def cast(value) do
value = value |> to_string() |> String.trim()
case Validate.phone_number(value) do
true -> {:ok, value}
false -> :error
end
end
def dump(value) do
{:ok, to_string(value)}
end
def load(value) do
{:ok, value}
end
def embed_as(_), do: :self
def equal?(term1, term2), do: term1 == term2
end