首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jsonschema验证失败

Jsonschema验证失败
EN

Stack Overflow用户
提问于 2013-09-11 11:09:47
回答 1查看 2.5K关注 0票数 1

我使用了jsonschme验证器来验证针对json文件的Json输出。

代码语言:javascript
复制
from jsonschema import validate    #https://pypi.python.org/pypi/jsonschema

def assertDataMatchesSchema(self, data, schema_file_name): 
        with open(os.path.join("mha/resource_jsonschema", schema_file_name)) as schema_file:
            validate(data, json.load(schema_file))

以下是我的jsonschema:

代码语言:javascript
复制
 { "code": {"type":["string","null"]},
       "codingMethod": {"type":["string","null"]}, 
       "priority":{"type":["string","null"]},
       "status":{"type":["string","null"]} ,
       "description" : {"type" : "string"} 


     }

终端输出:

代码语言:javascript
复制
SchemaError: {u'type': u'string'} is not of type u'string'

Failed validating u'type' in schema[u'properties'][u'description']:
    {u'type': u'string'}

On instance[u'description']:
    {u'type': u'string'}

问题:如果我从上面的文件中删除了 description 字段,或者更改为其他名称,那么它可以工作,但是我需要描述字段(必需的nne)。有解决这个问题的办法吗?

如果我在那里使用"type"字段,同样的问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-11 11:37:31

描述是json模式使用的关键。所以你的模式应该是:-

代码语言:javascript
复制
schema = {
    "type": "object",
    "properties": {
             "code": {"type":["string","null"]},
       "codingMethod": {"type":["string","null"]}, 
       "priority":{"type":["string","null"]},
       "status":{"type":["string","null"]} ,
       "description" : {"type" : "string"} 
    }
}

data = {"description" : "nowtry"}
validate(data, schema)

对我来说很管用..。

您可以在这里看到您的模式应该如何,http://www.w3resource.com/JSON/JSON-Schema.php

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18739558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档