23andMe / Yamale

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

Dict with keys that have different names but the values all match an include #168

Closed eamonnfaherty closed 3 years ago

eamonnfaherty commented 3 years ago

I have the following data

people:
    abc:
      name: eamonn
      age: 100
    def:
      name: tom
      age: 5
    ghi:
      name: finley
      age: 8

I have the following schema:

people:
  abc: include('person')
  def: include('person')
  ghi: include('person')
----------
person:
  name: str()
  age: int()

I want to make this more generic so I can add more people without specifying the keys upfront. e.g I want to be able to add

people:
    jkl:
      name: finley
      age: 8

without modifying the schema.

How I do specify that the key names of people match a regex and their values are of type person. I cannot see an example like this.

eamonnfaherty commented 3 years ago

I used a map to resolve this.

LuisAlejandro commented 2 years ago

@eamonnfaherty could you share how? I'm struggling with the same

mildebrandt commented 2 years ago

Hi @LuisAlejandro, using the yaml mentioned in the original message, the following schema will work:

people: map(include("person"))
---
person:
  name: str()
  age: int()

Let us know if you have any other questions.

LuisAlejandro commented 2 years ago

Thanks!

martinvonwittich commented 1 year ago

I struggled with the same problem; this might make a good addition to the examples in README.md.