【问题标题】:json schema for dynamic array动态数组的json模式
【发布时间】:2015-05-27 18:44:56
【问题描述】:

我有以下 json

    {
        "Dettype": "QTY",
 "Details": [
     {
  "12568": {
    "Id": 12568,
    "qty":1,
    "Freq":"2",
    "Option": 0,
    "promote":"yes"
  },
  "22456": {
    "Id": 22456,
    "qty":2,
    "Freq":"3",
    "Option": 1,
    "promote":"no"
  }
     }
 ]
}

对于上面的 json,我需要编写一个 json 模式文件来验证请求。

但问题在于数组中每个项目的键值会动态变化。如果它是一些常数值,我可以写但不知道如何做动态模式

我得到的 JSON 模式

{
"type": "object",
"additionalProperties": true,
"properties": {
    "Dettype": {
        "type": "string"
    },
    "Details": {
        "type": "array",
        "items": {
            "type": "object",
            "additionalProperties": true,
            "properties": {
                "**DYNAMIC VALUE**": {
                    "type": "object",
                    "additionalProperties": true,
                    "properties": {
                        "Id": {
                            "type": "integer"
                        },
                        "qty": {
                            "type": "integer"
                        },
                        "Freq": {
                            "type": "string"
                        },
                        "Option": {
                            "type": "integer"
                        },
                        "promote": {
                            "type": "string"
                        }
                    }
                }
            }
        }
    }
}

}

有人能说出需要对架构进行哪些更改

【问题讨论】:

  • 为什么数组中嵌入了一个对象?为什么它本身不是一个对象?

标签: json jsonschema json-schema-validator


【解决方案1】:

这就是patternProperties 的用途。

在这里,您的对象成员键似乎总是数字;因此你可以这样写:

"type": "object",
"patternProperties": {
    "^\\d+$": {
        "type": "object",
        "etc": "etc"
    }
}

【讨论】:

    【解决方案2】:

    如果您希望所有属性都匹配某个架构,您也可以使用 additionalProperties:

    {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "etc": "etc"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      相关资源
      最近更新 更多