How do I, in Postman, validate that in a "data" array all elements contain a specific value in the "name" field?

Asked

Viewed 125 times

1

Recebo como resposta de uma requisição, o seguinte response:

"data": [
    {
        "id": 148,
        "name": "Amrita Naik",
        "email": "[email protected]",
        "gender": "Female",
        "status": "Inactive",
        "created_at": "2021-01-25T03:50:06.849+05:30",
        "updated_at": "2021-01-25T03:50:06.849+05:30"
    },
    {
        "id": 156,
        "name": "Dron Naik",
        "email": "[email protected]",
        "gender": "Male",
        "status": "Active",
        "created_at": "2021-01-25T03:50:06.975+05:30",
        "updated_at": "2021-01-25T03:50:06.975+05:30"
    },
    {
        "id": 178,
        "name": "Rukmin Naik",
        "email": "[email protected]",
        "gender": "Male",
        "status": "Active",
        "created_at": "2021-01-25T03:50:07.351+05:30",
        "updated_at": "2021-01-25T03:50:07.351+05:30"
    },

I need to validate that all name fields have the name "Naik". Although my test passes, I can’t check, as it doesn’t print anything on the Postman console. Here’s what I tried to do:

pm.test("Deve validar o nome", function () {
const jsonData = JSON.parse(responseBody);
for(var i = 0; i < jsonData.length; i++) {
    console.log(data[i].name);
}
    pm.expect(jsonData.data[i].name).to.include("Naik");
});

1 answer

0


You can use the method every(), testing whether all elements within an array meet a condition:

var name = "Rukmin Naik";

var array = `{
   "data":[
      {
         "id":148,
         "name":"Amrita Naik",
         "email":"[email protected]",
         "gender":"Female",
         "status":"Active",
         "created_at":"2021-01-25T03:50:06.849+05:30",
         "updated_at":"2021-01-25T03:50:06.849+05:30"
      },
      {
         "id":156,
         "name":"Dron Naik",
         "email":"[email protected]",
         "gender":"Male",
         "status":"Active",
         "created_at":"2021-01-25T03:50:06.975+05:30",
         "updated_at":"2021-01-25T03:50:06.975+05:30"
      },
      {
         "id":178,
         "name":"Rukmin Naik",
         "email":"[email protected]",
         "gender":"Male",
         "status":"Active",
         "created_at":"2021-01-25T03:50:07.351+05:30",
         "updated_at":"2021-01-25T03:50:07.351+05:30"
      }
   ]
}`;
    
 var obj = JSON.parse(array);
 
if (!obj.data.every(e => e.name === name)) {
   console.log("name invalido");
}

if (!obj.data.every(e => e.status === "Active")) {
   console.log(" status invalido");
}

To demonstrate when it is ok, I put a validation to "status" and changed to all "Active"

  • Thank you! I was able to solve it as follows: pm.test("Must validate the name", Function() { var jsonData = pm.response.json(); for (var i = 0; i < jsonData.data.length; i++) { var obj = jsonData.data[i].name; console.log(obj); pm.expect(obj).to..(include"Naik"); } }); So all names containing the name "Naik" are validated.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.