Is it possible to have a shared Ajv schema?

I have a schema that I want to use in tests in several dozen individual requests. Instead of copying the same schema definition into the Test for each request, is it possible to define the schema at a Collection level and reference the schema as a Collection variable?

You can save your schema as a collection variable but it may not be a nice editing experience.

I edited my schema like normal in a request’s Tests. Ran it and it appeared to work correctly.

I cut/pasted the schema to a Collection variable with the name ttSchema. Updated my request test with

var Ajv = require('ajv'),
ajv = new Ajv({logger: console, allErrors: true, verbose: true}),
schema = pm.variables.get("ttSchema");

pm.test('Schema is valid', function() {
  pm.expect(ajv.validate(schema, pm.response.json()));

And this is the result of running the request

FAIL Schema is valid | Error: no schema with key or ref "{ "type": "object", "properties": { "employee": {...

My whole schema definition is displayed in the above message.

Any advice?

if you manually create variables, you can only save them as strings.

But your schema is an object. So you need to transform the object to a string with:


and the other way around to use if from a variable you need to
do something like:

const schema = JSON.parse(pm.variables.get("ttSchema"));

I hope this works.

That did it! I had a brain cramp on needing to parse the string back into an object.

For others that may read this Topic (ajv info in the documentation is lacking!):
This was an invaluable resource for learning JSON Schema
You can run/test ajv and your schema at the command line with ajv-cli
I saved my schema to a file and a sample data file as well. This let me debug the schema much faster than in Postman
I saved the schema as a collection variable and used this incantation in the Postman test

// Get the JSON schema from the collection variables
var Ajv = require('ajv'),
  ajv = new Ajv({allErrors: true, verbose: true}),
  schema = JSON.parse(pm.variables.get("ttSchema"));

// Test the payload against the JSON Schema
pm.test('Schema is valid', function() {
  let validationResult = ajv.validate(schema, pm.response.json());

  if (!validationResult) {


I used console.log() to see the errors. This isn’t an optimal way to get the errors. Better would be to get them to display in the Response/Test Results but I haven’t figured out how to do so.

Hey @jshea,

Just jumping in here, I was able to log those errors to the Test Results by using the additional argument on the pm.expect() function.

This test is slightly different from the way you have it but it should hopefully show you the method I used. :slight_smile:

pm.test('Schema is valid', function() {
    pm.expect(ajv.validate(schema, pm.response.json()), JSON.stringify(ajv.errors));

This was taken from this question:

1 Like

Putting the ajv.errors as the second arg to expect() is exactly what I was looking for!

For all my other tests I’m putting the property name being tested as the second arg to help understand what test failed. I didn’t think about it for the AJV validation.

JSON Schema and AJV are very powerful and a bit of a learning curve. For my multiple tests (following a work flow) that send/receive the same complex schema, it’s a life saver.

1 Like