空手道 - 条件 JSON 模式验证

Karate - Conditional JSON schema validation

我只是想知道如何进行条件模式验证。 API 响应是基于 customerType 键的动态响应。如果 customerTypeperson,则将包含人员详细信息,如果 customerTypeorg,则组织详细信息将包含在 JSON 响应中。所以响应可以是以下两种形式之一

{
    "customerType" : "person",
    "person" : {
        "fistName" : "A",
        "lastName" : "B"
    },
    "id" : 1,
    "requestDate" : "2021-11-11"
}

{
    "customerType" : "org",
    "organization" : {
        "orgName" : "A",
        "orgAddress" : "B"
    },
    "id" : 2,
    "requestDate" : "2021-11-11"
}

我创建的用于验证上述 2 个场景的模式如下

{
    "customerType" : "#string",
    "organization" : "#? response.customerType=='org' ? karate.match(_,personSchema) : karate.match(_,null)",
    "person" : "#? response.customerType=='person' ? karate.match(_,orgSchema) : karate.match(_,null)",
    "id" : "#number",
    "requestDate" : "#string"
}

但架构与实际响应不匹配。我应该对架构进行哪些更改才能使其正常工作?

Note : I am planning to reuse the schema in multiple tests so I will be keeping the schema in separate files, independent of the feature file

你能参考这个我认为更好的答案吗:

就是说,我认为您错过了 JS karate.match() API 不是 return 布尔值,而是包含 [=12= 的 JSON ] 布尔值 属性.

所以你必须这样做:

* def someVar = karate.match(actual, expected).pass ? {} : {}