1

Consider below JSON. I want to match each of temp has a partial string matching 'process' using a OR condition i.e. in each temp either severity or conditionName should have a partial matching of 'process'

* def temp = [{ "severity": "Critical","conditionName": "process"}, { "severity": "Critical 2","conditionName": "process 2" }, { "severity": "Critical 2","conditionName": "processor" } ]

I tried below code:

* def isMatch = function(x) { return x.severity == '#regex (?i).*process.*' || x.conditionName == '#regex (?i).*process.*' }
* match each temp == '#? isMatch(_)'
Suraj Ravi
  • 103
  • 7

1 Answers1

0

Please note that the things like #regex etc. work only for the Karate match keyword. You are attempting to use this in JavaScript code here, which will not work.

This should achieve what you want to do:

* def isMatch = function(x) { return x.severity.includes('process') || x.conditionName.includes('process') }

You can do more complicated matches (I think even regex in JS is possible) so keep that in mind. Also note that there is a karate.match() API - which will return an object with a pass property - but it depends on a variable being available in the Karate scope: https://stackoverflow.com/a/50350442/143475

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • I already tried karate.match() and some of JS regex including 'includes'. The below error is what I got: 'TypeError: x.severity.includes is not a function in ' – Suraj Ravi Mar 10 '21 at 08:54
  • @SurajRavi can you please start testing the 1.0 RC version: https://github.com/intuit/karate/wiki/1.0-upgrade-guide - else maybe `contains` would work instead of `includes` – Peter Thomas Mar 10 '21 at 08:56
  • I will try with 1.0 RC version and update here. I have already tried contains. – Suraj Ravi Mar 10 '21 at 09:01
  • @SurajRavi well, it works for me. follow this process if needed: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue - also my recommendation is try to convert your json into a "shape" easier to do a simple `match`, refer: https://stackoverflow.com/a/53120851/143475 – Peter Thomas Mar 10 '21 at 09:04