REST Assured Tutorial 56 – JSON Schema Validation Without Rest Assured

Introduction

As a part of End to End REST Assured Tutorial, in this post, we will learn about JSON Schema validation without Rest Assured.

We have already covered JSON Schema Validation In Rest Assured. You may be wondering why do we need to validate JSON schema without Rest Assured. We just not validate JSON response schema but we should validate JSON request payload as well just to ensure if the payload is correct. If we create it dynamically or get it through external sources we must have a schema check before we pass that payload to API.

We may need to verify schema of any JSON and it should not be restricted only through Rest Assured.

We can use json-schema-validator standalone as well. There is no need to use it with Rest Assured only. Something like TestNG can be integrated with Selenium or Rest Assured or any other libraries. We need to add Hamcrest (A Core API and libraries of hamcrest matcher framework) explicitly if want to use it without Rest Assured.

Prerequisite

I am using the below version of json-schema-validator and hamcrest:-



    io.rest-assured
    json-schema-validator
    4.3.1



    org.hamcrest
    hamcrest
    2.1
    test

Validate a JSON without Rest Assured

Since schema validation methods of json-schema-validator library create a hamcrest matcher, we must need to add Hamcrest library in our project. Now we can validate schema of any JSON without Rest Assured.

Sample JSON schema

I have stored the below JSON schema outside of resource folder.

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "title": "The root schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": {},
    "examples": [
        {
            "firstname": "Jim",
            "lastname": "Brown",
            "totalprice": 111,
            "depositpaid": true,
            "bookingdates": {
                "checkin": "2018-01-01",
                "checkout": "2019-01-01"
            },
            "additionalneeds": "Breakfast"
        }
    ],
    "required": [
        "firstname",
        "lastname",
        "totalprice",
        "depositpaid",
        "bookingdates",
        "additionalneeds"
    ],
    "properties": {
        "firstname": {
            "$id": "#/properties/firstname",
            "type": "string",
            "title": "The firstname schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "Jim"
            ]
        },
        "lastname": {
            "$id": "#/properties/lastname",
            "type": "string",
            "title": "The lastname schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "Brown"
            ]
        },
        "totalprice": {
            "$id": "#/properties/totalprice",
            "type": "integer",
            "title": "The totalprice schema",
            "description": "An explanation about the purpose of this instance.",
            "default": 0,
            "examples": [
                111
            ]
        },
        "depositpaid": {
            "$id": "#/properties/depositpaid",
            "type": "boolean",
            "title": "The depositpaid schema",
            "description": "An explanation about the purpose of this instance.",
            "default": false,
            "examples": [
                true
            ]
        },
        "bookingdates": {
            "$id": "#/properties/bookingdates",
            "type": "object",
            "title": "The bookingdates schema",
            "description": "An explanation about the purpose of this instance.",
            "default": {},
            "examples": [
                {
                    "checkin": "2018-01-01",
                    "checkout": "2019-01-01"
                }
            ],
            "required": [
                "checkin",
                "checkout"
            ],
            "properties": {
                "checkin": {
                    "$id": "#/properties/bookingdates/properties/checkin",
                    "type": "string",
                    "title": "The checkin schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": "",
                    "examples": [
                        "2018-01-01"
                    ]
                },
                "checkout": {
                    "$id": "#/properties/bookingdates/properties/checkout",
                    "type": "string",
                    "title": "The checkout schema",
                    "description": "An explanation about the purpose of this instance.",
                    "default": "",
                    "examples": [
                        "2019-01-01"
                    ]
                }
            },
            "additionalProperties": true
        },
        "additionalneeds": {
            "$id": "#/properties/additionalneeds",
            "type": "string",
            "title": "The additionalneeds schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": [
                "Breakfast"
            ]
        }
    },
    "additionalProperties": true
}

Program

package JsonSchema;

import java.io.File;

import org.hamcrest.MatcherAssert;
import org.testng.annotations.Test;

import io.restassured.module.jsv.JsonSchemaValidator;


public class VerifyJsonSchemaWithoutRestAssured {
	
	@Test
	public void verifyJsonSchemaWithoutRestAssured()
	{
		String json = "{\r\n" + 
				"    \"firstname\" : \"Jim\",\r\n" + 
				"    \"lastname\" : \"Brown\",\r\n" + 
				"    \"totalprice\" : 111,\r\n" + 
				"    \"depositpaid\" : true,\r\n" + 
				"    \"bookingdates\" : {\r\n" + 
				"        \"checkin\" : \"2018-01-01\",\r\n" + 
				"        \"checkout\" : \"2019-01-01\"\r\n" + 
				"    },\r\n" + 
				"    \"additionalneeds\" : \"Breakfast\"\r\n" + 
				"}";
		
		MatcherAssert.assertThat(json, JsonSchemaValidator.matchesJsonSchema(new File("C:\\Users\\amomahaj\\git\\master\\src\\test\\java\\JsonSchema\\SampleJsonSchemaCreateBooking.json")));
	}

}

If you keep the expected JSON schema in resource folder then you can use matchesJsonSchemaInClasspath also.

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

1 thought on “REST Assured Tutorial 56 – JSON Schema Validation Without Rest Assured

  1. Hi,
    Thanks for putting on so much effort.
    One of my friends referred your articles and these are really knowledgeable, especially, for beginners. while researching topics on rest api, i came across something called caching mechanism for rest apis. Can you please write a post on how to implement a generic cache for response received from get and post requests?

Leave a Reply

Your email address will not be published. Required fields are marked *