首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSON-Schema向项添加属性

JSON-Schema向项添加属性
EN

Stack Overflow用户
提问于 2020-02-19 06:41:08
回答 1查看 816关注 0票数 2

我有以下JSON数据

代码语言:javascript
复制
[
  {
    "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模式。

代码语言:javascript
复制
{
  "$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"
                                }
                              }
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      }
    ]
  }
}

我希望根据profiles项的keycustomLink的条件向customLink项添加新的属性customLink

如果key不是customLink,那么color属性就不应该存在。

https://www.jsonschemavalidator.net/验证架构会产生错误

代码语言:javascript
复制
 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

如何根据兄弟属性值有条件地附加新属性?

EN

回答 1

Stack Overflow用户

发布于 2020-02-19 12:43:20

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

additionalProperties依赖于在同一个模式对象中定义的properties,这意味着color总是不受欢迎。

您可以将您的关注点分为“允许哪些属性”和“如果它们的值是有效的”。

color对象的验证移到profiles.properties中,可以在该对象级别上维护additionalProperties: false

properties对象的模式值仅应用于键的实例位置(因此需要使用required来要求特定的键是.所需)。

简化条件部分后,您将得到一个更干净的架构。

现在只需要定义需要color的条件,而不必担心该值应该是什么样子。

Sudo代码:如果keycustomLink,那么需要color,否则就会破坏color

代码语言:javascript
复制
{
  "if": {
    "properties": {
      "key": {
        "const": "customLink"
      }
    }
  },
  "then": {
    "required": [
      "color"
    ]
  },
  "else": {
    "not": {
      "required": [
        "color"
      ]
    }
  }
}

使用not,您可以反转应用子架构的验证结果,当您想要定义不允许特定属性时,就需要这样做。

下面是一个可供使用的现场演示:https://jsonschema.dev/s/C9V6N

为了繁荣,这是一个完整的模式,去掉一两个冗余。

代码语言:javascript
复制
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "array",
  "items": {
    "type": "object",
    "required": [
      "type",
      "data"
    ],
    "additionalProperties": false,
    "properties": {
      "type": {
        "type": "string",
        "enum": [
          "social_media_profiles"
        ]
      },
      "data": {
        "type": "object"
      }
    },
    "allOf": [
      {
        "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"
                    },
                    "color": {
                      "type": "object",
                      "additionalProperties": false,
                      "properties": {
                        "button": {
                          "type": "string"
                        },
                        "text": {
                          "type": "string"
                        },
                        "border": {
                          "type": "string"
                        }
                      }
                    }
                  },
                  "allOf": [
                    {
                      "if": {
                        "properties": {
                          "key": {
                            "const": "customLink"
                          }
                        }
                      },
                      "then": {
                        "required": [
                          "color"
                        ]
                      },
                      "else": {
                        "not": {
                          "required": [
                            "color"
                          ]
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60294410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档