codegram / date_validator

A simple, ORM agnostic, Ruby >=2.2 compatible date validator for Rails, based on ActiveModel.
http://thoughts.codegram.com/date-validation-with-rails-3
MIT License
495 stars 82 forks source link

Not working in certain time zones #56

Closed sgringwe closed 7 years ago

sgringwe commented 9 years ago

Our tests run in random time zones. In some of the time zones, the date validator does not seem to work:

The code:

ap @appointment.valid?
ap @appointment.errors.full_messages.join(', ')
ap @appointment.start_date
ap @appointment.end_date

The output:

false
"End date must be a date on or after 2015-02-24"
Tue, 24 Feb 2015 01:11:09 KRAT +07:00
Tue, 24 Feb 2015 02:11:09 KRAT +07:00
akshaysmurthy commented 9 years ago

A snippet of your model validation code code would be very useful here. But it looks like the date given in the validation is a Date object while @appointment.end_date is an ActiveSupport::TimeWithZone object.

Try this -

DATE_GIVEN_IN_THE_VALIDATION.in_time_zone.utc

@appointment.end_date.utc

I think you will find that the validation is right :)

sgringwe commented 9 years ago

@akshaysmurthy my validations are

validates :start_date, presence: true, date: {}
validates :end_date, presence: true, date: { on_or_after: :start_date }

both are a datetime in the db

#  start_date                 :datetime
#  end_date                   :datetime

but it's possible rails is interpreting them differently from the server?

akshaysmurthy commented 9 years ago

@sgringwe If you look at https://github.com/codegram/date_validator/blob/master/lib/active_model/validations/date_validator.rb#L15, you'll see that on_or_after is not a valid check. You should use after_or_equal_to.