1

我无法针对模式验证 json:

架构如下:

{
"type":"object",
"$schema": "http://json-schema.org/draft-03/schema",
"required":true,
"properties":{
    "tagId": {
            "type":"string",
            "id": "tagId",
            "required":true
    },
    "data_library": {
        "type":"object",
        "id": "data_library",
        "properties":{
            "info": {
                "type":"object",
                "id": "info_library",
                "properties":{
                    "name": {
                        "type":"string",
                        "id": "name",
                        "required":true
                    },
                    "location": {
                        "type":"string",
                        "id": "location",
                        "required":true
                    },
                    "description": {
                        "type":"string",
                        "id": "description",
                        "required":true
                    },
                    "starttime": {
                        "type":"string",
                        "id": "starttime",
                        "required":true
                    },
                    "endtime": {
                        "type":"string",
                        "id": "endtime",
                        "required":true
                    },
                    "contact": {
                        "type":"string",
                        "id": "contact",
                        "required":true
                    }                           
                }
            },
            "videos": {
                "type":"array",
                "minitems": "0",
                "id": "videos",
                "items":
                {
                    "type":"string",
                    "required":true
                }
            },
            "images": {
                "type":"array",
                "minitems": "0",
                "id": "images",
                "items":
                {
                    "type":"string",
                    "required":true
                }
            }
        },
        "additionalProperties": false,
        "minProperties": 1
    },
    "data_book": {
        "type":"object",
        "id": "data_book",
        "properties":{
            "info": {
                "type":"object",
                "id": "info_book",
                "properties":{
                    "name": {
                        "type":"string",
                        "id": "name",
                        "required":true
                    },
                    "genre": {
                        "type":"string",
                        "id": "genre",
                        "required":true
                    },
                    "description": {
                        "type":"string",
                        "id": "description",
                        "required":true
                    },
                    "agegroup": {
                        "type":"string",
                        "id": "agegroup",
                        "required":true
                    },
                    "author": {
                        "type":"string",
                        "id": "author",
                        "required":true
                    },
                    "publisher": {
                        "type":"string",
                        "id": "publisher",
                        "required":true
                    }       
                }
            },
            "videos": {
                "type":"array",
                "minitems": "0",
                "id": "videos",
                "items":
                {
                    "type":"string",
                    "required":true
                }
            },
            "images": {
                "type":"array",
                "minitems": "0",
                "id": "images",
                "items":
                {
                    "type":"string",
                    "required":true
                }
            }
        },
        "additionalProperties": false,
        "minProperties": 1
    }
},
"oneOf": [
    {"required": ["data_library"]},
    {"required": ["data_book"]}
]

}

现在的要求是在 json 中支持 data_library 或 data_book。但是,当我尝试验证以下数据时:

{
    "tagId" : "DFGDASERTGSDG",
    "data_book" : {
            "info" :        {
                    "name" : "Pagdandi",
                    "location" : "Kaccha",
                    "description" : "..",
                    "starttime" : "..",
                    "endtime" : "..",
                    "contact" : ".."
            },
            "videos" :      [
                    "https://..."
            ],
            "images" :      [
                    "https://..."
            ]
    }

}

我收到以下错误:

Property: data_book.info.genre Msg:Is missing and it is requiredProperty: data_book.info.agegroup Msg:Is missing and it is requiredProperty: data_book.info.author Msg:Is missing and it is requiredProperty: data_book.info.publisher Msg:Is missing and it is required

我做错了什么?

4

1 回答 1

0

您已声明您正在使用 JSON Schema Draft 3,但块内oneOf的用法是 Draft 4。requiredoneOf

如果您将所有required关键字语句转换为 Draft 4 格式,那么您的架构将使用 Draft 4 验证器进行验证。

我不知道要在 Draft 3 中完成整个工作需要什么。我什至不确定这是否可能。我认为在 Draft 4 中添加了关键字 likeoneOf和新格式 of ,因此我们可以表达这样的情况。required

这是适用的 Draft 4 版本。

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "object",
    "properties": {
        "tagId": { "type": "string" },
        "data_library": {
            "type": "object",
            "properties": {
                "info": {
                    "type": "object",
                    "properties": {
                        "name": { "type": "string" },
                        "location": { "type": "string" },
                        "description": { "type": "string" },
                        "starttime": { "type": "string" },
                        "endtime": { "type": "string" },
                        "contact": { "type": "string" }
                    },
                    "required": ["name", "location", "description", "starttime", "endtime", "contact"]
                },
                "videos": {
                    "type": "array",
                    "items": { "type": "string" }
                },
                "images": {
                    "type": "array",
                    "items": { "type": "string" }
                }
            },
            "additionalProperties": false,
            "minProperties": 1
        },
        "data_book": {
            "type": "object",
            "properties": {
                "info": {
                    "type": "object",
                    "properties": {
                        "name": { "type": "string" },
                        "genre": { "type": "string" },
                        "description": { "type": "string" },
                        "agegroup": { "type": "string" },
                        "author": { "type": "string" },
                        "publisher": { "type": "string" }
                    },
                    "required": ["name", "genre", "description", "agegroup", "author", "publisher"]
                },
                "videos": {
                    "type": "array",
                    "items": { "type": "string" }
                },
                "images": {
                    "type": "array",
                    "items": { "type": "string" }
                }
            },
            "additionalProperties": false,
            "minProperties": 1
        }
    },
    "required": ["tagId"],
    "anyOf": [
        { "required": ["data_library"] },
        { "required": ["data_book"] }
    ]
}
于 2015-08-09T07:53:21.540 回答