Skip to content

Commit

Permalink
Merge pull request #1202 from alphagov/email-regex
Browse files Browse the repository at this point in the history
Improve email check regex
  • Loading branch information
MuriloDalRi authored Oct 13, 2023
2 parents 24b16b7 + 9c33e53 commit a457520
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/models/support/requests/requester.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ class Requester
attr_accessor :name
attr_reader :email

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

validates :email, presence: true

validates :email, format: { with: /@/ }
validates :email, format: { with: VALID_EMAIL_REGEX }

validates :name, presence: true

Expand Down Expand Up @@ -38,7 +40,7 @@ def anonymous
def collaborator_emails_are_all_valid
if collaborator_emails.present?
collaborator_emails.each do |collaborator_email|
unless /@/.match?(collaborator_email)
unless VALID_EMAIL_REGEX.match?(collaborator_email)
errors.add(:collaborator_emails, "#{collaborator_email} is not a valid email")
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/models/support/requests/requester_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Requests
it { should allow_value("[email protected]").for(:collaborator_emails) }
it { should allow_value("[email protected], [email protected]").for(:collaborator_emails) }
it { should_not allow_value("ab, [email protected]").for(:collaborator_emails) }
it { should_not allow_value("[email protected]").for(:collaborator_emails) }

it "removes all whitespace from the email" do
expect(Requester.new(email: " [email protected]").email).to eq("[email protected]")
Expand Down

0 comments on commit a457520

Please sign in to comment.