dry-rb / dry-validation

Validation library with type-safe schemas and rules
https://dry-rb.org/gems/dry-validation
MIT License
1.34k stars 188 forks source link

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

Open colszowka opened 10 months ago

colszowka commented 10 months ago

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

colszowka commented 10 months ago

On another note, I attempted to make a workaround using a complex dry-type instead of the contracts DSL, but this actually runs into the same issue as well

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

    TYPE = Types::Strict::Array.of(Types::Hash.schema(fails_at: Types::Params::DateTime))

    optional(:widgets).maybe(TYPE)
  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"]}}}

The only way I can get this to work is to skip the iso8601 parsing part and use Types::Params::Time and pass Time.now instead, but that's kinda missing the point because I want to return structured errors when a user submits an invalid timestamp

colszowka commented 10 months ago

I came up with a way how to work around this issue, by making my nested date time a string on the contract and then performing the type casting and param validation myself in a rule. This then yields the desired result:

require "pp"
require "dry-validation"
module Types
  include Dry.Types(default: :strict)
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::Strict::String)
      end
    end
  end

  rule(:widgets) do
    value.each.with_index do |entry, i|
      next unless entry[:fails_at]
      value[i][:fails_at] = Types::Params::DateTime[entry.fetch(:fails_at)]
    rescue => err
      key([:widgets, i, :fails_at]).failure "is invalid"
    end
  end
end

pp Hmm.new.call(works_at: Time.now.iso8601, widgets: [{fails_at: Time.now.iso8601}]).errors.to_h
# => {}
pp Hmm.new.call(works_at: Time.now.iso8601, widgets: [{fails_at: "this is not valid"}]).errors.to_h
# => {:widgets=>{0=>{:fails_at=>["is invalid"]}}}

However, this is not super great so I think it would be nice to get this bug fixed regardless of a workaround being available