Scenario: I am trying to create a JSON schema validator in python. In this case, I am building a dictionary which contain the information that will be used for the validation.
Code:
import json
import os
from pprint import pprint
from jsonschema import validate
from jsonschema import Draft4Validatorconfig_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"]} #problem1core["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)
Issue: Here I run into 3 problems: Problem1: Is it possible to create a list where the value in the JSON to be validated has to be in this list, otherwise it fails?
Problem2: Is it possible to use an if function like I wrote in this snippet?
Problem3: In an effort to decrease the possibility of mistakes, is it possible to create a dictionary in the following manner(?):
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"}}
Main Question: When I run the first piece of code, I create the dictionary and the whole thing runs without trowing errors, but when I print the result, I get a none
, which, as far as I understand, indicates there is something wrong. What am I doing wrong here?