From ec9d59df005506b63612c04de434045a50f06ef6 Mon Sep 17 00:00:00 2001 From: ydah Date: Thu, 1 Oct 2026 16:16:25 +0900 Subject: [PATCH] fix: preserve strict restriction validation failures --- lib/validates_timeliness/validator.rb | 6 +++--- spec/validates_timeliness/validator_spec.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index f5a87a2d..189c0cc0 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -73,14 +73,14 @@ def validate_restrictions(record, attr_name, value) @restrictions_to_check.each do |restriction| begin restriction_value = @converter.type_cast_value(@converter.evaluate(options[restriction], record)) - unless value.send(RESTRICTIONS[restriction], restriction_value) - add_error(record, attr_name, restriction, value: value, restriction_value: restriction_value) and break - end + valid = value.send(RESTRICTIONS[restriction], restriction_value) rescue => e unless ValidatesTimeliness.ignore_restriction_errors message = RESTRICTION_ERROR_MESSAGE % [ attr_name, restriction.inspect, e.message ] add_error(record, attr_name, message) and break end + else + add_error(record, attr_name, restriction, value: value, restriction_value: restriction_value) and break unless valid end end end diff --git a/spec/validates_timeliness/validator_spec.rb b/spec/validates_timeliness/validator_spec.rb index 4df22905..15292ad7 100644 --- a/spec/validates_timeliness/validator_spec.rb +++ b/spec/validates_timeliness/validator_spec.rb @@ -105,6 +105,19 @@ end end + describe ':strict option' do + [false, true].each do |ignore_restriction_errors| + it "raises the restriction error when ignore_restriction_errors is #{ignore_restriction_errors}" do + Person.validates_date :birth_date, before: :today, strict: true + + with_config(:ignore_restriction_errors, ignore_restriction_errors) do + expect { Person.new(birth_date: Date.today).valid? } + .to raise_error(ActiveModel::StrictValidationFailed, 'Birth date must be before 2010-01-01') + end + end + end + end + describe ":between option" do describe "array value" do it 'should be split option into :on_or_after and :on_or_before values' do