Python 运行时错误:有';在';验证';领域

Python 运行时错误:有';在';验证';领域,python,cerberus,Python,Cerberus,我正在使用cerberusv1.3.2和python-3.8stable来验证用于发送http请求的json数据。我在使用依赖项规则时遇到问题。我的对象有一个字段request\u type和一个可选字段payload,其中包含更多数据。只有在['CREATE','AMEND']中具有请求类型的对象才具有有效负载。当我运行验证时,我得到一个与有效负载中的一个字段相关的错误。以下是我正在运行的代码: from cerberus import Validator request = { "

我正在使用
cerberus
v1.3.2和
python-3.8
stable来验证用于发送http请求的json数据。我在使用
依赖项
规则时遇到问题。我的对象有一个字段
request\u type
和一个可选字段
payload
,其中包含更多数据。只有在
['CREATE','AMEND']
中具有
请求类型的对象才具有
有效负载。当我运行验证时,我得到一个与
有效负载中的一个字段相关的错误。以下是我正在运行的代码:

from cerberus import Validator

request = {
    "request_type": "CREATE",
    "other_field_1": "whatever",
    "other_field_2": "whatever",
    "payload": {
        "id": "123456",
        "jobs": [
            {
                "duration": 1800,
                "other_field_1": "whatever",
                "other_field_2": "whatever"
            }
        ]
    }
}

schema = {
    'request_type': {
        'type': 'string',
        'allowed': ['CREATE', 'CANCEL', 'AMEND'],
        'required': True,
        'empty': False
    },
    'other_field_1': {'type': 'string', },
    'other_field_2': {'type': 'string', },
    'payload': {
        'required': False,
        'schema': {
            'id': {
                'type': 'string',
                'regex': r'[A-Za-z0-9_-]`',
                'minlength': 1, 'maxlength': 32,
                'coerce': str
            },
            'jobs': {
                'type': 'list',
                'schema': {
                    'duration': {
                        'type': 'integer', 'min': 0,
                        'required': True, 'empty': False,
                    },
                    'other_field_1': {'type': 'string', },
                    'other_field_2': {'type': 'string', },
                }
            }
        },
        'dependencies': {'request_type': ['CREATE', 'AMEND']},
    }
}

validator = Validator(schema, purge_unknown=True)
if validator.validate(request):
    print('The request is valid.')
else:
    print(f'The request failed validation: {validator.errors}')
这就是我得到的错误:

"RuntimeError: There's no handler for 'duration' in the 'validate' domain."
我做错什么了吗

对于上下文,我通过使用完全相同的规则来进行验证,但没有使用
依赖项
,而是使用了两个单独的模式,分别命名为
有效负载(schema
无有效负载(schema
)。在
payload\u schema
中,我将
request\u type
的允许值设置为
['CREATE','AMEND']
,在
no\u payload\u schema
中,我将允许值设置为
['CANCEL']
。我在两个模式上运行验证,如果两个模式都没有通过,我将引发一个错误。这听起来有点老套,我想了解如何使用
依赖项
规则来做到这一点。

注意用于映射和序列的规则。
jobs
字段的值不会作为映射进行检查,因为您要求它属于
list
类型。您将需要以下模式:

{“作业”:
{
{“类型”:“列表”,“模式”:
{
“类型”:“dict”,“schema”:{“duration”:…}
}
}
}
}
模式
规则的这种模糊性将在Cerberus的下一个主要版本中解决。为了可读性,可以使用复杂的验证模式


通常建议使用最少的示例来请求支持。

您可以添加正在验证的数据的示例记录吗?刚刚编辑的问题:代码现在是单个块,您应该能够按原样运行它。