1

I have a json response like this(Response is getting in the com.jayway.restassured.response.Response format).

[{
        gameIdGlobal: 1947634,
        season: 2017,
        views: [{
                name: "Recap",
                displayOrder: 1,
                groups: [{
                        type: "static",
                        displayOrder: 1
                    }
                ],
                localizedName: {
                    ENG: "Recap",
                    ESP: "Resumen"
                }
            }
        ]
    }
]

From this I need to validate the json schema of views object only.No need to validate the entire json . for that I have created a json schema for views object only schema1.

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"
                    }
                }
            }
        }
    }
}

How can i perform the schema validation of particular json object(views object from josn response)

code

Response response = RestAssured.given().when().get(getURL);
 ValidatableResponse valResponse = response.then().contentType(ContentType.JSON);
  valResponse.body(schema1.json("schema1.json")).assertThat();
Psl
  • 3,830
  • 16
  • 46
  • 84

1 Answers1

1

You can specify that Additional Properties are allowed on the object that holds the array as its property. Here is the schema for the entire response json object:

{
    "$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",
                  ...
        }
    }
}
Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
  • that means only have to modify the josn schema right? – Psl Apr 03 '18 at 07:14
  • dont we have to do any modifications in code. could you please clarify it – Psl Apr 03 '18 at 07:15
  • yes. modify the json schema only. the schema in the answer is tailored to the response in the question (meaning that the response is an array containins objects and the `views` is a property of an item) you may have to play with the schema to fit the structure – Sharon Ben Asher Apr 03 '18 at 08:20
  • Ok. Also I can pass entire response body for validating particular views object.No need to extract views object response only..right? – Psl Apr 06 '18 at 02:54
  • In addition to this..Could you please suggest a good schema generator tool. – Psl Apr 06 '18 at 02:56