【问题标题】:JSON-Schema add property to the item conditionallyJSON-Schema 有条件地向项目添加属性
【发布时间】:2020-06-03 06:31:39
【问题描述】:

我有以下 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"
                                }
                              }
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          }
        }
      }
    ]
  }
}

我想根据keycustomLink 的条件为profiles 项目添加新属性color

如果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 jsonschema


【解决方案1】:

在您的 profiles.items 架构中,您定义了 additionalProperties: false

additionalProperties 依赖于在同一架构对象中定义的properties,这意味着color 将始终被禁止。

您可以将您的担忧分为“允许哪些属性”和“它们的值是否有效”。

color 对象的验证移至profiles.properties 允许您在该对象级别维护additionalProperties: false

properties 对象的架构值仅应用于键的实例位置(如果它们存在)(因此需要使用 required 来要求特定键是必需的)。

在简化了条件部分之后,您最终会得到一个更简洁的 Schema。

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

须藤代码:如果keycustomLink,则要求color,否则拒绝color

{
  "if": {
    "properties": {
      "key": {
        "const": "customLink"
      }
    }
  },
  "then": {
    "required": [
      "color"
    ]
  },
  "else": {
    "not": {
      "required": [
        "color"
      ]
    }
  }
}

使用not,您可以反转应用的子模式的验证结果,这是您想要定义不允许的特定属性时需要执行的操作。

这是一个现场演示:https://jsonschema.dev/s/C9V6N

为了繁荣,这里是完整的架构,去掉了一两个冗余。

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

【讨论】:

  • 是的,到目前为止,我正在做同样的事情。但这并不能解决我的问题。 color 属性是可选的,即使在 customLink 的情况下也是如此,但当存在时,应根据 color 属性下定义的属性进行验证。如果我将其设置为其他类型不需要,那么对于我不想要的非允许类型仍然可以接受该值。
  • color 总是可选的?对不起,我不清楚你在问什么。听起来您现在正在提供未包含在问题中的其他要求。您能否添加更多示例来说明您希望成为 VALID 和 INVALID 的内容?
  • “不 > 需要”不是“不需要”。 not 反转断言结果,如解释的那样。 { "not": {} } 的模式会使一切无效。它相当于 false 作为布尔模式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-04
  • 2021-01-25
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多