JSON Schema testing with Postman

JSON Schema testing with Postman

Postman is becoming quite the popular tool for accessing and testing REST api services. One of the cool features is that you can write some Javascript tests on your responses. Built into Postman is also the Tiny Validator v4 and Cheerio. Cheerio is a small core JQuery like implementation. The problem I have is that there is no way to import a schema file from a remote place or a file. This will be about how I get around that issue.

Pre-request Script

var uri='http://someUrl.com';
$.get(uri + '/someFolder/schema.json', function(schema) {
  postman.setEnvironmentVariable('schema', JSON.stringify(schema));
});

This Pre-request script is what saves the day. There are a couple of timing issues that are present due to async processing. If you were to put this in your test script your chances of it not completing in time are very high and you would get inconsistent results. If you are familiar with JQuery the $.get should look familiar as a ajax call that does a get request to a schema file. One thing to note it that what ever means you are using to serve the file you will need the proper Access-Control-Allow-Origin set. In most cases this will be set so that only that domain can access the file. If you were running this call from nodejs for example you wouldn’t have this issue. Postman however is a Chrome app. Its basically running as a webpage. Please note that setting the Access-Control-Allow-Origin on a server can be a dangerous thing. Only set it where properly monitored and administered.

The Tests

var schema = JSON.parse(environment["schema"]);
var jsonData = JSON.parse(responseBody);
var results = tv4.validateMultiple(jsonData, schema);
for (var i = 0; i < results.errors.length; i++) {
  tests[JSON.stringify(results.errors[i])] = false;
}

The tests are fairly simple. We read in the schema that was input in the Pre-request and read in the response from the query and parse them into JSON objects. Then we run tv4.validateMultiple on them. In most examples you would just run tv4.validate. However I wanted to see each failure issue within the JSON data. In order to fail a test you would just set the tests array at a certain position to false. To make it understandable I put the error message as the key to the array and set it to false. Each one that is false will now show up under the tests when you run the query.

Final Thoughts

You have to note that this is a work around. It does have its potential issues. For example the promise for the get request could take longer than it takes to get response and the validate step. Or Access-Control-Allow-Origin opens up potential security hole. Use your best judgement.