Reputation: 181
abit of a tricky one and i'll attempt to explain it as clearly as possible.
Schema:
"dogs": {
"dogId": "string",
"breed": [
{
"canaan": true,
"akita": false
}
]
}
}
Response:
{
"dogs": {
"dogId": "70872490",
"breed": []
}
}
Feature File:
[...]
And match each response.dogs contains
"""
{
"dogId": "##regex ^[0-9A-Za-z]*$",
"breed": [
{
"canaan": "##boolean",
"akita": "##boolean"
}
]
}
"""
Error:
path: $[0].breed[*], actual: [], expected: {canaan=##string, akita=##string}, reason: actual value does not contain expected
As you can see from the above, the breed
array sometimes doesn't return any values and at times and at other times, the array may be populated with canaan
or akita
values. What is the most elegant way to assert this kind of behaviour? I have tried optional assertions but it seems to always expect the array value to be present.
Let me know your thoughts, any support is appreciated. Thanks!
Upvotes: 0
Views: 70
Reputation: 1096
Sample Code:
Feature: Validation
Scenario:
* def isValid = function(x){ return x == "[]" || karate.match(x,karate.valuesOf([{"canaan":"#boolean","akita":"#boolean"}])).pass }
* def schema =
"""
{
"dogs": {
"dogId": "##regex ^[0-9A-Za-z]*$",
"breed": '#? isValid(_)'
}
}
"""
* def resp1 =
"""
{
"dogs": {
"dogId": "70872490",
"breed": []
}
}
"""
* def resp2 =
"""
{
"dogs": {
"dogId": "70872490",
"breed": [
{
"canaan": true,
"akita": false
}
]
}
}
"""
* match resp1 == schema
* match resp2 == schema
Upvotes: 1