Mo Pishdar
Mo Pishdar

Reputation: 123

How to validate an array with optional objects keys inside that must be checked for required keys themselves in Karate?

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.

Upvotes: 1

Views: 586

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

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' }

Upvotes: 1

Related Questions