23andMe / Yamale

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

[Enhancement] Support enum(*args) #188

Closed tjlaboss closed 2 years ago

tjlaboss commented 2 years ago

First of all: thanks for creating Yamale. I love it so far.

It would be useful for the enum validator to support argument expansion inside the API. For example,

# Current behavior
schema = """
meal:
  with_drink: bool()
  num_eggs: num(min=1)
  main_course: enum('eggs', 'eggs, bacon', 'eggs, bacon, sausage, and spam', 'spam, spam, spam, egg, and spam')
"""
# Desired behavior:
foods = ('eggs', 'eggs, bacon', 'eggs, bacon, sausage', 'spam, '*3 + 'egg')
main_courses = [x + ' and spam' for x in foods]
schema = f"""
meal:
  with_drink: bool()
  num_eggs: num(min=1)
  main_course: enum(*{main_courses})
"""

This way, main_courses can be defined or generated in the code without rewriting it by hand in the schema.

I can take a crack at it if there is interest in such a feature.

mildebrandt commented 2 years ago

Hi, thanks for using Yamale!

It seems that this can be solved using this syntax:

f"main_course: enum({', '.join(map(repr, main_courses))})"

Does this work for you?

tjlaboss commented 2 years ago

It does, and that is the workaround I am using. Thought it might be cleaner to handle it automatically within validators.Enum.

mildebrandt commented 2 years ago

I don't think this is something we'd like to support within Yamale. Thank you for your idea though and keep them coming!

tjlaboss commented 2 years ago

All right. Closing the issue.