JSON模式验证具有静止保证的 - 主张值不在JSON模式中

发布于 2025-02-08 04:19:31 字数 1361 浏览 2 评论 0原文

有没有办法断言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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文