Psl
Psl

Reputation: 3920

Validate json schema of a particular JSON object from a response

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();

Upvotes: 1

Views: 892

Answers (1)

Sharon Ben Asher
Sharon Ben Asher

Reputation: 14328

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

Upvotes: 1

Related Questions