# Using AJv to validate Json schema

**URL:** <https://community.postman.com/t/using-ajv-to-validate-json-schema/38309>\
**Category:** Help Hub\
**Tags:** javascript\
**Created:** [August 18, 2022, 6:42pm UTC](https://community.postman.com/t/using-ajv-to-validate-json-schema/38309 "2022-08-18T18:42:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![phuongphuong99](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@phuongphuong99](https://community.postman.com/u/phuongphuong99)\
**Post date:** [August 18, 2022, 6:42pm UTC](https://community.postman.com/t/using-ajv-to-validate-json-schema/38309/1 "2022-08-18T18:42:26Z")

</div>

Hello everyone, i’m having a trouble when i use ajv to validate Json schema. This is my test script:

const expectSchema = {  
“definitions”: {  
“Welcome”: {  
“type”: “object”,  
“additionalProperties”: false,  
“properties”: {  
“id”: {  
“type”: “integer”  
},  
“name”: {  
“type”: “string”  
},  
“type”: {  
“type”: “string”  
},  
“available”: {  
“type”: “boolean”  
}  
},  
“required”: [  
“available”,  
“id”,  
“name”,  
“type”  
],  
“title”: “Welcome”  
}  
}  
}

var Ajv = require(‘ajv’)  
var ajv = new Ajv({logger: console})  
pm.response.json().forEach(function(book){  
console.log(ajv.errors)  
pm.test(‘Schema is valid’, function() {  
pm.expect(ajv.validate(expectSchema, book)).to.be.true;  
});  
})  
And here is examples json when i get them from url:  
[  
{  
“id”: 1,  
“name”: “The Russian”,  
“type”: “fiction”,  
“available”: true  
},  
{  
“id”: 2,  
“name”: “Just as I Am”,  
“type”: “non-fiction”,  
“available”: false  
},  
{  
“id”: 3,  
“name”: “The Vanishing Half”,  
“type”: “fiction”,  
“available”: true  
},  
{  
“id”: 4,  
“name”: “The Midnight Library”,  
“type”: “fiction”,  
“available”: true  
},  
{  
“id”: 5,  
“name”: “Untamed”,  
“type”: “non-fiction”,  
“available”: true  
},  
{  
“id”: 6,  
“name”: “Viscount Who Loved Me”,  
“type”: “fiction”,  
“available”: true  
}  
]

All my tests are passing when it is run, but when i change type of any properties (example: i change type of id to string or boolean, type of name into integer) my tests still pass. Could you guys explain it to me? thank you sm.

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [August 19, 2022, 8:52pm UTC](https://community.postman.com/t/using-ajv-to-validate-json-schema/38309/2 "2022-08-19T20:52:59Z")

</div>

Hi @phuongphuong99

You won’t need to import AJV like that, it’s already built into Postman.  
As explained (about halfway through) in this video; [JSON Schema validation in Postman - YouTube](https://www.youtube.com/watch?v=haDQBmQii2g)

* * *

Just been playing around with this and even though the schema appears to validate I think your schema is incorrect.

It doesn’t recognise 1 as an int and there are a few other things that didn’t seem to work right.

I knocked up a new schema with an online [JSON to schema converter](https://www.liquid-technologies.com/online-json-to-schema-converter)… the result looks like this;  
Note: snipped to keep it short…

```auto
const expectSchema = {
    "type": "array",
  "items": [
    {
      "type": "object",
      "properties": {
        "id": {
          "type": "integer"
        },
        "name": {
          "type": "string"
        },
        "type": {
          "type": "string"
        },
        "available": {
          "type": "boolean"
        }
      },
      "required": [
        "id",
        "name",
        "type",
        "available"
      ]
    }
]
}

```

The validation looks like this;

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/6/6e54b6ad8b953e97cc2b23a44f6a4ca6eec2d26f.png)

and an invalid value;

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/4/494e84f677b018f740db4e3ee275299bbd8f76ef.png)

Hope this helps.

---

<div class="post-metadata">

**Author:** ![phuongphuong99](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@phuongphuong99](https://community.postman.com/u/phuongphuong99)\
**Post date:** [September 4, 2022, 12:04pm UTC](https://community.postman.com/t/using-ajv-to-validate-json-schema/38309/3 "2022-09-04T12:04:27Z")

</div>

thanks for your help. I edited my schema and it worked. thank you so much
