我的输入如下所示
{"contents":[{"type":"field"},{"type":"field","itemId":"594b9980e52b5b0768afc4e8"}]}条件是,如果类型是'itemId‘,那么'itemId’应该是必需的字段,如果类型是'fieldGroup‘或’分段‘,则’itemId‘是可选的。
这是我尝试过的Json模式,它不像预期的那样工作,
"type": "object",
"additionalProperties": false,
"properties" : {
"contents" : {
"type" : "array",
"items": {"$ref": "#displayItem" }
}
},
"definitions": {
"displayItem" : {
"id": "#displayItem",
"type": "object",
"items": {
"anyOf": [
{"$ref": "#fieldType"},
{"$ref": "#fieldGroupSubSectionType"}
]
}
},
"fieldType" : {
"id": "#fieldType",
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": ["field"]
}
}
},
"fieldGroupSubSectionType" : {
"id": "#fieldGroupSubSectionType",
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": [ "string", "null" ]
},
"type": {
"type": "string",
"enum": [
"fieldGroup",
"subSection"
]
}
}
}
}任何使用示例Json实现上述用例的帮助/解决方案都是非常感谢的。
发布于 2017-06-22 13:46:46
如果我正确地理解了您想要的内容的描述,那么您提供的json示例就无效了,因为它有一个类型:"itemId“,但是没有”“属性。
假设这是真的。而不是使用
类型:"string",null
使用必需的属性。
我更改了您的模式,而不是有单独的定义,但除此之外(以及必需的使用)是相同的:
{
"type": "object",
"additionalProperties": false,
"properties": {
"contents": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"field"
]
}
},
"required": [
"itemId"
]
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"fieldGroup",
"subSection"
]
}
}
}
]
}
}
}
}发布于 2017-06-23 04:24:36
以下是您的答案,并对最佳实践和风格进行了一些清理。诀窍是,您需要使用隐含的"a意味着b <=> (而不是a)或b“。在本例中,您有"type = field意味着itemId是必需的,<=>类型不是字段,或者itemId是必需的“。
{
"type": "object",
"properties": {
"contents": {
"type": "array",
"items": { "$ref": "#/definitions/displayItem" }
}
},
"definitions": {
"displayItem": {
"type": "object",
"properties": {
"itemId": { "type": "string" },
"type": { "enum": ["field", "fieldGroup", "subSection"] }
},
"anyOf": [
{ "not": { "$ref": "#/definitions/fieldType" } },
{ "required": ["itemId"] }
]
},
"fieldType": {
"properties": {
"type": { "enum": ["field"] }
}
}
}
}https://stackoverflow.com/questions/44699858
复制相似问题