我有一个像这样的 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"
}
}
]
}
]
由此,我只需要验证 View 对象的 json 模式。不需要验证整个 json 。为了那个原因 我仅为 View 对象 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 响应的 View 对象)
代码
Response response = RestAssured.given().when().get(getURL);
ValidatableResponse valResponse = response.then().contentType(ContentType.JSON);
valResponse.body(schema1.json("schema1.json")).assertThat();
最佳答案
您可以指定在将数组作为其属性的对象上允许使用附加属性。以下是整个响应 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",
...
}
}
}
关于java - 验证响应中特定 JSON 对象的 json 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49622348/