从响应中验证特定JSON对象的json模式

时间:2018-04-03 04:38:11

标签: java json jsonschema rest-assured rest-assured-jsonpath

我有这样的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();

1 个答案:

答案 0 :(得分: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",
                  ...
        }
    }
}