2015-08-08 5 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"]} 
] 

}

Теперь необходимо либо поддерживать data_library, либо data_book в json. Однако, когда я пытаюсь проверить следующие данные:

{ 
    "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 Schema Проект 3, но oneOf и использование required в oneOf блока являются Проект 4.

Если преобразовать все ваши required ключевых слов отчетности Проект формата 4, то ваша схема будет проверяться с помощью валидатора Draft 4.

Я не знаю, что потребуется, чтобы все это работало в проекте 3. Я не уверен, что это возможно. Я думаю, что добавление ключевых слов, таких как oneOf, и новый формат required были добавлены в Draft 4, чтобы мы могли выразить такие случаи.

Настоящая версия проекта 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"] } 
    ] 
}