空手道:复杂的JSON模式匹配

时间:2018-03-12 08:43:49

标签: json karate

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

以下是完整的可执行要素文件

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匹配

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

  

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

我如何获得以下工作

  * match JSONtobeValidiated == ValidJsonSchema

1 个答案:

答案 0 :(得分:1)

感谢您的详细信息!我同意复杂的嵌套数组似乎存在一些问题。我得到了以下工作,因为你可以看到空数组上的textfield匹配需要一些工作。你可以帮我一个忙,并简化这个,以便空手道处理中的任何空白都清楚 - 请记录一个问题。

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'