Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postman: How to check the data type of each value in a response

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?

like image 408
pgtips Avatar asked Feb 13 '18 22:02

pgtips


People also ask

How do I check my Postman response type?

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.

How do I get status code from Postman response?

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.

What is assertion in Postman?

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.


1 Answers

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')
    })
})
like image 94
Danny Dainton Avatar answered Oct 21 '22 03:10

Danny Dainton