# FAIL validating response if its in an array

**URL:** <https://community.postman.com/t/fail-validating-response-if-its-in-an-array/24390>\
**Category:** Help Hub\
**Created:** [June 2, 2021, 4:04am UTC](https://community.postman.com/t/fail-validating-response-if-its-in-an-array/24390 "2021-06-02T04:04:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aviation-meteorologi](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@aviation-meteorologi](https://community.postman.com/u/aviation-meteorologi)\
**Post date:** [June 2, 2021, 4:04am UTC](https://community.postman.com/t/fail-validating-response-if-its-in-an-array/24390/1 "2021-06-02T04:04:26Z")

</div>

_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)_:

 ![Screen Shot 2021-06-02 at 11.43.35 AM](https://us1.discourse-cdn.com/getpostman/original/3X/7/9/79e05e298100158bece6cdb6af0194159c87ac55.jpeg)

```auto
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)})
}

```

---

<div class="post-metadata">

**Author:** ![taehoshino](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/taehoshino/32/6608_2.png) [@taehoshino](https://community.postman.com/u/taehoshino)\
**Post date:** [June 2, 2021, 5:18am UTC](https://community.postman.com/t/fail-validating-response-if-its-in-an-array/24390/2 "2021-06-02T05:18:07Z")

</div>

Hey @aviation-meteorologi 👋 Welcome to the Postman Community 🚀

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?

```auto
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 🙂

---

<div class="post-metadata">

**Author:** ![aviation-meteorologi](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@aviation-meteorologi](https://community.postman.com/u/aviation-meteorologi)\
**Post date:** [June 2, 2021, 6:56am UTC](https://community.postman.com/t/fail-validating-response-if-its-in-an-array/24390/3 "2021-06-02T06:56:29Z")

</div>

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:

```auto
    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.

 ![Screen Shot 2021-06-02 at 2.52.53 PM](https://us1.discourse-cdn.com/getpostman/original/3X/9/7/9787beece8ef3fb0d7729dc298565c170417d528.jpeg)

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

---

<div class="post-metadata">

**Author:** ![taehoshino](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/taehoshino/32/6608_2.png) [@taehoshino](https://community.postman.com/u/taehoshino)\
**Post date:** [June 2, 2021, 7:19am UTC](https://community.postman.com/t/fail-validating-response-if-its-in-an-array/24390/4 "2021-06-02T07:19:52Z")

</div>

Hey @aviation-meteorologi 👋

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

---

<div class="post-metadata">

**Author:** ![aviation-meteorologi](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@aviation-meteorologi](https://community.postman.com/u/aviation-meteorologi)\
**Post date:** [June 2, 2021, 7:32am UTC](https://community.postman.com/t/fail-validating-response-if-its-in-an-array/24390/5 "2021-06-02T07:32:21Z")

</div>

Hi @taehoshino ,

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

```auto
    "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._

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

```

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

```auto
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)})
}

```

---

<div class="post-metadata">

**Author:** ![taehoshino](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/taehoshino/32/6608_2.png) [@taehoshino](https://community.postman.com/u/taehoshino)\
**Post date:** [June 3, 2021, 4:58am UTC](https://community.postman.com/t/fail-validating-response-if-its-in-an-array/24390/7 "2021-06-03T04:58:09Z")

</div>

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:

```auto
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! 😄

---

<div class="post-metadata">

**Author:** ![aviation-meteorologi](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@aviation-meteorologi](https://community.postman.com/u/aviation-meteorologi)\
**Post date:** [June 3, 2021, 5:20am UTC](https://community.postman.com/t/fail-validating-response-if-its-in-an-array/24390/8 "2021-06-03T05:20:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aviation-meteorologi](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@aviation-meteorologi](https://community.postman.com/u/aviation-meteorologi)\
**Post date:** [June 11, 2021, 1:16am UTC](https://community.postman.com/t/fail-validating-response-if-its-in-an-array/24390/9 "2021-06-11T01:16:30Z")

</div>

Its working properly as expected @taehoshino .

Sharing the full code for others reference:

```auto
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)
        }
})
}

```
