Validation 允许架构中存在未知键但已验证的值

Validation 允许架构中存在未知键但已验证的值,validation,cerberus,Validation,Cerberus,在键未知但值具有明确模式的情况下,验证dict的最佳方法是什么。 例如: data = { 'name': 'test', 'department': { 'unknown_key': { 'known_key': 'good', 'unknown_key': 'bad' } }} 我试过了 schema = { 'name': { 'type': 'string' }, 'department': { 'type': 'dict

在键未知但值具有明确模式的情况下,验证dict的最佳方法是什么。 例如:

data = {
'name': 'test',
'department': {
    'unknown_key': {
        'known_key': 'good',
        'unknown_key': 'bad'
    }
}}
我试过了

schema = {
'name': {
    'type': 'string'
},
'department': {
    'type': 'dict',
    'allow_unknown': {
        'schema': {
            'type': 'dict',
            'schema': {'known_key': {'type': 'string'},
                       'must_have_key': {'type': 'string'}}}
    },
}}
但随着验证的通过,这项工作失败了。它应该在缺少的
必须有\u键
未知\u键
上都失败。我定义了什么错误吗?

您可以使用该规则为映射中的原始值数据定义规则:

schema = {
    'name': {'type': 'string'},
    'department': {
        'type': 'dict',
        'valueschema': {
            'type': 'dict',
            'schema':  {
                'known_key': {'type': 'string'},
                'must_have_key': {'type': 'string', 'required': True}
            }
        }
    }
}

也许应该有一种方法来防止
allow_unknown
在所有嵌套子模式中传播?您可以覆盖子模式中的
allow_unknown