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

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{{apiID}}/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:

// convert YAML to json format
const yamljs_code = pm.collectionVariables.get('js_yaml_code')

I have also tried just using the example in this collection with the librairie it uses:
I get an error when loading the library from a variable:

// 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();


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.