FAIL validating response if its in an array

My question:
Hello, I would like to ask for your help to validate and display all the responseContent messages if it’s in an array.

It is currently working if it’s a single responseContent message, however, it’s FAILING if the responseContent messages is in an array.

Details (like screenshots):

let jsonData = pm.response.json(),
    messages = 
    [
        "Success",
        "Req_id does not exists.",
        "client_id should not be empty",
        "status must be a valid enum value",
        "signature should not be empty",
        "Unauthorized"
    ]


if (jsonData.statusCode === 400) {
    pm.test("Verify if Status code is 400", function () {
        pm.response.to.have.status(400);});
    pm.test(`*** E R R O R *** ${jsonData.message}`, () => {   //This FAILs if message is in array             
        pm.expect(jsonData.statusCode).to.equal(400)
        pm.expect(jsonData.message).to.be.oneOf(messages)
    })
}
else if(jsonData.statusCode === 401) {
    pm.test("Verify if Status code is 401", function () {
        pm.response.to.have.status(401);});
    pm.test(`*** E R R O R *** ${jsonData.message}`, () => {
        pm.expect(jsonData.statusCode).to.equal(401)
        pm.expect(jsonData.message).to.be.oneOf(messages)
    })
}
else{
    pm.test("Verify if Status code is 201", function () {
        pm.response.to.have.status(201);});
    pm.test(`*** S U C C E S S *** ${jsonData.message}`, () => {
        pm.expect(jsonData.statusCode).to.equal(200)})
}

Hey @aviation-meteorologi :wave: Welcome to the Postman Community :rocket:

If I understand your query correctly, you want to loop over jsonData.message array and validate for each element in that array, correct?
If that’s what you want to achieve, maybe you can use for loop as below?

pm.test('Some test', () => {
    for (var i = 0; i < jsonData.message.length; i++) {
        pm.expect(jsonData.message[i]).to.be.oneOf(messages)
    }
})

Hope this helps, but please do let me know if this is not working :slightly_smiling_face:

Thank you for your reply.

Yes, your understanding is correct.
I’ve implemented your loop suggestion and it’s working for some messages.

However, after I run the test, results were still FAILED for some messages even though the message is included and properly indicated in the array.

Here are the messages:

    messages = 
    [
        "Success",
        "Req_id does not exists.",
        "client_id should not be empty",
        "status must be a valid enum value",
        "signature should not be empty",
        "Unauthorized"
    ]

Here’s the test result.

Expected result should be PASS since the message is available in the array of messages.

Hey @aviation-meteorologi :wave:

Would you be able to share the whole response body as well as your script?

Hi @taehoshino ,

For Loop solved my problem for message array like the response below:

    "statusCode": 400,
    "message": [
        "client_id should not be empty"
    ],
    "error": "Bad Request"
}

But a response message not in array encountered the issue: (Some responses came from built-in NestJS validation [array ie. above response] some are handled in the code [not in array ie. below response]
NOTE: Before the change, I can handle the message not in array.

{
    "statusCode": 400,
    "message": "Req_id does not exists.",
    "error": "Bad Request"
}

Here’s the updated script using the suggested For Loop:

let jsonData = pm.response.json(),
    messages = 
    [
        "Success",
        "client_id should not be empty",
        "status must be a valid enum value",
        "signature should not be empty",
        "Unauthorized",
        "Req_id does not exists."
    ]

if (jsonData.statusCode === 400) {
    pm.test("Verify if Status code is 400", function () {
        pm.response.to.have.status(400);});
    pm.test(`*** E R R O R *** ${jsonData.message}`, () => {  //This section <-------
        for (var i = 0; i < jsonData.message.length; i++) {
            pm.expect(jsonData.message[i]).to.be.oneOf(messages)
    }
})
}
else if(jsonData.statusCode === 401) {
    pm.test("Verify if Status code is 401", function () {
        pm.response.to.have.status(401);});
    pm.test(`*** E R R O R *** ${jsonData.message}`, () => {
        pm.expect(jsonData.statusCode).to.equal(401)
        pm.expect(jsonData.message).to.be.oneOf(messages)
    })
}
else{
    pm.test("Verify if Status code is 201", function () {
        pm.response.to.have.status(201);});
    pm.test(`*** S U C C E S S *** ${jsonData.message}`, () => {
        pm.expect(jsonData.statusCode).to.equal(200)})
}

Hey @aviation-meteorologi,

Thanks for your response!!

I think the issue now is the script is trying to access array element while it is not an array.
The following should then work here:

pm.test(`*** E R R O R *** ${jsonData.message}`, () => {  //This section <-------
    if (Array.isArray(jsonData.message)) {
        for (var i = 0; i < jsonData.message.length; i++) {
        pm.expect(jsonData.message[i]).to.be.oneOf(messages)
    } else {
        pm.expect(jsonData.message).to.be.oneOf(messages)
    }
}

Hope this helps! :grinning_face_with_smiling_eyes:

Hi @taehoshino ,

Yes, to be able to determine if the element is not array.
I’ll update you the soonest once I’ve incorporated the suggested script.

Nevertheless, Thank you for your support and meaningful responses.

1 Like

Its working properly as expected @taehoshino .

Sharing the full code for others reference:

let jsonData = pm.response.json(),
    messages = 
    [
        "Success",
        "User is already activated",
        "Token has already expired.",
    ]

if (jsonData.statusCode === 201) {
    pm.test("Verify if Status code is 201", function () {
    pm.response.to.have.status(201);});
    pm.test(`*** S U C C E S S *** ${jsonData.message}`, () => {
    pm.expect(jsonData.statusCode).to.equal(200)})
}
else{
    pm.test("Verify if Status code is 400", function () {
        pm.response.to.have.status(400);});
    pm.test(`*** E R R O R *** ${jsonData.message}`, () => {  //This section <-------
    if (Array.isArray(jsonData.message)) {
        for (var i = 0; i < jsonData.message.length; i++) {
        pm.expect(jsonData.message[i]).to.be.oneOf(messages)} }
    else {
        pm.expect(jsonData.message).to.be.oneOf(messages)
        }
})
}
2 Likes