schema icon indicating copy to clipboard operation
schema copied to clipboard

Add ability to use enum for values in a schema

Open vero-tess opened this issue 6 years ago • 4 comments

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".

vero-tess avatar Feb 06 '19 18:02 vero-tess

Can you not already do this with an Or() ?

skorokithakis avatar Feb 20 '19 07:02 skorokithakis

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'}

kai-kzh avatar Mar 07 '19 18:03 kai-kzh

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...

skorokithakis avatar Mar 10 '19 13:03 skorokithakis

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.

Antrikshy avatar Jul 22 '22 05:07 Antrikshy