Ask Your Question
3

How can schema validation be performed on a multi-level nested array for Karate?

asked 2023-05-02 12:45:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-02 12:54:01 +0000

bukephalos gravatar image

To perform schema validation on a multi-level nested array in Karate, you can use the match keyword along with the JSON schema. Here is an example:

* def data =
"""
[
  {
    "id": 1,
    "name": "John",
    "grades": [
      {
        "subject": "Math",
        "score": 90
      },
      {
        "subject": "Science",
        "score": 80
      }
    ]
  },
  {
    "id": 2,
    "name": "Alice",
    "grades": [
      {
        "subject": "Math",
        "score": 85
      },
      {
        "subject": "Science",
        "score": 95
      }
    ]
  }
]
"""

* def schema =
"""
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": {"type": "integer"},
      "name": {"type": "string"},
      "grades": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "subject": {"type": "string"},
            "score": {"type": "integer"}
          },
          "required": ["subject", "score"]
        }
      }
    },
    "required": ["id", "name", "grades"]
  }
}
"""

* match data == schema

In this example, we have a multi-level nested array of students and their grades. The data is saved in the data variable and the JSON schema is saved in the schema variable. We then use the match keyword to compare data with schema, which performs the schema validation on the multi-level nested array.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-02 12:45:00 +0000

Seen: 16 times

Last updated: May 02 '23