# Apply nested schema to all values of a hash

**URL:** <https://discourse.dry-rb.org/t/apply-nested-schema-to-all-values-of-a-hash/997>\
**Category:** dry-validation\
**Created:** [February 7, 2020, 3:14pm UTC](https://discourse.dry-rb.org/t/apply-nested-schema-to-all-values-of-a-hash/997 "2020-02-07T15:14:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![scharrier](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/scharrier/32/484_2.png) [@scharrier](https://discourse.dry-rb.org/u/scharrier)\
**Post date:** [February 7, 2020, 3:14pm UTC](https://discourse.dry-rb.org/t/apply-nested-schema-to-all-values-of-a-hash/997/1 "2020-02-07T15:14:05Z")

</div>

Hello there,

I’d like to validate this structure:

```auto
paths:
  random_key_one:
    get:
      description: 'Hello'
  random_key_2:
    put:
      description: 'World'

```

I want to check, for each value of the `paths` hash (so, for any key of the hash) that it has a `get` or a `put` nested object with a `description` attribute.

I didn’t find any way to apply a nested schema in this situation. Does somebody have a hint for me?

Thanks a lot, and sorry for my trivial question.  
Sébastien

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [February 7, 2020, 9:28pm UTC](https://discourse.dry-rb.org/t/apply-nested-schema-to-all-values-of-a-hash/997/2 "2020-02-07T21:28:53Z")

</div>

This is unsupported, you can track this feature request in dry-schema [https://github.com/dry-rb/dry-schema/issues/37](https://github.com/dry-rb/dry-schema/issues/37)  
As a workaround, you can write a rule for `:paths` and iterate over keys there.

---

<div class="post-metadata">

**Author:** ![scharrier](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/scharrier/32/484_2.png) [@scharrier](https://discourse.dry-rb.org/u/scharrier)\
**Post date:** [February 8, 2020, 3:36pm UTC](https://discourse.dry-rb.org/t/apply-nested-schema-to-all-values-of-a-hash/997/3 "2020-02-08T15:36:24Z")

</div>

Thx for your quick answer @flash-gordon, and for the hint. I cannot find any example for this kind of situation, if you have a few minutes, could you please help me with that part (iterating over keys)?

I can guess that it starts with something like this, but I’m still confused on the validation part:

```auto
rule(:paths) do
  value.each do |(k, value)|
    # How to check that `value` matches a specific schema, AND to set the errors at the correct path?
    # Something like...
    key([:paths, k]).failure('invalid') if #what ?
    # But how to merge error messages of the nested schema test?
  end
end   

```

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [February 9, 2020, 3:19pm UTC](https://discourse.dry-rb.org/t/apply-nested-schema-to-all-values-of-a-hash/997/4 "2020-02-09T15:19:12Z")

</div>

There’s no standard solution for merging errors automatically, you’ll need to do it manually in the way that works for you. For example, I have code like this in one place

```ruby
rule(:some_collection).each do
  case validate_element.(value) # some other contract injected above
  in Success then nil
  in Failure(errors)
    errors.each do |path, messages|
      key([:some_collection, *path]).failure(message)
    end
  end
end

```

---

<div class="post-metadata">

**Author:** ![scharrier](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/scharrier/32/484_2.png) [@scharrier](https://discourse.dry-rb.org/u/scharrier)\
**Post date:** [February 9, 2020, 5:32pm UTC](https://discourse.dry-rb.org/t/apply-nested-schema-to-all-values-of-a-hash/997/5 "2020-02-09T17:32:02Z")

</div>

👋 , thanks for your answer, it helped a lot 🙂

I finally got this working:

```auto
rule(:paths) do
  value.each do |(key, value)|
    PathSchema.call(value).errors.each do |error|
      key([:paths, *error.path]).failure(error.text)
    end
  end
end

```
