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


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:

Operation = Dry::Schema.Params do
  required(:parameters).each { | }

Data example:

- 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 :heart:

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.

For now - yes.

Thanks for your answer. I just created the issue.

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!

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

Thanks for your answer @solnic, here is a gist with a simplified example:

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).

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.

Well, I think that this is what I did by adding this issue, but maybe am I missing something? (Sorry for that if it’s the case.)

d’ooh! of course you did :man_facepalming:

1 Like