dry-rb/dry-validation

False-positive when validating a nested datetime in an array of hashes

Open

#732 opened on Oct 18, 2023

View on GitHub
 (2 comments) (0 reactions) (0 assignees)Ruby (193 forks)github user discovery
bughelp wanted

Repository metrics

Stars
 (1,421 stars)
PR merge metrics
 (PR metrics pending)

Description

Hey all, first of all thanks for the great library, we're using this gem a lot (alongside a variety of other tools from the dry-rb ecosystem) and find it super-useful :tada: . Thank you all for the work you're putting into these great tools!

Describe the bug

I have encountered a very strange behaviour when dealing with validations on hashes inside an array, where the nested datetime fails to validate, whereas the same rule at the top level works just fine.

To Reproduce

require "pp"
require "dry-validation"
module Types
  include Dry.Types(default: :nominal)
end

class Hmm < Dry::Validation::Contract
  params do
    optional(:works_at).filled(Types::Params::DateTime)

    optional(:widgets).maybe(:array).each do
      hash do
        optional(:fails_at).filled(Types::Params::DateTime)
      end
    end
  end
end

pp Hmm.new.call(works_at: Time.now.iso8601, widgets: [{fails_at: Time.now.iso8601}]).errors.to_h
# => {:widgets=>{0=>{:fails_at=>["must be a date time"]}}}

Expected behavior

The example above should be valid, but is not. I also tried experimenting with a variety of other types but all have the same issue.

My environment

  • Affects my production application: YES (such that I cannot make valid parameters pass through my contract)
  • Ruby version: 3.2.2
  • OS: Linux
  • dry-validation 1.10.0 (latest)
  • dry-schema 1.13.3 (latest)
  • dry-types 1.7.1 (latest)

Contributor guide