简体   繁体   English

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

[英]Json schema with nested objects and conditionals

I'm trying to create a json schema for the following structure:我正在尝试为以下结构创建 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"
            }
        }
    }
}

The idea is that "stage_type" is an enum with possible values ["GSX", "GSZ",...].这个想法是“stage_type”是一个具有可能值的枚举[“GSX”,“GSZ”,...]。 The logic that I would like to implement is:我想实现的逻辑是:

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

I'm failing however, at implementing this logic for required properties... Here is how far I've got:但是,在为所需属性实现此逻辑时,我失败了……这是我已经走了多远:

{
    "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
}

I can't seem to make the nesting of the required fileds in the if-then clauses work... Help would be very much appreciated: )我似乎无法使 if-then 子句中所需文件的嵌套工作......非常感谢帮助:)

In your original schema, the allOf is applied to the schema level at the additionalProperties of "stages".在您的原始模式中, allOf应用于“阶段”的additionalProperties属性的模式级别。 At this level the validator has no scope of the child properties defined at the "params" property.在这个级别,验证器没有在“params”属性中定义的子属性的 scope。 A possible solution could be:一个可能的解决方案可能是:

{
  "$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