Date coercion in arrays

Hello. I’ve searched through the documentation but I haven’t found a way to coerce valid string dates when they are in an array for dry-validation 1.6.0.

When outside an array, the date coercion works as expected ie:

class TestDateCoercion1 < Dry::Validation::Contract
  params do
    optional(:start_date).value(:date)
  end
end

contract = TestDateCoercion1.new
contract.call(start_date: '2021-03-01')

But inside an array, we see an error ie:

class TestDateCoercion2 < Dry::Validation::Contract
  params do
    required(:projects).each do
      schema do
        optional(:start_date).value(:date)
      end
    end
  end
end

contract = TestDateCoercion2.new
contract.call(projects: [start_date: '2021-03-01'])

Any suggestions much appreciated. Thanks,

Jolyon

Turns out the solution was to use array(:hash) instead of each`. ie

class TestDateCoercion2 < Dry::Validation::Contract
  params do
    required(:projects).array(:hash) do
      optional(:start_date).value(:date)
    end
  end
end

contract = TestDateCoercion2.new
contract.call(projects: [start_date: '2021-03-01'])
1 Like