下面是JSON和JSON,如下所示,链接中提供了用于插画的内容。
格式:数组中的单个JSON对象(以及它们的附加属性和可能随数组中的其他对象而异)可以是任意3个区域:“america”、“asia”和“europe”,至少在区域对象的类型上应该存在。这可以通过数组minItems属性来实现)
问题陈述:
JSON模式
{
"type": "object",
"properties": {
"stat_data": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {},
"anyOf": [{
"required": ["region"],
"properties": {
"region": {
"enum": ["america"]
},
"country": {
"type": "string"
},
"population": {
"type": "string"
}
}
},
{
"oneOf": [
{
"required": ["region"],
"properties": {
"region": {
"enum": ["asia"]
},
"country": {
"type": "string"
},
"details": {
"type": "object",
"properties": {
"language": {
"type": "string"
},
"tz": {
"type": "string"
}
}
}
}
}, {
"required": ["region"],
"properties": {
"region": {
"enum": ["europe"]
},
"country": {
"type": "string"
},
"language": {
"type": "string"
}
}
}
]
}
]
}
}
}
}JSON反对失败,因为“亚洲”和“欧洲”类型的对象不能共存。
{
"stat_data": [{
"region": "america",
"country": "USA",
"states": "50"
}, {
"region": "asia",
"country": "Japan",
"details": {
"language": "Japanese",
"tz": "utc+9.00"
}
}, {
"region": "europe",
"country": "finland",
"language": "Finnish"
}
]
}JSON对象作为只有“亚洲”类型的对象存在。
{
"stat_data": [{
"region": "america",
"country": "USA",
"states": "50"
}, {
"region": "asia",
"country": "Japan",
"details": {
"language": "Japanese",
"tz": "utc+9.00"
}
}
]
}JSON对象作为只传递“欧洲”类型的对象存在。
{
"stat_data": [{
"region": "america",
"country": "USA",
"states": "50"
}, {
"region": "europe",
"country": "finland",
"language": "Finnish"
}
]
}发布于 2019-10-01 11:51:34
我可以理解为什么您尝试了这种方法,但是它并不像预期的那样工作,因为您已经定义了数组中的每个项可能是america或(europe或asia),这不是您想要的。
记住,items将值模式应用于数组中的每个元素。它对整个数组本身没有任何限制。contains检查数组中至少有一个项对其值架构进行验证。
您想要说的是,数组中的每一项都可能有america、europe或asia,但是如果数组中包含asia,则数组可能不包含europe,反之则不包含europe。
我重构了模式并做了一些更改。
希望您也能看到使用oneOf >> (contains和not > contains)的意图是明确的。
JSON模式通过添加约束来工作。通常不能通过省略来定义约束。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"containtsAsia": {
"contains": {
"properties": {
"region": {
"const": "asia"
}
}
}
},
"containsEurope": {
"contains": {
"properties": {
"region": {
"const": "europe"
}
}
}
}
},
"type": "object",
"properties": {
"stat_data": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"region": {
"enum": [
"america",
"asia",
"europe"
]
},
"country": {
"type": "string"
},
"population": {
"type": "string"
}
}
},
"oneOf": [
{
"allOf": [
{
"$ref": "#/definitions/containtsAsia"
},
{
"not": {
"$ref": "#/definitions/containsEurope"
}
}
]
},
{
"allOf": [
{
"$ref": "#/definitions/containsEurope"
},
{
"not": {
"$ref": "#/definitions/containtsAsia"
}
}
]
}
]
}
}
}https://stackoverflow.com/questions/58172931
复制相似问题