# Is it possible to have a shared Ajv schema?

**URL:** <https://community.postman.com/t/is-it-possible-to-have-a-shared-ajv-schema/7275>\
**Category:** Help Hub\
**Tags:** tests, ajv, shared-code\
**Created:** [August 20, 2019, 9:58pm UTC](https://community.postman.com/t/is-it-possible-to-have-a-shared-ajv-schema/7275 "2019-08-20T21:58:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jshea](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jshea](https://community.postman.com/u/jshea)\
**Post date:** [August 20, 2019, 9:58pm UTC](https://community.postman.com/t/is-it-possible-to-have-a-shared-ajv-schema/7275/1 "2019-08-20T21:58:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [August 21, 2019, 9:41am UTC](https://community.postman.com/t/is-it-possible-to-have-a-shared-ajv-schema/7275/2 "2019-08-21T09:41:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jshea](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jshea](https://community.postman.com/u/jshea)\
**Post date:** [August 21, 2019, 6:48pm UTC](https://community.postman.com/t/is-it-possible-to-have-a-shared-ajv-schema/7275/3 "2019-08-21T18:48:49Z")

</div>

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())).to.be.true;
});

```

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?

---

<div class="post-metadata">

**Author:** ![vdespa](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vdespa/32/2770_2.png) [@vdespa](https://community.postman.com/u/vdespa)\
**Post date:** [August 23, 2019, 7:06am UTC](https://community.postman.com/t/is-it-possible-to-have-a-shared-ajv-schema/7275/4 "2019-08-23T07:06:36Z")

</div>

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:

`JSON.stringify(yourObject)`

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.

---

<div class="post-metadata">

**Author:** ![jshea](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jshea](https://community.postman.com/u/jshea)\
**Post date:** [August 29, 2019, 6:27pm UTC](https://community.postman.com/t/is-it-possible-to-have-a-shared-ajv-schema/7275/5 "2019-08-29T18:27:26Z")

</div>

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 [https://json-schema.org/understanding-json-schema/](https://json-schema.org/understanding-json-schema/)  
You can run/test ajv and your schema at the command line with ajv-cli [https://www.npmjs.com/package/ajv-cli](https://www.npmjs.com/package/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) {
    console.log('ajv.errors');
    console.log(ajv.errors);
  }

  pm.expect(validationResult).to.be.true;
});

```

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.

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 29, 2019, 7:22pm UTC](https://community.postman.com/t/is-it-possible-to-have-a-shared-ajv-schema/7275/6 "2019-08-29T19:22:09Z")

</div>

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. 🙂

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

```

This was taken from this question:

> [@Testing Properties and Arrays correctly in GET](https://community.postman.com/t/testing-properties-and-arrays-correctly-in-get/7133):
>
> Hi, I just want to make sure that this is correctly testing that An Object and an Object within an array is present. Example api: { "items": [{ "oID": "0000001211", "oInvoiceNo": "1234567", "OrderBlocks": [{ "olLine": 1, "olProductCode": "001" }] } ] } Example test: pm.test("Order Details are Present", () =\> { pm.response.to.have.jsonBody("oID"); pm.response.to.have.jsonBody("oInvoiceNo"); pm.response.to…

---

<div class="post-metadata">

**Author:** ![jshea](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@jshea](https://community.postman.com/u/jshea)\
**Post date:** [August 29, 2019, 9:23pm UTC](https://community.postman.com/t/is-it-possible-to-have-a-shared-ajv-schema/7275/7 "2019-08-29T21:23:15Z")

</div>

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.
