https://linen.dev logo
#help
Title
# help
a

Andrew Ripley

12/29/2023, 10:29 PM
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

Oleg Nenashev

12/30/2023, 9:46 AM
Hmm, strange. I suggest filing a github issue, most likely the matcher is not adapted to the JSON matching interface.
t

Tom

01/02/2024, 10:21 AM
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

Andrew Ripley

01/02/2024, 7:52 PM
@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

Tom

01/02/2024, 8:02 PM
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

Andrew Ripley

01/02/2024, 8:27 PM
ah good to know. ill poke around 🙂
2 Views