diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index 5c99da31..c871353e 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -72,7 +72,7 @@ def validate_each(record, attr_name, value) def validate_restrictions(record, attr_name, value, converter) @restrictions_to_check.each do |restriction| begin - restriction_value = converter.type_cast_value(converter.evaluate(options[restriction], record)) + 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 @@ -81,6 +81,8 @@ def validate_restrictions(record, attr_name, value, converter) 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 c7699111..13ded3c8 100644 --- a/spec/validates_timeliness/validator_spec.rb +++ b/spec/validates_timeliness/validator_spec.rb @@ -123,6 +123,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