postmanlabs / postman-app-support

Postman is an API platform for building and using APIs. Postman simplifies each step of the API lifecycle and streamlines collaboration so you can create better APIs—faster.
https://www.postman.com
5.83k stars 838 forks source link

JSON schema validation not working. #11236

Open DanielGlos opened 2 years ago

DanielGlos commented 2 years ago

Is there an existing issue for this?

Describe the Issue

JSON schema validation will fail when I do this in pre-request:

Object.prototype.testJsonSchemaValidation = function(){ console.log("NOT WORKING!"); };

It will produce this error when using tv4: Validate JSON with TV4 | TypeError: e.definedKeywords[i].slice is not a function and this one when using AJV: Validate JSON with AJV | Error: schema is invalid: data.properties['testJsonSchemaValidation'] should be object,boolean

When I remove the Object.prototype... line from pre-request, validation starts working normally again.

Steps To Reproduce

Here is a simple Postman collection with one request that simulates this issue. Json validation.postman_collection.zip

Screenshots or Videos

No response

Operating System

Windows

Postman Version

9.30.0

Postman Platform

Postman App

Additional Context?

No response

DannyDainton commented 1 year ago

Hey @DanielGlos

I'm not really sure where to go with this issue - When you remove Object.prototype. it's working. Is there a reason why you need that in there? I'm currently unsure what the fix on our side would be for this problem.

DanielGlos commented 1 year ago

I have some shared functions used for testing that are stored in global variable. In pre-request script of each collection I do something like this: eval(pm.globals.get("functions")); This would load my testing functions something like this: Object.prototype.MyTestFunction = ..... So later in each request I can directly call my testing function just by calling MyTestFunction()

I was able to find a workaround for this but I think JSON validation should not crash just by adding anything to Object.prototype.

ABazso commented 1 year ago

What was the workaround? I'm facing the same issue, trying to share code across Postman tests by adding functions to Object.prototype in the pre-request script of the collection but also trying to validate response schemas using AJV or TV4. Getting the same cryptic errors as mentioned by the original author.

DanielGlos commented 1 year ago

You can assign the functions to your own object like this:

myFunctions = {
    function1: function(param1, param2) {
        ...
    },

    function2: function() {
        ...
    }
};

Then you can call it by accessing the object first

myFunctions.function1(val1, val2);