2013-04-18 137 views
0

我使用JSON模式和valdictory作爲解析器/驗證程序獲取以下模式驗證錯誤。該架構已使用jsonschema.net(使用相同的JSON數據)自動生成使用驗證的json-schema驗證

validictory.validator.FieldValidationError: Value '{"bp": [{"category": "bp", 
"created": "2013-03-08T09:14:48.148000", "value": 147.0, "day": "2013-03-11T00:00:00", 
"value2": 43.0, "id": "dc049c0e-d19a-4e3e-93ea-66438a239712", "unit": "mmHg"}]}' 
for field '_data' is not of type object 

代碼:

import json 
import validictory 

data = json.dumps({'bp': [{'category': 'bp', 
     'created': '2013-03-08T09:14:48.148000', 
     'day': '2013-03-11T00:00:00', 
     'id': 'dc049c0e-d19a-4e3e-93ea-66438a239712', 
     'unit': 'mmHg', 
     'value': 147.0, 
     'value2': 43.0}]}) 


schema = { 
    "type":"object", 
    "properties":{ 
     "bp": { 
      "type":"array", 
      "required":False, 
      "items": 
       { 
        "type":"object", 
        "required":False, 
        "properties":{ 
         "category": { 
          "type":"string", 
          "default": "bp", 
          "required":False 
         }, 
         "created": { 
          "type":"string", 
          "default": "2013-03-08T09:14:48.148000", 
          "required":False 
         }, 
         "day": { 
          "type":"string", 
          "default": "2013-03-11T00:00:00", 
          "required":False 
         }, 
         "id": { 
          "type":"string", 
          "default": "dc049c0e-d19a-4e3e-93ea-66438a239712", 
          "required":False 
         }, 
         "unit": { 
          "type":"string", 
          "default": "mmHg", 
          "required":False 
         }, 
         "value2": { 
          "type":"number", 
          "default":43, 
          "required":False 
         }, 
         "value": { 
          "type":"number", 
          "default":147, 
          "required":False 
         } 
        } 
       } 


     } 
    } 
} 

validictory.validate(data,schema) 

回答

2

我不熟悉的圖書館,但你似乎驗證JSON文本,而不是數據本身 - 所以驗證者正在查看一個字符串,當架構說數據應該是一個對象時。

如果不是data = json.dumps({...}),你剛纔有data = {...}