Validate a parameter value that is present in a list

Question for Kin:
I want to validate the values of a particular field in a list which is repeated all over.
Example:

{
  "deleates": [
    {
      "accepted": false,
      "applode": "T-APP-M01",
      "bot": {
        "code": "901",
        "key_types": [
          {
            "code": "uyt",
            "value": "1"
          }
        ]
      },
      "refusal": {
        "code": "1306",
        "reason": "LANDEDUND"
      },
      "retention_period_months": "1",
      "retention_start_date": "2020-03-10"
    },
    {
      "accepted": false,
      "application_code": "T-APP-M01",
      "bot": {
        "code": "901",
        "key_types": [
          {
            "code": "MA_ID",
            "value": "1"
          }
        ]
      },
      "refusal": {
        "code": "502",
        "reason": "LANDENVALID"
      },
      "retention_period_months": "3",
      "retention_start_date": "2020-03-10"
    }
  ],
  "transaction_id": 462
}

Above one is my response Body and i want to validate the value code which is present in the object โ€œrefusalโ€
deleates is a list and refusal object is repeated twice with 2 different values
Below is the code written by me:

pm.test("Verifying refusal code", function () {

    var jsonData = pm.response.json();
         console.log(jsonData.deleates[0].accepted);
      for (var i = 0; i < jsonData.deleates.length; i++) {
        //   if(jsonData.deleates[i].accepted == false){
          console.log(jsonData.deleates[i].refusal.code);
    pm.expect(jsonData.deleates[i].refusal.code).to.eql("1506");
    //   }
      }
});

Below is my test Results:
Verifying refusal code | AssertionError: expected โ€˜502โ€™ to deeply equal โ€˜1306โ€™

How to validate the other value in a List of objects. Kindly help

Hey @honeycle22

Welcome to the community! :trophy:

You could use something quite hardcoded like this:

pm.test("Verifying refusal code", function () {
    let jsonData = pm.response.json();
    pm.expect(jsonData.deleates[0].refusal.code).to.eql("1306");
    pm.expect(jsonData.deleates[1].refusal.code).to.eql("502");
}); 

Your test is looping through the right part of the response but itโ€™s asserting against a single string "1506" which is fine if itโ€™s thatโ€™s either the only code for that property or you only have a single object for that array but as neither of those is the case, youโ€™re seeing it fail on the "502" value.

If you have a known set of codes, you could do it like this:

pm.test("Verifying refusal code", function () {
    var jsonData = pm.response.json();
    for (var i = 0; i < jsonData.deleates.length; i++) {
        pm.expect(jsonData.deleates[i].refusal.code).to.be.oneOf(["1306", "502"]);
    }
});
1 Like

Thank You Very Much.

One other question is:
I have 3 requests in a Collection, in the 1st iteration i want to send 3 requests but in 2nd iteration i want to post only 2 requests from the collection. This should happen automatically instead of manually clicking the Run button on second iteration

Can you please help me to achieve this pls