The following is the response body of an API:
JavaScript
x
13
13
1
[
2
{
3
"exercise_num": "1",
4
"expire_date": "2019-03-11T16:31:17.935Z",
5
"created_at": "2019-03-15T11:44:35.698Z"
6
},
7
{
8
"exercise_num": "2",
9
"expire_date": "2019-03-11T16:31:17.935Z",
10
"created_at": "2019-03-15T11:44:38.363Z"
11
}
12
]
13
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.
JavaScript
1
10
10
1
pm.test("Your test name", function() {
2
var jsonData = pm.response.json();
3
pm.expect(jsonData.reduce(function(arr, b) {
4
if (!arr.includes(b.exercise_num)) {
5
arr.push(b.exercise_num);
6
}
7
return arr;
8
}, []).length).to.eql(jsonData.length);
9
});
10