2015-08-08 48 views
1

我无法验证对一个模式的JSON:PHP的Json验证了Oneof给出了错误的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 

它是什么,我做错了什么?

回答

0

你已经宣布你正在使用JSON模式草案3,但oneOfrequiredoneOf块内的使用率是草案4.

如果转换所有required关键字声明的草稿4格式,然后您的模式将使用草稿4验证器进行验证。

我不知道要在草案3中完成整个工作需要做什么。我不确定这甚至是可能的。我认为在草案4中增加了像oneOf这样的关键字和新格式的required,这样我们就可以表达这样的情况。

这是草案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"] } 
    ] 
} 
相关问题