NOT verifying error schema and getting error

I define my error schema at collection level in Tests. I have following code

const errorSchema = {
“type”: “object”,
“properties”: {
“httpStatus”: { “type”: “string” },
“status”: { “type”: “string” },
“message”: { “type”: “string” },
“timestamp”: { “type”: “string” }
},
“required”: [“httpStatus”, “status”, “message”, “timestamp”]
}

pm.globals.set(“errorSchema”, JSON.stringify(errorSchema))

postman.setGlobalVariable(“verifyErrorSchema”, () => {
pm.test(“Validate JSON Error schema”, function () {
console.log(“Before Error Schema”)
pm.response.to.have.jsonSchema(JSON.parse(errorSchema));
console.log(“After Error Schema”)
});
});

While in test I am calling this way
eval(globals.verifyErrorSchema)();

it’s giving error saying
Validate JSON Error schema | ReferenceError: errorSchema is not defined

Any idea or clue what the error is in this?

There is a lot going on here.

First you define the errorSchema within the collection.

Then you set it as a global variable with the same variable name (not good practice, keep the variable names separate) and you also convert it to a string. If you have variables with the same names, then scope becomes an issue. Just name them uniquely. Much easier to track what is going on.

In your test, you then parse it back to a JavaScript object. Why not just leave it as a JavaScript object when you set the global variable. Then you don’t have to use stringify or parse.

I don’t get what you are doing with “verifyErrorSchema”. It looks like you are trying to set another global variable using the test as a function. (Are you sure you want this to be global variables - available to all collections in Postman).

Usually this is as simple as the following…

// In your collection tests tab

const schemaDefinition = {
    'type': 'object',
    'properties': {
        'httpStatus': {
            type: 'string'
        },
        'status': {
            type: 'string'
        },
        'message': {
            type: 'string'
        },
       'timestamp': {
            type: 'string'
        }
    },
    required: ['httpStatus', 'status', 'message', 'timestamp']
};

pm.collectionVariables.set("collectionSchema", schemaDefinition);

// Your test

const schema = pm.collectionVariables.get("collectionSchema");

const response = pm.response.json();

pm.test('Schema is valid', () => {
    pm.expect(response).to.have.jsonSchema(schema);
});

Just try this within the tests tab for a single request first. Get it working there, before you move it to the collection level and start using global variables (although pretty sure it should be a collection variable instead).