1

我已经使用 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))

这是我的jsonschema:

 { "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'}

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

如果我在那里使用“类型” 字段,也会出现同样的问题。

4

1 回答 1

4

描述是 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

于 2013-09-11T11:37:31.370 回答