diff --git a/lib/validates_timeliness/validator.rb b/lib/validates_timeliness/validator.rb index 1c1c0d0..16bd42d 100644 --- a/lib/validates_timeliness/validator.rb +++ b/lib/validates_timeliness/validator.rb @@ -60,6 +60,7 @@ module ValidatesTimeliness def validate_restrictions(record, attr_name, value) if configuration[:with_time] || configuration[:with_date] value = combine_date_and_time(value, record) + return if value.nil? end value = self.class.type_cast_value(value, implied_type, configuration[:ignore_usec]) diff --git a/spec/validator_spec.rb b/spec/validator_spec.rb index 657228b..b9826fa 100644 --- a/spec/validator_spec.rb +++ b/spec/validator_spec.rb @@ -442,6 +442,12 @@ describe ValidatesTimeliness::Validator do should_have_no_error(:birth_date, :on_or_before) end + it "should skip restriction validation if :with_time value evaluates to nil" do + configure_validator(:type => :date, :with_time => lambda { nil }, :on_or_before => Time.mktime(2000,1,1,12,30)) + validate_with(:birth_date, "2000-01-01") + should_have_no_error(:birth_date, :on_or_before) + end + it "should should ignore usec value on combined value if :ignore_usec option is true" do configure_validator(:type => :date, :with_time => Time.mktime(2000,1,1,12,30,0,500), :is_at => Time.mktime(2000,1,1,12,30), :ignore_usec => true) validate_with(:birth_date, "2000-01-01") @@ -463,6 +469,12 @@ describe ValidatesTimeliness::Validator do should_have_no_error(:birth_time, :on_or_before) end + it "should skip restriction validation if :with_date value is nil" do + configure_validator(:type => :time, :with_date => lambda { nil }, :on_or_before => Time.mktime(2000,1,1,12,30)) + validate_with(:birth_time, "12:30") + should_have_no_error(:birth_time, :on_or_before) + end + it "should should ignore usec value on combined value if :ignore_usec option is true" do configure_validator(:type => :time, :with_date => Date.new(2000,1,1), :on_or_before => Time.mktime(2000,1,1,12,30), :ignore_usec => true) validate_with(:birth_time, Time.mktime(2000,1,1,12,30,0,50))