Using wiremock-standalone (version 2.29.1), I want to verify a request with its body holding an array of objects, containing optional properties.
For instance, consider this request:
Request body (JSON format)
{
"foo": [
{ "bar": "1" },
{ "qux": "oh hai" },
{ "bar": "ohnoes" }
]
}
And let's say I want to match requests only if all the foo.bar
attributes are either present, or contain only a single digit (it's only for the sake of example). The example above should not match (the third object has a bar
attributes with non-digits characters).
I tried different approches, and the closest I got is this:
{
"matchesJsonPath": {
"expression": "$.foo[*].bar",
"or": [
{ "matches": "^\\d$" },
{ "absent": true }
]
}
}
There are 2 problems:
- if there is no
bar
attribute at all, the request does not match - if at least 1
bar
attribute passes the check, then the whole request passes, even though otherbar
values are invalid (the example above passes)
Does anyone know how to create such a rule in wiremock?