Python 如何验证映射字段是否至少有一个带有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},

我正在用Cerberus验证大致如下所示的文档:

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