Skip to content

Commit

Permalink
Merge pull request #167 from vdolbilov/whitelisted-disposable-domains
Browse files Browse the repository at this point in the history
Whitelisted disposable domains
  • Loading branch information
micke authored Oct 30, 2020
2 parents eb82d74 + 967b01f commit c0405c9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,17 @@ To validate that the domain is not a disposable email (checks domain only, does
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):
To validate that the domain is not a disposable email or a disposable email (checks domain and MX server) but whitelisted (under config/whitelisted_email_domains.yml):
```ruby
validates :email, 'valid_email_2/email': { disposable_with_whitelist: true }
```

To validate that the domain is not a disposable email or a disposable email (checks domain only, does not check MX server) but whitelisted (under config/whitelisted_email_domains.yml):

```ruby
validates :email, 'valid_email_2/email': { disposable_domain_with_whitelist: true }
```

To validate that the domain is not blacklisted (under config/blacklisted_email_domains.yml):
```ruby
validates :email, 'valid_email_2/email': { blacklist: true }
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 @@ -37,6 +37,10 @@ def validate_each(record, attribute, value)
error(record, attribute) && return if addresses.any? { |address| address.disposable? && !address.whitelisted? }
end

if options[:disposable_domain_with_whitelist]
error(record, attribute) && return if addresses.any? { |address| address.disposable_domain? && !address.whitelisted? }
end

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

class TestUserDisallowDisposableDomainWithWhitelist < TestModel
validates :email, 'valid_email_2/email': { disposable_domain_with_whitelist: true }
end

class TestUserDisallowBlacklisted < TestModel
validates :email, 'valid_email_2/email': { blacklist: true }
end
Expand Down Expand Up @@ -180,8 +184,18 @@ class TestUserMultiple < TestModel
let(:whitelist_domain) { disposable_domain }
let(:whitelist_file_path) { "config/whitelisted_email_domains.yml" }

# Some of the specs below need to explictly set the whitelist var or it
# may be cached to an empty set
def set_whitelist
ValidEmail2.instance_variable_set(
:@whitelist,
ValidEmail2.send(:load_if_exists, ValidEmail2::WHITELIST_FILE)
)
end

after do
FileUtils.rm(whitelist_file_path, force: true)
set_whitelist
end

it "is invalid if the domain is disposable and not in the whitelist" do
Expand All @@ -191,9 +205,22 @@ class TestUserMultiple < TestModel

it "is valid if the domain is disposable but in the whitelist" do
File.open(whitelist_file_path, "w") { |f| f.write [whitelist_domain].to_yaml }
set_whitelist
user = TestUserDisallowDisposableWithWhitelist.new(email: "foo@#{whitelist_domain}")
expect(user.valid?).to be_truthy
end

it "is invalid if the domain is a disposable_domain and not in the whitelist" do
user = TestUserDisallowDisposableDomainWithWhitelist.new(email: "foo@#{whitelist_domain}")
expect(user.valid?).to be_falsey
end

it "is valid if the domain is a disposable_domain but in the whitelist" do
File.open(whitelist_file_path, "w") { |f| f.write [whitelist_domain].to_yaml }
set_whitelist
user = TestUserDisallowDisposableDomainWithWhitelist.new(email: "foo@#{whitelist_domain}")
expect(user.valid?).to be_truthy
end
end
end

Expand Down

0 comments on commit c0405c9

Please sign in to comment.