【问题标题】:http://www.jsonschemavalidator.net/ doesn't appear to honour "uniqueitems": true or "additionalProperties": falsehttp://www.jsonschemavalidator.net/ 似乎不尊重 "uniqueitems": true 或 "additionalProperties": false
【发布时间】:2017-04-12 15:23:23
【问题描述】:

我一直在阅读有关如何定义 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"

以及重复的地址对象。

我一直在搜索 stackoverflow [标记为 json.net]、阅读(以及其他)、http://grokbase.comhttp://json-schema.org,并且我正在努力起草 04。

我也试过https://jsonschemalint.com/#/version/draft-04/markup/json,但这也没有抱怨。

任何人都可以将我指向其他验证器、json 架构文档和示例,或者如果很明显,请告诉我我做错了什么?

【问题讨论】:

    标签: json.net jsonschema


    【解决方案1】:

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

    uniqueItems 有两个问题。首先是另一个区分大小写的问题。您已使用uniqueitems。另一个问题是它在错误的地方。它是 items 架构(它是一个对象)的一部分,而它应该是根架构(它是一个数组)的一部分。

    所以,修正你的大小写并将uniqueItems 上移一级,它应该会按预期工作。

    【讨论】:

    • 谢谢杰森。我希望这是我所做的愚蠢的事情。现在我再次查看资源,它们显然区分大小写!
    猜你喜欢
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多