我使用了jsonschme验证器来验证我对json文件的json输出。
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))
这是我的jsonschemas:
{ "code": {"type":["string","null"]},
"codingMethod": {"type":["string","null"]},
"priority":{"type":["string","null"]},
"status":{"type":["string","null"]} ,
"description" : {"type" : "string"}
}
终端输出:
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”字段,同样的问题。
答案 0 :(得分:4)
description是json-schema使用的关键。所以你的架构应该像: -
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