[英]Python Cerberus JSON schema validation
I have no clue why my code doesn't work, hence looking for some help.我不知道为什么我的代码不起作用,因此寻求帮助。
That's my sample JSON array :这是我的示例 JSON数组:
[
{
"bookingid": 1774
},
{
"bookingid": 1020
}
]
and my code is as follows:我的代码如下:
def test_get_booking_ids_correct_schema():
schema = {
"type": "array",
"items":
{
"properties":
{
"bookingid":
{
"type": "integer"
}
}
}
}
response = requests.get("https://restful-booker.herokuapp.com/booking")
response_body = response.json()
v = Validator(schema)
is_valid = v.validate(response_body)
assert is_valid == True
and the error I'm getting is as follows:我得到的错误如下:
if not self.schema_validator(test_schema, normalize=False):
> raise SchemaError(self.schema_validator.errors)
E cerberus.schema.SchemaError: {'items': [{'properties': ['unknown rule']}], 'type': ['must be of dict type']}
Do you see any obvious mistake in my schema?您在我的架构中看到任何明显的错误吗?
On the contrary, the code below works perfectly fine:相反,下面的代码工作得很好:
def test_temp():
schema = {"origin": {"type": "string"}}
json = {
"origin": "185.21.87.131"
}
v = Validator(schema)
is_valid = v.validate(json)
assert is_valid == True
It's not possible to validate a document which is an array as root element.无法验证作为根元素的数组文档。 As you can see on: https://github.com/pyeve/cerberus/issues/220如您所见: https://github.com/pyeve/cerberus/issues/220
by the way, the type array
didn't exist in the Cerberus schema, you should use list
instead.顺便说一下,Cerberus 模式中不存在类型array
,您应该改用list
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.