2013-09-16 112 views
2

我使用了一個字段「時間戳」的類型爲「datetime.datetime」在models.py應用於Django項目之一,模式驗證錯誤

這裏是數據:

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

得到了您的問題: -

你給鍵入,而不是格式。

下面的例子是爲我工作: -

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))