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?
The Postman Body tab gives you several tools to help you understand the response quickly. You can view the body in one of four views: Pretty, Raw, Preview, and Visualize. in the results pane. You can also place your cursor in the response and select ⌘+F or Ctrl+F.
Getting started with tests To write your first test script, open a request in Postman, then select the Tests tab. Enter the following JavaScript code: pm.test("Status code is 200", function () { pm.response.to.have.status(200); }); This code uses the pm library to run the test method.
Advertisements. Assertions are used to verify if the actual and expected values have matched after the execution of a test. If they are not matching, the test shall fail and we shall get the reason for failure from the output of the test. An assertion returns a Boolean value of either true or false.
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.
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')
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With