This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #102 from mena-devs/user-email-validation
User email validation improvements
- Loading branch information
Showing
6 changed files
with
94 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,34 @@ | ||
class EmailValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
record.errors[attribute] << (options[:message] || "must be a valid email address") unless email_valid?(value) | ||
record.errors[attribute] << email_invalid_error_message unless email_valid?(value)\ | ||
&& handle_contains_number_of_dots(value)\ | ||
&& handle_last_character_is_not_dot(value) | ||
end | ||
|
||
private | ||
|
||
def email_invalid_error_message | ||
# options[:message] || I18n.t('errors.messages.email') | ||
(options[:message] || 'must be a valid email address') | ||
end | ||
|
||
def email_valid?(email_address) | ||
return true if email_address.blank? | ||
|
||
email_address =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i | ||
end | ||
|
||
def handle_contains_number_of_dots(email_address) | ||
return true if email_address.blank? | ||
|
||
email_handle = email_address.split('@') | ||
email_handle[0].count('.') < 3 if email_handle && !email_handle.blank? | ||
end | ||
|
||
def handle_last_character_is_not_dot(email_address) | ||
return true if email_address.blank? | ||
|
||
email_handle = email_address.split('@') | ||
email_handle[0][-1] != '.' if email_handle && !email_handle.blank? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe EmailValidator, type: :model do | ||
describe "Email validations" do | ||
let(:user) { create(:user) } | ||
|
||
it 'email_valid?' do | ||
user.email = 'random-email' | ||
|
||
expect(user.valid?).to be_falsey | ||
end | ||
|
||
it 'handle_contains_number_of_dots' do | ||
user.email = 'x.d.va.as.z@gmail.com' | ||
|
||
expect(user.valid?).to be_falsey | ||
end | ||
|
||
it 'handle_last_character_is_not_dot' do | ||
user.email = 'x.blue-as.@gmail.com' | ||
|
||
expect(user.valid?).to be_falsey | ||
end | ||
end | ||
end |