将 json 的每个值与正则表达式和逻辑运算符匹配
Match each value of json with a regex and logical operator
考虑以下 JSON。我想使用 OR 条件匹配每个 temp 都有部分字符串匹配 'process' 即在每个 temp 中,severity 或 conditionName 应该有 'process'
的部分匹配
* def temp = [{ "severity": "Critical","conditionName": "process"}, { "severity": "Critical 2","conditionName": "process 2" }, { "severity": "Critical 2","conditionName": "processor" } ]
我尝试了以下代码:
* def isMatch = function(x) { return x.severity == '#regex (?i).*process.*' || x.conditionName == '#regex (?i).*process.*' }
* match each temp == '#? isMatch(_)'
请注意,#regex
等内容 仅 用于空手道 match
关键字。您正试图在此处的 JavaScript 代码中使用它,这将不起作用。
这应该可以实现你想要做的事情:
* def isMatch = function(x) { return x.severity.includes('process') || x.conditionName.includes('process') }
你可以做更复杂的匹配(我认为甚至 JS 中的正则表达式也是可能的)所以记住这一点。另请注意,有一个 karate.match()
API - 它将 return 一个带有 pass
属性 的对象 - 但它取决于空手道中可用的变量范围:
考虑以下 JSON。我想使用 OR 条件匹配每个 temp 都有部分字符串匹配 'process' 即在每个 temp 中,severity 或 conditionName 应该有 'process'
的部分匹配* def temp = [{ "severity": "Critical","conditionName": "process"}, { "severity": "Critical 2","conditionName": "process 2" }, { "severity": "Critical 2","conditionName": "processor" } ]
我尝试了以下代码:
* def isMatch = function(x) { return x.severity == '#regex (?i).*process.*' || x.conditionName == '#regex (?i).*process.*' }
* match each temp == '#? isMatch(_)'
请注意,#regex
等内容 仅 用于空手道 match
关键字。您正试图在此处的 JavaScript 代码中使用它,这将不起作用。
这应该可以实现你想要做的事情:
* def isMatch = function(x) { return x.severity.includes('process') || x.conditionName.includes('process') }
你可以做更复杂的匹配(我认为甚至 JS 中的正则表达式也是可能的)所以记住这一点。另请注意,有一个 karate.match()
API - 它将 return 一个带有 pass
属性 的对象 - 但它取决于空手道中可用的变量范围: