Skip to content

Commit

Permalink
Merge pull request #173 from lostapathy/strict_mx
Browse files Browse the repository at this point in the history
Add strict_mx validation
  • Loading branch information
micke authored Jan 30, 2021
2 parents 1c9208a + 13c8e65 commit 511a7c7
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ class User < ActiveRecord::Base
end
```

To validate that the domain has a MX record:
To validate that the domain has an MX record or A record:
```ruby
validates :email, 'valid_email_2/email': { mx: true }
```
To validate strictly that the domain has an MX record:
```ruby
validates :email, 'valid_email_2/email': { strict_mx: true }
```

To validate that the domain is not a disposable email (checks domain and MX server):
```ruby
Expand Down Expand Up @@ -109,6 +113,7 @@ address = ValidEmail2::Address.new("lisinge@gmail.com")
address.valid? => true
address.disposable? => false
address.valid_mx? => true
address.valid_strict_mx? => true
address.subaddressed? => false
```

Expand All @@ -120,6 +125,7 @@ Do so by adding this in your `spec_helper`:
```ruby
config.before(:each) do
allow_any_instance_of(ValidEmail2::Address).to receive(:valid_mx?).and_return(true)
allow_any_instance_of(ValidEmail2::Address).to receive(:valid_strict_mx?).and_return(true)
end
```

Expand Down
13 changes: 12 additions & 1 deletion lib/valid_email2/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ def blacklisted?
def valid_mx?
return false unless valid?

mx_or_a_servers.any?
end

def valid_strict_mx?
return false unless valid?

mx_servers.any?
end

Expand Down Expand Up @@ -119,7 +125,12 @@ def address_contain_emoticons?(email)

def mx_servers
@mx_servers ||= Resolv::DNS.open do |dns|
mx_servers = dns.getresources(address.domain, Resolv::DNS::Resource::IN::MX)
dns.getresources(address.domain, Resolv::DNS::Resource::IN::MX)
end
end

def mx_or_a_servers
@mx_or_a_servers ||= Resolv::DNS.open do |dns|
(mx_servers.any? && mx_servers) ||
dns.getresources(address.domain, Resolv::DNS::Resource::IN::A)
end
Expand Down
6 changes: 5 additions & 1 deletion lib/valid_email2/email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module ValidEmail2
class EmailValidator < ActiveModel::EachValidator
def default_options
{ regex: true, disposable: false, mx: false, disallow_subaddressing: false, multiple: false }
{ regex: true, disposable: false, mx: false, strict_mx: false, disallow_subaddressing: false, multiple: false }
end

def validate_each(record, attribute, value)
Expand Down Expand Up @@ -48,6 +48,10 @@ def validate_each(record, attribute, value)
if options[:mx]
error(record, attribute) && return unless addresses.all?(&:valid_mx?)
end

if options[:strict_mx]
error(record, attribute) && return unless addresses.all?(&:valid_strict_mx?)
end
end

def error(record, attribute)
Expand Down
21 changes: 21 additions & 0 deletions spec/valid_email2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class TestUserMX < TestModel
validates :email, 'valid_email_2/email': { mx: true }
end

class TestUserStrictMX < TestModel
validates :email, 'valid_email_2/email': { strict_mx: true }
end

class TestUserDisallowDisposable < TestModel
validates :email, 'valid_email_2/email': { disposable: true }
end
Expand Down Expand Up @@ -253,6 +257,23 @@ def set_whitelist
end
end

describe "with strict mx validation" do
it "is valid if mx records are found" do
user = TestUserStrictMX.new(email: "foo@gmail.com")
expect(user.valid?).to be_truthy
end

it "is invalid if A records are found but no mx records are found" do
user = TestUserStrictMX.new(email: "foo@ghs.google.com")
expect(user.valid?).to be_falsey
end

it "is invalid if no mx records are found" do
user = TestUserStrictMX.new(email: "foo@subdomain.gmail.com")
expect(user.valid?).to be_falsey
end
end

describe "with dotted validation" do
it "is valid when address does not contain dots" do
user = TestUserDotted.new(email: "johndoe@gmail.com")
Expand Down

0 comments on commit 511a7c7

Please sign in to comment.