【问题标题】:Organizing pythonic dictionaries for a JSON schema validation为 JSON 模式验证组织 pythonic 字典
【发布时间】:2019-03-19 14:11:58
【问题描述】:

场景:我正在尝试在 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:是否可以像我在这个sn-p中写的那样使用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,它,据我了解,表示有问题。我在这里做错了什么?

【问题讨论】:

  • check_schema 不返回任何内容;如果架构无效,它只会引发SchemaError - 即您的架构是有效的。
  • @meowgoesthedog 对我来说它正在返回 None
  • Python 函数默认返回None

标签: python json validation dictionary


【解决方案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)

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

【讨论】:

    猜你喜欢
    • 2010-12-20
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2011-06-08
    • 2016-10-05
    • 2020-11-06
    相关资源
    最近更新 更多