Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to check for disposable domain without MX server check #141

Merged
merged 3 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ To validate that the domain has a MX record:
validates :email, 'valid_email_2/email': { mx: true }
```

To validate that the domain is not a disposable email:
To validate that the domain is not a disposable email (checks domain and MX server):
```ruby
validates :email, 'valid_email_2/email': { disposable: true }
```

To validate that the domain is not a disposable email (checks domain only, does not check MX server):
```ruby
validates :email, 'valid_email_2/email': { disposable_domain: true }
```

To validate that the domain is not a disposable email or a disposable email but whitelisted (under config/whitelisted_email_domains.yml):
```ruby
validates :email, 'valid_email_2/email': { disposable_with_whitelist: true }
Expand Down
56 changes: 31 additions & 25 deletions lib/valid_email2/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,28 @@ def initialize(address)
end

def valid?
return false if @parse_error

if address.domain && address.address == @raw_address
domain = address.domain

domain !~ PROHIBITED_DOMAIN_CHARACTERS_REGEX &&
# Domain needs to have at least one dot
domain =~ /\./ &&
# Domain may not have two consecutive dots
domain !~ /\.{2,}/ &&
# Domain may not start with a dot
domain !~ /^\./ &&
# Domain may not start with a dash
domain !~ /^-/ &&
# Domain name may not end with a dash
domain !~ /-\./ &&
# Address may not contain a dot directly before @
address.address !~ /\.@/
else
false
@valid ||= begin
return false if @parse_error

if address.domain && address.address == @raw_address
domain = address.domain

domain !~ PROHIBITED_DOMAIN_CHARACTERS_REGEX &&
# Domain needs to have at least one dot
domain =~ /\./ &&
# Domain may not have two consecutive dots
domain !~ /\.{2,}/ &&
# Domain may not start with a dot
domain !~ /^\./ &&
# Domain may not start with a dash
domain !~ /^-/ &&
# Domain name may not end with a dash
domain !~ /-\./ &&
# Address may not contain a dot directly before @
address.address !~ /\.@/
else
false
end
end
end

Expand All @@ -51,11 +53,15 @@ def subaddressed?
end

def disposable?
valid? &&
(
domain_is_in?(ValidEmail2.disposable_emails) ||
mx_server_is_in?(ValidEmail2.disposable_emails)
)
disposable_domain? || disposable_mx_server?
end

def disposable_domain?
valid? && domain_is_in?(ValidEmail2.disposable_emails)
end

def disposable_mx_server?
valid? && mx_server_is_in?(ValidEmail2.disposable_emails)
end

def whitelisted?
Expand Down
4 changes: 4 additions & 0 deletions lib/valid_email2/email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def validate_each(record, attribute, value)
error(record, attribute) && return if address.disposable?
end

if options[:disposable_domain]
error(record, attribute) && return if address.disposable_domain?
end

if options[:disposable_with_whitelist]
error(record, attribute) && return if address.disposable? && !address.whitelisted?
end
Expand Down
33 changes: 22 additions & 11 deletions spec/valid_email2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class TestUserDisallowDisposable < TestModel
validates :email, 'valid_email_2/email': { disposable: true }
end

class TestUserDisallowDisposableDomain < TestModel
validates :email, 'valid_email_2/email': { disposable_domain: true }
end

class TestUserDisallowDisposableWithWhitelist < TestModel
validates :email, 'valid_email_2/email': { disposable_with_whitelist: true }
end
Expand Down Expand Up @@ -111,19 +115,26 @@ class TestUserMessage < TestModel
expect(user.valid?).to be_truthy
end

it "is invalid if it's a disposable email" do
user = TestUserDisallowDisposable.new(email: "foo@#{disposable_domain}")
expect(user.valid?).to be_falsey
end
context "with disposable domain" do
it "is invalid with disposable domain" do
user = TestUserDisallowDisposable.new(email: "foo@#{disposable_domain}")
expect(user.valid?).to be_falsey
end

it "is invalid if the domain is a subdomain of a disposable domain" do
user = TestUserDisallowDisposable.new(email: "foo@bar.#{disposable_domain}")
expect(user.valid?).to be_falsey
end
it "is invalid with disposable domain even without mx server check" do
user = TestUserDisallowDisposableDomain.new(email: "foo@#{disposable_domain}")
expect(user.valid?).to be_falsey
end

it "allows example.com" do
user = TestUserDisallowDisposable.new(email: "foo@example.com")
expect(user.valid?).to be_truthy
it "is invalid if the domain is a subdomain of a disposable domain" do
user = TestUserDisallowDisposable.new(email: "foo@bar.#{disposable_domain}")
expect(user.valid?).to be_falsey
end

it "allows example.com" do
user = TestUserDisallowDisposable.new(email: "foo@example.com")
expect(user.valid?).to be_truthy
end
end

context "with domain that is not disposable but it's mx server is disposable" do
Expand Down