23andMe / Yamale

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

Required key with optional value #219

Closed felipe88alves closed 1 year ago

felipe88alves commented 1 year ago

Can the none flag be used when the required flag is set to True (by default)? I need to validate manifests where the value of the required field is set dynamically using ytt (https://carvel.dev/ytt/)

Sample: required_key: #@ "{}".format(source)

Attempted yamale schema: required_key: str(none=True)

Somehow this does not seem to work. Am I doing this wrong? Is there another way of doing this?

mildebrandt commented 1 year ago

Since there is no value, it's not considered a string. Instead, you can use the null() validator:

required_key: null()
felipe88alves commented 1 year ago

Mea culpa.

Forgot to mention that ytt will not always be used. So I need it to be a string or null(). But I guess that answers the question anyway.

I can just use:

required_key: any(str(), null())

Thanks again for the great support!