如何使用 JSON 模式中的任何定义验证数组中的每个实例

How to validate every instance in an array with any definition in JSON schema

我需要验证以下数组项。

{
    contents: [{
        type: "text",
        content: "This is text context"
    }, {
        type: "image",
        content: "http://image.url"
    }]
}

我需要验证内容数组中的每一项。

每个内容对象都应具有 typecontent 属性。 type 可以是 "text"、"image" 或 "video"。 对于图片或视频,content 应该是有效的 url。

为此,我编写了以下架构。

{
    "id": "post",
    "description": "generell schema for a post",
    "definitions": {
        "contents": {
            "type": "array",
            "minItems": 1,
            "items": {
                "allOf": [
                    { "$ref": "#/definitions/text" },
                    { "$ref": "#/definitions/image" },
                    { "$ref": "#/definitions/video" },
                    { "$ref": "#/definitions/mention" }
                ]
            }
        },
        "text": {
            "properties": {
                "type": {"enum": ["text"]},
                "content": {"type": "string"}
            },
            "required": [
                "content",
                "type"
            ]
        },
        "image": {
            "properties": {
                "type": {"enum": ["image"]},
                "content": {
                    "type": "string", 
                    "format": "url"
                }
            },
            "required": [
                "content",
                "type"
            ]
        },
        "video": {
            "properties": {
                "type": {"enum": ["video"]},
                "content": {
                    "type": "string", 
                    "format": "url"
                }
            },
            "required": [
                "content",
                "type"
            ]
        }
    }

}

但高于 JSON 对我的架构无效。它说 data.contents[0].type should be equal to one of the allowed values

如果我使用 oneOf 而不是 allOf,它是有效的。但是图像内容可以是没有合法 URL 的字符串。

正确的架构是什么?

对于初学者,您使用的是 allOf,而您应该使用 oneOf

根项目还需要属性定义。希望以下更改可以帮助您获得所需的解决方案或为您指明正确的方向。

{
    "id": "post",
    "description": "generell schema for a post",
    "properties": {
        "contents": {
            "type": "array",
            "minItems": 1,
            "items": {
                "oneOf": [
                    { "$ref": "#/definitions/text" },
                    { "$ref": "#/definitions/image" },
                    { "$ref": "#/definitions/video" },
                    { "$ref": "#/definitions/mention" }
                ]
            }
        }
    }
    "definitions": {
        "text": {
            "properties": {
                "type": {"enum": ["text"]},
                "content": {"type": "string"}
            },
            "required": [
                "content",
                "type"
            ]
        },
        "image": {
            "properties": {
                "type": {"enum": ["image"]},
                "content": {
                    "type": "string", 
                    "format": "url"
                }
            },
            "required": [
                "content",
                "type"
            ]
        },
        "video": {
            "properties": {
                "type": {"enum": ["video"]},
                "content": {
                    "type": "string", 
                    "format": "url"
                }
            },
            "required": [
                "content",
                "type"
            ]
        }
    }

}