From 2c1d65862a4988c1d12de6f520434c53094606b7 Mon Sep 17 00:00:00 2001 From: Francis Sinson Date: Tue, 3 Oct 2023 12:57:31 +0200 Subject: [PATCH] Allow dynamic error message via Proc or Lambda --- lib/valid_email2/email_validator.rb | 4 ++- spec/valid_email2_spec.rb | 40 ++++++++++++++++++++++++----- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/lib/valid_email2/email_validator.rb b/lib/valid_email2/email_validator.rb index 27bf616..799d3cb 100644 --- a/lib/valid_email2/email_validator.rb +++ b/lib/valid_email2/email_validator.rb @@ -66,7 +66,9 @@ def sanitized_values(input) end def error(record, attribute) - record.errors.add(attribute, options[:message] || :invalid) + message = options[:message].respond_to?(:call) ? options[:message].call : options[:message] + + record.errors.add(attribute, message || :invalid) end end end diff --git a/spec/valid_email2_spec.rb b/spec/valid_email2_spec.rb index 96d4bdd..501448f 100644 --- a/spec/valid_email2_spec.rb +++ b/spec/valid_email2_spec.rb @@ -47,6 +47,14 @@ class TestUserMessage < TestModel validates :email, 'valid_email_2/email': { message: "custom message" } end +class TestUserMessageWithLambda < TestModel + validates :email, 'valid_email_2/email': { message: -> { "custom message lambda" } } +end + +class TestUserMessageWithProc < TestModel + validates :email, 'valid_email_2/email': { message: Proc.new { "custom message proc" } } +end + class TestUserMultiple < TestModel validates :email, 'valid_email_2/email': { multiple: true } end @@ -188,11 +196,11 @@ class TestUserMultiple < TestModel let(:whitelist_domain) { disposable_domain } let(:whitelist_file_path) { "config/whitelisted_email_domains.yml" } - # Some of the specs below need to explictly set the whitelist var or it + # Some of the specs below need to explictly set the whitelist var or it # may be cached to an empty set def set_whitelist ValidEmail2.instance_variable_set( - :@whitelist, + :@whitelist, ValidEmail2.send(:load_if_exists, ValidEmail2::WHITELIST_FILE) ) end @@ -308,11 +316,29 @@ def set_whitelist end end - describe "with custom error message" do - it "supports settings a custom error message" do - user = TestUserMessage.new(email: "fakeemail") - user.valid? - expect(user.errors.full_messages).to include("Email custom message") + context "when message is present" do + describe "with custom error message" do + it "supports settings a custom error message" do + user = TestUserMessage.new(email: "fakeemail") + user.valid? + expect(user.errors.full_messages).to include("Email custom message") + end + end + + describe "with custom error message lambda" do + it "supports settings a custom error message lambda" do + user = TestUserMessageWithLambda.new(email: "fakeemail") + user.valid? + expect(user.errors.full_messages).to include("Email custom message lambda") + end + end + + describe "with custom error message proc" do + it "supports settings a custom error message proc" do + user = TestUserMessageWithProc.new(email: "fakeemail") + user.valid? + expect(user.errors.full_messages).to include("Email custom message proc") + end end end