这是我修改过的问题,我在借鉴其中一个问题中所学到的相同知识。我在此问题的底部添加了架构和JSON。
条件:
因此此条件有效:
此处的数组为[“ stat_data”:{},{},{}]
此条件无效:
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
答案 0 :(得分:0)
我相信您需要为此使用if-then-else流程:
{
"type": "object",
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "JSON schema generated with JSONBuddy https://www.json-buddy.com",
"properties": {
"stat_data": {
"type": "array",
"if": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [ "europe" ]
}
}
}
},
"then": {
"not": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [ "asia", "australia" ]
}
}
}
}
},
"else": {
"if": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [ "asia" ]
}
}
}
},
"then": {
"not": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [ "europe", "australia" ]
}
}
}
}
},
"else": {
"if": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [ "australia" ]
}
}
}
},
"then": {
"not": {
"contains": {
"type": "object",
"properties": {
"region": {
"type": "string",
"enum": [ "europe", "asia" ]
}
}
}
}
},
"else": {
}
}
},
"items": {
"type": "object",
"properties": {
"details": {
"$ref": "#/definitions/details"
},
"population": {
"$ref": "#/definitions/population"
},
"region": {
"enum": [ "asia", "europe", "australia", "some-pencil-region", "some-oil-pastels-region" ]
}
}
}
}
},
"definitions": {
"details": {
"type": "object",
"properties": {
"brand": {
"type": "string"
},
"year": {
"type": "integer"
}
}
},
"population": {
"type": "object",
"properties": {
"change": {
"type": "integer"
},
"year": {
"type": "integer"
}
}
}
}
}