2

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

这是数据:

{"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 中使用的时间戳的类型格式是什么?请帮忙

4

1 回答 1

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))
于 2013-09-17T06:15:47.687 回答