Home > Mobile >  How to validate an array with optional objects keys inside that must be checked for required keys th
How to validate an array with optional objects keys inside that must be checked for required keys th

Time:03-18

I am trying to validate an API response schema with this structure in Karate:

{
  "payoutQuotes": [ //could be empty or not but when not empty must have the required object structure inside
    {
      "customerDomain": {
        "financialAccountData": { 
          "payoutQuoteId": "#number",
          "payoutQuoteExpiryDate": "#? isValidDate(_)",
          "totalAmountOfPayoutQuote": "#number",
          "payoutQuoteCreateDate": "#? isValidDate(_)",
          "payoutRequestedBy": "#string"
        }
      }
    }
  ]
}

Now if the response returns an empty "payoutQuotes" array then it is acceptable but when it also contains the object(s) inside, I want to check they have the proper required key-value combinations. Please note that I have this schema saved as a separate file in my data folder of the project to be used for my schema validation.

CodePudding user response:

Here's the approach, it should be simple. You can read the value of quote from a single file.

* def quote = { foo: '#string' }
* def response1 = { payout: [] }
* def response2 = { payout: [{ foo: 'bar' }] }
* match response1 == { payout: '#[] quote' }
* match response2 == { payout: '#[] quote' }
  • Related