架构验证错误

时间:2013-09-16 14:28:03

标签: django json jsonschema

我使用了一个字段'timestamp'是'datetime.datetime'类型,在其中一个django项目中的models.py中使用,

这是数据:

{"bedId": "5807a14", "description": "", 

"timestamp": "2013-09-16T15:40:16.383133", "encounterId": null, "patientId": null, "type": 

"end_of_shift_note", "triggeredBy": "abc"}

这是架构:

schema =
 {
    "type":"object",
    "$schema": "http://json-schema.org/draft-03/schema",
    "id": "http://jsonschema.net",
    "required":true,
    "properties":{
        "type": {
            "type":"string",
            "id": "http://jsonschema.net/bedNumber",
            "required":true
        },
        "encounterId": {
            "type":"string",
            "id": "http://jsonschema.net/encounterId",
            "required":true
        },
        "timestamp": { 
            "type" :["null","string","date-time"],
            'required' :false
        },
        "bedId": {
            "type":"string",
            "id": "http://jsonschema.net/cleaningStatus",
            "required":true
        },
        "patientId": {
            "type":["string","null"],
            "id": "http://jsonschema.net/facilityId",
            "required":true
        } ,
        "id": {
            "type":["string","null"],
            "id": "http://jsonschema.net/id",
            "required":false
        },
        "triggeredBy": {
            "type":["string","null"],
            "id": "http://jsonschema.net/lastCleanedTime",
            "required":false
        } 
    }
}

在进行架构验证时:https://pypi.python.org/pypi/json-schema-validator

def assertDataMatchesSchema(self, data, schema_file_name): 
    with open(os.path.join("hma/resource_jsonschema", schema_file_name)) as schema_file:
        try:
            schema = json.load(schema_file)
            validate(data, schema) 
        except Exception as e:
            print "Schema validation Error Message :",e.message 

在终端上发出错误:

Schema validation Error Message : Expecting property name: line 19 column 5 (char 401)

问题很简单:上面jsonschema中使用的时间戳的类型格式是什么? 请帮忙

1 个答案:

答案 0 :(得分:1)

得到了你的问题: -

您提供的是格式而非格式。

以下示例对我有用: -

data = {"timestamp": "2013-09-16T15:40:16.21211"}

schema ="""{
    "type":"object",
    "$schema": "http://json-schema.org/draft-03/schema",
    "id": "http://jsonschema.net",
    "required":true,
    "properties":{
        "timestamp": { 
            "format" :"date-time",
            "type":"string",
            "required" :false
        }
     }}"""

jsonschema.validate(data,json.loads(schema))