cwacek/python-jsonschema-objects

Can't load example scheme

kacperpaczos opened this issue · 2 comments

Describe the bug
I use example scheme and python code, and i can't make it work, because:
Traceback (most recent call last):
File "/home/kacper/Dokumenty/Bennet/models/classes/schema.py", line 7, in
builder = pjs.ObjectBuilder(schema['Example Schema'])
KeyError: 'Example Schema'

Example Schema and code

{
    "title": "Example Schema",
    "type": "object",
    "properties": {
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        },
        "age": {
            "description": "Age in years",
            "type": "integer",
            "minimum": 0
        },
        "dogs": {
            "type": "array",
            "items": {"type": "string"},
            "maxItems": 4
        },
        "address": {
            "type": "object",
            "properties": {
                "street": {"type": "string"},
                "city": {"type": "string"},
                "state": {"type": "string"}
                },
            "required":["street", "city"]
            },
        "gender": {
            "type": "string",
            "enum": ["male", "female"]
        },
        "deceased": {
            "enum": ["yes", "no", 1, 0, "true", "false"]
            }
    },
    "required": ["firstName", "lastName"]
}
import json
import python_jsonschema_objects as pjs

with open('models/classes/sss.json', 'r') as file:
    schema = json.load(file)

builder = pjs.ObjectBuilder(schema['Example Schema'])
ns = builder.build_classes()
Person = ns.ExampleSchema
james = Person(firstName="James", lastName="Bond")
print(james)

Expected behavior
It should be work out of the box.

the 'title' field in your JSON schema is set to "Example Schema," but you're trying to access it using schema['Example Schema'], which is not a valid key in the dictionary.

import json
import python_jsonschema_objects as pjs

with open('models/classes/sss.json', 'r') as file:
    schema = json.load(file)

# Use the correct key to access the schema
builder = pjs.ObjectBuilder(schema['title'])
ns = builder.build_classes()
Person = ns.ExampleSchema
james = Person(firstName="James", lastName="Bond")
print(james)

Thank you, how i can load more complex schema?
Like this:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/anvil.schema.json",
  "title": "AnvilBase2",
  "description": "Anvil is a class for management of projects for Bennet",
  "type": "object",
  "properties": {
    "projects": {
      "type": "object",
      "properties": {
        "name": {"type": "string", "default": "None"},
        "version": {
          "type": "string",
          "pattern": "^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$",
          "default": "1.0.0"
        },
        "description": {"type": "string", "default": "None"},
        "main": {
          "type": "string",
          "minLength": 1,
          "default": "main.js"
        },
        "scripts": {
          "type": "object",
          "additionalProperties": {
            "type": "object",
            "properties": {
              "name": {"type": "string", "default": "None"},
              "command": {"type": "string", "default": "None"}
            },
            "required": ["name", "command"]
          }
        },
        "keywords": {"type": "string", "default": "None"},
        "author": {"type": "string", "default": "None"},
        "license": {
          "type": "string",
          "minLength": 1,
          "default": "GNU GPLv3"
        },
        "dependencies": {"type": "string", "default": "None"},
        "devDependencies": {"type": "string", "default": "None"}
      },
      "required": ["name", "version","main", "license"]
    }
  }
}

My code don't work very well, i don't have nothing form inside of "project":

import json
import python_jsonschema_objects as pjs
import os

def load_json_recursively(path):
    if isinstance(path, dict):
        data = path
    else:
        with open(path, 'r') as file:
            data = json.load(file)
    for key, value in data.items():
        if isinstance(value, dict):
            data[key] = load_json_recursively(value)
    return data

schema = load_json_recursively('models/schemes/anvil_schema.json')

builder = pjs.ObjectBuilder(schema)
ns = builder.build_classes()
AnvilBase2 = ns.Anvilbase2
print(AnvilBase2)
# project = AnvilBase2.projects(name="Project1", version="1.0.0", main="main.js", license="GNU GPLv3")
# print(project)