使用有效性的json-schema验证

时间:2013-04-18 09:50:32

标签: json validation jsonschema

我使用JSON模式和valdictory作为解析器/验证器获得以下模式验证错误。模式已使用jsonschema.net(使用相同的JSON数据)自动生成

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)

1 个答案:

答案 0 :(得分:2)

我不熟悉该库,但您似乎正在验证JSON文本,而不是数据本身 - 因此当架构说数据应该是对象时,验证器正在查看字符串。

如果不是data = json.dumps({...})而是data = {...},那该怎么办?