想在空手道 DSL 中尝试 JSON 模式验证中的条件之一

Wanted to try oneof conditon in JSON schema validation in Karate DSL

我一直在使用类似的东西。

* def schema =
  """
    {
    eligible: #string,
    Reason: ##string,
    enrolled: '##regex ^\d{4}-\d{2}-\d{2}$',
    modifiable: ##string,
    Date: '##regex ^\d{4}-\d{2}-\d{2}$',
    status: #string,
    Id: #string,
    email: #string,
    serviceAddressDetails: ##[] firstSchema,
    DeviceIds: #[] #string
    }
  """

预期的响应有两种可能的结果,我想断言,如果我们得到其中任何一个,测试应该通过。

首先,

DeviceIds : ["abcderfg"]

第二

    DeviceIds : [
                  {
                     id : "abcd"
                   }
                ],

如果我们在响应中得到它们中的任何一个,test/schema 应该通过。如何在同一个模式中断言这两种情况? 任何帮助深表感谢。谢谢!

只是 运行 第二次检查。我知道,它可能不像一个“单一可重用模式”,但听取我的建议,这是不值得的。这是一个解决方案:

* def response1 = { deviceIds: ['abcd'] }
* def firstDevice = response1.deviceIds[0]
* def isTypeOne = karate.typeOf(firstDevice) == 'string'
* def expectedDevice = isTypeOne ? '#[] #string' : '#object'
* match response1 == { deviceIds: '#(expectedDevice)' }

* def response2 = { deviceIds: { id: 'abcd' } }
* def firstDevice = response2.deviceIds[0]
* def isTypeOne = karate.typeOf(firstDevice) == 'string'
* def expectedDevice = isTypeOne ? '#[] #string' : '#object'
* match response2 == { deviceIds: '#(expectedDevice)' }

其他想法: