23andMe / Yamale

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

[Feature request] "in" validator #71

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi,

first of all thanks for the work on this. It's very easy to use. I do have a feature request though.

It would be great if it were possible to add a validator that checks if a value is within a list provided by the user.

A simple example to illustrate what I'm talking about:

Schema

map(include('category'))
---
category:
  default: in('category.tags')
  tags: list(str(), int())

Data

rating:
  default: Unrated
  # default: must be within the user-defined tags
  # default: "LMAO" would be invalid for example
  tags:
    - Unrated
    - 1
    - 2
    - 3
    - 4
    - 5

Thank you

mildebrandt commented 4 years ago

Hi, thanks for using Yamale!

I'm usually not a fan of schema contained within the file being validated. One, it requires multiple passes through each structure, the schema and the target files, which complicates the code and requires special handling just for that validator. Two, more opinion based, it's better to keep validation values within the schema. If there's a different structure that's valid, then it should require a different schema.

I would suggest you use the enum validator for this case.

ghost commented 4 years ago

Hi,

OK, that's a pity. I'll close this then and handle it manually at runtime.

Cheers