PythonCerberus:单个字段的多模式?
我试图使用Cerberus验证一些数据,但遇到了一个问题 我定义了几个较小的模式,例如:PythonCerberus:单个字段的多模式?,python,validation,cerberus,Python,Validation,Cerberus,我试图使用Cerberus验证一些数据,但遇到了一个问题 我定义了几个较小的模式,例如: A = {"type": "dict", "required": False, "schema": {"name": {"type": "string"}}} B = {"type": "dict", "required": False, "schema": {"age": {"type": "integer"}}} C = {"type": "dict", "required": False, "sc
A = {"type": "dict", "required": False, "schema": {"name": {"type": "string"}}}
B = {"type": "dict", "required": False, "schema": {"age": {"type": "integer"}}}
C = {"type": "dict", "required": False, "schema": {"gender": {"type": "string"}}}
更高级别的模式如下所示:
{"something": {"type": "list", "schema": "type": [A, B, C]}}
这显然行不通
我想验证一个列表,其中的元素只需要由(a、B、C)
之一验证。我不知道如何与Cerberus合作,我正在寻求帮助
谢谢。试试这个:
A = {"type": "dict", "schema": {"name": {"type": "string"}}}
B = {"type": "dict", "schema": {"age": {"type": "integer"}}}
C = {"type": "dict", "schema": {"gender": {"type": "string"}}}
schema = {'field':{'type':'list','anyof_schema':[A,B,C]}}
v = Validator(schema)
challenge = {'field':[{'name':'a name'}]}
v.validate(challenge)
True
这要归功于anyof.*
,这是所谓的of rules提供的几个选项之一。这些规则允许您定义不同的规则集进行验证。如果根据前缀逻辑all、any、one或none对列表中的集合进行验证,则该字段将被视为有效。有关详细信息,请参阅