Home > other >  How to validate two primitive types in one answered API response with Karate
How to validate two primitive types in one answered API response with Karate

Time:04-02

I'm using Karate to validate an endpoint answer. And my API is answering with a key-value structure. The problem is the value could be a boolean or a string.

How can I validate this with only one expression this OR with Karate?

Answer example:

{
  "value": true,
  "key": "key1"
},
{
  "value": "This is my value",
  "key": "key2"
}

I tried things like

onfStructure: "#? _ == ^*(confStructure1 || confStructure2)"

confStructure1:
  value: "#boolean"
  key: "#string"

confStructure2:
  value: "#string"
  key: "#string"

or

confStructure: "#? _ == (^*confStructure1 || ^*confStructure2)"

or

confStructure: 
  value: "#(^*newSchema)"
  key: "#string"

newSchema:
  value: "#boolean"
  value: "#string"

But nothing is working.

CodePudding user response:

This is the only way I can think of to solve this. Sometimes the Karate "patterns" can't be used. To be honest most APIs never have these kinds of schema variation.

* def response = [{value: 'foo'},{value: true}]
* match each response == { value: "#? typeof _ == 'string' || typeof _ == 'boolean'" }
  • Related