如何验证空手道中 json 模式中的可选对象?

问题描述 投票:0回答:1

可选字段可以使用 # 符号 (##number) 进行验证。 我需要验证我的响应中的可选对象。 listValues 是响应中的可选对象。我如何检查这一点,因为我收到错误,因为实际不包含键 - 'listValues',因为第一个对象没有 listValues。如何将其设为可选

{"filter": {
    "ITEM": {
      "ITEM": [
        
        {
          "m_operators": ["A", "B"],
          "operators": ["A1", "B1"],
          "logicalDataType": "STRING",
          "tableName": "ITEM"
        },
        {
          "m_operators": ["C", "D"],
          "operators": ["C1", "D1"],
          "logicalDataType": "STRING",
          "tableName": "ITEM"
        },
        {
          "m_operators": ["E", "F"],
          "operators": ["E1", "F1"],
          "logicalDataType": "String",
          "listValues": [
            {
              "m_key": "String",
              "m_value": "String"
            },
            {
              "m_key": "String",
              "m_value": "String"
            }
          ],
          "tableName": "ITEM"
        }

      ]
    }
  }}
   * def listValuesSchema = {"m_key": "#string", "m_value": "#string}
    * def itemsSchema = {"m_operators": "#[] #string", "operators": "#[] #string", "logicalDataType": "#string", "listValues": "#[] ##(listValuesSchema)", "tableName": "#string"}
    * def filterSchema = {"ITEM": {"ITEM": "#[] ##(itemsSchema)"}}
    * def jsonExpected = {"filter": "#(filterSchema)"}
    * match response == jsonExpected

karate
1个回答
0
投票

如果您在

match
中“组合”可选块,则会出现边缘情况。阅读此说明

所以解决方案应该是这样的:(我没有测试这个,因为你的例子太复杂了,下次请使用MCVE

* def filterSchema = ({"ITEM": {"ITEM": "#[] ##(itemsSchema)"}})

然后做

match

© www.soinside.com 2019 - 2024. All rights reserved.