JSON Schema Validation in Postman

JSON Schema Validation In Postman

In the previous articles on Postman Tutorial, we have covered “How to send JWT Token as header

In this “JSON Schema Validation in Postman” article, I will be demonstrating as to how you can implement this concept and get a tight grip over this.

What is JSON?

JSON stands for JavaScript Object Notation. JSON is a lightweight format for storing and transporting data. JSON is often used when data is sent from a server to a web page. JSON is not a programming language. It is a common and open standard format for storing and exchanging data. JSON is language independent.

Rules – JSON:

1. Data is in key/value pairs: Key/value pair consists of a key in double quotes followed by a colon, followed by a value. In JSON, keys must be strings in double quotes but values can be of any type such as a string, a number, an object (JSON object), an array, a Boolean, null.

Example:

2. Data is separated by commas: Data is separated by a comma in JSON

Example:

3. Object starts and ends with curly braces {} : JSON Objects are surrounded by Curly braces {}

Example:

4. Arrays starts and ends with square brackets [] : Square brackets hold arrays.

Example:

What is JSON Path?

It is the specific value or data in a JSON. We can create a JSON Path manually or by using tools like JSON PathFinder – http://jsonpathfinder.com/

We just need to copy the JSON and paste it in the tool.

Example:

Let the complete Students array is stored in a variable say x.

  • JSON path of age of John is: x.Students[0].age
  • JSON path of name of Mark is: x.Students[1].name

What is JSON Schema?

JSON Schema is a contract for JSON document that defines the expected data types and format of each field in the response.

It describes the existing data format with clear, human and machine-readable documentation for complete structural validation, useful for automated testing and validating client submitted data.

Postman JSON PathFinder

Why is JSON Schema Validation required?

JSON Schema Validation is required because:

  1. We monitor API responses and ensure that the format that we are getting is same as the expected one.
  2. We get alert whenever there is any breaking change in JSON response.
  3. We use JSON Schema to construct a model of API response and it makes easier to validate that API is returning the valid data.

JSON Object:

JSON Schema Example:

JSON Schema Validation

Schema itself is an object which has different properties. One of these properties is type. If we declare the type as an object, it means if we send something that is not an object that will not work. We will be using Another JSON Schema Validator (Ajv). It’s recently built-in in postman.

  • Go to https://jsonschema.net/home
  • Add JSON body on left side
  • Click Submit
  • It will create a schema on right side
  • Copy the schema and add in Tests tab in Postman to perform schema validation
  • Add this code for schema validation:
  • We have created a JSON schema out of our response and now we validate JSON response with our JSON schema and if anything related to the properties of this schema and JSON’s response don’t match, the test will fail.

Postman JSON Schema Validation

We can edit the settings as per our need from gear icon on top right corner. We can change certain things. For example, in Object I want to have additionalProperties and don’t want every property to be required. I can even choose which keyword should be included in JSON Schema from keyword visibility option.

Postman JSON Schema Validation

Let’s look into more details of JSON Schema Validation:

1. For an empty object {} in response, the Validate schema test passed.

Under Tests tab,

2. To verify whether the schema is actually valid, let’s replace the body with Foo. We will get a JSON error. So schema will fail if the body is not JSON.

3. Instead of empty body Let’s say we have a property having a string in the response body:

The schema will be valid because it is an object.

If we have to test for this property we will need to add another property to the schema and the property is called properties and in the properties object, we can define multiple properties.

When we send this, Schema will be valid.

4. If we have an integer value,

Schema validation will fail. The error will say code should be a string.

5. Let’s check what will happen if this will be renamed to statusCode.

When we run this, the schema will still be valid, even though we have specified a property called code that should be a string. This is the reason, we need to test our schema in detail to make sure everything works.

In this case, we can specify that this property is actually required.

When we send the request again, we will see that validation is now correct. It checks for a property code and in the response, there is no property code. Therefore, the Validate Schema test failed. The error says “data should have required property ‘code’ “.

6. To limit the number of properties that we get from the response, we specify additionalProperties in our schema.

Example:

If we want to avoid having the properties that we don’t know about and we want to be informed if something really changes in the response, then we can add an additional configuration to the schema. This is called additionalProperties. If we specify it false, then it will not allow additional properties.

Validate Schema will fail. It says data should not have additional properties. But the response has error as an additional property.

7. Let’s suppose we have a nested object in the response called error and error itself has a property called message.

Example:

The schema will be :

Validate Schema test will pass.

8. If we have arrays in our response,

Example:

The schema will be :

Validate Schema test will pass. If we want to have atleast 1 item, we will specify as minItems property and similarly, we can specify maxItems. To define the types of items in an array, we use items property.

Next steps:

Learn “Data-Driven Testing in Postman” in the next tutorial.

Author Bio: This article is written by Harsha Mittal an ISTQB-CTFL Certified Software Test Engineer having 3.5+ years of experience in Software Testing.

Related posts:

JSON Schema Validation In Postman

Get our latest blog posts delivered to your inbox

Subscribe and get popular blog posts about software testing industry.

Rajkumar

Leave a Comment

Share via
Copy link
Powered by Social Snap