From a06fdf72cfc0df51c81057c6094aba332b692741 Mon Sep 17 00:00:00 2001 From: Rubens Stulzer Date: Sun, 20 Oct 2024 14:21:58 +0200 Subject: [PATCH] fix(email_validator): handle trailing whitespace in emails (#255) - 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. --- lib/valid_email2/email_validator.rb | 4 ++-- spec/valid_email2_spec.rb | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/valid_email2/email_validator.rb b/lib/valid_email2/email_validator.rb index d96e772..812f328 100644 --- a/lib/valid_email2/email_validator.rb +++ b/lib/valid_email2/email_validator.rb @@ -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) diff --git a/spec/valid_email2_spec.rb b/spec/valid_email2_spec.rb index c1c1a3a..58b8543 100644 --- a/spec/valid_email2_spec.rb +++ b/spec/valid_email2_spec.rb @@ -158,6 +158,11 @@ class TestUserMultiple < TestModel user = TestUser.new(email: "foo@gmail-.com") expect(user.valid?).to be_falsy end + + it "is invalid with trailing whitespace" do + user = TestUser.new(email: "foo@example.com ") + expect(user.valid?).to be_falsey + end end describe "with disposable validation" do