空手道 - 验证整个 json 响应的模式

Karate - Validating schema for the entire json response

我正在使用空手道验证 GET 请求的整个 json 响应。

这是请求的示例 json 响应(我只显示了 items 的两个元素)

[
  {
    "items": [
      {
        "createdById": "ADMIN",
        "createdByName": "ADMIN",
        "changedByName": "ADMIN",
        "oid": "121212fgfg2123",
        "links": {
          "self": {
            "href": "/internal/organiz/12345"
          },
          "subcom": []
        },
        "name": "NewETLTesting"
      },
      {
        "createdById": "ADMIN",
        "createdByName": "ADMIN",
        "changedByName": "ADMIN",
        "oid": "1212dfffg45",
        "links": {
          "self": {
            "href": "/internal/organiz/5a6e0"
          },
          "subcom": []
        },
        "name": "FromKarateModified"
      }
    ]
  }
]

这是我尝试验证的方式:

 And match response.*.* ==
    """
  {
    "createdById" : '#string',
    "createdByName" : '#string',
    "changedByName" : '#string',
    "oid" : '#string',
    "links" : '#object',
    "name" : '#string'
  }
    """

但是,我收到断言错误:reason: actual value is not map-like。如果我尝试将方括号放在大括号周围,我会得到 reason: actual and expected arrays are not the same size。我也尝试使用 $.[*]. 等响应,但无法正常工作。

提前致谢!!

你需要注意你的JSON结构,还要更好地理解JsonPath。您可以剪切并粘贴以下内容并查看它是否有效:

* def response = 
"""
[
  {
    "items": [
      {
        "createdById": "ADMIN",
        "createdByName": "ADMIN",
        "changedByName": "ADMIN",
        "oid": "121212fgfg2123",
        "links": {
          "self": {
            "href": "/internal/organiz/12345"
          },
          "subcom": []
        },
        "name": "NewETLTesting"
      },
      {
        "createdById": "ADMIN",
        "createdByName": "ADMIN",
        "changedByName": "ADMIN",
        "oid": "1212dfffg45",
        "links": {
          "self": {
            "href": "/internal/organiz/5a6e0"
          },
          "subcom": []
        },
        "name": "FromKarateModified"
      }
    ]
  }
]
"""
And match each response[0].items ==
"""
  {
    "createdById" : '#string',
    "createdByName" : '#string',
    "changedByName" : '#string',
    "oid" : '#string',
    "links" : '#object',
    "name" : '#string'
  }
"""

下面的架构将正确验证整个响应

* def refSubcom = {<object schema>}
* def refself = {href : '#string'}
* def refLinks = {self : '#object refself', subcom:'##[] refSubcom'}
* def optionalItemArr = 
    """
    {
        createdById:'#string',
        createdByName:'#string',
        changedByName:'#string',
        oid: '#string',
        links: '#object refLinks',
        name:'#string'
    }
    ###
* def itemData = 
    """
    {
        item : '##[] optionalItemArr'
    }
    """
* def reponseSchema = '##object itemData'

* def SuccessSchema = '##[] reponseSchema'

你可以参考下面的link :