首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套列表中的Cerberus强制

嵌套列表中的Cerberus强制
EN

Stack Overflow用户
提问于 2018-12-09 10:01:18
回答 1查看 632关注 0票数 0

我得到了以下代码的意外行为:

代码语言:javascript
复制
import cerberus
v = cerberus.Validator()
schema = {'list_of_values': {'type': 'list',
                             'schema': {'items': [{'type': 'string', 'coerce': str},
                                                  {'type': 'integer', 'coerce': int}]}}
                             }
document = {'list_of_values': [['hello', 100], [123, "122"]]}
v.validate(document, schema)
v.errors

我希望没有错误,因为强制应该处理类型。但我得到了

代码语言:javascript
复制
{'list_of_values': [{1: [{0: ['must be of string type'],
     1: ['must be of integer type']}]}]}

这是一个bug吗?我是不是误解了强制是如何工作的?

EN

回答 1

Stack Overflow用户

发布于 2019-03-15 20:47:43

@funky-未来

如果你有什么不对劲的地方,我确实可以通过将示例复制粘贴到提示符中来重现这个问题:

代码语言:javascript
复制
>>> import cerberus
>>> v = cerberus.Validator()
>>> schema = {'list_of_values': {'type': 'list',
...                              'schema': {'items': [{'type': 'string', 'coerce': str},
...                                                   {'type': 'integer', 'coerce': int}]}}
...                              }
>>> document = {'list_of_values': [['hello', 100], [123, "122"]]}
>>> v.validate(document, schema)
False
>>> v.errors
{'list_of_values': [{1: [{0: ['must be of string type'], 1: ['must be of integer type']}]}]}

Python3.5.2,cerberus1.2

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53688770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档