Cerberus 依赖项如何引用文档中较高的字段?
How can a Cerberus dependency reference a field higher up in the document?
我正在尝试为具有引用文档中较高字段的依赖项的文档创建架构。例如:
document = {
'packages': {
'some-package': {'version': 1}
},
'build-steps': {
'needs-some-package': {'foo': 'bar'},
'other-thing': {'funky': 'stuff'}
}
}
我在这里苦苦挣扎的是加强 build-steps.needs-some-package 和 packages.some-package 之间的依赖关系。只要构建步骤包含 "needs-some-package",包就必须包含 "some-package".
当 "needs-some-package" 不存在时,不需要 "some-package"。所以这个文件也应该有效。
other_document = {
'packages': {
'other-package': {'version': 1}
},
'build-steps': {
'other-thing': {'funky': 'stuff'}
}
}
在合适的位置具有依赖关系的模式是
schema = {
'packages': {
'type': 'dict',
'valueschema': {
'type': 'dict'
}
},
'build-steps': {
'type': 'dict',
'schema': {
'needs-some-package': {
'type': 'dict',
'dependencies': 'packages.some-package'
},
'other-thing': {
'type': 'dict'
}
}
}
}
但这不起作用,因为 Cerberus 似乎正在 "build-steps" 下的子文档中寻找 "packages"。有什么办法可以上文档树吗?或者引用与文档根相关的内容?
描述的问题已在版本 1.0.2 中得到解决:
When a subdocument is processed the lookup for a field in question
starts at the level of that document. In order to address the
processed document as root level, the declaration has to start with a
^. An occurance of two initial carets (^^) is interpreted as a
literal, single ^ with no special meaning.
示例代码:
import cerberus
schema = {
'packages': {
'type': 'dict',
'valueschema': {
'type': 'dict'
}
},
'build-steps': {
'type': 'dict',
'schema': {
'needs-some-package': {
'type': 'dict',
'dependencies': '^packages.some-package'
},
'other-thing': {
'type': 'dict'
}
}
}
}
document = {
'packages': {
'some-package': {'version': 1}
},
'build-steps': {
'needs-some-package': {'foo': 'bar'},
'other-thing': {'funky': 'stuff'}
}
}
validator = cerberus.Validator(schema)
print(validator.validate(document))
我正在尝试为具有引用文档中较高字段的依赖项的文档创建架构。例如:
document = {
'packages': {
'some-package': {'version': 1}
},
'build-steps': {
'needs-some-package': {'foo': 'bar'},
'other-thing': {'funky': 'stuff'}
}
}
我在这里苦苦挣扎的是加强 build-steps.needs-some-package 和 packages.some-package 之间的依赖关系。只要构建步骤包含 "needs-some-package",包就必须包含 "some-package".
当 "needs-some-package" 不存在时,不需要 "some-package"。所以这个文件也应该有效。
other_document = {
'packages': {
'other-package': {'version': 1}
},
'build-steps': {
'other-thing': {'funky': 'stuff'}
}
}
在合适的位置具有依赖关系的模式是
schema = {
'packages': {
'type': 'dict',
'valueschema': {
'type': 'dict'
}
},
'build-steps': {
'type': 'dict',
'schema': {
'needs-some-package': {
'type': 'dict',
'dependencies': 'packages.some-package'
},
'other-thing': {
'type': 'dict'
}
}
}
}
但这不起作用,因为 Cerberus 似乎正在 "build-steps" 下的子文档中寻找 "packages"。有什么办法可以上文档树吗?或者引用与文档根相关的内容?
描述的问题已在版本 1.0.2 中得到解决:
When a subdocument is processed the lookup for a field in question starts at the level of that document. In order to address the processed document as root level, the declaration has to start with a ^. An occurance of two initial carets (^^) is interpreted as a literal, single ^ with no special meaning.
示例代码:
import cerberus
schema = {
'packages': {
'type': 'dict',
'valueschema': {
'type': 'dict'
}
},
'build-steps': {
'type': 'dict',
'schema': {
'needs-some-package': {
'type': 'dict',
'dependencies': '^packages.some-package'
},
'other-thing': {
'type': 'dict'
}
}
}
}
document = {
'packages': {
'some-package': {'version': 1}
},
'build-steps': {
'needs-some-package': {'foo': 'bar'},
'other-thing': {'funky': 'stuff'}
}
}
validator = cerberus.Validator(schema)
print(validator.validate(document))