Skip to content

Commit

Permalink
Merge pull request #119 from keiko713/whitelist
Browse files Browse the repository at this point in the history
Add feature to whitelist disposable domains with vendor/whitelist.yml
  • Loading branch information
micke authored Aug 7, 2018
2 parents 493e359 + 1e3a496 commit d276b4f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ To validate that the domain is not a disposable email:
validates :email, 'valid_email_2/email': { disposable: true }
```

To validate that the domain is not a disposable email or a disposable email but whitelisted (under vendor/whitelist.yml):
```ruby
validates :email, 'valid_email_2/email': { disposable_with_whitelist: true }
```

To validate that the domain is not blacklisted (under vendor/blacklist.yml):
```ruby
validates :email, 'valid_email_2/email': { blacklist: true }
Expand Down
5 changes: 5 additions & 0 deletions lib/valid_email2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ def self.blacklist
blacklist_file = "vendor/blacklist.yml"
@@blacklist ||= File.exists?(blacklist_file) ? YAML.load_file(File.expand_path(blacklist_file)) : []
end

def self.whitelist
whitelist_file = "vendor/whitelist.yml"
@@whitelist ||= File.exists?(whitelist_file) ? YAML.load_file(File.expand_path(whitelist_file)) : []
end
end
4 changes: 4 additions & 0 deletions lib/valid_email2/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def disposable?
valid? && domain_is_in?(ValidEmail2.disposable_emails)
end

def whitelisted?
domain_is_in?(ValidEmail2.whitelist)
end

def blacklisted?
valid? && domain_is_in?(ValidEmail2.blacklist)
end
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_with_whitelist]
error(record, attribute) && return if address.disposable? && !address.whitelisted?
end

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

class TestUserDisallowDisposableWithWhitelist < TestModel
validates :email, 'valid_email_2/email': { disposable_with_whitelist: true }
end

class TestUserDisallowBlacklisted < TestModel
validates :email, 'valid_email_2/email': { blacklist: true }
end
Expand Down Expand Up @@ -82,6 +86,22 @@ class TestUserDisallowBlacklisted < TestModel
user = TestUserDisallowDisposable.new(email: "foo@example.com")
expect(user.valid?).to be_truthy
end

describe "with whitelisted emails" do
it "should be invalid when the domain is in the list of disposable and there is no whitelist" do
user = TestUserDisallowDisposableWithWhitelist.new(email: "foo@#{ValidEmail2.disposable_emails.first}")
expect(user.valid?).to be_falsey
end

it "should be valid when the domain is in the list of disposable but it is in the whitelist" do
whitelist_domain = ValidEmail2.disposable_emails.first
whitelist_file_path = "vendor/whitelist.yml"
File.open(whitelist_file_path, "w") {|f| f.write whitelist_domain.to_yaml }
user = TestUserDisallowDisposableWithWhitelist.new(email: "foo@#{whitelist_domain}")
expect(user.valid?).to be_falsey
File.delete(whitelist_file_path)
end
end
end

describe "blacklisted emails" do
Expand Down

0 comments on commit d276b4f

Please sign in to comment.