2013-04-29 80 views
2

以下数据+ JSON模式(使用具有相同数据的JSON模式生成器生成)应该正确验证。但是,我在这里收到一个valdation错误。JSON模式不针对有效数据进行验证(validictory)

验证基于验证模块。

import json 
import validictory 
import jsonschema 

data = [{u'text': 
     u'<h1>The quick brown fox</h1>', 
     u'title': u'hello world', 
     u'location': u'Berlin', 
     u'created': u'2013-03-12T12:13:14'}] 

schema = { 
    "$schema": "http://json-schema.org/draft-03/schema", 
    "id": "http://jsonschema.net", 
    "required": False, 
    "type": "object" , 
    "properties": { 
     "0" : { 
      "id": "http://jsonschema.net/0", 
      "required": False, 
      "type": "object" , 
      "properties": { 
       "created" : { 
        "id": "http://jsonschema.net/0/created", 
        "required": False, 
        "type": "string" 
       }, 
       "location" : { 
        "id": "http://jsonschema.net/0/location", 
        "required": False, 
        "type": "string" 
       }, 
       "text" : { 
        "id": "http://jsonschema.net/0/text", 
        "required": False, 
        "type": "string" 
       }, 
       "title" : { 
        "id": "http://jsonschema.net/0/title", 
        "required": False, 
        "type": "string" 
       } 
      } 
     } 
    } 
} 
print validictory.validate(data,schema) 

validictory.validator.FieldValidationError: Value [{u'text': u'<h1>The quick brown fox</h1>', u'created': u'2013-03-12T12:13:14', u'location': u'Berlin', u'title': u'hello world'}] for field '_data' is not of type object 

回答

0

您的验证错误告诉你问题是什么...

它说Value [{u'text': u'<h1>The quick brown fox</h1>', u'created': u'2013-03-12T12:13:14', u'location': u'Berlin', u'title': u'hello world'}] for field '_data' is not of type object, 它不是,它是一个list。您需要验证其内容i.e. data[0],而不是整个列表。

此外,它看起来像你在jsonschema.net之前生成了这个模式,修正了他们如何使用id,这在规范下是不正确的,所以你可能想要删除那些id属性。

+0

正如我所写:架构已经使用_same_ data – validator 2013-04-29 15:55:19

+0

自动生成。然后它生成不正确:)。 – Julian 2013-04-29 17:57:51