Skip to content

Commit

Permalink
fix(email_validator): handle trailing whitespace in emails (#255)
Browse files Browse the repository at this point in the history
- Updated email_validator to strip trailing whitespace from emails
  when the `multiple` option is enabled.
- Added a test case to ensure emails with trailing whitespace are
  considered invalid.
  • Loading branch information
stulzer authored Oct 20, 2024
1 parent d68c5d7 commit a06fdf7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/valid_email2/email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ def sanitized_values(input)
options = default_options.merge(self.options)

if options[:multiple]
email_list = input.is_a?(Array) ? input : input.split(',')
email_list = input.is_a?(Array) ? input : input.split(',').map(&:strip)
else
email_list = [input]
end

email_list.reject(&:empty?).map(&:strip)
email_list.reject(&:empty?)
end

def error(record, attribute)
Expand Down
5 changes: 5 additions & 0 deletions spec/valid_email2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ class TestUserMultiple < TestModel
user = TestUser.new(email: "[email protected]")
expect(user.valid?).to be_falsy
end

it "is invalid with trailing whitespace" do
user = TestUser.new(email: "[email protected] ")
expect(user.valid?).to be_falsey
end
end

describe "with disposable validation" do
Expand Down

0 comments on commit a06fdf7

Please sign in to comment.