Search code examples
ruby-on-railspostgresqlvalidationjsonb

Ruby on Rails Validate date in jsonb (hash)


I have jsonb column in my Postgres db and i have a form with pref["date"] date_field for user.

How I can convert to date and validate user.pref["date"] that is date without error if convert to date will falls?


Solution

  • You can rescue exception for invalid date conversion and add error then

    begin
      date = Date.parse(pref['date'])
    rescue ArgumentError
      # add error for invalid date
    end