From 6f9f7f8610d026fcbbb94fe779422235d75c9936 Mon Sep 17 00:00:00 2001 From: Zac Clay Date: Tue, 3 Nov 2026 15:20:37 -0500 Subject: [PATCH] perf: check the allow list before disposable? in disposable_with_allow_list disposable? also checks the MX servers, so it does a DNS lookup. It ran before allow_listed?, so every address did the DNS lookup, also when the domain was on the allow list (for example gmail.com). Check allow_listed? first. Allow-listed addresses now skip the DNS lookup. The result does not change. --- lib/valid_email2/email_validator.rb | 2 +- spec/valid_email2_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/valid_email2/email_validator.rb b/lib/valid_email2/email_validator.rb index a88f77f..d9fd3e5 100644 --- a/lib/valid_email2/email_validator.rb +++ b/lib/valid_email2/email_validator.rb @@ -44,7 +44,7 @@ def validate_each(record, attribute, value) end if options[:disposable_with_allow_list] - error(record, attribute, :disposable_with_allow_list) && return if addresses.any? { |address| address.disposable? && !address.allow_listed? } + error(record, attribute, :disposable_with_allow_list) && return if addresses.any? { |address| !address.allow_listed? && address.disposable? } end if options[:disposable_domain_with_allow_list] diff --git a/spec/valid_email2_spec.rb b/spec/valid_email2_spec.rb index 70cca55..eae6184 100644 --- a/spec/valid_email2_spec.rb +++ b/spec/valid_email2_spec.rb @@ -297,6 +297,14 @@ def set_allow_list expect(user.valid?).to be_truthy end + it "does not look up MX servers if the domain is in the allow list" do + File.open(allow_list_file_path, "w") { |f| f.write ["gmail.com"].to_yaml } + set_allow_list + expect_any_instance_of(ValidEmail2::Dns).not_to receive(:mx_servers) + user = TestUserDisallowDisposableWithAllowList.new(email: "foo@gmail.com") + expect(user.valid?).to be_truthy + end + it "is invalid if the domain is a disposable_domain and not in the allow list" do user = TestUserDisallowDisposableDomainWithAllowList.new(email: "foo@#{allow_list_domain}") expect(user.valid?).to be_falsey