如何验证响应至少有一个有效的数组数据?
How to verify the response has at least one valid array data?
我收到了这样的回复:
{
"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-transforms
我收到了这样的回复:
{
"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-transforms