Python cerberus – 字符串的选择

Python cerberus – choices of strings

如何验证字符串选择列表的模式?

假设我希望以下动物字符串有效:

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

用于检查 animal 的键是否包含其中任何一个的模式会是什么样子?

我不太明白如何在这种情况下使用 anyOf 规则。

谢谢!

使用正则表达式如何?

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

我认为 allowed 规则更可取,因为它传达了目的:

schema = {"animal": {"allowed": ["cat", "dog", "lion"]}}