Reputation: 33
I got a response like:
{
"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": []
}
]
}
schema used here:
* def schema2 = { key : '#string', value : '##number? _ >= 0' }
* def schema1 = { key : '#string', by_state : '#[_ > 0] schema2' }
And match response ==
"""
{
by_group: '#[_ > 0] schema1'
}
"""
It failed when we got the 3rd item which was an empty array, but we allowed this to happen.
We just need to make sure there is at least one valid "by_state" array in the response.
Upvotes: 1
Views: 151
Reputation: 58058
Your requirement can be expressed as one line:
* match response.by_group contains { key: '#string', by_state: '#[_ > 0]' }
Note that karate.filter()
is something you should be aware of. So you can filter out the elements you want, and then count the number of results returned etc: https://github.com/intuit/karate#json-transforms
Upvotes: 1