I wanted to do a simple regex-based email validation:
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /\A([\S]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
record.errors[attribute] << (options[:message] || 'is not an email')
end
end
end
Which is mentioned in rails documentation. I have checked it in rubular and everythings works fine, but in rails, emails with dot in local address fails validation for some reason ([email protected]). What's weird is that it accepts any other non-whitespace characters (e.g &^%#!*() excluding @ of course).
class User < ApplicationRecord
validates :email, presence: true, length: { maximum: 64 },
uniqueness: { case_sensitive: false }, email: true
validates :password, presence: true, length: { in: 8..64 }
validates :first_name, length: { in: 2..16 }, allow_blank: true
validates :last_name, length: { in: 2..32 }, allow_blank: true
validates :student_id, length: { in: 5..6 }, allow_blank: true,
numericality: true
end
And failing test:
test 'should validate email with special characters in local address' do
@user.email = '[email protected]'
assert @user.valid?
end
Whole user_test.rb
class UserTest < ActiveSupport::TestCase
def setup
@user = User.new(email: '[email protected]', password: 'password12',
first_name: 'John', last_name: 'Doe',
student_id: '112233')
end
# Presence validation
test 'user validate user' do
assert @user.valid?
end
test 'should not validate empty email' do
@user.email = ''
assert_not @user.valid?
end
test 'should not validate blank password' do
@user.password = ' '
assert_not @user.valid?
end
test 'should validate blank first name' do
@user.first_name = ''
assert @user.valid?
end
test 'should validate blank last name' do
@user.last_name = ''
assert @user.valid?
end
test 'should validate blank student id' do
@user.student_id = ''
assert @user.valid?
end
# Length validation
test 'should not validate too long email' do
@user.password = 'a' * 65
assert_not @user.valid?
end
test 'should not validate too long password' do
@user.password = 'a' * 65
assert_not @user.valid?
end
test 'should not validate too short password' do
@user.password = 'a' * 7
assert_not @user.valid?
end
test 'should not validate too long first name' do
@user.first_name = 'a' * 17
assert_not @user.valid?
end
test 'should not validate too short first name' do
@user.first_name = 'a'
assert_not @user.valid?
end
test 'should not validate too long last name' do
@user.last_name = 'a' * 33
assert_not @user.valid?
end
test 'should not validate too short last name' do
@user.last_name = 'a'
assert_not @user.valid?
end
test 'should not validate too long student id' do
@user.student_id = '1234567'
assert_not @user.valid?
end
test 'should not validate too short student id' do
@user.student_id = '1234'
assert_not @user.valid?
end
# Email format validation
test 'should not validate email with wrong format' do
@user.email = 'john.doe@domain'
assert_not @user.valid?
end
test 'should validate email with special characters in local address' do
@user.email = '[email protected]'
assert @user.valid?
end
test 'should not validate email with special characters in domain' do
@user.email = 'john.doe@dom_ain.com'
assert_not @user.valid?
end
# Numeric-only validation
test 'should not validate student id with non-numeric characters' do
@user.student_id = 'ab123c'
assert_not @user.valid?
end
# Uniqueness validation
test 'should not validate duplicated email' do
@copycat_user = @user.dup
@user.save
assert_not @copycat_user.valid?
end
end
Any idea what could be the reason for that weird behaviour?
I've got the resolution.
I had fixture with same parameters as
Since email must be unique, it gave me an error that email has already been taken.