1

Is there a way to do a json schema validation with no need to refer to all entries we want to #ignore?

In the sample below is a Json Response and then a Schema that is giving me valid and another schema that is giving me error.

Scenario: Validate only what I want and ignore the rest
* def response = 
"""
[
  {
    "arrObj": [
      {
        "o0attr1": "attr1",
        "o0attr2": "attr2",
        "obj1": {
          "obj2": {
            "o2attr1": "attr1",
            "o2attr2": "attr2"
          },
          "o1arr1": []
        },
        "o0attr3": "attr3"
      },
      {
        "o0attr1": "attr1A",
        "o0attr2": "attr2A",
        "obj1": {
          "obj2": {
            "o2attr1": "attr1A",
            "o2attr2": "attr2A"
          },
          "o1arr1": []
        },
        "o0attr3": "attr3A"
      }
    ]
  }
]
"""
* def schemaValid = 
"""
  {
        "o0attr1": "#string",
        "o0attr2": "#string",
        "obj1": {
          "obj2": {
            "o2attr1": "#string",
            "o2attr2": "#string"
          },
          "o1arr1": []
        },
        "o0attr3": "#string"
      }
"""
  * def schemaNotValid = 
"""
  {
    "o0attr2" : '#string',
    "obj1" : {
      "obj2": {
        "o2attr1": "#string"
      }
    },
    "attr3" : '#string'
  }
"""
## WORKING
# And match each response[0].arrObj == schemaValid

## NOT WORKING
# And match each response[0].arrObj == schemaNotValid
And match each response[0].arrObj contains deep schemaNotValid

result:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.724 s <<< FAILURE! - in com.intuit.karate.junit4.demos.SchemaLikeRunner [ERROR] [7:175] Validate only what I want and ignore the rest([schema-like] json-schema like validation) Time elapsed: 0.022 s <<< ERROR! com.intuit.karate.exception.KarateException: schema-like.feature:243 - path: $[0].obj1, actual: {obj2={o2attr1=attr1, o2attr2=attr2}, o1arr1=[]}, expected: {obj2={o2attr1=#string}}, reason: actual value has 1 more key(s) than expected: {o1arr1=[]}

[INFO] [INFO] Results: [INFO] [ERROR] Errors: [ERROR]
schema-like.feature:243 - path: $[0].obj1, actual: {obj2={o2attr1=attr1, o2attr2=attr2}, o1arr1=[]}, expected: {obj2={o2attr1=#string}}, reason: actual value has 1 more key(s) than expected: {o1arr1=[]}

I wanted to use this solution for huge response payload, that I only want to validate some pertinent values, but at the same time I want this to be managed from a external schema file, so that I can simply add additional entries to be validated.

I would appreciate your help. Thanks in advance,

  • highly unlikely you will have a "magic" solution if `contains deep` did not work. if the linked threads did not help, re-ask this question with a **simpler** example please, this one is unreadable – Peter Thomas Sep 04 '20 at 03:13

0 Answers0