Custom error messages for array of hashes

Hello, I’ve encountered a problem when attempting to define custom error messages for array of hashes - I can’t figure out the way to define the structure of errors in YAML file to match my schema. My data structure looks as follows:

{
  events: [
    { name: "Some name", number: 1 },
    { name: "Other name", number: 2 },
  ],
}

Schema:

schema = Dry::Validation.Schema do
  configure { config.messages_file = "config/errors.yml" }

  required(:events).each do
    required(:number).filled(:int?)
    required(:name).filled(:str?)
  end
end

config/errors.yml:

en:
  errors:
    rules:
      events:
        rules:
          name:
            key?: 'custom message: Name key is missing'
            filled?: 'custom message: Missing name in events array'

And the schema call schema.call({ events: [{ number: 2 }] }) which results in the following:

{
  :events => {
    0 => {
       :name => [[0] "is missing"]
    }
  }
}

while I would expect it to be my custom error message for name key.

So I nested rules keyword under the events namespace and expected it to treat everything nested below events as objects. But I suspect that it won’t work with arrays anyway. Does anyone have an idea how should the config/errors.yml look like in order for custom error messages be working for objects in a given array? Thank you very much in advance!

1 Like

I have the same problem, any news about this? Thanks in advance!