ThrowableException
ThrowableException

Reputation: 1426

Karate Api : check if a phrase is available response object array

I've a response

{ errors: [
       {
        code: 123,
        reason: "this is the cause for a random problem where the last part of this string is dynamically generated"
     }  ,
     {
        code: 234,
        reason: "Some other error for another random reason"
     }
    ...
    ...
}

Now when I validate this response I use following

...
...    
And match response.errors[*].reason contains "this is the cause"

This validation fails, because there is an equality check for complete String for every reason ,

I all I want is, to validate that inside the errors array, if there is any error object, which has a reason string type property, starting with this is the cause phrase.

I tried few wild cards but didn't work either, how to do it ?

Upvotes: 1

Views: 436

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

For complex things like this, just switch to JS.

* def found = response.errors.find(x => x.reason.startsWith('this is the cause'))
* match found == { code: 123, reason: '#string' }
# you can also do
* if (found) karate.log('found')

Any questions :)

Upvotes: 1

Related Questions