From 69ad226ac765bcd8eb4283ea3ea4a52b4e501807 Mon Sep 17 00:00:00 2001 From: Roman Laukhin Date: Wed, 10 May 2023 10:29:34 +0300 Subject: [PATCH] change ActiveRecordValidator - changed method to add error to the record by adding "error_code" --- lib/email_address/active_record_validator.rb | 7 ++++++- test/activerecord/test_ar.rb | 10 ++++++++++ test/activerecord/user.rb | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/email_address/active_record_validator.rb b/lib/email_address/active_record_validator.rb index e47411a..2ede5fe 100644 --- a/lib/email_address/active_record_validator.rb +++ b/lib/email_address/active_record_validator.rb @@ -12,8 +12,12 @@ module EmailAddress # * field: email, # * fields: [:email1, :email2] # + # * code: custom error code (default: :invalid_address) + # * message: custom error message (default: "Invalid Email Address") + # # Default field: :email or :email_address (first found) # + # class ActiveRecordValidator < ActiveModel::Validator def initialize(options = {}) @opt = options @@ -35,10 +39,11 @@ def validate_email(r, f) return if r[f].nil? e = Address.new(r[f]) unless e.valid? + error_code = @opt[:code] || :invalid_address error_message = @opt[:message] || Config.error_message(:invalid_address, I18n.locale.to_s) || "Invalid Email Address" - r.errors.add(f, error_message) + r.errors.add(f, error_code, message: error_message) end end end diff --git a/test/activerecord/test_ar.rb b/test/activerecord/test_ar.rb index 9ba6d50..3e40b53 100644 --- a/test/activerecord/test_ar.rb +++ b/test/activerecord/test_ar.rb @@ -9,11 +9,21 @@ def test_validation user = User.new(email: "Pat.Jones+ASDF#GMAIL.com") assert_equal false, user.valid? assert user.errors.messages[:email].first + user = User.new(email: "Pat.Jones+ASDF@GMAIL.com") assert_equal true, user.valid? end end + def test_validation_error_message + if RUBY_PLATFORM != "java" # jruby + user = User.new(alternate_email: "Pat.Jones+ASDF#GMAIL.com") + assert_equal false, user.valid? + assert user.errors.messages[:alternate_email].first.include?("Check your email") + assert_equal :some_error_code, user.errors.details[:alternate_email].first[:error] + end + end + def test_datatype # Disabled JRuby checks... weird CI failures. Hopefully someone can help? if RUBY_PLATFORM != "java" # jruby diff --git a/test/activerecord/user.rb b/test/activerecord/user.rb index 65e6ca3..ac3ad42 100644 --- a/test/activerecord/user.rb +++ b/test/activerecord/user.rb @@ -48,10 +48,13 @@ class User < ApplicationRecord if defined?(ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 5 attribute :email, :email_address attribute :canonical_email, :canonical_email_address + attribute :alternate_email, :email_address end validates_with EmailAddress::ActiveRecordValidator, fields: %i[email canonical_email] + validates_with EmailAddress::ActiveRecordValidator, + field: :alternate_email, code: :some_error_code, message: "Check your email" def email=(email_address) self[:canonical_email] = email_address