Skip to content

Commit

Permalink
Merge pull request #79 from fredngo/master
Browse files Browse the repository at this point in the history
Namespace EmailValidator under the ValidEmail2 namespace.
  • Loading branch information
micke authored Aug 2, 2017
2 parents 550e209 + 88fbf62 commit e812c95
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 37 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ language: ruby
sudo: false

rvm:
- 2.4
- 2.3
- 2.2
- 2.1
- 2.0

gemfile:
- gemfiles/activemodel3.gemfile
- gemfiles/activemodel4.gemfile
- gemfiles/activemodel5.gemfile
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,28 @@ Or install it yourself as:
If you just want to validate that it is a valid email address:
```ruby
class User < ActiveRecord::Base
validates :email, presence: true, email: true
validates :email, presence: true, 'valid_email_2/email': true
end
```

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

To validate that the domain is not a disposable email:
```ruby
validates :email, email: { disposable: true }
validates :email, 'valid_email_2/email': { disposable: true }
```

To validate that the domain is not blacklisted (under vendor/blacklist.yml):
```ruby
validates :email, email: { blacklist: true }
validates :email, 'valid_email_2/email': { blacklist: true }
```

All together:
```ruby
validates :email, email: { mx: true, disposable: true }
validates :email, 'valid_email_2/email': { mx: true, disposable: true }
```

> Note that this gem will let an empty email pass through so you will need to
Expand Down
6 changes: 3 additions & 3 deletions gemfiles/activemodel3.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source "https://rubygems.org"
source 'https://rubygems.org'

gem "activemodel", "~> 3.2.13"
gem 'activemodel', '~> 3.2.22'

gemspec path: "../"
gemspec path: '../'
6 changes: 3 additions & 3 deletions gemfiles/activemodel4.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source "https://rubygems.org"
source 'https://rubygems.org'

gem "activemodel", "~> 4.2.0"
gem 'activemodel', '~> 4.2.9'

gemspec path: "../"
gemspec path: '../'
5 changes: 5 additions & 0 deletions gemfiles/activemodel5.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem 'activemodel', '~> 5.1.2'

gemspec path: '../'
4 changes: 4 additions & 0 deletions lib/email_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This is a shim for keeping backwards compatibility.
# See the discussion in: https://github.com/lisinge/valid_email2/pull/79
class EmailValidator < ValidEmail2::EmailValidator
end
1 change: 1 addition & 0 deletions lib/valid_email2.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "valid_email2/email_validator"
require "email_validator" # Backwards-compatibility shim

module ValidEmail2
def self.disposable_emails
Expand Down
42 changes: 22 additions & 20 deletions lib/valid_email2/email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@
require "active_model"
require "active_model/validations"

class EmailValidator < ActiveModel::EachValidator
def default_options
{ regex: true, disposable: false, mx: false }
end
module ValidEmail2
class EmailValidator < ActiveModel::EachValidator
def default_options
{ regex: true, disposable: false, mx: false }
end

def validate_each(record, attribute, value)
return unless value.present?
options = default_options.merge(self.options)
def validate_each(record, attribute, value)
return unless value.present?
options = default_options.merge(self.options)

address = ValidEmail2::Address.new(value)
address = ValidEmail2::Address.new(value)

error(record, attribute) && return unless address.valid?
error(record, attribute) && return unless address.valid?

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

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

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

def error(record, attribute)
record.errors.add(attribute, options[:message] || :invalid)
def error(record, attribute)
record.errors.add(attribute, options[:message] || :invalid)
end
end
end
17 changes: 13 additions & 4 deletions spec/valid_email2_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
require "spec_helper"

class TestUser < TestModel
validates :email, email: true
validates :email, 'valid_email_2/email': true
end

class TestUserMX < TestModel
validates :email, email: { mx: true }
validates :email, 'valid_email_2/email': { mx: true }
end

class TestUserDisallowDisposable < TestModel
validates :email, email: { disposable: true }
validates :email, 'valid_email_2/email': { disposable: true }
end

class TestUserDisallowBlacklisted < TestModel
validates :email, email: { blacklist: true }
validates :email, 'valid_email_2/email': { blacklist: true }
end

class BackwardsCompatibleUser < TestModel
validates :email, email: true
end

describe ValidEmail2 do
Expand Down Expand Up @@ -44,6 +48,11 @@ class TestUserDisallowBlacklisted < TestModel
user = TestUser.new(email: "[email protected]")
expect(user.valid?).to be_falsey
end

it "still works with the backwards-compatible syntax" do
user = BackwardsCompatibleUser.new(email: "[email protected]")
expect(user.valid?).to be_truthy
end
end

describe "disposable emails" do
Expand Down

0 comments on commit e812c95

Please sign in to comment.