[JSON-Schema有条件地向该项目添加属性

问题描述 投票:2回答:1

我有以下JSON数据

[
  {
    "type": "social_media_profiles",
    "data": {
      "profiles": [
        {
          "key": "twitter",
          "data": "username",
          "field": "handle",
          "label": "Tweet"
        },
        {
          "key": "customLink",
          "data": "abc",
          "field": "url",
          "label": "Click",
          "color": {
            "button": "red",
            "text": "green",
            "border": "yellow"
          }
        }
      ]
    }
  }
]

和以下jsong模式

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "additionalProperties": false,
  "items": {
    "type": "object",
    "required": ["type", "data"],
    "additionalProperties": false,
    "properties": {
      "type": {
        "type": "string",
        "enum": [
          "social_media_profiles"
        ]
      },
      "data": {
        "type": "object"
      }
    },
    "allOf": [
      {
        "if": {
          "properties": {
            "type": {
              "const": "social_media_profiles"
            }
          }
        },
        "then": {
          "properties": {
            "data": {
              "type": "object",
              "required": ["profiles"],
              "additionalProperties": false,
              "properties": {
                "profiles": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "required": ["data", "field", "key"],
                    "additionalProperties": false,
                    "properties": {
                      "data": {
                        "type": "string",
                        "description": "Data contains either profile url, handle id, etc."
                      },
                      "field": {
                        "type": "string",
                        "enum": ["url", "handle", "id", "tel"],
                        "description": "Type of field value."
                      },
                      "key": {
                        "type": "string",
                        "description": "Social media name used to distinguish each social network"
                      },
                      "label": {
                        "type": "string",
                        "description": "Label to display on the landing page"
                      }
                    },
                    "allOf": [
                      {
                        "if": {
                          "properties": {
                            "key": {
                              "const": "customLink"
                            }
                          }
                        },
                        "then": {
                          "properties": {
                            "color": {
                              "type": "object",
                              "additionalProperties": false,
                              "required": [],
                              "properties": {
                                "button": {
                                  "type": "string"
                                },
                                "text": {
                                  "type": "string"
                                },
                                "border": {
                                  "type": "string"
                                }
                              }
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      }
    ]
  }
}

我要根据项目的colorprofiles的条件,向key项目添加新属性customLink。>

如果key不是customLink,则color属性不应存在。

https://www.jsonschemavalidator.net/验证模式给出了错误

 Found 2 error(s)

 Message: JSON does not match all schemas from 'allOf'. Invalid schema indexes: 0.
 Schema path: #/items/allOf

    Message: JSON does not match schema from 'then'.
    Schema path: #/items/allOf/0/then/then

如何根据同级属性值有条件地附加新属性?

我有以下JSON数据[{“ type”:“ social_media_profiles”,“ data”:{“ profiles”:[{“ key”:“ twitter”,“ data”:“用户名”,“ field”:“ ...

json jsonschema
1个回答
0
投票

profiles.items模式中,您定义了additionalProperties: false

© www.soinside.com 2019 - 2024. All rights reserved.