http://www.jsonschemavalidator.net/ 似乎不支持 "uniqueitems": true 或 "additionalProperties": false

http://www.jsonschemavalidator.net/ doesn't appear to honour "uniqueitems": true or "additionalProperties": false

我一直在阅读如何定义 json 架构来保存不同形状对象的列表:人、地址、车辆。部分研究让我看到了一些帖子,其中提出了有用的补充建议。例如添加 "uniqueitems": true 以便列表不包含重复项。

我发现站点 http://www.jsonschemavalidator.net/ 在验证我的模式和 json 数据方面非常有用,但我无法弄清楚我在这两个方面做错了什么

"additionalProperties": false 

"uniqueitems": true

这是我的示例架构和数据:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "an array of Insurable Items",
  "type": "array",
  "items": {
    "type": "object",
    "OneOf": [
      {
        "type": "object",
        "description": "A Person",
        "properties": {
          "person": {
            "type": "object",
            "$ref": "#/definitions/person"
          }
        },
        "required": [
          "person"
        ],
        "additionalProperties": false
      },
      {
        "type": "object",
        "description": "An Address",
        "properties": {
          "address": {
            "type": "object",
            "$ref": "#/definitions/address"
          }
        },
        "required": [
          "address"
        ],
        "additionalProperties": false
      },
      {
        "type": "object",
        "description": "A Vehicle",
        "properties": {
          "vehicle": {
            "type": "object",
            "$ref": "#/definitions/vehicle"
          }
        },
        "required": [
          "vehicle"
        ],
        "additionalProperties": false
      }
    ],
    "uniqueitems": true
  },

  "definitions": {
    "person": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "title": {
          "type": "string"
        },
        "firstname": {
          "type": "string"
        },
        "lastname": {
          "type": "string"
        },
        "dateofbirth": {
          "type": "string"
        },
        "employmentstatus": {
          "type": "string"
        },
        "occupation": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "address": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "line1": {
          "type": "string"
        },
        "line2": {
          "type": "string"
        },
        "line3": {
          "type": "string"
        },
        "line4": {
          "type": "string"
        },
        "line5": {
          "type": "string"
        },
        "postcode": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "vehicle": {
      "type": "object",
      "properties": {
        "id": {
          "type": "string"
        },
        "vehiclecode": {
          "type": "string"
        },
        "registrationyear": {
          "type": "string"
        },
        "registrationletter": {
          "type": "string"
        },
        "registrationnumber": {
          "type": "string"
        },
        "description": {
          "type": "string"
        }
      },
      "additionalProperties": false
    }
  }
}

数据:

[
  {
    "person": {
      "id": "123456789-01",
      "title": "Mr",
      "firstname": "Joe",
      "lastname": "blogs"
    },
    "badadditional": "thing"
  },
  {
    "address": {
      "id": "123456789-A",
      "line1": "1 The Mall",
      "line2": "Westminster",
      "line3": "London",
      "postcode": "SW1A 1AA"
    }
  },
  {
    "address": {
      "id": "123456789-A",
      "line1": "1 The Mall",
      "line2": "Westminster",
      "line3": "London",
      "postcode": "SW1A 1AA"
    }
  },
  {
    "vehicle": {
      "id": "123456789-01-01",
      "vehiclecode": "string",
      "registrationyear": "string",
      "registrationletter": "string",
      "registrationnumber": "string",
      "description": "string",
      "badadditional": "other thing"
    }
  }
]

如果您将这些复制到 http://www.jsonschemavalidator.net/ 架构验证器中,尽管

存在,但它不会报告任何问题
"badadditional": "thing"

"badadditional": "other thing"

和重复的地址对象。

我一直在搜索 Whosebug [标记为 json.net],正在阅读(除其他外)http://grokbase.com, http://json-schema.org,并且我正在研究 draft-04。

我也试过 https://jsonschemalint.com/#/version/draft-04/markup/json 但那也没有问题。

任何人都可以向我指出其他验证器、json 架构文档和示例,或者让我知道我做错了什么(如果很明显)吗?

additionalProperties 工作得很好。问题是您使用了 OneOf 而不是 oneOf。 JSON 架构关键字区分大小写,因此验证器无法识别此关键字并忽略它及其定义的所有内容。

uniqueItems 你有两个问题。第一个是另一个区分大小写的问题。您已使用 uniqueitems。另一个问题是它在错误的地方。当它应该是根模式(数组)的一部分时,它是 items 模式(一个对象)的一部分。

因此,修复您的大小写并将 uniqueItems 提高一级,它应该会按预期工作。