如何使用空手道中的读取文件对包含其他对象的对象进行JSON模式验证

时间:2019-10-10 15:01:34

标签: karate

我想用包含其他对象的对象来验证json响应的json模式。 GET / persons / id请求的json响应示例,它将发送一个对象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请求将发送一个包含person对象的数组。

用于验证的功能示例(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'
      }
    """

我不想在此功能* match each response == personSchema* match response == personSchema后面加上

我想从我的主要功能中调用模式验证功能,如下所示:

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')

验证两个请求的响应。我需要将此架构放入功能或json文件中,以便只有一个地方可以修改json架构。我该怎么做?

1 个答案:

答案 0 :(得分:2)

您提到您不想使用* match response == personSchema

您尝试过这样吗

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