Hey friends! I am trying to do some request matchi...
# help
a
Hey friends! I am trying to do some request matching on the json body and i am running into an issue the
"or"
operator. the goal is to validate that an optional body parameter (if present) has either 0, 1, or 2 items and each item must be either "rx" or "alcohol". this is tricky because absent, empty, 1, and 2 should all be allowed as specific cases.
Copy code
{
  "matchesJsonPath": {
    "expression": "$.restrictions",
    "or": [
      {
        "matches": "[]"
      },
      {
        "matches": "(rx|alcohol)"
      },
      {
        "matches": "(rx|alcohol),(rx|alcohol)"
      },
      {
        "absent": true
      }
    ]
  }
}
the
absent
option is not being respected as a valid option. this is the body i send to the stub:
Copy code
{}
Any ideas on how I could approach this? I have also tried going the route of validating the values separate from validating the size:
Copy code
{
  "matchesJsonPath": {
    "expression": "$.restrictions.size()",
    "or": [
      {
        "matches": "0"
      },
      {
        "matches": "1"
      },
      {
        "matches": "2"
      },
      {
        "absent": true
      }
    ]
  }
}
but the array.size() rule fails when array is absent 😞.
o
Hmm, strange. I suggest filing a github issue, most likely the matcher is not adapted to the JSON matching interface.
t
Hi @Andrew Ripley, this seems to do the trick I think:
Copy code
{
  "request": {
    "method": "POST",
    "urlPath": "/data",
    "bodyPatterns": [
      {
        "or": [
          {
            "matchesJsonPath": {
              "expression": "$.restrictions.size()",
              "or": [
                {
                  "matches": "0"
                },
                {
                  "matches": "1"
                },
                {
                  "matches": "2"
                }
              ]
            }
          },
          {
            "matchesJsonPath": {
              "expression": "$.restrictions",
              "absent": true
            }
          }
        ]
      }
    ]
  },
  "response": {
    "status": 200,
    "body": "\nOK\n"
  }
}
🙌 1
a
@Tom that did the trick!! thank you so much! and happy new year!!
👍 1
@Oleg Nenashev do you still think i should open a github issue for this? Id love to take a crack at helping to solve the issue too!
t
I think you might struggle to get the array size thing working without applying a fix to the upstream JSONPath library, but if you can find a solution we’d be happy to merge it
a
ah good to know. ill poke around 🙂