I’m trying to match a response that may have any of three optional keys that have the same JSON object schema but it doesn’t seem like the optional syntax is working for this case. Does Karate support the kind of functionality I’m looking for? It works when all three schemas are present, but not when any of them are missing.
<code>Scenario:
* def schema_optional =
"""
{
"a": ##string,
"b": ##number
}
"""
* def schema =
"""
{
"c": ##(schema_optional),
"d": ##(schema_optional),
"e": ##(schema_optional)
}
"""
* def result =
"""
{
"d": {
"a": "hi",
"b": 19
}
}
"""
* match result == schema
</code>
<code>Scenario:
* def schema_optional =
"""
{
"a": ##string,
"b": ##number
}
"""
* def schema =
"""
{
"c": ##(schema_optional),
"d": ##(schema_optional),
"e": ##(schema_optional)
}
"""
* def result =
"""
{
"d": {
"a": "hi",
"b": 19
}
}
"""
* match result == schema
</code>
Scenario:
* def schema_optional =
"""
{
"a": ##string,
"b": ##number
}
"""
* def schema =
"""
{
"c": ##(schema_optional),
"d": ##(schema_optional),
"e": ##(schema_optional)
}
"""
* def result =
"""
{
"d": {
"a": "hi",
"b": 19
}
}
"""
* match result == schema
The error is
<code>match failed: EQUALS
$ | not equal | actual does not contain key - 'c' (MAP:MAP)
</code>
<code>match failed: EQUALS
$ | not equal | actual does not contain key - 'c' (MAP:MAP)
</code>
match failed: EQUALS
$ | not equal | actual does not contain key - 'c' (MAP:MAP)
New contributor
Claire C is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.