我无法根据模式验证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
我做错了什么?
您已经声明您正在使用JSON Schema Draft 3,但是oneOf
和required
在oneOf
块中的使用是Draft 4。
如果您将所有的required
关键字语句转换为Draft 4格式,那么您的模式将使用Draft 4验证器进行验证。
我不知道怎样才能使草案3中的所有东西都工作。我甚至不确定这是否可能。我认为在Draft 4中添加了oneOf
这样的关键字和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"] }
]
}