json 的值有多种数据类型 (json-shema)

Value of json has multiple data-types (json-shema)

我对 karatesl 上的 json-schema 有疑问.. 例如我有一个 json,json 的其中一个键有多种数据类型(例如,它可以是#string 或#object) 如何验证在 karatesl 上具有多种数据类型的 json ??

示例#string:

{
  "customer_id":"081234562002",
  "order_id":"",
  "amount":20700,
  "price":20700,
  "created":1560684244,
  "changed":1560684246,
  "data":"Internal Server Error"
}

示例#object:

{
  "customer_id":"081234562002",
  "order_id":"",
  "amount":20700,
  "price":20700,
  "created":1560684244,
  "changed":1560684246,
  "data":
   {"message": "Internal Server Error"}
}

我已经分离了 json 文件来验证上面的 json,像这样:

{
  "customer_id":"#string",
  "order_id":"#string",
  "amount":"#number",
  "price":"#number",
  "created":"#number",
  "changed":"#number",
  "data":"???"
}

在空手道中有多种选择可以做到这一点,我可以在这里举一个简单的例子,

* match response.myKey == "#? karate.match(_,'#string').pass || karate.match(_,'#object').pass"

因为我们需要像 #string 这样的标记,所以我必须使用 karate.match。您可以编写 return 布尔值的任何函数,并在此处用作替代函数。

从空手道文档中阅读 fuzzy matching, Self validation expression , Schema Validation

编辑:更新问题

将您的 ??? 替换为

"#? karate.match(_,'Internal Server Error').pass || karate.match(_,{'message' :'Internal Server Error'} ).pass"