Permalink
Browse files

(Feature) Added strict email notion for email_validator

  • Loading branch information...
cfabianski committed Feb 27, 2012
1 parent 0acaa0e commit d77d9105e5831bfac16cadbeaf9dc055d5e941ff
Showing with 66 additions and 20 deletions.
  1. +12 −1 lib/active_model/validations/email_validator.rb
  2. +54 −19 test/validations/email_test.rb
@@ -2,10 +2,21 @@
module ActiveModel
module Validations
class EmailValidator < EachValidator
EMAIL_REGEXP = %r{\A[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\z}
def validate_each(record,attribute,value)
opts = options.dup
opts[:strict] ||= false
begin
address = Mail::Address.new(value)
valid = address.domain && value.include?(address.address)
valid = address.domain &&
value.include?(address.address) &&
(
!opts[:strict] ||
address.domain.match(EMAIL_REGEXP)
)
rescue Mail::Field::ParseError
valid = false
end
@@ -1,48 +1,83 @@
require 'test_helper.rb'
describe "Email Validation" do
it "accepts valid emails" do
subject = build_email_record :email => 'franck@verrot.fr'
subject.valid?.must_equal true
subject.errors.size.must_equal 0
end
describe "strict: true" do
it "accepts valid emails" do
subject = build_email_record({:email => 'franck@verrot.fr'}, {:strict => true})
subject.valid?.must_equal true
subject.errors.size.must_equal 0
end
it "accepts valid emails" do
subject = build_email_record :email => 'franck@edu.verrot-gouv.fr'
subject.valid?.must_equal true
subject.errors.size.must_equal 0
it "accepts valid emails" do
subject = build_email_record({:email => 'franck@edu.verrot-gouv.fr'}, {:strict => true})
subject.valid?.must_equal true
subject.errors.size.must_equal 0
end
it "accepts complete emails" do
subject = build_email_record({:email => 'Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>'}, {:strict => true})
subject.valid?.must_equal true
subject.errors.size.must_equal 0
end
it "accepts complete emails" do
subject = build_email_record({:email => 'franck@verrotfr'}, {:strict => true})
subject.valid?.must_equal false
subject.errors.size.must_equal 1
end
end
it "accepts complete emails" do
subject = build_email_record :email => 'Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>'
subject.valid?.must_equal true
subject.errors.size.must_equal 0
describe "strict: false (default)" do
it "accepts valid emails" do
subject = build_email_record :email => 'franck@verrot.fr'
subject.valid?.must_equal true
subject.errors.size.must_equal 0
end
it "accepts valid emails" do
subject = build_email_record :email => 'franck@edu.verrot-gouv.fr'
subject.valid?.must_equal true
subject.errors.size.must_equal 0
end
it "accepts complete emails" do
subject = build_email_record :email => 'Mikel Lindsaar (My email address) <mikel@test.lindsaar.net>'
subject.valid?.must_equal true
subject.errors.size.must_equal 0
end
it "accepts complete emails" do
subject = build_email_record :email => 'franck@verrotfr'
subject.valid?.must_equal true
subject.errors.size.must_equal 0
end
end
describe "for invalid emails" do
it "rejects invalid emails" do
subject = build_email_record :email => 'franck.fr'
subject.valid?.must_equal false
subject.errors.size.must_equal 1
end
it 'rejects local emails' do
subject = build_email_record :email => 'franck.fr'
subject.email = 'franck'
subject.valid?.must_equal false
subject.errors.size.must_equal 1
end
it 'generates an error message of type invalid' do
subject = build_email_record :email => 'franck.fr'
subject.valid?.must_equal false
subject.errors[:email].include?(subject.errors.generate_message(:email, :invalid)).must_equal true
end
end
def build_email_record(attrs = {})
def build_email_record(attrs = {}, opts = {})
TestRecord.reset_callbacks(:validate)
TestRecord.validates :email, :email => true
TestRecord.validates :email, :email => { :strict => opts[:strict] }
TestRecord.new attrs
end
end

0 comments on commit d77d910

Please sign in to comment.