23andMe / Yamale

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

[Feature request] 'in' validator additional usecase #212

Open Amndeep7 opened 1 year ago

Amndeep7 commented 1 year ago

Hi folks, first just wanted to say that I appreciate this project. It's been useful in applying any amount of order at all to our yaml files.

I'm trying to make a schema as follows:

---
actions: list(include('action'))
fields: list(include('field'))
coverage_map: map(map(str(), key=a field's name), key=an action's name)
---
action:
  name: str()
  description: str()
field:
  name: str()
  description: str()

Sample file:

actions:
  - name: open
    description: opens
  - name: close
     description: closes
  - name: idle
    description: idles
fields:
  - name: date
    description: date of action
  - name: which
    description: which item
# mapping incompleteness is intentional
coverage_map:
  open:
    which: one
    date: today
  close:
    which: one

The specific actions and fields defined in this sample are not likely to be anywhere close to the actions and fields that would be in a different sample, but the requirement that the top level keys should be actions and the inner keys be fields will hold true for them all. Consequently, it would be very hard to follow the advice of the maintainer in https://github.com/23andMe/Yamale/issues/71 since the way I interpret what they're saying would imply needing to create a schema for each file, which defeats the purpose of having a schema in the first place.

Unless I'm missing something, it seems like having an 'in' validator would be the only way to make my usecase possible. Is this something that yall would be willing to add as a built-in validator?

nbaju1 commented 1 year ago

If the possible action and field names across the various samples are known you can use enum to solve this, i.e.:

---
actions: list(include('action'))
fields: list(include('field'))
coverage_map: map(map(str(), key=enum('date', 'which')), key=enum('open', 'close', 'idle'))
---
action:
  name: enum('open', 'close', 'idle')
  description: str()
field:
  name: enum('date', 'which')
  description: str()

where you would add all possible values to the enum validators (I only included the ones in your sample).

If you want the keys to only be one of the action and field names present in the yaml file being validated you either need a schema per file or create a custom validator/constraint.

Amndeep7 commented 1 year ago

If the possible action and field names across the various samples are known

They are not, unfortunately. Thanks for your reply.