如何验证带有可选对象键的数组,必须在空手道中自行检查所需的键?

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

我正在尝试在空手道中使用此结构验证 API 响应模式:

{
  "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"
        }
      }
    }
  ]
}

现在,如果响应 returns 是一个空的“payoutQuotes”数组,那么它是可以接受的,但是当它还包含内部对象时,我想检查它们是否具有所需的正确键值组合。请注意,我将此架构作为单独的文件保存在我的项目数据文件夹中,用于我的架构验证。

方法是这样的,应该很简单。您可以从单个文件中读取 quote 的值。

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