I am trying to write JSON schema for the below response. The response is dynamic it can be a person details or organization details. If the customerType in the response is person, then the response will contain person object (organization object will not be available). If the customerType is org, organization details will be included in the response (person object will not be available). Two different flavors of the expected response is given below
{
"customerType" : "person",
"person" : {
"fistName" : "A",
"lastName" : "B"
},
"id" : 1,
"requestDate" : "2021-11-11"
}
{
"customerType" : "org",
"organization" : {
"orgName" : "A",
"orgAddress" : "B"
},
"id" : 2,
"requestDate" : "2021-11-11"
}
I am trying to valid the above condition using the schema as given below
{
"customerType" : "#string",
"organization" : "#? karate.match(response.customerType, 'org').pass ? karate.match(_, organizationSchema).pass : true)",
"person" : "#? karate.match(response.customerType, 'person').pass ? karate.match(_, personSchema).pass : true"),
"id" : "#number",
"requestDate" : "#string"
}
The problem I am currently facing is that, if customerType in the response is person, it throws below error
all key-values did not match, expected has un-matched keys: [organization]
Is there any way I can specify in the schema that if person object is available, organization object will not be available and vice versa