Skip to content

Commit

Permalink
fix: accept nil values when validating URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveitaly committed Aug 21, 2023
1 parent 8c57df9 commit 68995a5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/url.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ defmodule Fields.Url do

def type, do: :string

def cast(nil), do: {:ok, nil}

def cast(value) do
value = value |> to_string() |> String.trim()

Expand Down
4 changes: 4 additions & 0 deletions test/url_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ defmodule Fields.UrlTest do
end

describe "cast" do
test "Url.cast accepts a nil" do
assert {:ok, nil} == Url.cast(nil)
end

test "Url.cast accepts a string" do
assert {:ok, "http://www.test.com"} == Url.cast("http://www.test.com")
end
Expand Down

0 comments on commit 68995a5

Please sign in to comment.