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

时间:2020-02-19 06:41:08

标签: json jsonschema

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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

简化了条件部分后,您将获得更简洁的模式。

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

Sudo代码:如果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"
                          ]
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    ]
  }
}
相关问题