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

Fix domain validation behavior #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions lib/valid_email/email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def validate_each(record,attribute,value)
elsif r && options[:mx_with_fallback]
require 'valid_email/mx_with_fallback_validator'
r = MxWithFallbackValidator.new(:attributes => attributes, message: options[:message]).validate(record)
elsif r && options[:domain]
require 'valid_email/domain_validator'
r = DomainValidator.new(:attributes => attributes, message: options[:message]).validate(record)
end
# Check if domain is disposable
if r && options[:ban_disposable_email]
Expand Down
8 changes: 4 additions & 4 deletions lib/valid_email/validate_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ def valid?(value, user_options={})

# Check if domain has DNS MX record
if options[:mx]
require 'valid_email/mx_validator'
return mx_valid?(value)
end

if options[:domain]
require 'valid_email/domain_validator'
return domain_valid?(value)
end

Expand All @@ -58,7 +56,7 @@ def valid_local?(local)
return false unless local.length <= LOCAL_MAX_LEN
# Emails can be validated by segments delineated by '.', referred to as dot atoms.
# See http://tools.ietf.org/html/rfc5322#section-3.2.3
return local.split('.', -1).all? { |da| valid_dot_atom?(da) }
local.split('.', -1).all? { |da| valid_dot_atom?(da) }
end

def valid_dot_atom?(dot_atom)
Expand All @@ -83,7 +81,7 @@ def valid_dot_atom?(dot_atom)
# If we're not in a quoted dot atom then no special characters are allowed.
return false unless ((SPECIAL_CHARS | SPECIAL_ESCAPED_CHARS) & dot_atom.split('')).empty?
end
return true
true
end

def mx_valid?(value, fallback=false)
Expand All @@ -110,6 +108,8 @@ def domain_valid?(value)
return false unless m.domain

!(m.domain =~ DOMAIN_REGEX).nil?
rescue Mail::Field::ParseError
false
end

def ban_disposable_email?(value)
Expand Down
30 changes: 30 additions & 0 deletions spec/email_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
end

person_class_domain = Class.new do
include ActiveModel::Validations
attr_accessor :email
validates :email, :email => { :domain => true }
end

person_class_domain_separated = Class.new do
include ActiveModel::Validations
attr_accessor :email
validates :email, :domain => true
Expand Down Expand Up @@ -235,6 +241,30 @@
expect(subject.errors[:email]).to be_empty
end
end

describe "validating domain separately" do
subject { person_class_domain_separated.new }

it "does not pass with an invalid domain" do
subject.email = "[email protected]$\'"
expect(subject.valid?).to be_falsey
expect(subject.errors[:email]).to eq errors
end

it "passes with valid domain" do
subject.email = '[email protected]'
expect(subject.valid?).to be_truthy
expect(subject.errors[:email]).to be_empty
end

context 'with a mail that would raise a parsing error' do
it 'does not raise' do
subject.email = '@example.org'
expect(subject.valid?).to be_falsey
expect { subject.valid? }.not_to raise_error
end
end
end
end

describe "Can allow nil" do
Expand Down