Search code examples
rubyvalidationhanami

Hanami validate year less than X


I want to check that a date object I have in a validator.rb file has a year field that is less than the year 10000.

required(:my_date_object).maybe(
            :date?,
            lt?: '10000-01-01'
          )

When running system tests, the following error shows up:

ArgumentError:
   comparison of Date with String failed

Should I look into converting the date field into a string using to_s or something similar and then doing a regexp format check? Or is there a more straightforward way of checking that the date is less than the year 10000?


Solution

  • You need to create a Date for the lt?.

    You can write it like follows:

    required(:my_date_object) { lt?(Date.new(10000, 1, 1)) }