空手道:复杂的JSON模式匹配 [英] karate : Complex JSON Schema matching

查看:288
本文介绍了空手道:复杂的JSON模式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用可选键来验证复杂的JSON

I am trying to Validate a complex JSON with Optional keys

以下是完整的可执行功能文件

Following is the complete executable Feature file

Feature: Complex Schema
  Background:
    * def JSONtobeValidiated =
    """
    {
  "MyArray": {
    "MYinternalDetails": [
      {
        "SomeField": "dc",
        "textfield": [],
        "OptionalArray1": [
          {
            "Users": "Y",
            "IsAllowed": "Y"
          },
          {
            "Users": "Y",
            "IsAllowed": "Y"
          }
        ]
      },
      {
        "SomeField": "nb",
        "textfield": [],
        "OptionalArray2": [
          {
            "CodingP": "GZ",
            "Name": "Ok"
          },
          {
            "CodingP": "BO",
            "Name": "Saved"
          },
          {
            "CodingP": "YB",
            "Name": "Done"
          }
        ]
      },
      {
        "SomeField": "TRY",
        "textfield": []
      },
      {
        "SomeField": "cd",
        "textfield": [],
        "OptionalArray3": [
          {
            "Astring": "Lazy",
            "AnotherString": "N",
            "textfield": []
          },
          {
            "Astring": "FREE",
            "AnotherString": "N",
            "textfield": [
              {
                "title": "Name",
                "alertMessage": "Please enter valid name."
              },
              {
                "title": "Mobile No",
                "alertMessage": "Please enter mobile no."
              }
            ]
          }
        ]
      },
      {
        "SomeField": "gv",
        "textfield": [
          {
            "title": "First",
            "alertMessage": "SOme Alert"
          }
        ]
      },
      {
        "SomeField": "OTP",
        "textfield": [
          {
            "title": "Second",
            "alertMessage": "SOme other elert."
          }
        ]
      },
      {
        "SomeField": "rp",
        "textfield": [],
        "OptionalArray4": [
          {
            "Code": "ACODE",
            "textfield": []
          }
        ]
      }
    ]
  }
}
    """

    * def textfield = { title: '#string',alertMessage: '#string' }

    * def OptionalArray1 = { Users: '#string',IsAllowed: '#string' }
    * def OptionalArray2 = { CodingP: '#string',Name: '#string'}
    * def OptionalArray3 = { Astring: '#string' ,AnotherString: '#string' ,textfield: '##[] textfield'  }
    * def OptionalArray4 = { Code: '#string',textfield: '##[] textfield' }

    * def MYinternalDetails =
    """
      {
        SomeField: '#string',
        textfield: '##[] textfield',
        OptionalArray1: '#[] OptionalArray1',
        OptionalArray2: '#[] OptionalArray2',
        OptionalArray3: '#[] OptionalArray3',
        OptionalArray4: '#[] OptionalArray4',

      }

    """

    * def MYinternalDetailsTest =
    """
      {
        SomeField: '#string',
        textfield: '#ignore',
        OptionalArray1: '#ignore',
        OptionalArray2: '#ignore',
        OptionalArray3: '#ignore',
        OptionalArray4: '#ignore',
      }

    """

    * def ValidJsonSchema = { MyArray: { MYinternalDetails: '#[] MYinternalDetails' } }
    * def ValidJsonSchemaTest = { MyArray: { MYinternalDetails: '#[] MYinternalDetailsTest' } }

    Scenario: This works fine

      * match JSONtobeValidiated == ValidJsonSchemaTest
      * match JSONtobeValidiated.MyArray.MYinternalDetails == '#[] MYinternalDetailsTest'


  Scenario: This should work
      * match JSONtobeValidiated.MyArray.MYinternalDetails == '#[] MYinternalDetails'

  Scenario: THis is what I am trying to get matched
      * match JSONtobeValidiated == ValidJsonSchema

我想验证ValidJsonSchema,但无法解决完整的Schema匹配

I want to validate ValidJsonSchema But not able to get around the complete Schema matching

当我尝试匹配匹配的可选字段时,我不断收到错误消息

When I try to match Optional fields matching I keep getting error as

com.intuit.karate.exception.KarateException:期望找到一个 在路径$中具有属性['MyArray']的对象,但找到了 'net.minidev.json.JSONArray'.这不是一个json对象 JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.

com.intuit.karate.exception.KarateException: Expected to find an object with property ['MyArray'] in path $ but found 'net.minidev.json.JSONArray'. This is not a json object according to the JsonProvider: 'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'.

我如何关注工作

  * match JSONtobeValidiated == ValidJsonSchema

推荐答案

谢谢!我同意复杂的嵌套数组等存在一些问题.我得到了下面的工作,因为您可以看到空数组上的textfield匹配项需要一些工作.您能帮我一个忙,简化一下吗,以解决空手道处理中的所有空白-请记录下来.

Thanks for the details ! I agree there seems to be some issues with complex nested arrays etc. I got the below working, as you can see the textfield match on an empty array needs some work. Can you do me a favor and simplify this so that any gaps in Karate handling are clear - and please log an issue.

Scenario:
    * def json =
"""
    [
         {
            "SomeField":"dc",
            "textfield":[

            ],
            "OptionalArray1":[
               {
                  "Users":"Y",
                  "IsAllowed":"Y"
               },
               {
                  "Users":"Y",
                  "IsAllowed":"Y"
               }
            ]
         },
         {
            "SomeField":"nb",
            "textfield":[

            ],
            "OptionalArray2":[
               {
                  "CodingP":"GZ",
                  "Name":"Ok"
               },
               {
                  "CodingP":"BO",
                  "Name":"Saved"
               },
               {
                  "CodingP":"YB",
                  "Name":"Done"
               }
            ]
         },
         {
            "SomeField":"TRY",
            "textfield":[

            ]
         },
         {
            "SomeField":"cd",
            "textfield":[

            ],
            "OptionalArray3":[
               {
                  "Astring":"Lazy",
                  "AnotherString":"N",
                  "textfield":[

                  ]
               },
               {
                  "Astring":"FREE",
                  "AnotherString":"N",
                  "textfield":[
                     {
                        "title":"Name",
                        "alertMessage":"Please enter valid name."
                     },
                     {
                        "title":"Mobile No",
                        "alertMessage":"Please enter mobile no."
                     }
                  ]
               }
            ]
         },
         {
            "SomeField":"gv",
            "textfield":[
               {
                  "title":"First",
                  "alertMessage":"SOme Alert"
               }
            ]
         },
         {
            "SomeField":"OTP",
            "textfield":[
               {
                  "title":"Second",
                  "alertMessage":"SOme other elert."
               }
            ]
         },
         {
            "SomeField":"rp",
            "textfield":[

            ],
            "OptionalArray4":[
               {
                  "Code":"ACODE",
                  "textfield":[

                  ]
               }
            ]
         }
      ]
"""

* def textfield = { title: '#string', alertMessage: '#string' }
* def OptionalArray1 = { Users: '#string',IsAllowed: '#string' }
* def OptionalArray2 = { CodingP: '#string',Name: '#string'}
* def OptionalArray3 = { Astring: '#string' ,AnotherString: '#string', textfield: '##[]'  }
* def OptionalArray4 = { Code: '#string',textfield: '##[]' }

* def MYinternalDetails =
    """
      {
        SomeField: '#string',
        textfield: '##[] textfield',
        OptionalArray1: '##[] OptionalArray1',
        OptionalArray2: '##[] OptionalArray2',
        OptionalArray3: '##[] OptionalArray3',
        OptionalArray4: '##[] OptionalArray4',

      }
    """
* match json == '#[] MYinternalDetails'

这篇关于空手道:复杂的JSON模式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆