23andMe / Yamale

A schema and validator for YAML.
MIT License
670 stars 88 forks source link

Unable to specify required regex within a list #193

Closed rachelrice closed 2 years ago

rachelrice commented 2 years ago

With the following schema: tags: list(regex('^sometimes$'), regex('^always$', required=True), required=True)

And the following yaml to be validated:

tags:
  - sometimes

Expectation is that validation would fail, because required list item matching '^always$' is not present. Actual result is that validation succeeds.

Is there any way to specify a mixture of optional and required regex within a list?

mildebrandt commented 2 years ago

Hi, thanks for using Yamale!

This is one of those tricky areas....and it affects all validators, not just regex. From the documentation:

Every validator takes a required keyword telling Yamale whether 
or not that node must exist. By default every node is required.

And for list():

If one or more validators are passed to list() only nodes that pass 
at least one of those validators will be accepted.

So, every validator by default is required...and in a list, it checks if the element matches at least one validator.

These two statements are at odds with each other, and I don't think they can be reconciled. If a user added required=True to their validator, that doesn't change the behavior since that's the default setting. And if the user wanted to set all the optional validators of the list to required=False, that's a major breaking change to the current behavior of lists.

Unfortunately, I don't think the base validators, as written, can validate what you need. If you want to support this use case, you'll need to write a custom validator: https://github.com/23andMe/Yamale#custom-validators

Let us know if you have any follow up questions.

rachelrice commented 2 years ago

@mildebrandt thank you for the response