Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  96] [ 4]  / answers: 1 / hits: 17986  / 6 Years ago, tue, february 13, 2018, 12:00:00

How do I check that the data type of each value in an API response is NOT an integer?



For example, if my API returns this:



 teamPermissions: [
Edit,
Administrator,
ReadOnly,
etc
]


I need a Postman test to make sure that an integer is never returned in the teamPermission array.



Here's what I started but need assistance:



var jsonData = JSON.parse(responseBody);
tests[Team Permissions Do Not Contain Integers] = typeof(jsonData.teamPermissions) !== number


This passes because teamPermissions is an object but how do I check each value of the object is not an integer?


More From » postman

 Answers
13

This should do the check for you:



pm.test('Not contain numbers', () => {
var jsonData = pm.response.json()
for (i = 0; i < jsonData.teamPermissions.length; i++) {
pm.expect(jsonData.teamPermissions[i]).to.not.be.a('number')
}
})


Here's what the check will do if a number is part of the array, I've logged out the types so you can see what it's checking against.



Postman



Another alternative is to use Lodash, it's a build-in module for the Postman native app. This code will run the same check as the one above:



pm.test('Not contain numbers', () => {
_.each(pm.response.json().teamPermissions, (arrItem) => {
pm.expect(arrItem).to.not.be.a('number')
})
})

[#55159] Monday, February 12, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bradenc

Total Points: 75
Total Questions: 96
Total Answers: 129

Location: Burundi
Member since Thu, Feb 10, 2022
2 Years ago
;