# \[dry-validation 1.0\] rule key path syntax with array

**URL:** https://discourse.dry-rb.org/t/dry-validation-1-0-rule-key-path-syntax-with-array/805
**Category:** dry-validation
**Created:** [June 13, 2019, 5:43am UTC](https://discourse.dry-rb.org/t/dry-validation-1-0-rule-key-path-syntax-with-array/805 "2019-06-13T05:43:40Z")
**Posts on this page:** 5
**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: [June 13, 2019, 5:43am UTC](https://discourse.dry-rb.org/t/dry-validation-1-0-rule-key-path-syntax-with-array/805/1 "2019-06-13T05:43:40Z")

</div>

Hello!

I’m upgrading dry-validation in our app to 1.0 and am running into issues with key path syntax when one of the params is an array. The schema looks like this:

```auto
params do
  optional(:rooms).array(:hash) do
    required(:guests).array(:hash, GuestSchema)
  end
end

```

Now if I want to validate for example that number of guests in each room has to be 2 how would I go about writing a rule to catch that? I thought it would be a simple case of `rule(rooms: :guests)`, but that raises `rule specifies keys that are not defined by the schema: [{:rooms=>[:guests]}]`.

---

<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: [June 13, 2019, 10:48am UTC](https://discourse.dry-rb.org/t/dry-validation-1-0-rule-key-path-syntax-with-array/805/2 "2019-06-13T10:48:31Z")

</div>

Looks like you hit [this bug](https://github.com/dry-rb/dry-validation/issues/547) so try to use `rule('rooms.guests') { ... }` instead. I’ll fix it in the meantime.

---

<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: [June 13, 2019, 11:52am UTC](https://discourse.dry-rb.org/t/dry-validation-1-0-rule-key-path-syntax-with-array/805/3 "2019-06-13T11:52:50Z")

</div>

Thanks @solnic, but I’m afraid this is something else. The error message is different and the proposed solution doesn’t work for me. I think the difference is that I’m using an array of hashes rather than just a hash.

---

<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: [June 14, 2019, 10:54am UTC](https://discourse.dry-rb.org/t/dry-validation-1-0-rule-key-path-syntax-with-array/805/4 "2019-06-14T10:54:17Z")

</div>

OK so in `1.1.0` this will be possible:

```ruby
require 'dry/validation'

class C < Dry::Validation::Contract
  params do
    optional(:rooms).array(:hash) do
      required(:guests).array(:integer)
    end
  end

  rule(:rooms).each do
    key.failure('must have 2 guests') if key? && value[:guests].equal?(2)
  end
end

contract = C.new

puts contract.call(rooms: [{ guests: [1, 2] }, { guests: [3] }]).errors.to_h.inspect
# {:rooms=>{1=>["must have 2 guests"]}}

```

I’ll be releasing it shortly.

---

<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: [June 16, 2019, 11:08am UTC](https://discourse.dry-rb.org/t/dry-validation-1-0-rule-key-path-syntax-with-array/805/5 "2019-06-16T11:08:45Z")

</div>

That’s great news, thank you!
