23andMe / Yamale

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

Use case: optionality for list elements after first element #162

Closed dutzXCIV closed 3 years ago

dutzXCIV commented 3 years ago

Hello everyone,

first of all thank you very much for this useful package. I came across something that might be already implemented but I couldn't figure it out using the existing documentation. I have a yaml file which looks somewhat like that:

elements:

    - element0:
        param0: ...
        param1: ...
    - element1:
        param1: ... 

As you can see I have a list of "elements", the first one is fully defined and the second one only contains param1. The schema should check if all "params" are present in the first element of the list (element0) and for every other element that comes after it, all these params should be optional. I created a simple schema where I wrote something like this:

elements: List(include('element_parameters'))
---
element_paramters:
    param0: str()
    param1: str()`

Just making all params optional is out of question, they should be defined at least once. So is it possible to define rules for a single element in the list and apply "other rules" to every other element?

Thank you very much in advance!

mildebrandt commented 3 years ago

Hi, thanks for your interest in Yamale!

I don't think this is something that the current set of Yamale validators can enforce. If you control the format of the yaml, I'd be happy to suggest alternative structures that would be better suited towards validation. For example:

objects:
  default_params:
    param0: "my name"
    param1: 123
    param2: "red"
  my_objects:
    - object0:
        param0: "their name"
        param1: 234
    - object1:
        param1: 987
        param2: "blue"
    - object2:
        param0: "dog's name"

Otherwise, take a look at writing a custom validator: https://github.com/23andMe/Yamale#custom-validators

Let me know if you have any questions.

mildebrandt commented 3 years ago

Closing due to inactivity.