gpt4 book ai didi

python - Cerberus 的严格模式验证

转载 作者:太空宇宙 更新时间:2023-11-03 14:27:22 25 4
gpt4 key购买 nike

我使用的是 Cerberus 版本 1.1。

Cerberus required 验证规则似乎默认为 False,结果是空文档完全有效。

>>> schema = {
'spam': {'type': 'string'}
}

>>> v = Validator()
>>> v.validate({}, schema)
True

但是我有一个非常冗长的架构文档,需要严格执行。有没有办法为整个架构默认设置 'required': True ,这样每个定义的字段都是必填的?

就目前情况而言,我似乎必须将 'required': True 粘贴到每个字段定义中,这似乎是不必要的冗余。

最佳答案

当然,您可以使用简单的 Python 习惯用法来设计您的架构:

schema = {
'spam': {'type': 'string'},

}
for field in schema:
schema[field].update({'required': True})

关于python - Cerberus 的严格模式验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47538299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com