JSON Schema $ref to file

Hi!

I'm currently trying to create schemas that have a reference to another file.

Something like:

$ref: file:///C:/path/schema.scm

But, it doesn't work.
I have tried a lot of things and I got to the conclusion that it has to do with the class com.inductiveautomation.ignition.common.jsonschema.

Could someone check for me or explain to me how could I check the code to verify that is not possible (or maybe explain how to do it correctly)?

Here is a link to another forum where I asked the same question but I had given more information about the structure of the schemas and the messages: jsonschema - How to reference to another JSON Schema? - Stack Overflow

Thanks in advance!

PS: Feel free to ask for more info

You should use relative paths, but without a file structure i cant really tell more

$ref: ../path/schema.scm

There is all the info and the file structure.

Where are you doing this, are you making a module?
Where are these files located? are they in the same folder or not?

I'm using a scrip to validate the schemas. This is the code that I use:

def validate_schema(message, schema_path):

	try:
		# We encapsulate the code in a try-catch to always
		# have a controlled answer

		message_json = TypeUtilities.pyToGson(message)

		# We load the schema from the file
		with open(schema_path) as f:
			schema_file = json.load(f)

		# We create the schema
		schema = JsonSchema(TypeUtilities.pyToGson(schema_file))

		# Validation of the message with the schema
		errors = JsonValidator.validate(schema, message_json)

		if (len(errors) != 0):
			return_message = (
				"Check that the format follows the schema ("
				+ schema_path.split(os.sep)[-1]
				+ "). "
				+ str(errors)
			)
			return False, return_message

		# Everything goes right
		result = True
		return_message = ""

	except:
		filename = str(sys._getframe().f_code.co_filename)
		func = str(sys._getframe().f_code.co_name),
		line = str(sys._getframe().f_lineno)
		exception_message = get_exception_message(filename, func, line)
		system.util.getLogger("SGP.Core").fatal(exception_message)
		result = False
		return_message = exception_message

	return result, return_message

My file schema structure is this one:

{
    "$schema":"https://json-schema.org/draft/2020-12/schema",
    "$id":"wms_mission.scm",
    "type":"object",
    "properties":{
        "header":{
            "$ref":"file:///C:/schemas/header.scm"
        },
        "body":{
            "additionalProperties":false,
            "type":"object",
            "properties":{
                "mission_id":{
                    "type":"string",
                    "pattern":"^(MISSION)([0-9]{17})$"
                },
                "command":{
                    "type":"string",
                    "enum":[
                        "CREATE",
            "CANCEL"
                    ]
                }
            },
            "required":[
                "mission_id",
                "command"
            ]
        }
    },
    "required":[
        "header",
        "body"
    ],
    "additionalProperties":false
}

All the schemas are in the same folder.

My problem is that the library of ignition doesn't check for errors in the reference. Hope I explain myself now, I didn't understand you at first, sorry.

This where such references would have to be interpreted. Is python's json module capable of doing that?

Yeah that doesnt seem like its gonna work. no way the validator can check references accross files that are not provided in the function.

there has to be a way to add in all the other schemas

I mean, by far the easiest solution here is going to be to merge your schemas together or load them separately or something.
If you really insist on fetching them dynamically, it's at least supposed to work, according to the code...
The json primitive value "file:///C:/schemas/header.scm" will be passed to Java's URL class as an initial constructor value, and then JsonSchemaFactory calls openStream() on that URL to attempt to find the file and parse it. The contents are read in as UTF-8 encoded text.
The com.inductiveautomation.ignition.common.jsonschema.JsonSchemaFactory logger may have more useful information for you.

Thank you!

I don't have a lot of time in this project to test more things.

Also I thought it would be easier...

For now I simply created all the schemas with the header. I won't use $ref.

I'll come here and check it at another time when I have time to do it.
Thanks a lot to everyone that answered!

1 Like