У меня есть схема 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
. Таким образом, полезная нагрузка, как показано ниже, преуспевает:
{
"post": {
"image": "Hey there!"
},
"type": "image"
}
Теперь проблема в том, что если пользователь устанавливает значение type
на text
, я не могу проверить, была ли задана схема текста. Как мне это проверить? Как мне проверить, установлен ли type
на image
, а затем убедиться, что image
существует внутри сообщения?