cwacek/python-jsonschema-objects

Question on 'correct' use of schema

Closed this issue · 2 comments

Hi - just hoping to ask a 'how best to do things' type question if that's OK.

Background

I've got a python image analysis project that outputs a moderately complicated json file. That json is then read in by a node/typescript backend for storage/display.

Currently I use a set of hierarchical dataclasses to define and instantiate the output data. Once an analysis is complete, I serialize the parent dataclass to json to get the output.

We're looking to move to using defining the output format using json schemas, as this is easier to use by both the python code and the node/TS code.

To avoid having to duplicate the schema - first in a json schema and then separate in my python dataclass's - I'm looking to use python-jsonschema-objects to automatically generate the classes from the json schema, in place of using a whole bunch of nested dataclasses, as I do now.

At a first try, this all seems to work at a high level: define the hierarchical schema in json, then use json-schema-objects to create the classes from that.

My Question

I'm a complete newbie at json schema's, so I'm not sure if how I'm doing things is correct/appropriate/good practice.

My schema structure goal is:

  • a TestResult has multiple 'Series', and multiple output 'OutputFiles'.
  • a Series has multiple 'Images'
  • an Image has it's own multiple output
    'OutputFiles'

I want these as separate classes (ie separate schemas?) rather than one single definition, as in my python code I need to instantiate them separate, then combine them together later into the final overall TestResult.

I did read the project docs, where it says in this type of situation, put the sub-classes into a 'definitions' - but is there a problem doing it as I did?

Specific Questions

Right now (see below) I'm defining the classes underneath TestResult at a top-level, rather than as part of a definitions field. Is this something Bad that will cause the sky to fall later?

Is there a better way of doing things?

What works

This is the schema I'm using now, which works correctly (ie when I pass it into python-jsonschema-objects it returns separate classes:

schema:

{
        "$schema": "http://json-schema.org/draft-07/schema#",
        "id": "testResult",
        "title": "TestResult",
        "description": "Test result output from analysis",

        "TestOutputFile": {
                "type": "object",
                "title": "TestOutputFile",
                "properties": {
                        "file_type": { "type": "string" },
                        "location": { "type": "string" },
                        "label": { "type": "string" }
                }   
        },  

        "TestResultImage": {
                "type": "object",
                "title": "TestResultImage",
                "properties": {
                        "imageUID": { "type": "string" },
                        "image_type": { "type": "string" },
                        "origFile": { "type": "string" },
                        "outputFiles": {
                                "type": "array",
                                "items": { "$ref": "#/TestOutputFile" }
                        }   
                }   
        },  

        "TestResultSeries": {
                "type": "object",
                "title": "TestResultSeries",
                "properties": {
                        "seriesUID": { "type": "string" },
                        "description": { "type": "string" },
                        "images": {
                                "type": "array",
                                "items": { "$ref": "#/TestResultImage" }
                        }   
                }   
        },  



        "type": "object",
        "properties": {
                "test_id": { "type": "string" },
                "result": { "type": "string" },
                "series": {
                        "type": "array",
                        "items": { "$ref": "#/TestResultSeries" }
                },  
                "testOutputFiles": { 
                        "type": "array",
                        "items": { "$ref": "#/TestOutputFile" }
                }   
        }   
}

Stale issue message