schema
schema copied to clipboard
Add ability to use enum for values in a schema
Use Case: I am creating a schema to validate a dictionary and for a key "key_1" I would like to use an enum to validate that the value is one of "value_1, value_2, value_3".
Can you not already do this with an Or() ?
Or() would allow both, not exclusively.
In [17]: schema = Schema({Or('name', 'value'): six.string_types[0]})
In [18]: schema.validate({'name': '123'}) Out[18]: {'name': '123'}
In [19]: schema.validate({'name': '123', 'value': '234'}) Out[19]: {'name': '123', 'value': '234'}
Ah, the way Schema works you can validate key names individually, not in conjunction to one another, so you can't perform an action (deny) if another key already exists...
Regardless of that, it would be nice to have support for actual Enum subclasses. Right now I'm stuck doing:
Schema(lambda v: v in {i.value for i in SomeEnum})
I might think about this and try implementing it. In the meantime, if anyone has ideas for the interface, let me know.