如何验证响应是否至少有一个有效的数组数据



我得到了这样的响应:

{
"by_group": [
{
"key": "2021-03-17T00:00:00.000+08:00",
"by_state": [
{
"key": "STATE1",
"value": 1
},
{
"key": "STATE2",
"value": 2
}
]
},
{
"key": "2021-03-20T00:00:00.000+08:00",
"by_state": [
{
"key": "STATE3",
"value": 3
},
{
"key": "STATE4",
"value": 4
}
]
},
{
"key": "2021-03-24T00:00:00.000+08:00",
"by_state": []
}
]
}

此处使用的模式:

* def schema2 = { key : '#string', value : '##number? _ >= 0' }
* def schema1 = { key : '#string', by_state : '#[_ > 0] schema2' }
And match response ==
"""
{
by_group: '#[_ > 0] schema1'
}
"""

当我们得到第三个空数组时,它失败了,但我们允许这种情况发生。

我们只需要确保至少有一个有效的";by_ state";响应中的数组。

您的需求可以表示为一行:

* match response.by_group contains { key: '#string', by_state: '#[_ > 0]' }

请注意,karate.filter()是您应该注意的内容。因此,您可以筛选出所需的元素,然后计算返回的结果数等:https://github.com/intuit/karate#json-转换

相关内容

最新更新