# Need to validate the specific response fields

**URL:** <https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836>\
**Category:** Help Hub\
**Created:** [February 22, 2022, 6:22pm UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836 "2022-02-22T18:22:24Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![minal888](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@minal888](https://community.postman.com/u/minal888)\
**Post date:** [February 22, 2022, 6:22pm UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836/1 "2022-02-22T18:22:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ianthepostmanaut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ianthepostmanaut/32/11109_2.png) [@ianthepostmanaut](https://community.postman.com/u/ianthepostmanaut)\
**Post date:** [February 23, 2022, 12:06am UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836/2 "2022-02-23T00:06:42Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![minal888](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@minal888](https://community.postman.com/u/minal888)\
**Post date:** [February 23, 2022, 2:41am UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836/3 "2022-02-23T02:41:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ianthepostmanaut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ianthepostmanaut/32/11109_2.png) [@ianthepostmanaut](https://community.postman.com/u/ianthepostmanaut)\
**Post date:** [February 23, 2022, 5:17pm UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836/4 "2022-02-23T17:17:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![minal888](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@minal888](https://community.postman.com/u/minal888)\
**Post date:** [February 23, 2022, 5:44pm UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836/5 "2022-02-23T17:44:20Z")

</div>

Hi Ian,

No worries. 😊

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

---

<div class="post-metadata">

**Author:** ![minal888](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@minal888](https://community.postman.com/u/minal888)\
**Post date:** [February 23, 2022, 5:46pm UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836/6 "2022-02-23T17:46:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevinc-postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevinc-postman/32/12774_2.png) [@kevinc-postman](https://community.postman.com/u/kevinc-postman)\
**Post date:** [February 23, 2022, 9:10pm UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836/7 "2022-02-23T21:10:25Z")

</div>

Schema Validation!

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/b/bf4e708c3b19edd12dce870cd9a0159fccdb77bc.png)

Hope this helps 🙂

---

<div class="post-metadata">

**Author:** ![minal888](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@minal888](https://community.postman.com/u/minal888)\
**Post date:** [February 24, 2022, 3:11am UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836/8 "2022-02-24T03:11:39Z")

</div>

Hi Kevin,

Thanks for the help !!

Sorry but I am still stuck in the same situation ☹

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.

---

<div class="post-metadata">

**Author:** ![kevinc-postman](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/kevinc-postman/32/12774_2.png) [@kevinc-postman](https://community.postman.com/u/kevinc-postman)\
**Post date:** [February 24, 2022, 10:12pm UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836/9 "2022-02-24T22:12:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ianthepostmanaut](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/ianthepostmanaut/32/11109_2.png) [@ianthepostmanaut](https://community.postman.com/u/ianthepostmanaut)\
**Post date:** [March 1, 2022, 11:05pm UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836/10 "2022-03-01T23:05:30Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![zaheerahmad](https://avatars.discourse-cdn.com/v4/letter/z/3da27b/32.png) [@zaheerahmad](https://community.postman.com/u/zaheerahmad)\
**Post date:** [April 28, 2022, 5:25am UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836/11 "2022-04-28T05:25:59Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![jchaugule](https://avatars.discourse-cdn.com/v4/letter/j/a183cd/32.png) [@jchaugule](https://community.postman.com/u/jchaugule)\
**Post date:** [April 29, 2022, 10:51am UTC](https://community.postman.com/t/need-to-validate-the-specific-response-fields/32836/12 "2022-04-29T10:51:53Z")

</div>

Hi @minal888

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

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

```
