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

Validation doesn't work if the value is represented as a string #91

Open krtschmr opened 2 years ago

krtschmr commented 2 years ago

We have an object that can hold a value which is just a string. Depends on the STI Child of the object, a specific validation is running.

t.string :value, null: true

class SmartField < ApplicationRecord
  validates :value, date: true
end

SmartField.create! value: Date.today
ActiveRecord::RecordInvalid: Validatie mislukt: Value is geen datum

this looks very odd to me and was unexpected.

      def is_time?(object)
        object.is_a?(Time) || (defined?(Date) and object.is_a?(Date)) || (defined?(ActiveSupport::TimeWithZone) and object.is_a?(ActiveSupport::TimeWithZone))
      end

do we need a string check, aka Date.parse rescue false?