Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Amndeep7 opened this issue Feb 21, 2023 · 2 comments
Open

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

Amndeep7 opened this issue Feb 21, 2023 · 2 comments

Comments

@Amndeep7
Copy link

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 #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
Copy link

nbaju1 commented Feb 21, 2023

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
Copy link
Author

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

They are not, unfortunately. Thanks for your reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants