23andMe / Yamale

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

How to make schema using regex / or accepting keys #216

Closed dschiller closed 1 year ago

dschiller commented 1 year ago

My yaml looks like

sjdneh7:
  data: testdata
  option: anyoption
xbchd7:
  data: testdata
  option: anyoption
z73ue8:
  data: testdata
  option: anyoption

My scheme looks like

library:
  data: str()
  option: str()

How can I describe the root entries ? As they are indistinguible and vari per entry I would like to know how I can describe them in the yaml schema.

When I change it to

  data: str()
  option: str()

And use --no-strict mode, it somehow works but fails with

  data: Required field missing
  option: Required field missing

Is there a possibility to see the line number it fails at ?

dschiller commented 1 year ago

I copied all keys and used include(), and now I can not find out how to ignore parent tags:

Schema.yaml

sjdneh7: include('library')
xbchd7: include('library')
z73ue8: include('library')
---
library:
  data: str()
  option: str()
  tools:
    any: str(required=false)

Yaml data to validate:

Data.yaml

sjdneh7:
  data: testdata
  option: anyoption
xbchd7:
  data: testdata
  option: anyoption
  tools:
    any: abc
z73ue8:
  data: testdata
  option: anyoption

How can I make tools not mandatory ?

sjdneh7.tools: Required field missing

I tried all validators but none is working. All fail with

yaml.scanner.ScannerError: mapping values are not allowed in this context
dschiller commented 1 year ago

Found out how it works regarding the not mandatory tags:

Schema.yaml

sjdneh7: include('library')
xbchd7: include('library')
z73ue8: include('library')
---
library:
  data: str()
  option: str()
  tools: include('library_tools', required=False)
---
library_tools:
  any: str(required=false)
mildebrandt commented 1 year ago

I think something like this will work for you:

map(include('library'))
---
library:
  data: str()
  option: str()
  tools: map(required=False)
dschiller commented 1 year ago

Thank you a lot Chris, that works perfect!