【问题标题】:Optional field in JSON responseJSON 响应中的可选字段
【发布时间】:2021-05-27 08:27:48
【问题描述】:

我运行的查询将返回一个响应,我将其分成两个模式:

     * 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 中的值是强制性的,而其他值可以由用户随时创建,我们没有针对它们的策略。我不想每次运行时都调整代码,只依赖强制值。

【问题讨论】:

  • 但是现在的答案是什么?我怎样才能让它们成为可选的?
  • 没有勾选框,我仍在寻找答案。现在我在提出这个问题时仍然在原处

标签: json karate


【解决方案1】:

我不确定您为什么认为需要使用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

【讨论】:

    猜你喜欢
    • 2018-12-21
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2019-12-20
    • 2015-12-04
    相关资源
    最近更新 更多