Hi Team, Can I know how I can verify if a particul...
# pact-jvm
k
Hi Team, Can I know how I can verify if a particular field has a specific value where this field can be present anywhere in the response (within array/objects). I tried below but the pact file generated fails at provider side
Copy code
val pactBody = PactDslJsonBody()
    .stringMatcher("test", "^(test1|test2)$")
m
what do you mean, sorry?
k
Consider below is my response, I wanted to verify that test_type is either test1 or test2 it being present anywhere in the response.
Copy code
"tests": [{
          "test_type": "test1",
          "tests": [{
            "test_type": "test2",
            "test": {
              "test_type": "test3",
              "tests": [{
                "test_type": "test4",
                "image": {
                  "assetId": "runway_brand_flying_roo_platinum"
                }
                }
              }
            }
r
That's not what Pact is designed to do. It is designed to confirm that the entire response conforms to a contract.
k
We have a complicated response where in objects are nested and hence can't really verify the entire response confirms to a particular contract
m
We have a complicated response where in objects are nested and hence can’t really verify the entire response confirms to a particular contract
It must have some shape, otherwise how does the client know how to parse it? If you were to describe it as OAS or JSON schema, is that possible?
k
Here is how the schema for the above looks
Copy code
tests*  [Test{
isConsistent* boolean
id$regression_tests  string
}]
m
sorry what is that syntax? do you have JSON schema or the OAS for it?
k
Sorry, I don't think we have one
We have a model defined on how the response should be. Both Backend and Front end use the same model. What I wanted to verify as part of the contract testing is since both use the same contract, there would be issues only when both use different versions of the model. So, wanted to verify that from the above "test_type" should have either of these values. If we get a new value, then the front-end cannot process it.
m
you can put a constraint on
test_type
as a property, but the way you framed it is that it could appear anywhere in the response at any level - this feels very “loose” and I don’t think Pact would be able to support it. If it is a structured object, then it’s probably testable. So I was just trying to understand the model. I don’t know this syntax, so would just be guessing what it is:
Copy code
tests*  [Test{
isConsistent* boolean
id$regression_tests  string
}]
I think it might be something like this:
Copy code
{
  "tests": [
    {
      "isConsistent": true,
      "id$regression_tests": "some string"
    },
    {
      "isConsistent": true,
      "id$regression_tests": "some other string"
    }
  ]
}
If this structure, you could apply a constraint to
id$regression_tests
in the way you’re describing.