JSON模式验证具有静止保证的 - 主张值不在JSON模式中
有没有办法断言API响应包含JSON模式中不包含的值? (例如:“名称”:“ nick”)
API响应示例(名称未指定在下面的模式中):
{
"id": "390",
"name": "Nick",
"data": {
"leagueId": 35,
"homeTeam": "Norway",
"visitingTeam": "England",
}
}
JSON Schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"data": {
"type": "object",
"properties": {
"leagueId": {
"type": "integer"
},
"homeTeam": {
"type": "string"
},
"visitingTeam": {
"type": "string"
}
},
"required": [
"leagueId",
"homeTeam",
"visitingTeam"
]
}
},
"required": [
"id",
"data"
]
}
比较代码示例:
@Test
public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() {
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder()
.setValidationConfiguration(
ValidationConfiguration.newBuilder()
.setDefaultVersion(SchemaVersion.DRAFTV4).freeze())
.freeze();
get("/events?id=390").then().assertThat()
.body(matchesJsonSchemaInClasspath("event_0.json")
.using(jsonSchemaFactory));
}
Is there a way how to assert that API response contains values which are NOT in JSON schema? (e.g.: "name": "Nick")
API response example (name is not specified in schema below):
{
"id": "390",
"name": "Nick",
"data": {
"leagueId": 35,
"homeTeam": "Norway",
"visitingTeam": "England",
}
}
JSON schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"data": {
"type": "object",
"properties": {
"leagueId": {
"type": "integer"
},
"homeTeam": {
"type": "string"
},
"visitingTeam": {
"type": "string"
}
},
"required": [
"leagueId",
"homeTeam",
"visitingTeam"
]
}
},
"required": [
"id",
"data"
]
}
Comparison code example:
@Test
public void givenUrl_whenValidatesResponseWithInstanceSettings_thenCorrect() {
JsonSchemaFactory jsonSchemaFactory = JsonSchemaFactory.newBuilder()
.setValidationConfiguration(
ValidationConfiguration.newBuilder()
.setDefaultVersion(SchemaVersion.DRAFTV4).freeze())
.freeze();
get("/events?id=390").then().assertThat()
.body(matchesJsonSchemaInClasspath("event_0.json")
.using(jsonSchemaFactory));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论