在 JSON 模式中使用 oneOf 时如何防止添加属性

How to prevent additions properties when using oneOf in JSON Schema

我有一个 JSON 配置,其中包含一个 "specs" 数组,具有许多不同类型的 "path" 属性。 "kind" 属性 是变体的选择器。

我想将验证限制为不允许添加属性。在 JSON 示例中,属性 "noAllowedInt""notAllowedObject" 必须 报告验证错误。

如何将此规则添加到 JSON 架构?

{
    "specs": [
        {
            "id": 12,
            "label": "Serial",
            "path": {
                "kind": "serial",
                "speed": 9600,
                "parity": "even"
            }
        },
        {
            "id": 13,
            "label": "Memory",
            "path": {
                "kind": "memory",
                "storage": "permanent",
                "location": "external"
            },
            "noAllowedInt": 42,
            "notAllowedObject": {
                "value": 3.1415
            }
        }
    ]
}

有两个 Json 模式来验证这一点,一个简单的和一个 "definitions"

简单JSON架构:

    {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "specs": {
            "type": "array",
            "items": {
                "type": "object",
                "additionalProperties": true,
                "properties": {
                    "id": {
                        "type": "integer"
                    },
                    "label": {
                        "type": "string"
                    }
                },
                "required": [
                    "id",
                    "label"
                ],
                "oneOf": [
                    {
                        "type": "object",
                        "properties": {
                            "path": {
                                "type": "object",
                                "additionalProperties": false,
                                "properties": {
                                    "kind": {
                                        "type": "string",
                                        "enum": [
                                            "serial"
                                        ]
                                    },
                                    "speed": {
                                        "type": "integer"
                                    },
                                    "parity": {
                                        "type": "string"
                                    }
                                },
                                "required": [
                                    "kind"
                                ]
                            }
                        }
                    },
                    {
                        "type": "object",
                        "properties": {
                            "path": {
                                "type": "object",
                                "additionalProperties": false,
                                "properties": {
                                    "kind": {
                                        "type": "string",
                                        "enum": [
                                            "memory"
                                        ]
                                    },
                                    "storage": {
                                        "type": "string"
                                    },
                                    "location": {
                                        "type": "string"
                                    }
                                },
                                "required": [
                                    "kind"
                                ]
                            }
                        }
                    }
                ]
            }
        }
    },
    "required": [
        "specs"
    ]
}

JSON 架构 "definitions"

    {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "specs": {
            "type": "array",
            "items": {
                "type": "object",
                "additionalProperties": true,
                "properties": {
                    "id": {
                        "type": "integer"
                    },
                    "label": {
                        "type": "string"
                    }
                },
                "required": [
                    "id",
                    "label"
                ],
                "oneOf": [
                    {
                        "$ref": "#/definitions/PathKindSerialType"
                    },
                    {
                        "$ref": "#/definitions/PathKindMemory"
                    }
                ]
            }
        }
    },
    "required": [
        "specs"
    ],
    "definitions": {
        "PathKindSerialType": {
            "type": "object",
            "additionalProperties": true,
            "properties": {
                "path": {
                    "type": "object",
                    "additionalProperties": false,
                    "properties": {
                        "kind": {
                            "type": "string",
                            "enum": [
                                "serial"
                            ]
                        },
                        "speed": {
                            "type": "integer"
                        },
                        "parity": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "kind"
                    ]
                }
            }
        },
        "PathKindMemory": {
            "type": "object",
            "additionalProperties": true,
            "properties": {
                "path": {
                    "type": "object",
                    "additionalProperties": false,
                    "properties": {
                        "kind": {
                            "type": "string",
                            "enum": [
                                "memory"
                            ]
                        },
                        "storage": {
                            "type": "string"
                        },
                        "location": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "kind"
                    ]
                }
            }
        }
    }
}

我想您需要在 specs 数组级别将路径定义保持为 属性。这是我的提议:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "specs": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "properties": {
          "id": {
            "type": "integer"
          },
          "label": {
            "type": "string"
          },
          "path": {
            "oneOf": [
              {
                "$ref": "#/definitions/PathKindSerialType"
              },
              {
                "$ref": "#/definitions/PathKindMemory"
              }
            ]
          }
        },
        "required": [ "id", "label" ]
      }
    }
  },
  "required": [ "specs" ],
  "definitions": {
    "PathKindSerialType": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": {
          "type": "string",
          "enum": [ "serial" ]
        },
        "speed": {
          "type": "integer"
        },
        "parity": {
          "type": "string"
        }
      },
      "required": [ "kind" ]
    },
    "PathKindMemory": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "kind": {
          "type": "string",
          "enum": [ "memory" ]
        },
        "storage": {
          "type": "string"
        },
        "location": {
          "type": "string"
        }
      },
      "required": [ "kind" ]
    }
  }
}