JSON模式 - 基于枚举的属性

时间:2017-11-06 14:46:50

标签: json enums properties schema

我正在尝试为屏幕上的控件构建一个json架构。我将控件(controltype)限制为枚举,并为所有控件添加了所有可能的属性。 问题是并非所有属性都与所有控件相关。 在我的例子中:"选项"财产与" SectionTitle"无关。控制类型,但它与" Dropdown"。

相关
{
    "controls": {
        "type": "array",
        "$id": "/properties/data/properties/metadata/properties/page/properties/controls",
        "properties": {
            "controlType": {
                "type": "string",
                "enum": ["SectionTitle",
                "Dropdown",
                "CustomButton"],
                "$id": "/properties/data/properties/metadata/properties/page/properties/controls/items/properties/controlType",
                "title": "The Controltype Schema.",
                "description": "The name of the control",
                "default": "",
                "examples": ["Dropdown"]
            },
            "id": {
                "type": "integer",
                "$id": "/properties/data/properties/metadata/properties/page/properties/controls/items/properties/id",
                "title": "The Id Schema.",
                "description": "Unique identifier",
                "default": 0,
                "examples": [10]
            },
            "name": {
                "type": "string",
                "$id": "/properties/data/properties/metadata/properties/page/properties/controls/items/properties/name",
                "title": "The Name Schema.",
                "description": "Unique identifier",
                "default": 0,
                "examples": [""]
            },
            "options": {
                "type": "array",
                "$id": "/properties/options",
                "items": {
                    "type": "object",
                    "$id": "/properties/options/items",
                    "properties": {
                        "title": {
                            "type": "string",
                            "$id": "/properties/options/items/properties/title",
                            "title": "The Title Schema.",
                            "description": "Display value of the presented options in the control",
                            "default": "",
                            "examples": ["Yes"]
                        },
                        "value": {
                            "type": "boolean",
                            "$id": "/properties/options/items/properties/value",
                            "title": "The Value Schema.",
                            "description": "Enum value of the presented options in the control",
                            "default": false,
                            "examples": [true]
                        },
                        "sequence": {
                            "type": "integer",
                            "$id": "/properties/options/items/properties/sequence",
                            "title": "The Sequence Schema.",
                            "description": "Unique. Determine the location of the value",
                            "default": 0,
                            "examples": [10]
                        }
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用IF..Then..Else new in Draft-07

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "controlType": {
        "type": "string",
        "enum": ["title", "dropdown", "button"]
      },
      "options:": {
        "type": "array",
        "items": {"type": "string"}
      }
    },
      
    "if": {
      "properties": {
        "controlType": {"const": "dropdown"}
      }
    },
    "then": {
      "required": ["options"]
    }
  }
}

使用oneOfanyOf

如果您的属性的可接受值数量有限(例如枚举),但是每个可能的值都需要单独映射,则此功能很有用。

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "controlType": {
        "type": "string",
        "enum": ["title", "dropdown", "button"]
      },
      "options:": {
        "type": "array",
        "items": {"type": "string"}
      }
    },  
    "anyOf": [
      {
        "properties": {
          "controlType": {"const": "dropdown"}
        },
        "required": ["controlType", "options"]
      },
      {
        "properties": {
          "controlType": {"const": "title"}
        },
        "required": ["controlType"]
      },
      {
        "properties": {
          "controlType": {"const": "button"}
        },
        "required": ["controlType"]
      }
    ]
  }
}

进一步阅读

相关问题