Skip to content
Advertisement

How to verify if a node in response body has unique value with postman?

The following is the response body of an API:

[
{
    "exercise_num": "1",
    "expire_date": "2019-03-11T16:31:17.935Z",
    "created_at": "2019-03-15T11:44:35.698Z"
},
{
    "exercise_num": "2",
    "expire_date": "2019-03-11T16:31:17.935Z",
    "created_at": "2019-03-15T11:44:38.363Z"
}
]

In Postman Tests, how to verify if exercise_num node in the response body above is unique?

Advertisement

Answer

Filter out unique exercise_num values and compare the length of the actual array and unique value array. Where you can use Array#reduce method for filtering unique values.

pm.test("Your test name", function() {
  var jsonData = pm.response.json();
  pm.expect(jsonData.reduce(function(arr, b) {
    if (!arr.includes(b.exercise_num)) {
      arr.push(b.exercise_num);
    }
    return arr;
  }, []).length).to.eql(jsonData.length);
});
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement