簡體   English   中英

Python Cerberus JSON 模式驗證

[英]Python Cerberus JSON schema validation

我不知道為什么我的代碼不起作用,因此尋求幫助。

這是我的示例 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

無法驗證作為根元素的數組文檔。 如您所見: https://github.com/pyeve/cerberus/issues/220

順便說一下,Cerberus 模式中不存在類型array ,您應該改用list

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM