1

我有一个像这样的 json 响应(响应正在获取com.jayway.restassured.response.Response格式)。

[{
        gameIdGlobal: 1947634,
        season: 2017,
        views: [{
                name: "Recap",
                displayOrder: 1,
                groups: [{
                        type: "static",
                        displayOrder: 1
                    }
                ],
                localizedName: {
                    ENG: "Recap",
                    ESP: "Resumen"
                }
            }
        ]
    }
]

从这里我只需要验证视图对象的 json 模式。不需要验证整个 json 。为此,我为仅视图对象 schema1 创建了一个 json 模式。

schema1.json

{
    "type": "array",
    "items": {
        "id": "view.json",
        "type": "object",
        "properties": {
            "name": {
                "type": "string"
            },
            "displayOrder": {
                "type": "integer",
                "minimum": 1
            },
            "groups": {
                "type": "array"             
            },
            "localizedName": {
                "type": "object",
                "properties": {
                    "ENG": {
                        "type": "string",
                        "description": "the name of the view in english"
                    },
                    "ESP": {
                        "type": "string",
                        "description": "the name of the view in spanish"
                    }
                }
            }
        }
    }
}

我如何执行特定 json 对象的模式验证(从 josn 响应查看对象)

代码

Response response = RestAssured.given().when().get(getURL);
 ValidatableResponse valResponse = response.then().contentType(ContentType.JSON);
  valResponse.body(schema1.json("schema1.json")).assertThat();
4

1 回答 1

1

您可以指定在将数组作为其属性的对象上允许附加属性。这是整个响应 json 对象的架构:

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "array",
    "items": {
        "type": "object",
        "required": ["views"],
        "additionalProperties": true,
        "properties": {
            "views": {
                "type": "array",
                "items": {
                    "id": "view.json",
                  ...
        }
    }
}
于 2018-04-03T05:06:32.523 回答