karate : Complex JSON Schema matching

2019-07-22 19:17发布

问题:

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

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: 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'.

How do I get following Working

  * match JSONtobeValidiated == ValidJsonSchema

回答1:

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 karate