23andMe / Yamale

A schema and validator for YAML.
MIT License
680 stars 89 forks source link

Custom key validation #62

Closed Dejv456 closed 5 years ago

Dejv456 commented 5 years ago

Hello,

is it possible to validate custom key ('abc' or 'bcd' or whatever str()) as below:

Data:

abc:
  type: 1
  option: a
bcd:
  type: 2
  option: b

Schema:

???: include('profile')
---
profile:
  type: int()
  option: str()
mildebrandt commented 5 years ago

Hi, there's a PR in review that may allow for this: https://github.com/23andMe/Yamale/pull/60

mildebrandt commented 5 years ago

This is available in version 2.0.

Phidica commented 4 years ago

Hi @mildebrandt, this may be a dumb question but... Could you please explain how to do this now? I couldn't spot this scenario in the readme, and I tried writing the schema like

str(): include('profile')
---
profile:
  type: int()
  option: str()

but just get

Error validating data data.yaml with schema schema.yaml
        str(): Required field missing

I tried searching the repo with words like "custom" or "dynamic" but the former is a common word otherwise and the latter is nowhere to be found... Thanks for your help.

mildebrandt commented 4 years ago

Hi @Phidica, thanks for your interest in Yamale.

For this, it seemed that @Dejv456 didn't want to validate the key per se, but wanted the ability to use anything as the key at the root of the document. To achieve that, we let map() be defined at the top level of the schema, like this:

map(include('user'))
---
user:
    name: str()
    age: int()

Then the following yaml would validate as successful:

bob:
    name: Bob Ross
    age: 52

If you want actual validation for the keys of a map, that was briefly discussed in issue #53. We would welcome a PR that added the functionality outlined in comment https://github.com/23andMe/Yamale/issues/53#issuecomment-516957631

Phidica commented 4 years ago

Ah, okay, thank you for the advice :)