# How to validate with schema block after maybe

**URL:** <https://discourse.dry-rb.org/t/how-to-validate-with-schema-block-after-maybe/284>\
**Category:** Uncategorized\
**Created:** [June 2, 2017, 5:03am UTC](https://discourse.dry-rb.org/t/how-to-validate-with-schema-block-after-maybe/284 "2017-06-02T05:03:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradparker](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/bradparker/32/106_2.png) [@bradparker](https://discourse.dry-rb.org/u/bradparker)\
**Post date:** [June 2, 2017, 5:03am UTC](https://discourse.dry-rb.org/t/how-to-validate-with-schema-block-after-maybe/284/1 "2017-06-02T05:03:39Z")

</div>

I have a case where I need to validate a structure which is allowed to be nil… sounds odd but bear with me. It doesn’t look like nested validations are respected when the parent is allowed to be nil.

```auto
require "dry-validation"

SomeSchema = Dry::Validation.Schema do
  optional(:parent_hash).maybe.schema do
    required(:child_hash).schema do
      required(:attribute).filled(:str?)
    end
  end
end

puts SomeSchema.(
  parent_hash: {
    child_hash: { attribute: "Foobar" }
  },
).inspect

puts SomeSchema.(
  parent_hash: {
    child_hash: { attribute: nil }
  },
).inspect

puts SomeSchema.(
  parent_hash: {
    child_hash: nil
  },
).inspect

```

```auto
#<Dry::Validation::Result output={:parent_hash=>{:child_hash=>{:attribute=>"Foobar"}}} errors={}>
#<Dry::Validation::Result output={:parent_hash=>{:child_hash=>{:attribute=>nil}}} errors={}>
#<Dry::Validation::Result output={:parent_hash=>{:child_hash=>nil}} errors={}>

```

Removing the `maybe` I get this:

```auto
#<Dry::Validation::Result output={:parent_hash=>{:child_hash=>{:attribute=>"Foobar"}}} errors={}>
#<Dry::Validation::Result output={:parent_hash=>{:child_hash=>{:attribute=>nil}}} errors={}>
#<Dry::Validation::Result output={:parent_hash=>{:child_hash=>nil}} errors={:parent_hash=>{:child_hash=>["must be filled"]}}>

```

Even with the maybe removed the validation is only so deep.

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![bradparker](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/bradparker/32/106_2.png) [@bradparker](https://discourse.dry-rb.org/u/bradparker)\
**Post date:** [June 2, 2017, 5:24am UTC](https://discourse.dry-rb.org/t/how-to-validate-with-schema-block-after-maybe/284/2 "2017-06-02T05:24:39Z")

</div>

THE SECRET SAUCE IS PASSING A BLOCK TO `maybe`

```auto
optional(:parent_hash).maybe.schema do

```

Does not work, whereas:

```auto
optional(:parent_hash).maybe do
  schema do

```

Does.

---

<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 6, 2017, 9:31am UTC](https://discourse.dry-rb.org/t/how-to-validate-with-schema-block-after-maybe/284/3 "2017-06-06T09:31:12Z")

</div>

Glad you figured it out. In 1.0.0 we’ll either add support for chaining, or at least raise an error when somebody is trying to chain.
