首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何根据文档类型验证特定的jsonschema

如何根据文档类型验证特定的jsonschema
EN

Stack Overflow用户
提问于 2018-06-05 20:47:16
回答 1查看 26关注 0票数 0

我对用户的新消息有一个JSON模式:

代码语言:javascript
复制
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。因此,如下所示的有效负载成功了:

代码语言:javascript
复制
{
    "post": {
        "image": "Hey there!"
    },
    "type": "image"
}

现在的问题是,如果用户将type的值设置为text,我就无法验证是否已经给出了文本的模式。我应该如何验证这个?如果type设置为image,我应该如何检查,然后确保post中存在image

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-09 03:46:46

你可以做到,但这很复杂。这使用了一个称为隐含的布尔逻辑概念,以确保如果模式A匹配,那么模式B也必须匹配。

代码语言:javascript
复制
{
  "type": "object",
  "properties": {
    "post": {
      "type": "object",
      "properties": {
        "content": { "type": "string" },
        "image": { "type": "string" },
        "video_path": { "type": "string" }
      },
      "additionalProperties": false
    },
    "doc_type": {
      "type": "string",
      "enum": ["text", "image", "video"]
    }
  },
  "required": ["post", "doc_type"],
  "additionalProperties": false,
  "allOf": [
    { "$ref": "#/definitions/image-requires-post-image" },
    { "$ref": "#/definitions/text-requires-post-content" },
    { "$ref": "#/definitions/video-requires-post-video-path" }
  ],
  "definitions": {
    "image-requires-post-image": {
      "anyOf": [
        { "not": { "$ref": "#/definitions/type-image" } },
        { "$ref": "#/definitions/post-image-required" }
      ]
    },
    "type-image": {
      "properties": {
        "doc_type": { "const": "image" }
      }
    },
    "post-image-required": {
      "properties": {
        "post": { "required": ["image"] }
      }
    },
    "text-requires-post-content": {
      "anyOf": [
        { "not": { "$ref": "#/definitions/type-text" } },
        { "$ref": "#/definitions/post-content-required" }
      ]
    },
    "type-text": {
      "properties": {
        "doc_type": { "const": "text" }
      }
    },
    "post-content-required": {
      "properties": {
        "post": { "required": ["content"] }
      }
    },
    "video-requires-post-video-path": {
      "anyOf": [
        { "not": { "$ref": "#/definitions/type-video" } },
        { "$ref": "#/definitions/post-video-path-required" }
      ]
    },
    "type-video": {
      "properties": {
        "doc_type": { "const": "video" }
      }
    },
    "post-video-path-required": {
      "properties": {
        "post": { "required": ["video_path"] }
      }
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50700456

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档