【发布时间】:2017-06-01 15:40:13
【问题描述】:
我正在尝试在 Python 中使用 Cerberus 来验证一些数据。
我发现对于 'boolean' 类型,验证器总是返回 True,如下所示:
import cerberus
bool_schema = {'name': {'type': 'boolean', 'required': True}}
cerberus.schema_registry.add('bool_schema', bool_schema)
v = cerberus.Validator({'name': {'schema': 'bool_schema'}})
test1 = {'name': 'a'}
test2 = {'name': 0}
print(v.validate(test1))
print(v.validate(test2))
上面的代码打印了两个 True。
实际上,我需要验证该值是 True 还是 False(Python 中的 bool 类型),其他值不应该通过验证器。
【问题讨论】:
-
您使用的是哪个版本的
cerberus?上面的代码会导致错误(“raise SchemaError(self.schema_validator.errors) cerberus.schema.SchemaError: {'name': [{'schema': [{'anyof': ['no definitions validate', {'anyof definition 1': ['Rules set definition boolean not found.'], 'anyof definition 0': ['Schema definition boolean not found.']}]}]}]}”与cerberus1.0.1 是最新的。 -
我通过
pip安装了cerberus,而cerberus.__version__显示它正是1.0.1 版 -
抱歉,我在问题中写错了,
Validator中的架构名称是'bool_schema'而不是'schema'。
标签: python validation boolean schema cerberus