Search code examples
pythoncerberus

Python cerberus – choices of strings


How can I validate a schema for a list of string choices?

Say I want the following animal strings to be valid:

['dog', 'cat', 'lion']

What would the schema look like for checking whether the key of animal contains any of these?

I can't quite figure out how to use the anyOf rule in this context.

Thanks!


Solution

  • How about using regex?

    schema = {'foo': {'regex': r'(ham|spam)'}} 
    document = {'foo': 'ham'}
    v = Validator(schema) 
    v.validate(document)  # returns True