cygne
cygne

Reputation: 547

How to make a JSON schema validation for objects containing another objects using read file in karate

I would like to validate a json schema for a json response with objects containing another objects. Example for json response for GET/persons/id request which will send an object person:

{
"id":"789",
"name":"Jane",
"children":[
{
"id":"111",
"name":"Bill",
"hobbies":[
"chess",
"knitting"
],
"schools":[
{
"id":"111A",
"name":"LA public"
},
{
"id":"111B",
"name":"NY public"
}
]
}
]
}

GET/persons request will send an array containing objects person.

Example of feature for validation (Validator.feature):

Feature: json schema validation
   Scenario: 
   * def schoolSchema =
     """
      {
        id: '#string',
        name: '#string'
      }
    """

    * def childrenSchema =
     """
      {
        id: '#string',
        name: '#string',
        hobbies: '#regex \d (playing|singing|knitting|chess)',
        schools: '##[] schoolSchema'
      }
    """

    * def personSchema =
    """
      {
        id: '#string',
        name: '#string',
        children: '#[] childrenSchema'
      }
    """

I don't want to put in the end of this feature * match each response == personSchema or * match response == personSchema

I would like to call the schema validation feature from my main feature like this:

Given url url
And path 'persons'
When method get
Then status 200
And match each response == call read('Validator.schema')
Given url url
And path 'persons', id
When method get
Then status 200
And match response == call read('Validator.schema')

to validate responses from both requests. I need to put this schema in a feature or in a json file in order to have only one place to modify for json schema. How I could do that?

Upvotes: 1

Views: 316

Answers (1)

Babu Sekaran
Babu Sekaran

Reputation: 4239

You mentioned you don't want to use  * match response == personSchema

Have you tried like this,

* call read('Validator.feature')
And match response == personSchema

Upvotes: 2

Related Questions