【发布时间】:2018-11-14 23:05:33
【问题描述】:
我有一个用于用户新消息的 JSON 模式:
message_creation = {
"title": "Message",
"type": "object",
"properties": {
"post": {
"oneOf": [
{
"type": "object",
"properties": {
"content": {
"type": "string"
}
},
"additionalProperties": False,
"required": ["content"]
},
{
"type": "object",
"properties": {
"image": {
"type": "string"
}
},
"additionalProperties": False,
"required": ["image"]
},
{
"type": "object",
"properties": {
"video_path": {
"type": "string"
}
},
"additionalProperties": False,
"required": ["video"]
}
]
},
"doc_type": {
"type": "string",
"enum": ["text", "image", "video"]
}
},
"required": ["post", "doc_type"],
"additionalProperties": False
}
就这么简单!有两个字段,一个是type,另一个是post。所以像下面这样的payload就成功了:
{
"post": {
"image": "Hey there!"
},
"type": "image"
}
现在的问题是,如果用户将type 值设置为text,我无法验证是否已给出文本架构。我应该如何验证这一点?我应该如何检查 type 是否设置为 image 然后确保 image 存在于帖子中?
【问题讨论】:
标签: python schema jsonschema