# Validating schema using AJV

**URL:** <https://community.postman.com/t/validating-schema-using-ajv/43794>\
**Category:** Help Hub\
**Created:** [February 9, 2023, 10:17pm UTC](https://community.postman.com/t/validating-schema-using-ajv/43794 "2023-02-09T22:17:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tvu](https://avatars.discourse-cdn.com/v4/letter/t/e47774/32.png) [@tvu](https://community.postman.com/u/tvu)\
**Post date:** [February 9, 2023, 10:17pm UTC](https://community.postman.com/t/validating-schema-using-ajv/43794/1 "2023-02-09T22:17:10Z")

</div>

I’m trying to validate this scheme and don’t understand why it failed.

{  
“id”: 1,  
“displayName”: “Age”,  
“createdAt”: “2021-07-06T19:22:10Z”,  
“modifiedAt”: “2021-07-06T19:22:10Z”  
}

var Ajv = require(‘ajv’),  
ajv = new Ajv({  
logger: console,  
allErrors: true,  
verbose: true  
}),  
schema = {  
“type”: “object”,  
“properties”: {  
“id”: {  
“type”: “integer”  
},  
“displayName”: {  
“type”: “string”  
},  
“createdAt”: {  
“type”: “string”  
},  
“modifiedAt”: {  
“type”: “string”  
}  
},  
“required”: [  
“id”,  
“displayName”,  
“createdAt”,  
“modifiedAt”  
]  
};

pm.test(‘Schema is valid’, function () {  
var data = pm.response.json();  
pm.expect(ajv.validate(schema, data), printErrors(ajv)).to.be.true;  
});

---

<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:** [February 10, 2023, 9:39am UTC](https://community.postman.com/t/validating-schema-using-ajv/43794/2 "2023-02-10T09:39:14Z")

</div>

Hi @tvu

What failure do you see?  
I’m guessing you are seeing:

> Schema is valid | ReferenceError: printErrors is not defined

This is because you are using “printErrors” without declaring the function.

Add this to the top of your test script:

```auto
function printErrors(ajv){
    if(ajv.errors !== null){
        console.log(JSON.stringify(ajv.errors[0]));    
    }
}

```

Example working:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/9/5/958fc2a5f5aa6819eab86748cf3d531e9cf40b89.png)

---

<div class="post-metadata">

**Author:** ![tvu](https://avatars.discourse-cdn.com/v4/letter/t/e47774/32.png) [@tvu](https://community.postman.com/u/tvu)\
**Post date:** [February 10, 2023, 3:06pm UTC](https://community.postman.com/t/validating-schema-using-ajv/43794/3 "2023-02-10T15:06:48Z")

</div>

> [@w4dd325](#):
>
> ```auto
> function printErrors(ajv){
> if(ajv.errors !== null){
> console.log(JSON.stringify(ajv.errors[0]));    
> }
> }
> 
> ```

Oh shoot I forgot about the function! Thank you
