Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 ,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ?

>Solution :

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 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading