繁体   English   中英

带有嵌套对象和条件的 Json 模式

[英]Json schema with nested objects and conditionals

我正在尝试为以下结构创建 json 架构:

{
    "stages":
    {
        "STAGE1":
        {
            "stage_type" : "GSX",
            "params":
            {
                "x": "setting_x", <- x mandatory for stage type "GSX"
                "y": "setting_y"  <- y mandatory for stage type "GSX"
            }
        },

        "STAGE2":
        {
            "stage_type" : "GSZ",
            "params":
            {
                "z": "setting_z" <- z mandatory for stage type "GSZ"
            }
        }
    }
}

这个想法是“stage_type”是一个具有可能值的枚举[“GSX”,“GSZ”,...]。 我想实现的逻辑是:

  • 如果 "stage_type" == "GSX" -> 需要 "params": {"x"} 并且需要 "params": {"y"}
  • 如果 "stage_type" == "GSZ" -> 需要 "params": {"z"}

但是,在为所需属性实现此逻辑时,我失败了……这是我已经走了多远:

{
    "type": "object",
    "properties":
    {
        "stages":
        {
            "type": "object",
            "additionalProperties":
            {
                "type": "object",
                "properties":
                {
                    "stage_type":
                    {
                        "type": "string",
                        "enum": [ "GSX", "GSZ" ]
                    },

                    "params":
                    {
                        "type": "object",
                        "properties":
                        {
                            "x": { "type": "string" },
                            "y": { "type": "string" },
                            "z": { "type": "string" }
                        },
                        "additionalProperties": false
                    }
                },
                "required": ["stage_type", "params"],

                "allOf":
                [
                    {
                        "if":   { "properties": { "stage_type": { "enum": ["GSX"] } } },
                        "then": { "required": ["x", "y"] }
                    },
                    {
                        "if":   { "properties": { "stage_type": { "enum": ["GSZ"] } } },
                        "then": { "required": ["z"] }
                    }
                ]
            },
            "minProperties": 1,
            "uniqueItems": true
        }
    },
    "additionalProperties": false
}

我似乎无法使 if-then 子句中所需文件的嵌套工作......非常感谢帮助:)

在您的原始模式中, allOf应用于“阶段”的additionalProperties属性的模式级别。 在这个级别,验证器没有在“params”属性中定义的子属性的 scope。 一个可能的解决方案可能是:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
  "type": "object",
  "definitions": {
    "params": {
      "type": "object",
      "properties": {
        "x": { "type": "string" },
        "y": { "type": "string" },
        "z": { "type": "string" }
      },
      "additionalProperties": false
    },
    "params_required_z": {
      "allOf": [
        { "$ref": "#/definitions/params" },
        { "required": [ "z" ] }
      ]
    },
    "params_required_x_y": {
      "allOf": [
        { "$ref": "#/definitions/params" },
        { "required": [ "x", "y" ] }
      ]
    }
  },
  "properties": {
    "stages": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "properties": {
          "stage_type": {
            "type": "string",
            "enum": [ "GSX", "GSZ" ]
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "stage_type": {
                  "enum": [ "GSX" ]
                }
              }
            },
            "then": {
              "properties": {
                "params": {
                  "$ref": "#/definitions/params_required_x_y"
                }
              }
            }
          },
          {
            "if": {
              "properties": {
                "stage_type": {
                  "enum": [ "GSZ" ]
                }
              }
            },
            "then": {
              "properties": {
                "params": {
                  "$ref": "#/definitions/params_required_z"
                }
              }
            }
          }
        ],
        "required": [ "stage_type", "params" ]
      },
      "minProperties": 1,
      "uniqueItems": true
    }
  },
  "additionalProperties": false
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM