【问题标题】:How to validate a specific jsonschema based on document type如何根据文档类型验证特定的 jsonschema
【发布时间】: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


    【解决方案1】:

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

    {
      "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"] }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 2017-03-07
      • 1970-01-01
      • 2014-02-10
      • 2019-10-25
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多