Skip to content

Commit

Permalink
Merge pull request #221 from franee/allow-dynamic-error-message
Browse files Browse the repository at this point in the history
Allow dynamic error message via Proc or Lambda
  • Loading branch information
micke authored Oct 3, 2023
2 parents 7ff85c4 + 2c1d658 commit fe74f18
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/valid_email2/email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 33 additions & 7 deletions spec/valid_email2_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit fe74f18

Please sign in to comment.