Using OR Statement to Validate String In Multiple JSON Attributes?

I am trying to validate the results from a Search query. The objects within the JSON response include a Title and Description. For my basic test, I want to make sure that the search query is included in the title OR the description. To try and solve this, I used:
(pm.expect(title).to.include(search) || pm.expect(description).to.include(search)) and the test is passing, but I want to make sure its not a false positive. Any tips are appreciated, thanks.

@SKSG,

Because a failing assertion will throw an error, using || won’t work. You can try something like this:

const search = 'find me';
const title = 'hi there';
const description = 'going to find me soon';

pm.test('search is included in title or description', () => {
    pm.expect(search).satisfies((s) => {
        return title.includes(search) ||
            description.includes(search);
    });
});

Best,

Kevin

I’ll try that, thanks! I just realized, couldn’t I also use an If -> Else If statement as well? While the order of the conditions being met doesn’t really matter, it will potentially look through both attributes anyway to find the search term.

P.S. - What is that .satisfies((s) function? I’m not finding any documentation on it, but I’m assuming it expects a ‘true’ value to be returned from any of the assertions being tested. What else can I use it for?

Postman uses the Expect assertion style from Chai, and .satisfy/.satisfies is documented as part of the Chai JavaScript API for expect.

.satisfy(matcher[, msg])

  • @param { Function } matcher
  • @param { String } msg optional

Invokes the given matcher function with the target being passed as the first argument, and asserts that the value returned is truthy.

To answer your other question…

Yes, you can flip the logic if you like and use pm.expect.fail.

It would look something like this:

pm.test('search is included in title or description', () => {
    if (!title.includes(search) && !description.includes(search)) {
        pm.expect.fail(`expected title or description to include "${search}"`)
    }
});

Best,

Kevin

I thought of a third, “dirty” option: Since I’m essentially searching for a word within returned text, regardless of which attribute its from, I just created a variable equal to the combined response of title + description and searched though that. That way, I only have to satisfy 1 validation instead of using AND/OR statements.