【发布时间】: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.com、http://json-schema.org,并且我正在努力起草 04。
我也试过https://jsonschemalint.com/#/version/draft-04/markup/json,但这也没有抱怨。
任何人都可以将我指向其他验证器、json 架构文档和示例,或者如果很明显,请告诉我我做错了什么?
【问题讨论】:
标签: json.net jsonschema