# Specifying rule on a field in a hash in an array

**URL:** <https://discourse.dry-rb.org/t/specifying-rule-on-a-field-in-a-hash-in-an-array/865>\
**Category:** dry-validation\
**Created:** [July 31, 2019, 11:46pm UTC](https://discourse.dry-rb.org/t/specifying-rule-on-a-field-in-a-hash-in-an-array/865 "2019-07-31T23:46:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/paul/32/202_2.png) [@paul](https://discourse.dry-rb.org/u/paul)\
**Post date:** [July 31, 2019, 11:46pm UTC](https://discourse.dry-rb.org/t/specifying-rule-on-a-field-in-a-hash-in-an-array/865/1 "2019-07-31T23:46:39Z")

</div>

Upgrading to dry-validation 1.x, we’d like to validate a structure that looks like this:

```ruby
{
  phones: [
    {
      number: "13035551234"
    }
  ]
}

```

```ruby
      params do
        required(:phones).filled.array(:hash) do
          required(:number).value(PhoneNumber::Type, :filled?)
          optional(:type).maybe(:str?)
        end

      rule(:phones).each do
        key.failure("does not seem to be possible") unless PhoneNumber.build(value[:number]).possible?
      end
    end

```

In the previous version of dry-validate, the errors.to\_h came back looking like:

```ruby
{:phones => {0=>{:number=>["phone does not seem to be possible"]}}}

```

And in 1.x, it looks like:

```ruby
{:phones => {0=>["does not seem to be possible"]}}

```

How do you specify the `rule` should match on a hash nested in an array? So far I’ve tried:

```ruby
rule(:phones).each do
  rule(:number) do # undefined method `rule' for #<Dry::Validation::Evaluator:0x000055b9a92786f8>

rule("phones.number") do
rule(phones: number) do # .rule specifies keys that are not defined by the schema

```

Is there some other way to specify this, or is it just not supported (yet)?

---

<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:** [August 1, 2019, 7:53am UTC](https://discourse.dry-rb.org/t/specifying-rule-on-a-field-in-a-hash-in-an-array/865/2 "2019-08-01T07:53:00Z")

</div>

You can do this:

```ruby
key([*path, :number]).failure("does not seem to be possible") unless PhoneNumber.build(value[:number]).possible?

```

In the future there will be a way of specifying a path including array members, ie `rule("phones[].number")` or something like this.
