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.
Asked: 2023-05-02 12:45:00 +0000
Seen: 16 times
Last updated: May 02 '23