我有一个API的响应是JSON数组的情况。下面我给出了JSON响应的示例,其中“ test”是键,该键可在整个数组中使用。因此,我必须写到Feature步骤,在其中我应该在单个步骤中同时使用null和正则表达式来验证值。如果我正在验证reg exp并且值为null,反之亦然。步骤失败。
对于Ex:
* match each $..test== '#regex ^[ A-Za-z0-9-]*$'
* match each $..test== null
我尝试了以下步骤,但失败了。
* match each $..test== '#regex ^[ A-Za-z0-9-]*$' | test == null
"response": [
{
"test": "Anand07"
},{
"test": null
},{
"test": "Archu06"
}]
感谢您的帮助。
示例代码:
Feature: Validation
Scenario:
* def isValid = function(x){ return x == null || karate.match(x,'#regex ^[ A-Za-z0-9-]*$').pass }
* def response =
"""
[
{
"test": "Anand07"
},
{
"test": null
},
{
"test": "Archu06"
}
]
"""
* match each response[*].test == '#? isValid(_)'