From ca2c6cdb83fb722be7dbca603bd3739f93dc3365 Mon Sep 17 00:00:00 2001 From: Eric Saxby Date: Fri, 31 May 2024 11:34:13 -0700 Subject: [PATCH] Email addresses with different cases are considered equal --- CHANGELOG.md | 1 + lib/ecto_email.ex | 1 + test/ecto_email_test.exs | 4 ++++ 3 files changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f9d96f..1fdc4bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Change log +- Email addresses with different cases are considered equal. - **Breaking change:** Use `EctoEmail` instead of `Ecto.Email`. ## 1.0.0 diff --git a/lib/ecto_email.ex b/lib/ecto_email.ex index e5ec13e..4f4b5cd 100644 --- a/lib/ecto_email.ex +++ b/lib/ecto_email.ex @@ -115,6 +115,7 @@ defmodule EctoEmail do """ @impl Ecto.Type def equal?(address, address) when is_binary(address), do: true + def equal?(left, right) when is_binary(left) and is_binary(right), do: String.downcase(left) == String.downcase(right) def equal?(_, _), do: false @doc "Callback implementation for `c:Ecto.Type.load/1`" diff --git a/test/ecto_email_test.exs b/test/ecto_email_test.exs index d288091..a2eca8b 100644 --- a/test/ecto_email_test.exs +++ b/test/ecto_email_test.exs @@ -83,6 +83,10 @@ defmodule EctoEmailTest do assert "address@example.com" |> EctoEmail.equal?("address@example.com") end + test "is true when the addresses have the same content but different case" do + assert "AddreSS@example.COM" |> EctoEmail.equal?("address@example.com") + end + test "is false when the addresses are the same" do refute "address@example.com" |> EctoEmail.equal?("address2@example.com") end