Skip to content

Commit

Permalink
Parse addresses in strict mode (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol authored Feb 8, 2024
1 parent 1c68292 commit 4466d72
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ecto_network/inet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule EctoNetwork.INET do
address
|> String.trim()
|> String.to_charlist()
|> :inet.parse_address()
|> :inet.parse_strict_address()

parsed_netmask = cast_netmask(netmask, parsed_address)

Expand Down
8 changes: 8 additions & 0 deletions test/ecto_network_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ defmodule EctoNetworkTest do
{"is invalid", [type: EctoNetwork.INET, validation: :cast]}
end

test "converts shortened address into changeset error" do
changeset = Device.changeset(%Device{}, %{ip_address: "111"})
{:error, changeset} = TestRepo.insert(changeset)

assert changeset.errors[:ip_address] ==
{"is invalid", [type: EctoNetwork.INET, validation: :cast]}
end

test "converts ipv4 with incomplete CIDR into changeset error" do
changeset = Device.changeset(%Device{}, %{ip_address: "1.2.3.0/"})
{:error, changeset} = TestRepo.insert(changeset)
Expand Down

0 comments on commit 4466d72

Please sign in to comment.