How to Perform JSON Schema Validation?
Due to the rise in API consumption, it is becoming tedious for QA teams to test the APIs. So before testing the API, most of the teams prefer to define the data your service will accept and the data format it will return. This process of validating the data format and defining it for the ease of validation is basically Schema Validation.
In API Testing there are three types of validation, out of which one of the most important ones is Schema Validation. Sometimes after providing the detailed documentation of the APIs, the testing team identifies defects due to the structure of data. In some scenarios the response body from server is too large to put assertion and also diverse in nature to consider for response body validation
JSON Schema Validation comes to the rescue in the above mentioned problem statement, the structural differences can now be caught easily and can save a lot of your efforts and time in debugging and finding the issue. That’s the main reason why the popularity of JSON Schema has been at the top and also called as the King of API data formats.
Before we start discussing JSON Schema validation, let’s first understand what JSON is, and then continue with the JSON Schema Validation.
What is JSON?
- Data is in name/value pairs
- Data is separated by commas
- Curly braces hold objects
- Square brackets hold arrays
In JSON, values must be one of the following data types:
- a string
- a number
- an object
- an array
- a boolean
JSON is one of the most common formats for exchanging data over the internet. And even though most APIs work with JSON, there’s no guarantee that the consumer will be able to interact with data in this format. A schema aims to fix all those pain points.
What is JSON Schema?
Schema is nothing but a JSON file. It will only have datatype information and the expected keys of the JSON. There won’t be any values present in the schema.
JSON Schema is a JSON media type for defining the structure of JSON data. JSON Schema provides a contract for what JSON data is required for a given application and how to interact with it. JSON Schema is intended to define validation, documentation, hyperlink navigation, and interaction control of JSON data.
If we try to put it in simple terms, JSON Schema is basically a contract for JSON document that defines the expected data types and format of each field in the response. JSON Schema helps us in describing the existing data format and providing clear, human and machine readable documentation. As JSON Schema provides complete structural validation, it helps in automated tests and also validating the client-submitted data for verification.
JSON Schema Example
Let’s try to convert a JSON into schema to understand the basic concepts around JSON Schema properly.
The structure of this JSON response has some points to note, let’s understand it:
- The API returns a top level array
- Each array entry is an object (a user).
- Each user has a name, location, and experienceInYears key.
- The name and location are strings, while the experienceInYears is a number.
The following JSON Schema definition describes the response above:
If we go through the JSON Schema line by line, it details the type and structure of the entire body, as well as each individual field.
To go over a few points: The $schema keyword states that this schema is written according to a specific draft of the standard and used for a variety of reasons, primarily version control.
- The top-level type key tells us the API returns an “array”.
- The items describe the shape of each array entry (each user).
- The name and location fields are strings, while experienceInYears is an integer.
How to Perform JSON Schema validation
We can perform schema validation manually by using some websites and tools, you can also refer to the website HERE to validate schemas. In the above mentioned website you can paste both the schemas and do a comparison, it will highlight the differences which can be reported further. As manual approach is not always convenient, so most of the QA team prefer to automate the Schema Validation, in this section we will try to go through the steps to automate the schema validation using Rest Assured. For those who are new to API Automation, Rest Assured is a Java library that provides a domain-specific language for writing powerful, maintainable tests for RESTful APIs.
Step-01: Add dependency
Step-02: Load expected schema
Load the expected “schema.JSON” in a file object.
You can create a schema from JSON using website HERE:
Step-03: Validate using matchesJSONSchema
Validate the response body using the matchesJsonSchema method.
JSON Schema validation is considered as one of the most efficient ways of ensuring your APIs are working as expected. In the above post we have discussed both manual and automation approaches of performing the schema validation, so let us know if you find it useful. Apart from the API Testing, JSON Schema also helps during the Contract Testing. In fact we can use JSON Schema along with a schema matcher in the integration tests.
Those who are looking out of QA Automation jobs do mention Schema Validation in your skill set, and upload your resume at Talent500. You can find multiple opportunities in the jobs link here.