23andMe / Yamale

A schema and validator for YAML.
MIT License
680 stars 89 forks source link

Conditional with strict #91

Closed devlucasc closed 4 years ago

devlucasc commented 4 years ago

I can't validate a schema like this: Data 1:

id: example
type: type1
value: 10

Data 2:

id: example
type: type2
value: value

Schema:

any(include('v1'), include('v2), include('v3'))
---
v1:
  id: str()
---
v2:
 type: regex('^type1$')
 value: int()
---
v3:
 type: regex('^type2$')
 value: str()

I was only able to do that when I duplicated schema like this:

any(include('v2'), include('v3'))
---
v2:
 id: str()
 type: regex('^type1$')
 value: int()
---
v3:
 id: str()
 type: regex('^type2$')
 value: str()

This occurs because validator in any operator are validated separately. I would like to make a conditional schema validator, defined by the type field

mildebrandt commented 4 years ago

Hi, thanks for using Yamale.

The any validator will return true if at least one of the validators match. When running in strict mode, none of the validators you've defined will match. And when not running in strict mode, v1 would always match. But you've figured that out.

There was a similar request for a conditional schema validator, you can read my thoughts about that here: https://github.com/23andMe/Yamale/issues/71#issuecomment-549932853

So I think you've found the best way to handle your case in the final iteration of your schema.

Let me know if you have any other questions.