http://www.jsonschemavalidator.net/似乎并不尊重" uniqueitems":true或" additionalProperties":false

时间:2017-04-12 15:23:24

标签: json.net jsonschema

我一直在阅读如何定义json架构以保存不同形状对象的列表:人,地址,车辆。部分研究使我找到了有助于添加建议的帖子。例如添加" uniqueitems":为真,以便列表不包含重复项。

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

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

数据:

"badadditional": "thing"

如果将这些内容复制到http://www.jsonschemavalidator.net/架构验证程序中,尽管存在

,它仍然没有问题
"badadditional": "other thing"

ggplot(data=data, aes(x=Mean, y=CV2)) +
    geom_point() +
    stat_smooth(method='nls',formula=y~(a/(b+x))+c, se=FALSE, 
        method.args = list(algorithm="port", start = c(a=1, b=1,c=1))) +
    labs(title = "RA vs CV (All analytes)") +
    labs(x = "Mean [%]") +
    labs(y = "CV [%]") +
    theme(plot.title = element_text(hjust = 0.5))

和重复的地址对象。

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

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

任何人都可以指向其他验证器,json架构文档和示例,或者如果它显而易见的话,让我知道我做错了什么?

1 个答案:

答案 0 :(得分:2)

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

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

因此,请修正您的大小写并将uniqueItems向上移动一个级别,它应该按预期工作。