JSON响应中的可选字段 [英] Optional field in JSON response

查看:22
本文介绍了JSON响应中的可选字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行的查询将返回一个响应,我将该响应拆分为两个架构:

     * 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",
       }
     """
标记模式是可选的,我已经在##对象中介绍了这一点。当我现在运行查询时,它失败了,因为我在tag模式中有其他值。

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'

问题来自空手道。Match,但没有空手道。包含。如何修改架构以避免此错误。标签模式中的值是强制的,而其他值可以由用户随时创建,我们没有针对它们的策略。我不想在每次运行时都调整代码,并且只依赖于必需值。

推荐答案

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

这篇关于JSON响应中的可选字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆