# Nested schemas logic (do a OR between 2 nested schemas)

**URL:** <https://discourse.dry-rb.org/t/nested-schemas-logic-do-a-or-between-2-nested-schemas/1000>\
**Category:** dry-validation\
**Created:** [February 10, 2020, 9:13am UTC](https://discourse.dry-rb.org/t/nested-schemas-logic-do-a-or-between-2-nested-schemas/1000 "2020-02-10T09:13:06Z")\
**Posts on this page:** 8\
**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 10, 2020, 9:13am UTC](https://discourse.dry-rb.org/t/nested-schemas-logic-do-a-or-between-2-nested-schemas/1000/1 "2020-02-10T09:13:06Z")

</div>

Hello,

I have a structure in which a property can take multiple forms. I would like to check that it’s valid for one of these defined forms.

Something like:

```auto
Operation = Dry::Schema.Params do
  required(:parameters).each { ParameterSchema.call(value) | ReferenceSchema.call(value) }
end

```

Data example:

```auto
parameters:
- name: Hello # Valid (ParameterSchema)
  description: World
- $ref: /somewhere #Valid (ReferenceSchema)
- 123 # Invalid

```

Is there a way to handle this with schemas, or should I migrate to a contract with a custom rule as it’s not supported (yet)?

Thanks for your answer ❤

---

<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:** [February 10, 2020, 9:36am UTC](https://discourse.dry-rb.org/t/nested-schemas-logic-do-a-or-between-2-nested-schemas/1000/2 "2020-02-10T09:36:04Z")

</div>

This could be supported if we made schemas quack like `dry-logic`’s `Rule` (which is actually how it used to work in dry-validation 0.x). Feel free to report a feature request under dry-schema project, it would be great to restore this behavior.

> [@scharrier](#):
>
> Is there a way to handle this with schemas, or should I migrate to a contract with a custom rule as it’s not supported (yet)?

For now - yes.

---

<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 10, 2020, 11:03am UTC](https://discourse.dry-rb.org/t/nested-schemas-logic-do-a-or-between-2-nested-schemas/1000/3 "2020-02-10T11:03:23Z")

</div>

Thanks for your answer. I just [created the issue](https://github.com/dry-rb/dry-schema/issues/231).

I was trying to migrate my schema to a contract, but as it’s called from another nested schema (I’m implementing a basic validation on a complex struct with multiple levels - OpenAPI JSON schema), I’m a bit stuck.

Is there a “good” way to call a contract from a schema? If not, maybe there’s a way to add some logic to a schema with a small hack? (calling a private method, adding rules on the fly, or something like that?). I’ve been on this for 2 days, reading all the existing examples I could find, without finding any good option.

Thanks again!

---

<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:** [February 10, 2020, 1:01pm UTC](https://discourse.dry-rb.org/t/nested-schemas-logic-do-a-or-between-2-nested-schemas/1000/4 "2020-02-10T13:01:30Z")

</div>

@scharrier please provide an executable script with a minimal example of what you need and I’ll figure something out

---

<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 11, 2020, 1:48pm UTC](https://discourse.dry-rb.org/t/nested-schemas-logic-do-a-or-between-2-nested-schemas/1000/5 "2020-02-11T13:48:32Z")

</div>

Thanks for your answer @solnic, here is a gist with a simplified example: [https://gist.github.com/scharrier/a7eb40a8421531bc75d24436cc781244](https://gist.github.com/scharrier/a7eb40a8421531bc75d24436cc781244)

If there is not easy way to handle this, I’ll wait until the logic handling is implemented at the Schema level! (At the moment I use a JSON schema, but I’d really prefer to implement my validation process with `dry-validation`).

---

<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:** [February 17, 2020, 8:43am UTC](https://discourse.dry-rb.org/t/nested-schemas-logic-do-a-or-between-2-nested-schemas/1000/6 "2020-02-17T08:43:52Z")

</div>

> [@scharrier](#):
>
> If there is not easy way to handle this, I’ll wait until the logic handling is implemented at the Schema leve

I gave it a shot and adding `|` is simple BUT I need to implement dumping composed schemas into dry-logic AST to make this fully work. Could you report an issue in dry-schema about making schemas quack like a dry-logic rule? I think this would be a wonderful addition.

---

<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 18, 2020, 10:19am UTC](https://discourse.dry-rb.org/t/nested-schemas-logic-do-a-or-between-2-nested-schemas/1000/7 "2020-02-18T10:19:06Z")

</div>

> [@solnic](#):
>
> Could you report an issue in dry-schema about making schemas quack like a dry-logic rule?

Well, I think that this is what I did by [adding this issue](https://github.com/dry-rb/dry-schema/issues/231), but maybe am I missing something? (Sorry for that if it’s the case.)

---

<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:** [February 18, 2020, 5:15pm UTC](https://discourse.dry-rb.org/t/nested-schemas-logic-do-a-or-between-2-nested-schemas/1000/8 "2020-02-18T17:15:08Z")

</div>

> [@scharrier](#):
>
> Well, I think that this is what I did by [adding this issue](https://github.com/dry-rb/dry-schema/issues/231), but maybe am I missing something? (Sorry for that if it’s the case.)

d’ooh! of course you did 🤦‍♂️
