Validating JSON schema in Postman

1.9k views Asked by At

When using Postman I validate the JSON response like so:

tv4.addSchema(globalSchema);                
const valResult = tv4.validate(data, schema);

// schema is an object, which is a subschema from the larger globalSchema

which works fine, except for the error reporting. The error object I get is missing dataPath and schemaPath, making it hard for my user to find out where the actual problem is. Is there a way to get those properties? (tried validateResult and validateMultiple to no avail)

As an alternative I tried ajv, but as I am in draft-04, it gives me errors. The advice from their site

var ajv = new Ajv({schemaId: 'id'});
// If you want to use both draft-04 and draft-06/07 schemas:
// var ajv = new Ajv({schemaId: 'auto'});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));

does not work because the Postman sandbox does not allow me to require that… any thoughts?

See also: https://community.getpostman.com/t/json-schema-validation-troubles/5024

1

There are 1 answers

3
Muck On

Here's how I validate schema's with postman to get more detailed errors:

const schema = {
};

var jsonData = JSON.parse(responseBody);

pm.test('Checking Response Against Schema Validation', function() {
    var result=tv4.validateMultiple(jsonData, schema);
    console.log(result);
    pm.expect(result.valid).to.be.true;
});