0

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));
}
JustAG33K
  • 1,403
  • 3
  • 13
  • 28
  • instead of validating through schema. Convert your response into Map Object and validate using the Key "Name". – Saurabh Jun 15 '22 at 09:50
  • "Name" is example only, I need to find any keys which are not in schema.. – Nikola Jakubiak Jun 15 '22 at 11:36
  • Difference between two json files could work -> https://github.com/java-json-tools/json-patch from this query -> https://stackoverflow.com/questions/3214108/getting-a-diff-of-two-json-strings-using-java-code – Grasshopper Jun 16 '22 at 06:02
  • you can use https://github.com/flipkart-incubator/zjsonpatch library. One of the best library to compare json. It return what has been "add/remove/replace", etc.. try it. – Saurabh Jun 20 '22 at 16:57

0 Answers0