Python 如何验证映射字段是否至少有一个带有Cerberus的项?
我正在用Cerberus验证大致如下所示的文档:Python 如何验证映射字段是否至少有一个带有Cerberus的项?,python,validation,cerberus,Python,Validation,Cerberus,我正在用Cerberus验证大致如下所示的文档: {"a_dict": {"field1": "test1", "field2": "test2", "field3": "test3"}} "a_dict": {"type": "dict", "schema": {"field1": {"type": "string", "required": False},
{"a_dict": {"field1": "test1",
"field2": "test2",
"field3": "test3"}}
"a_dict": {"type": "dict",
"schema": {"field1": {"type": "string",
"required": False},
"field2": {"type": "string",
"required": False},
"field3": {"type": "string",
"required": False}}}
并非子文档中的所有字段都需要存在,但应该存在一个字段。到目前为止,我的模式如下所示:
{"a_dict": {"field1": "test1",
"field2": "test2",
"field3": "test3"}}
"a_dict": {"type": "dict",
"schema": {"field1": {"type": "string",
"required": False},
"field2": {"type": "string",
"required": False},
"field3": {"type": "string",
"required": False}}}
如何强制至少提供一个字段x
这个问题是从问题中衍生出来的。这就做到了:
string_field = {'type': 'string'}
schema = {'a_dict': {'type': 'dict',
'minlength': 1,
'allow_unknown': False,
'schema':
{f: string_field for f in ('field1', 'field2', 'field3')}
}}
规则确保minlength
中至少有一个字段a_dict
规则确保除allow_unknown
之外没有其他字段通过验证field
- 默认情况下,
规则为required
False