【问题标题】:JSON schema does not validate against valid data (validictory)JSON 模式不针对有效数据进行验证(有效)
【发布时间】:2013-04-29 12:24:42
【问题描述】:

以下数据 + JSON 模式(使用具有相同数据的 JSON 模式生成器生成)应该可以正确验证。但是,我在这里收到验证错误。

验证基于有效性模块。

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

【问题讨论】:

    标签: python json jsonschema


    【解决方案1】:

    您的验证错误告诉您问题所在...

    上面写着Value [{u'text': u'&lt;h1&gt;The quick brown fox&lt;/h1&gt;', 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 属性。

    【讨论】:

    • 正如我所写:架构已使用 same 数据自动生成
    猜你喜欢
    • 2023-03-08
    • 2019-04-21
    • 2023-02-17
    • 2017-12-18
    • 2014-07-16
    • 2016-06-20
    • 2021-06-02
    • 2017-01-09
    • 2019-07-24
    相关资源
    最近更新 更多