0

I am using wiremock for stubbing and it uses Jayway JsonPath.

I want to create a stub only when the json element doesn't contain exactly 10 digits.

stub is

"request": {
    "bodyPatterns": [
      {
        "matchingJsonPath": "$.employees[?(@.employeeContact =~/[^0-9]{10}/)]"
      }
    ]
  }

I have tried multiple combinations like:

1. $.employees[?(@.employeeContact =~/[^0-9]{10}/)]
2. $.employees[?(@.employeeContact =~/^[0-9]{10}/)]
3. $.employees[?(@.employeeContact !=~/[0-9]{10}/)]
4. $.employees[?(@.employeeContact <>~/[^0-9]{10}/)]

But none of these have worked. Example json which should NOT work:

{
"employee": {
    "employeeContact": "1234567890"
}
}

while these employee should work (anything other than 10 digits):

 1. "employeeContact": "1a34567890"  // character in between
 2. "employeeContact": "12345678901" // more than 10
 3. "employeeContact": "123456789"   // less than 10
 4. "employeeContact": "123456 89"   //space 
Deep
  • 528
  • 3
  • 12
  • 27

2 Answers2

1

You could use the logical or operator to match for lengths less than 10 and greater than 10.

"bodyPatterns": [
  "or": [
    { "matchingJsonPath": "$.employees[?(@.employeeContact =~/[^0-9]{1,9}/)]" },
    { "matchingJsonPath": "$.employees[?(@.employeeContact =~/[^0-9]{11,}/)]" }
  ]
]
agoff
  • 5,818
  • 1
  • 7
  • 20
  • I can but using this given me: "error parsing JSON" with the details "Cannot deserualise value of type 'java.util.ArrayList>' from Object value (token 'JsonToken.START_OBJECT')" – Deep Jun 24 '22 at 19:02
  • 1
    I think this should work. Surely that error would be a bug in WireMock? – Jerry Jeremiah Jun 26 '22 at 21:39
  • "bodyPatterns": [{ "matchesJsonPath": "$.employees[?(@.employeeContact =~/[^0-9]{1,9}/ || $.employees[?(@.employeeContact =~/[^0-9]{11,}/)]" }] – Deep Jun 27 '22 at 09:00
  • Logical `and` and `or` were not released until WireMock 2.29.0, so no bug, OP is just using a lower version than where this was implemented. – agoff Jun 27 '22 at 13:22
0

This is what worked for me:

"bodyPatterns": [{
    "matchesJsonPath": "$.employees[?(@.employeeContact =~/[^0-9]{1,9}/ || $.employees[?(@.employeeContact =~/[^0-9]{11,}/)]" 
    }]

Watch that it is matchesJsonPath instead of matchingJsonPath. Even with that "or" didnt work with my wiremock 2.28.1 so may well be a wiremock bug.

Deep
  • 528
  • 3
  • 12
  • 27