admin管理员组文章数量:1287869
I use Cerberus to validate a loaded configuration file. The user may make changes to that configuration within the program, however, and when they do, I’d like to validate only that change with Cerberus – not validate the whole document in vain, when I know that the rest hasn’t changed.
Take the following minimal example:
import cerberus
SCHEMA = {
'a': {'type': 'string'},
'b': {'type': 'integer', 'allowed': (39, 93)}
}
v = cerberus.Validator(SCHEMA)
config = {
'a': 'Wow!',
'b': 39
}
print(v.validate(config), v.errors) # So far, so good.
config['b'] = 93 # Then the user changes only one property…
# How do I now validate *only* config['b'], without validating config['a']?
I considered using cerberus.Validator._get_child_validator
, which by name would appear to do exactly what I ask, but apparently it still checks the whole document:
child = v._get_child_validator(document_crumb='b')
# This is wrong, but I don’t want to validate it – it’s just to illustrate that it *is* being validated.
config['a'] = None
print(child.validate(config), v.errors) # False, {'a': ['null value not allowed']}
How do I validate only the part that has changed?
本文标签: pythonHow to validate only part of a document with CerberusStack Overflow
版权声明:本文标题:python - How to validate only part of a document with Cerberus? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741323198a2372325.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论