-
Notifications
You must be signed in to change notification settings - Fork 12
/
url.ex
37 lines (28 loc) · 620 Bytes
/
url.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
defmodule Fields.Url do
@moduledoc """
An Ecto Type for urls.
Use `Fields.UrlEncrypted` for encrypted urls.
## Example
schema "retailers" do
field :url, Fields.Url
end
"""
alias Fields.Validate
use Ecto.Type
def type, do: :string
def cast(value) do
value = value |> to_string() |> String.trim()
case Validate.url(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