【问题标题】:JSON schema properties duplicationJSON 模式属性重复
【发布时间】:2021-11-05 16:22:38
【问题描述】:

我想开发一个不需要复制属性的架构。我有一个可以存储不同对象的数组。

数组输入示例:

[ 
  { "name": "someName" },
  { "name": "someName", "version": 1 }
  { "name": "someName", "weight": 15 }
]

如您所见,name 是必需属性,版本和权重是可选属性。

我想开发一个不需要重复版本和重量的方案。

以下架构将有效:

{
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "type": "array",
    "items": {
      "type": "object",
      "anyOf": [
        { 
          "properties": {
            "name": {
              "type": "string"
            }
          },
          "required": ["name"]
        },
        { 
          "properties": {
            "name": {
              "type": "string"
            },
            "version": {
              "type": "number"
            }
          },
          "required": ["name"]
        },
        { 
          "properties": {
            "name": {
              "type": "string"
            },
            "weight": {
              "type": "number"
            }
          },
          "required": ["name"]
        }
      ]
    },
   "additionalProperties": false
}

我不想重复name 属性。我怎样才能做到这一点?

重要提示:除了name,我还有其他几个必需的属性

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    所以,在伪代码中,“我只希望数组中的一项具有“名称”属性”?您可以使用 containsmaxContains 做到这一点:

    https://json-schema.org/understanding-json-schema/reference/array.html#contains

    【讨论】:

    • 这不会解决我的问题,因为我的数组包含具有许多属性的对象
    猜你喜欢
    • 2016-04-23
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    相关资源
    最近更新 更多