我有这个父架构:
{
"definitions": {
"parcel": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"accountNumber": {
"type": "string"
},
"parcelNumber": {
"type": "string"
},
"propertyType": {
"type": "string"
},
"address": {
"$ref": "address.json#/definitions/address"
},
"coordinates": {
"$ref": "coordinates.json#/definitions/coordinates"
}
},
"required": ["accountNumber", "parcelNumber"]
}
}
}
Run Code Online (Sandbox Code Playgroud)
以下是引用的子模式:
{
"definitions": {
"address": {
"type": "object",
"properties": {
"addressString": {
"type": "string",
"addressType": {
"enum": ["residential", "business"]
}
},
"required": ["addressString"]
}
}
}
}
{
"definitions": {
"coordinates": {
"type": "object",
"properties": {
"latitude": {
"type": "number"
},
"longitude": {
"type": "number"
},
"projection": {
"type": "string"
}
},
"required": ["latitude ", "longitude", " projection"]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想将以下条件应用于父架构。
您的anyOf解决方案有效。您可以通过从anyOf属性组中分离固定的必需属性(accountNumber 和parcelNumber)来使其更简洁:
{
"type": "object",
"required": [
"accountNumber",
"parcelNumber"
],
"anyOf": [
{"required" : ["address"]},
{"required" : ["coordinates"]}
],
"properties": {
"id": {
"type": "string"
},
"accountNumber": {
"type": "string"
},
"parcelNumber": {
"type": "string"
},
"propertyType": {
"type": "string"
},
"address": {
"type": "object",
"properties": {
"addressString": {
"type": "string"
},
"addressType": {
"enum": [
"residential",
"business"
]
}
},
"required": [
"addressString"
]
},
"coordinates": {
"type": "object",
"properties": {
"latitude": {
"type": "number"
},
"longitude": {
"type": "number"
},
"projection": {
"type": "string"
}
},
"required": [
"latitude",
"longitude",
"projection"
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里有一个要点供参考:
http://jsonschemalint.com/#/version/draft-05/markup/json?gist=f36d9a7e080c4d25dbbf09b7dd03137e