组织pythonic字典以进行JSON模式验证

时间:2019-03-19 14:11:59

标签: python json validation dictionary

场景::我正在尝试在python中创建JSON模式验证器。在这种情况下,我将构建一个字典,其中包含将用于验证的信息。

代码:

import json
import os
from pprint import pprint
from jsonschema import validate
from jsonschema import Draft4Validator

config_dir = r"D:\PROJECTS\etc"
config_file = r"schema.json"

schema = dict()

schema["$schema"] = "https://json-schema.org/schema#"
schema["title"] = "Index Schema"
schema["description"] = "Schema to describe calendar"
schema["type"] = "object"

# core
core = dict()
core["type"] = "object"
core["description"] = "Core settings for calendar"

core["frequency"] = {"type": "string",
                     "description": "Base frequency ",
                     "enum": ["monthly", "daily", "weekly"]}  #problem1

core["mark"] = {
                "type": "string",
                "description": "Mask defining the months",
                "if": {"core": {"frequency": {"enum": "monthly"}}}, #problem2
                "then": {
                         "pattern": "[01]{12}",
                         "minLength": 12,
                         "maxLength": 12
                         }

                }

core["ref_day"] = {"type": "string",
                   "description": "First day"
                   }

core["objective1"] = {"type": "object",
                     "description": "Information Calendar",
                     "properties": {"day": "string",
                                    "holiday": "string",
                                    "relative": {"unit": ["D", "M", ""],
                                                        "offset": "number"
                                                        }
                                    }
                    }

core["objective2"] = {"type": "object",
                       "description": "Information Calendar 2",
                       "properties":{
                               "day": {
                                       "type": "string",
                                       "value": "string"
                                       },
                               "holiday": "string",
                               "relative": {
                                       "unit": ["D", "M", ""],
                                       "offset": "number"
                                       }
                               }
                       }

core["required"] = ["mark", "ref_day", "frequency", "objective1", "objective2"]

schema["core"] = core

# required
schema["required"] = ["core"]

config_file_path = os.path.join(config_dir, config_file)

with open(config_file_path, 'w') as f:
    json.dump(schema, f, indent=4)

validation_result = Draft4Validator.check_schema(schema)
print(validation_result)

问题:在这里我遇到了3个问题: 问题1 :是否可以创建一个列表,其中要验证的JSON中的值必须在此列表中,否则将失败?

问题2:是否可以使用我在此代码段中编写的if函数?

问题3::为减少出错的可能性,是否可以通过以下方式创建字典(?):

core["holidays"]["properties"]["default"] = {
                                "type": "object",
                                "description": "",
                                "properties":{
                                        "ref",
                                        "type",
                                        "value"
                                        }
                                    }

core["holidays"]["properties"]["interim"] = {"interim": ""}
core["holidays"]["properties"]["selected"] = {"selection": {"ref": "default"}}
core["holidays"]["properties"]["exante"] = {"exante": {"ref": "default"}}
core["holidays"]["properties"]["expost"] = {"expost": {"ref": "default"}}


core["holidays"] = {"type": "object",
                    "description": "Holiday schedule",
                    "properties": {"default", "interim", "selected", "exante", "expost"}
                    }

主要问题::当我运行第一段代码时,我创建了字典,并且整个过程都没有出现错误,但是当我打印结果时,我得到了none ,据我了解,表明存在问题。我在这里做什么错了?

1 个答案:

答案 0 :(得分:1)

Draft4Validator.check_schema并不意味着返回任何内容。 (换句话说,它返回None。)

check_schema在出现问题时引发异常;如果没有,它将运行完成。

您可以在check_schema的代码中看到这一点:

    @classmethod
    def check_schema(cls, schema):
        for error in cls(cls.META_SCHEMA).iter_errors(schema):
            raise exceptions.SchemaError.create_from(error)

所以,这种行为是正确的。