【发布时间】:2017-10-24 21:39:16
【问题描述】:
所以我使用 Cerberus 进行架构验证,但我在验证密钥未知的字典的子字典时遇到了问题。
假设我有以下文件:
dict = {
'things': {
'0463': {
'foo': 'blah',
'bar': 'bleep'
},
'0464': {
'foo': 'x',
'bar': 'y'
},
'another_random_id': {
'foo': 'blah',
'bar': 'bleep'
}
}
所以我想验证子字典是否具有特定的结构(foo 和 bar 作为键),但我无法在不知道键的情况下找到一种验证方法(在我的case 是随机 id。我认为这是 valueschema 的一个很好的用途,但我似乎无法让 valueschema 与“dict”类型的东西一起使用。我尝试在 cerberus 中设置以下模式:
schema = {
'things': {
'type': 'dict',
'valueschema': {
'type': 'dict',
'foo': {'type': 'string'},
'bar': {'type': 'string'}
}
}
}
我是否错误地定义了我的架构,或者这对于valueschema 的当前实现是不可能的。我在存储库中看到了一些使用 valueschema 的测试,但它们只测试 valueschema 的类型是 int 或 string。
【问题讨论】:
-
如果这仍然是一个问题,请打开一个错误报告,其中包含对您遇到的和预期的行为的描述。