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

Add feature to whitelist disposable domains with vendor/whitelist.yml #119

Merged
merged 1 commit into from
Aug 7, 2018
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
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: "[email protected]")
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