Organizing pythonic dictionaries for a JSON schema validation

2024/11/15 22:09:19

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?

Answer

Draft4Validator.check_schema is not meant to return anything. (In other words it returns None.)

check_schema raises an exception if there is a problem; if not, it runs to completion.

You can see this in the code for check_schema:

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

So, this behavior is correct.

https://en.xdnf.cn/q/119241.html

Related Q&A

Scraping a specific website with a search box and javascripts in Python

On the website https://sray.arabesque.com/dashboard there is a search box "input" in html. I want to enter a company name in the search box, choose the first suggestion for that name in the d…

Uppercasing letters after ., ! and ? signs in Python

I have been searching Stack Overflow but cannot find the proper code for correcting e.g."hello! are you tired? no, not at all!"Into:"Hello! Are you tired? No, not at all!"

Why does list() function is not letting me change the list [duplicate]

This question already has answers here:How do I clone a list so that it doesnt change unexpectedly after assignment?(24 answers)Python pass by value with nested lists?(1 answer)Closed 2 years ago.If …

How can I explicitly see what self does in python?

Ive read somewhere that the use of ‘self’ in Python converts myobject.method (arg1, arg2) into MyClass.method(myobject, arg1, arg2). Does anyone know how I can prove this? Is it only possible if I…

Recieve global variable (Cython)

I am using Cython in jupyter notebook. As I know, Cython compiles def functions.But when I want to call function with global variable it doesnt see it. Are there any method to call function with variab…

Counting elements in specified column of a .csv file

I am programming in Python I want to count how many times each word appears in a column. Coulmn 4 of my .csv file contains cca. 7 different words and need to know how many times each one appears. Eg. t…

Why does genexp(generator expression) is called genexp? not iterexp?

A generator is a special kind of iterator, and it has some methods that an normal iterator doesnt have such as send(), close()... etc. One can get a generator by using a genexp like below:g=(i for i in…

why does no picture show

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg if __name__ == "__main__":fig1 = ...print("start plotting")canvas = FigureCanvasQTAgg(fig1)canvas.draw()canvas.show(…

How Normalize Data Mining Min Max from Mysql in Python

This is example of my data in mysql, I use lib flashext.mysql and python 3RT NK NB SU SK P TNI IK IB TARGET 84876 902 1192 2098 3623 169 39 133 1063 94095 79194 …

complex json file to csv in python

I need to convert a complex json file to csv using python, I tried a lot of codes without success, I came here for help,I updated the question, the JSON file is about a million,I need to convert them t…