我不知道为什么我的代码不工作,因此寻求一些帮助。
这是我的示例JSON 数组
[
{
"bookingid": 1774
},
{
"bookingid": 1020
}
]我的代码如下:
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我得到的错误如下:
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']}您看到我的架构中有什么明显的错误吗?
相反,下面的代码工作得非常好:
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发布于 2022-11-23 14:15:05
不可能验证作为根元素的数组的文档。如您所见:https://github.com/pyeve/cerberus/issues/220
顺便说一句,Cerberus模式中不存在array类型,您应该使用list。
https://stackoverflow.com/questions/72791379
复制相似问题