# Assert contains multiple strings

**URL:** <https://community.postman.com/t/assert-contains-multiple-strings/7383>\
**Category:** Help Hub\
**Tags:** tests, responseassertion\
**Created:** [August 23, 2019, 6:04pm UTC](https://community.postman.com/t/assert-contains-multiple-strings/7383 "2019-08-23T18:04:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![e.mikhailov](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@e.mikhailov](https://community.postman.com/u/e.mikhailov)\
**Post date:** [August 23, 2019, 6:04pm UTC](https://community.postman.com/t/assert-contains-multiple-strings/7383/1 "2019-08-23T18:04:42Z")

</div>

How to assert that variable contains multiple strings?

Something like  
`pm.expect(myText).to.include("text1", "text2");`

Is there a ready-made solution without looping?

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [August 23, 2019, 6:24pm UTC](https://community.postman.com/t/assert-contains-multiple-strings/7383/2 "2019-08-23T18:24:03Z")

</div>

@e.mikhailov Since postman uses [a fork of chai plugin](https://github.com/postmanlabs/chai-postman) for assertions, the library doesn’t support this feature.

Without looping over an array it won’t be possible.

Code snippet could be something like this:

```javascript
let expectedStrings = ['text1', 'text2'];

expectedStrings.forEach((s) => pm.expect(responseString).to.have.string(s));

```

---

<div class="post-metadata">

**Author:** ![e.mikhailov](https://avatars.discourse-cdn.com/v4/letter/e/dc4da7/32.png) [@e.mikhailov](https://community.postman.com/u/e.mikhailov)\
**Post date:** [August 23, 2019, 6:30pm UTC](https://community.postman.com/t/assert-contains-multiple-strings/7383/3 "2019-08-23T18:30:52Z")

</div>

Thanks a lot, your solution seems compact too

---

<div class="post-metadata">

**Author:** ![ofir1212](https://avatars.discourse-cdn.com/v4/letter/o/c89c15/32.png) [@ofir1212](https://community.postman.com/u/ofir1212)\
**Post date:** [May 31, 2021, 6:41am UTC](https://community.postman.com/t/assert-contains-multiple-strings/7383/4 "2021-05-31T06:41:02Z")

</div>

Hi @singhsivcan .  
I tried your snippet but I need something a little different:

pm.test(“Verify any of the following strings”, function () {

let expectedStrings = [‘1110’, ‘1119’];  
responseText = pm.response.text();  
expectedStrings.forEach((s) =\> pm.expect(responseText).to.have.string(s));

});

but I need a relation of **ANY** of the expected strings, not AND (Meaning, if **any** of the expected is found , the test Pass)

Can you please help?

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [May 31, 2021, 6:52am UTC](https://community.postman.com/t/assert-contains-multiple-strings/7383/5 "2021-05-31T06:52:58Z")

</div>

I think it can be quickly done using [Array.some](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) and a little tweaking of the test case:

```auto
pm.test("Verify any of the following strings", function () {
  let expectedStrings = ["1110", "1119"];
  responseText = pm.response.text();

  const hasAnyExpectedString = expectedStrings.some((s) => responseText.includes(s));
  pm.expect(hasAnyExpectedString).to.be.true;
});

```

---

<div class="post-metadata">

**Author:** ![ofir1212](https://avatars.discourse-cdn.com/v4/letter/o/c89c15/32.png) [@ofir1212](https://community.postman.com/u/ofir1212)\
**Post date:** [May 31, 2021, 7:10am UTC](https://community.postman.com/t/assert-contains-multiple-strings/7383/6 "2021-05-31T07:10:00Z")

</div>

Thanks! works perfectly!

---

<div class="post-metadata">

**Author:** ![ofir1212](https://avatars.discourse-cdn.com/v4/letter/o/c89c15/32.png) [@ofir1212](https://community.postman.com/u/ofir1212)\
**Post date:** [May 31, 2021, 5:04pm UTC](https://community.postman.com/t/assert-contains-multiple-strings/7383/7 "2021-05-31T17:04:05Z")

</div>

Hi @singhsivcan .  
Can you help me with verifying also ANY OF given strings in ANY OF selected field value of an array?  
eaxmple:  
Verify that 1234 or 4567 equals the value of any “check\_code” field below:

{  
“id”: “0”,  
“checks”: [  
{  
“check\_number”: 1,  
“check\_code”: 1234  
},  
{  
“check\_number”: 2,  
“check\_code”: 5678  
}  
]  
}

---

<div class="post-metadata">

**Author:** ![singhsivcan](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/singhsivcan/32/1595_2.png) [@singhsivcan](https://community.postman.com/u/singhsivcan)\
**Post date:** [May 31, 2021, 8:38pm UTC](https://community.postman.com/t/assert-contains-multiple-strings/7383/8 "2021-05-31T20:38:39Z")

</div>

I think you can simply map over the items and find the intersection. An optimized way would be to loop and bail-out, but that’s something you can try yourself also.

I’ve not tested the following code, but something like this should do the job in a “compact” (looking) way:

```auto
const _ = require('lodash');
pm.test("Verify any of the following strings", function () {
  let expectedCodes = [1234, 5678],
   codes = _.map(pm.response.json().checks, 'check_code');

  pm.expect(_.intersection(codes, expectedCodes)).to.not.be.empty;
});

```
