Search code examples
regexwiremockwiremock-standalone

How to check that value of field in request body is json using Wiremock


I have a JSON request and I need to check that the value of one field is json. I'm trying use regex:

"request": {
    ...
    "bodyPatterns": [
      {
        "equalToJson": {
          "field_1": "Value",
          "field_2": "${json-unit.any-number}",
          "field_3": "${json-unit.regex}^\u007B.\u007D$"
        }
      }
    ]
  }

Also I have tried:

"field_3": "${json-unit.regex}\\{.\\}"

"field_3": "${json-unit.regex}{.}"

But none of this works. How will it be correct to determine that the value of the field contains JSON?

An example request that must match the stub:

{
    "field_1": "Value",
    "field_2": 100,
    "field_3": {
        "1.1": 2,
        "1.2": null,
        "1.3": null,
        "1.4": null,
         ...
    }
}

Solution

  • You can only use the regex placeholder against string values, so this won't work in this instance as the element's value is an object.

    However, if you just want to say "I want this element to be present but don't care about the contents" you can use "${json-unit.ignore}" as the value.

    If you do need to be more explicit I'd suggest still doing the above, but adding a second body matcher with a JSONPath expression stating what the object should contain.