Rails 7 - ActiveModel date validation | Is there a replacement for validates_timeliness gem?

1.2k Views Asked by At
  • While trying to upgrade my application to Rails 7, I came across the following deprecation warning : ActiveRecord::Base.default_timezone is deprecated and will be removed in Rails 7.1. Use ActiveRecord.default_timezone instead.

  • On further inspection, i found that this is being thrown by the validates_timeliness gem which has not been updated yet.

  • A PR addressing this issue has been made by Mitsuru, but the owner of the gem hasn't merged it yet. I'm guessing that the gem isn't being actively worked on now.

  • My question is this, in Rails 7, can I validate Date and Time (formats and >,< operations) without using an external gem?

  • If not, is there a replacement for validates_timeliness gem that is actively being worked on?

  • In my app, the way that i'm using the validates_timeliness gem is as follows :

      validates :graduation_date, 
                timeliness: { type: :date }, 
                unless: lambda{ |e| e.graduation_date_not_required.present? }
    
    
      validates :start_date, timeliness: { type: :date }
      validates :end_date, timeliness: { type: :date }, unless: :is_current?
    
1

There are 1 best solutions below

2
Rabin Poudyal On

You might be able to use the Rails' new Comparision Validator

Try using like this:

class Event < ApplicationRecord
  validates :start_date, presence: true
  validates :end_date, presence: true
  
  validates_comparison_of :end_date, greater_than: :start_date, other_than: Date.today
end