# How to automatically validate response json schema based on API definition(yaml)

**URL:** <https://community.postman.com/t/how-to-automatically-validate-response-json-schema-based-on-api-definition-yaml/44548>\
**Category:** Help Hub\
**Tags:** tests, json, schema, schemavalidation, yaml\
**Created:** [February 28, 2023, 2:06pm UTC](https://community.postman.com/t/how-to-automatically-validate-response-json-schema-based-on-api-definition-yaml/44548 "2023-02-28T14:06:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![v-andre](https://avatars.discourse-cdn.com/v4/letter/v/da6949/32.png) [@v-andre](https://community.postman.com/u/v-andre)\
**Post date:** [February 28, 2023, 2:06pm UTC](https://community.postman.com/t/how-to-automatically-validate-response-json-schema-based-on-api-definition-yaml/44548/1 "2023-02-28T14:06:40Z")

</div>

_My question_:

How can I automate schema validation at a higher folder level to compare with api YAML definition from workspace. I am a big fan of the DRY principle, as such, I am putting my tests at higher folder levels to only script them once. The way I figured I could validate the response schema at a folder level is by having it compared to a variable of the expected schema. I would like to push it further and not have to enter the expected response schema for each endpoint. I was thinking I could extract the expected schema from my API definition already liked to that collection. I am blocked at converting my YAML to a json. I can’t seem to figure it out…

Here is how my test is setup so far:

1. in my init requests, I created a a call to [https://api.getpostman.com/apis/{{apiID}](https://api.getpostman.com/apis/%7B%7BapiID%7D)}/versions/{{versionID}}/schemas/{{schemaID}} and extracted the value in YAML and inserted it in a variable “APIYamlSchema”

2. What I would like to add at this level is the conversion of the yaml to json format and insert that into a variable instead of the one at step 1

3. In the folder that will contain the test, I will get that variable, and compare it with the response for the requests.

For step 2, I was trying to convert yaml to json using the js-yaml library. But I can’t seem to make it work…

What I already tried is:

```auto
// convert YAML to json format
const yamljs_code = pm.collectionVariables.get('js_yaml_code')
eval(yamljs_code);
yamljs.load(fullYamlSchema)

```

I have also tried just using the example in this collection with the librairie it uses:  
([Postman](https://www.postman.com/postman/workspace/postman-answers/collection/13455110-7a6c90f0-0062-4089-b206-27c803dc1c37?action=share&creator=25188228))  
I get an error when loading the library from a variable:

```auto
// dayjs_code now contains the library code

const dayjs_code = pm.collectionVariables.get('dayjs_code');

// Invoke an anonymous function to get access to the dayjs library methods

(new Function(dayjs_code))();

let today = new Date();

console.log(dayjs(today).format())

```

I get this error: “ReferenceError: dayjs is not defined”

Also, if anyone has a suggestion on how to do this in a cleaner way, I am open to them.
