【问题标题】:Json Schema Dependencies with Object Definitions带有对象定义的 Json Schema 依赖项
【发布时间】:2020-05-01 22:01:22
【问题描述】:

当使用 json schema 7 时,是否可以设置对对象定义引用而不是单个字段的依赖关系?

例如,我有一个类型字符串和属性对象。根据类型,属性对象字段会有所不同,但我不想设置每个可能字段的依赖关系。这相当乏味,例如

{
  "$id": "https://sibytes.datagovernor.com/dataset.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "dataset",
  "type": "object",
  "properties": {
    "dataset_type": {
      "type": "string",
      "description": ""
    },
    "dataset_properties": {
      << BASICALLY HERE I WANT THE REFERENCE OBJECT DEFINITION TO BE DEPENDENT ON THE DATASET TYPE ABOVE>>
      "$ref": "http://example.com/tableproperties.schema.json"
      "$ref": "http://example.com/tableproperties.fileproperties.json"
      ...there will be others.
    }
  }
}

【问题讨论】:

标签: json jsonschema


【解决方案1】:

事实证明,这可以使用子模式和谓词来完成 https://json-schema.org/understanding-json-schema/reference/conditionals.html

{
  "$id": "https://sibytes.datagovernor.com/dataset.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "dataset",
  "type": "object",
  "properties": {

    "dataset_type": {
      "enum": ["Table","View"],
      "description": "This is the type of physical object that the dataset describes."
    }
  },
  "dataset_properties": {
    "allOf": [
      {
        "if": {
          "properties": {"dataset_type":{"const": "Table"}}
        },
        "then": {
          "properties": {
            "dataset_properties" : {
              "$ref": "dataset.table.schema.json#/definitions/dataset_properties"
            }
          }
        }
      },

      {
        "if": {
          "properties": {"dataset_type":{"const": "View"}}
        },
        "then": {
          "properties": {
            "dataset_properties" : {
              "$ref": "dataset.view.schema.json#/definitions/dataset_properties"
            }
          }
        }
      }
    ]
  },
  "required": ["dataset_type","dataset_properties"]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-11
    • 2022-12-17
    • 2016-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多