【发布时间】:2020-02-03 03:39:28
【问题描述】:
这是我修改后的问题,我借用了我在其中一个问题中学到的相同知识。我在这个问题的底部添加了我的架构和 JSON。
标准:
- 每个区域对象总是有一个属性为“区域”,并且可能有其他不同的属性和一个嵌套的 json 对象。 (请注意该对象没有相似的属性,所以我使用的是定义)
- 该数组必须包含至少一个区域对象,但只能是以下类型:澳大利亚或亚洲或欧洲以及其他区域类型的对象。 [澳大利亚或亚洲或欧洲不能共存]
- JSON 架构应该抱怨缺少必需的属性。
所以这个条件是有效的:
这里的数组是 ["stat_data":{},{},{}]
- [{"asia"}] //或欧洲或澳大利亚
- [{"some-pencil-region"},{"asia"}]
- [{"some-pencil-region"},{some-oil-pastels-region}, {"asia"}]
- [{some-oil-pastels-region}, {"europe"}]
- [{"some-pencil-region"}, {"europe"}]
而且这个条件是无效的:
- []
- [{"some-pencil-region"},{"asia"},{"europe"} {australia}] //亚洲、欧洲、澳大利亚不能共同退出
- [{some-oil-pastels-region},{"some-pencil-region"},{"asia"},{"asia"}, {australia}] //亚洲、欧洲、澳大利亚不能共同退出
- [{"some-pencil-region"}] //缺少:亚洲、欧洲或澳大利亚应该与其他对象一起存在
JSON 架构
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"Pencils": {
"contains": {
"properties": {
"region": {
"const": "some-pencil-region"
},
"details": {
"type": "object",
"required": [
"brand",
"year"
],
"properties": {
"brand": {
"type": "string"
},
"year": {
"type": "number"
}
}
}
}
}
},
"OilPastels": {
"contains": {
"required": [
"population"
],
"properties": {
"region": {
"const": "some-oil-pastels-region"
},
"details": {
"type": "object",
"properties": {
"size": {
"type": "number"
}
}
}
}
}
},
"containsAsia": {
"contains": {
"required": [
"population"
],
"properties": {
"region": {
"const": "asia"
},
"population": {
"type": "object",
"required": [
"year"
],
"properties": {
"year": {
"type": "number"
},
"change": {
"type": "number"
}
}
}
}
}
},
"containsEurope": {
"contains": {
"properties": {
"region": {
"const": "europe"
},
"tourist": {
"type": "number"
}
}
}
},
"containsAustralia": {
"contains": {
"properties": {
"region": {
"const": "australia"
},
"stadium": {
"type": "object",
"required": [
"year"
],
"properties": {
"year": {
"type": "number"
},
"area": {
"type": "number"
}
}
}
}
}
}
},
"type": "object",
"properties": {
"stat_data": {
"type": "array",
"minItems": 1,
"items": {
"type": "object"
},
"oneOf": [
{
"$ref": "#/definitions/Pencils"
},
{
"$ref": "#/definitions/OilPastels"
},
{
"allOf": [
{
"$ref": "#/definitions/containsAsia"
},
{
"not": {
"$ref": "#/definitions/containsEurope"
}
},
{
"not": {
"$ref": "#/definitions/containsAustralia"
}
}
]
},
{
"allOf": [
{
"$ref": "#/definitions/containsEurope"
},
{
"not": {
"$ref": "#/definitions/containsAsia"
}
},
{
"not": {
"$ref": "#/definitions/containsAustralia"
}
}
]
},
{
"allOf": [
{
"$ref": "#/definitions/containsAustralia"
},
{
"not": {
"$ref": "#/definitions/containsAsia"
}
},
{
"not": {
"$ref": "#/definitions/containsEurope"
}
}
]
}
]
}
}
}
JSON(这是失败的)[我尝试了所有的验证,但都是徒劳的]
{
"stat_data":[
{
"region":"some-pencil-region",
"details":{
"brand":"Camlin",
"year": 2019
}
},
{
"region":"asia",
"population":{
"year":2018,
"change":2
}
}
]
}
10/06 未验证强制属性 LINK1
【问题讨论】:
标签: json jsonschema ajv