23andMe / Yamale

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

pattern-match elements and retain requirement #143

Closed RobertHana closed 3 years ago

RobertHana commented 4 years ago

I want to somehow match keys AND retain value requirement like so:

# data
req_key: value
key1: value
key2: value
# schema
req_key: any()
regex('^key.*$'): any(required=False)

I can't figure out how to do this currently with a map and without a custom validator.

mildebrandt commented 4 years ago

Hi, thanks for using Yamale. I think you're right...I don't see a way to perform exactly this validation. Here's the closest I could get:

req_key: any()
keys: any(include('keys'), required=False)
---
keys:
    map(any(), key=regex('^key.*$'))

Of course, that means putting your optional keys under a separate mapping like this:

req_key: value
keys:
  key1: value
  key2: value

Validating a required key and optional mapping at the same node isn't something we support at this time.

mildebrandt commented 3 years ago

Closing due to lack of activity, and I don't see us supporting this exact use case.