Postman: JSON Schema Validation
By: Deepak Attri
Tags: Postman, Schema Validation, Testing, API, Automation Testing

In my previous article, we discussed how to automate and validate APIs using postman. In this article, we will see how to validate a JSON schema using postman.

What is JSON schema?

JSON Schema is a specification for defining the structure of JSON data i.e. JSON request body and response in case of APIs. Basically, JSON schema defines various keys and their values and certain constraints on the values.

The various important keywords that are used in the JSON schema are as follows:

type: The type keyword defines the first constraint on our JSON data. Type can be null, boolean, object, array, number, integer, or string. properties: The value of properties is an object, where each key is the name of a property and each value is a JSON schema used to validate that property. required: This keeps a list of required properties i.e the mandatory properties. minimum: Represents the minimum acceptable value for a number type property. maximum: Represents the maximum acceptable value for a number type property. maxItems: The maximum number of items in an array type property. minItems: The minimum number of items in an array type property. maxLength: Represents the maximum length for a string type property. minLength: Represents the minimum length for a string type property. pattern: A string instance is considered valid if the regular expression matches the instance successfully. additionalProperties: Used to handle properties whose names are not listed in the properties keyword. It may be either a boolean or an object. If it is a boolean and set to false, no additional properties will be allowed.

The entire list can be found here. You can write JSON schema by yourself or it can be generated online. Go to the URL, paste your JSON and submit and JSON schema will be generated.

Why JSON schema validation is required?

JSON schema validation ensures that the JSON response format that we are getting is the same as the expected one. Validating the structure of the RESTful services using a schema is simple, otherwise, it would be almost impossible to test each field manually every time there is a change

How to validate JSON schema using Postman?

It is a JavaScript library that provides functions to validate JSON schema. It’s built-in in postman.

Let’s validate the JSON schema for the following API response.

API Response

Test Script And Test result

We have successfully validated JSON response against a JSON schema. Now, what if the type of property age is changed to Integer .

Changed Response

Our test would fail since in JSON schema we have defined type of property age as String

Failed Test

Let’s validate a more complex JSON response.

Here you can see that the test has passed even though our JSON schema does not include all the other properties. This is because additionalProperties is by default true. What if additionalProperties is set to false?

additionalProperties :false

The test has failed and the string data should NOT have additional properties is repeated thrice, one for each isEmployed , familyMembers and features .

Now let’s validate the following conditions:

name should contain only alphabets and space i.e matches the regex ^[A-Za-z\\s]+$ One of the property isEmployed is missing from the response. The minimum length of the familyMembers array should be 2 and the maximum length should be 4. The minimum and maximum value of property height .

JSON schema for the including above conditions:

Now suppose response has changed

Modified Response

For the above response, our test should fail because in JSON schema we have defined that the property name should have only alphabets and spaces, familyMembers must have at least one item, isEmployed is a mandatory property and minimum height is 4.

We can see that the test failed and the assertion clearly shows the reason for failure.

Using JSON Schema solves most of the communication problems between provider and consumer of the RESTful API service. A provider can be a backend developer and a consumer can be mobile development team. JSON Schema has a surprisingly sharp learning curve. Some developers feel it’s hard to work with, dismissing it as “too verbose”.

That is all for this article.

Full projects and more repositories can be found on my github.