Automated API Schema Validation: Uniting Newman and Jenkins for Error-Free Responses

Minhazul Billah
4 min readSep 7, 2023

APIs are vital in today’s software development environment for tying together diverse systems and applications. It is crucial to make sure that these APIs deliver correct and reliable results. Validating API replies against a predetermined schema is one approach to accomplish this. In this post, we’ll go over how to use Postman, a well-liked API testing tool, to develop a test script for validating API replies using schema validation.

Think about being entrusted with testing an API and needing to make sure the responses follow a particular schema or structure. Manual validation in these circumstances may be time-consuming and error-prone. Using a tool like Postman to automate the validation process can greatly increase accuracy and efficiency.

You may combine JavaScript, the Postman scripting environment, and the Ajv (Another JSON Schema Validator) module to validate API answers against a schema in Postman. The code you gave is explained in detail here:

var response = pm.response.json();

The response variable is where this line keeps the JSON response that was received in response to the API call.

let schema = //schema goes here

You would specify your JSON schema here. The format and limitations of the anticipated JSON response are specified by a JSON schema. You can substitute your actual JSON schema for //schema goes here.

let Ajv = require('ajv');
let ajv = new Ajv({ allErrors: true });

The JSON schema validation process is carried out by these lines, which import the Ajv library. By ensuring that all validation failures are gathered, the allErrors: true option enables you to submit every problem at once.

let validate = ajv.compile(schema);
let isValid = validate(response);

The API response is validated using the compiled function, which is used to build the JSON schema in this case. The outcome is kept in the isValid variable, where it will either be true or false depending on whether the response follows the schema.

if (!isValid) {
// Handle validation errors
} else {
// API response is valid
}

This conditional sentence verifies that the API response complies with the schema. It enters the if-block to address validation errors if it is not valid. If not, it goes into the else-block to show that the response was valid according to the schema.

validate.errors.forEach(error => {
// Process each validation error
});

This loop iterates through each validation error discovered in the response inside the if-block. You can use it to report particular problems with the answer data.

const product = _.get(response, error.dataPath.split('.').filter(s => s.startsWith('ObjectName'))[0]);
const filteredValue = _.pick(product, ['Property', 'Property_01']);
const keyValuePairs = _.map(filteredValue, (value, key) => `${key}: ${typeof value === 'object' ? JSON.stringify(value) : value}`);

These lines precisely target the “ObjectName” field in the response to extract and filter the pertinent information from it. In the test report, this information is utilized to provide context.

pm.test(
"Schema Mismatched: " + error.message + "\t[" + keyValuePairs + ']\t path: ' + error.dataPath,
() => pm.expect(isValid).to.be.true
);

This code creates a Postman test for each validation issue, complete with the error message, pertinent information, and the data flow that led to the error, at the end of the loop. This aids developers and testers in identifying problems with the API response.

Now comes the Newman and Jenkins Part.

First, it’s needed to get the API key from Postman and install Newman and report plugins.

To generate report of the collection run and tests we can use

newman run https://api.postman.com/collections/{collectionID}?apikey={yourAPIkey} --timeout 999999 --reporters cli,htmlextra --reporter-htmlextra-logs --reporter-htmlextra-export report.html

Configure Jenkins and Create a job for running above command using a cron job schedue.

Setup Node and npm bin/folder to PATH

Add a build step and put above command.

Step-up post build actions, setup email configuration and other things and finally set recipients list

Add a schedule to build the job periodically.

You can automate your API testing and make sure that your tests run regularly without manual intervention by integrating Postman, Jenkins, and a cron job scheduler. This method helps preserve the dependability of your APIs while also saving time. Additionally, email notifications will keep your team updated on test findings and enable prompt problem-solving during development and deployment.

Connect with me on LinkedIn: https://www.linkedin.com/in/minhazbillah

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response