Skip to content

fix: persist dates and times parsed by format validations - #255

Open
ydah wants to merge 1 commit into
adzap:masterfrom
ydah:fix/formatted-value-persistence
Open

ydah wants to merge 1 commit into
adzap:masterfrom
ydah:fix/formatted-value-persistence

Conversation

@ydah

@ydah ydah commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

With format: 'dd/mm/yyyy', "01/02/2026" can pass a February 1 restriction but be persisted as January 2. Validation uses the specified format, while persistence uses the attribute's default parsing.

require 'active_record'
require 'validates_timeliness'

Time.zone = 'UTC'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.define do
  create_table :events do |t|
    t.date :d
  end
end

ValidatesTimeliness.setup do |config|
  config.extend_orms = [:active_record]
  config.use_plugin_parser = true
end

class Event < ActiveRecord::Base
  validates_date :d, format: 'dd/mm/yyyy', on_or_after: Date.new(2026, 2, 1)
end

record = Event.create!(d: '01/02/2026')
p record.reload.d # Before: 2026-01-02; after: 2026-02-01

Write successfully parsed formatted input back to writable temporal attributes before checking restrictions, then validate the attribute's cast value. This makes validation and persistence use the same interpretation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant