JSON 响应中的可选字段

Optional field in JSON response

我 运行 的查询将 return 一个响应,我将其分为两个模式:

     * def tagsSchema =
      """
       {
         "lifecycle-status": "#string",
         "infrastructure-environment": "#string",
         "managed-by": "#string",
         "supported-by": "#string",
         "operated-by": "#string"
       }
      """

并且此架构已集成到我的内容架构中:

     * def contentSchema =
      """
       {
        "phase": "##string",
        "managedBy": "##string",
        "assetId":"##string",
        "isValid": ##boolean,
        "name": "#string",
        "supportedBy": "##string",
        "links": '#[] linksSchema',
        "ownedBy": "##string",
        "cmdbInstanceId":"#string",
        "tags": "##object? karate.match(_,tagsSchema).tags",
       }
     """

tagsSchema 是可选的,我已在 ##object 中介绍了它。当我 运行 查询现在失败,因为我在 tagsSchema 中有额外的值。

getList.feature:159 - path: $[0].tags, actual: {"technicalreferant":"email1","billingowner":"xyz","responsibleManager":"email1","environment":"abc","application":"tbd","consumer":"cdr","cr":"12345678"}, expected: '##object? karate.match(_,tagsSchema).tags', reason: did not evaluate to 'true'

问题来自 karate.match,但没有 karate.contains。我该如何修改架构以避免此错误。 tagsSchema 中的值是强制性的,而其他值可以由用户随时创建,我们没有针对它们的策略。我不想每 运行 次调整代码,只依赖强制值。

我不确定您为什么认为需要使用 karate.match() 并且您需要阅读文档。下面是一个简单的例子:

* def innerSchema = { foo: '#string' }
* def outerSchema = { bar: '#string', baz: '##(innerSchema)' }

* def response1 = { bar: 'x' }
* match response1 == outerSchema

* def response2 = { bar: 'x', baz: { foo: 'y' } }
* match response2 == outerSchema