如何将对象数组设置为空手道中的标记?

How to set array of objects as a marker in karate?

我有 file.json 内容:

{
  "key": "#notnull",
  "value": {
    "id": "#notnull",
    "data": {
      "array": "marker <--"
    }
  }
}


我应该设置什么而不是“标记”来使用 file.json 作为匹配此类对象的模板:

{
  "key": "1",
  "value": {
    "id": "1",
    "data": {
      "array": [
        {  
          "name": "Some name",
          "surname": "Some surname" 
        },
        {  
          "name": "Some name",
          "surname": "Some surname" 
        },
      ]
    }
  }
}

我需要做的事情:

* def objectTemplate = read('path/to/file.json')
* match realObject == objectTemplate

请阅读“嵌入式表达式”和模式验证的文档:https://github.com/karatelabs/karate#schema-validation

这个简单的例子应该很清楚:

* def item = { foo: '#string' }
* def schema = { array: '#[] item' }
* def response = { array: [{ foo: 'bar1' }, { foo: 'bar2' }] }
* match response == schema

在 Peter 的回答和一些调查之后 - 这里是它应该如何配置(不是真正的 json 结构,只是一个例子)。所以我们有:

event.json:

{
  "key": "#notnull",
  "value": {
    "id": "#notnull",
    "data": {
      "persons": "#[] person"
    }
  }
}

person.json:

{
  "name": "#notnull",
  "surname": "#notnull"
}

和some-feature.特征:

Feature: some feature
  Background:
    # below variable name should be the same like in event.json persons field: "#[] person"
    * def person = read('classpath:test/person.json')
    * def eventTemplate = read('classpath:test/event.json')

    * def realEvent =
    """
      {
        "key": "1",
        "value": {
          "id": "1",
          "data": {
            "persons": [
              {
                "name": "MyName",
                "surname": "MySurname"
              }
            ]
          }
        }
      }
    """

  Scenario: some scenario
    # the sequence is important: eventTemplate == realEvent will not work
    * match realEvent == eventTemplate