# Generic and specific rule for a nested array

**URL:** <https://discourse.dry-rb.org/t/generic-and-specific-rule-for-a-nested-array/986>\
**Category:** dry-validation\
**Created:** [January 21, 2020, 8:50am UTC](https://discourse.dry-rb.org/t/generic-and-specific-rule-for-a-nested-array/986 "2020-01-21T08:50:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DawidJanczak](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/dawidjanczak/32/261_2.png) [@DawidJanczak](https://discourse.dry-rb.org/u/DawidJanczak)\
**Post date:** [January 21, 2020, 8:50am UTC](https://discourse.dry-rb.org/t/generic-and-specific-rule-for-a-nested-array/986/1 "2020-01-21T08:50:21Z")

</div>

Hi again,

This is probably an odd example, but I was wondering if it’s possible and/or how else we should be handling this. We have a deeply nested schema with an array of hashes where one of the attributes is also array of hashes. Now for that nested array it doesn’t seem to be possible to add an error both to the attribute itself and to its nested children. You can find a trivial example where an error is always being set below:

```auto
class RulesExample < ApplicationContract
  params do
    required(:outer).array(:hash) do
      required(:inner).array(:hash) do
        required(:attr).filled(:integer)
      end
    end
  end

  rule(:outer).each do
    value[:inner].each_with_index do |inner, idx|
      inner_nested_key = key(key.path.keys + [:inner, idx])
      inner_nested_key.failure("Inner #{inner} specific failure")
    end

    inner_key = key(key.path.keys + [:inner])
    inner_key.failure('Inner generic failure')
  end
end

```

This fails with

```auto
NoMethodError: undefined method `[]' for nil:NilClass
from /usr/local/bundle/gems/dry-validation-1.4.2/lib/dry/validation/message_set.rb:108:in `block in messages_map'

```

From glancing over the code it seems to me like once a message is being found on `outer[i]inner[j]` it can no longer be set on `outer[i]inner` because in the first example a hash is expected and in the other it’s an array (or the other way around).

Having just one rule (either on the attribute or on the children) works just fine.

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [January 22, 2020, 8:30am UTC](https://discourse.dry-rb.org/t/generic-and-specific-rule-for-a-nested-array/986/2 "2020-01-22T08:30:39Z")

</div>

That’s a bug, please report an issue about it in dry-validation project.

---

<div class="post-metadata">

**Author:** ![DawidJanczak](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/dawidjanczak/32/261_2.png) [@DawidJanczak](https://discourse.dry-rb.org/u/DawidJanczak)\
**Post date:** [January 22, 2020, 9:08am UTC](https://discourse.dry-rb.org/t/generic-and-specific-rule-for-a-nested-array/986/3 "2020-01-22T09:08:36Z")

</div>

All done, thanks!
