Q: Write a test to validate JSON schema in Postman?

Validating JSON schemas in Postman is an effective way to ensure that your APIs are returning data in the expected format. Here's how you can set up and implement this validation in a few simple steps.

Step 1: Create Your Postman Request

  1. Open Postman and create a new request by clicking on the “+” icon.
  2. Fill in the required details such as the request type (GET, POST, etc.) and the URL of the API endpoint you want to test.
  3. If needed, set any necessary headers, authentication, or parameters for your request.

Step 2: Define Your JSON Schema

Before you can validate, you need to have your JSON schema defined. Here’s a simple example of a JSON schema that describes a user object:

{ "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "string" }, "email": { "type": "string", "format": "email" } }, "required": ["id", "name", "email"] }

Step 3: Write a Test in Postman

After you have your schema defined, you can write a test in Postman to validate the response. Here’s how to do it:

  1. Click on the “Tests” tab in your Postman request.
  2. Use the following code snippet to validate the response against your schema:
const Ajv = require("ajv"); const ajv = new Ajv(); // Create a new Ajv instance const schema = { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "id": { "type": "integer" }, "name": { "type": "string" }, "email": { "type": "string", "format": "email" } }, "required": ["id", "name", "email"] }; // Parse the response body const responseBody = pm.response.json(); // Validate the response against the schema const valid = ajv.validate(schema, responseBody); // Check if the response is valid pm.test("Response has valid JSON schema", function() { pm.expect(valid).to.be.true; // Ensure the response is valid }); // If validation fails, log the errors if (!valid) { console.log(ajv.errors); }

Step 4: Send the Request and Review Results

  1. Click the “Send” button to execute your request.
  2. After the request is complete, switch to the "Tests" tab where you will see the results of the validation test.
  3. If the response matches the schema, the test will pass. If not, check the console log to see the errors and understand what part of the response is invalid.

Explanation of the Code

  • Ajv Library: We’re using the Ajv (Another JSON Schema Validator) library to validate the response against the JSON schema.
  • Schema Definition: We define our schema directly in the test. You can also define it externally if your schema is complex or reused across multiple tests.
  • Response Validation: We parse the JSON response using pm.response.json() and validate it against the schema using ajv.validate().
  • Test Assertion: Using Postman's built-in assertion library, we confirm whether the validation passed and log errors if it didn't.

This straightforward validation process can greatly enhance the reliability of your API tests in Postman, helping you catch issues early and ensuring your application behaves as expected.

Share now!