Need to validate the specific response fields

Hi,

I am getting below response for my API.

“type”: “people”,
“id”: “42”

I want to do a validation that if any field other than “Type” and “ID” will come in my response, script should fail.

How can I do that? Can someone please help.

If I understand your question correctly, when you run your request you want to stop your scripting if a result comes in incorrectly?

You can write validators like

let data = pm.response.json()
let keys = Object.keys(data)
pm.expect(keys).to.equal(2)
pm.expect(keys).to.include("type")
pm.expect(keys).to.include("id")

If ID or Type are missing, those tests will fail, and if you get any other count of attributes than 2, it will fail.

Hi Ian,

Thanks for the reply !!

I am not looking for this exactly. Let me explain my question in another way.

Correct response for my API -

“type”: “people”,
“id”: “42”

Incorrect response for my API -
“type”: “people”,
“id”: “42”
“name” : “Zoya”

I need to keep a validation in my script in such a way that it should work ONLY for “type” and “ID”.

Script should fail if “name” will come in my JSON response.

Hi again.

I actually had a typo in my first expectation, sorry for the confusion there!
pm.expect(keys.length).to.equal(2)

This should ensure that you only get exactly 2 attributes, which it then follows up to make sure you get ID and Type. If you get “name” in the response data, then the length will be 3, and the test will fail at that line.

Does that solve the problem?

Hi Ian,

No worries. :blush:

I understood your point but is there any other way to validate this scenario?

Because what if in worst case scenario the attribute count will be 2 but the attribute would be different.

Like below example: (name will come in place of ID that is incorrect)

type”: “people”,
“name” : “Zoya

I want to run my script only for “ID” and “Type”. No other attributes will be consider as valid.

Schema Validation!

Hope this helps :slight_smile:

Hi Kevin,

Thanks for the help !!

Sorry but I am still stuck in the same situation :frowning:

Schema validation is working for the required attribute. But, if I am getting some additional attributes in the JSON response as I have mentioned above like “name”, script is not failing.

Schema is just checking if the required attributes are present in JSON response but it is not validating if some additional attributes are present in JSON response.

Ah! So close!

Perhaps a combination of what Ian and I had suggested could do the trick?

Schema validation to make sure what should be there is there and then also a check to make sure there are only two properties included.

Hi @minal888

My code should have worked okay to look for exactly 2 attributes:

pm.expect(keys.length).to.equal(2)

If you get anything more than ID/Type, this line of test code will fail.

Can you show us a little more about what’s currently in your test that’s making it through?

Hi,

Bit late. For schema validation the “additionalProperties”: false just before or after “required” attribute will do for you. The schema validation will fail if the response contains any property which is not mentioned in the schema.
Thanks,

Hi @minal888

You can use keys to find all the keys and then compare if they are only “type” and “id”

var arr = Object.keys(pm.response.json())