Search code examples
postmanjsonschemapostman-pre-request-script

Postman JSON Schema Validation fails, if an Object.prototype function declared prior to the validation


I have a schema validation test in my postman collection, which validates if the response adhere to the schema. This is how I do it.

var schema = 
{
  
  "type": "object",
  "properties": {
    "data": {
      "type": "object",
      "properties": {....
}

    pm.test("Schema Validation - TC001", function(){

    pm.response.to.have.jsonSchema(schema);
    

});

When I execute just this script, it validates the schema of the response successfully.

However, in my postman collection I have declared a global function, prior to the schema validation, using Object.prototype() and I'm calling the function as _.funcABC("a","b","c")

Object.prototype.funcABC = function (var1, var2, var3) {
   console.log("test");
}

And, my schema validation fails, when I run the entire collection.

While troubleshooting, I came across this, which indicates that the Object.prototype could interfere with JSONschema.

Is there a way to overcome this interference of Object.prototype() on JSONschema? So far, I couldn't find a workable solution.

Thanks.


Solution

  • What stops you from doing this:

    pm.test('validate schema', function () {
        let temp = Object.prototype.function1
        delete Object.prototype.function1
        pm.expect(ajv.validate(schema_response, response)).to.true;
        Object.prototype.function1 = temp
    })