You can use the pm.test
function with the pm.expect
assertions in the Pre-request Scripts
.
As Postman comes with Lodash, you can use the _.get() function in the sandbox to get the data from the stores
array. You would need to use JSON.parse()
to correctly assign the data from the request body in the _.get()
function.
let requestBody = _.get(JSON.parse(pm.request.body.raw), 'stores[0]')
pm.test("Check Body", () => {
pm.expect(requestBody).to.have.keys(['city', 'name'])
})
Or something like this without Lodash:
let requestBody = JSON.parse(pm.request.body.raw)
pm.test("Check Body", () => {
pm.expect(requestBody.stores[0]).to.have.keys(['city', 'name'])
})
More info on the pm.*
API can be found here:
https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/