# JSON Schema $ref to file

**URL:** <https://forum.inductiveautomation.com/t/json-schema-ref-to-file/92073>\
**Category:** Ignition\
**Created:** [June 19, 2024, 11:34am UTC](https://forum.inductiveautomation.com/t/json-schema-ref-to-file/92073 "2024-06-19T11:34:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielps1818](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@danielps1818](https://forum.inductiveautomation.com/u/danielps1818)\
**Post date:** [June 19, 2024, 11:34am UTC](https://forum.inductiveautomation.com/t/json-schema-ref-to-file/92073/1 "2024-06-19T11:34:30Z")

</div>

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](https://stackoverflow.com/questions/78632474/how-to-reference-to-another-json-schema/78633408#78633408)

Thanks in advance!

_PS: Feel free to ask for more info_

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [June 19, 2024, 1:25pm UTC](https://forum.inductiveautomation.com/t/json-schema-ref-to-file/92073/2 "2024-06-19T13:25:44Z")

</div>

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

```python
$ref: ../path/schema.scm

```

---

<div class="post-metadata">

**Author:** ![danielps1818](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@danielps1818](https://forum.inductiveautomation.com/u/danielps1818)\
**Post date:** [June 19, 2024, 1:34pm UTC](https://forum.inductiveautomation.com/t/json-schema-ref-to-file/92073/3 "2024-06-19T13:34:34Z")

</div>

> [@danielps1818](#):
>
> [...] more information about the structure of the schemas and the messages: [jsonschema - How to reference to another JSON Schema? - Stack Overflow](https://stackoverflow.com/questions/78632474/how-to-reference-to-another-json-schema/78633408#78633408)
> 
> [...]

There is all the info and the file structure.

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [June 19, 2024, 1:38pm UTC](https://forum.inductiveautomation.com/t/json-schema-ref-to-file/92073/4 "2024-06-19T13:38:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danielps1818](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@danielps1818](https://forum.inductiveautomation.com/u/danielps1818)\
**Post date:** [June 19, 2024, 1:51pm UTC](https://forum.inductiveautomation.com/t/json-schema-ref-to-file/92073/5 "2024-06-19T13:51:20Z")

</div>

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

```python
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:

```python
{
    "$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.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 19, 2024, 1:55pm UTC](https://forum.inductiveautomation.com/t/json-schema-ref-to-file/92073/6 "2024-06-19T13:55:59Z")

</div>

> [@danielps1818](#):
>
> `schema_file = json.load(f)`

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

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [June 19, 2024, 2:51pm UTC](https://forum.inductiveautomation.com/t/json-schema-ref-to-file/92073/7 "2024-06-19T14:51:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 19, 2024, 5:36pm UTC](https://forum.inductiveautomation.com/t/json-schema-ref-to-file/92073/8 "2024-06-19T17:36:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danielps1818](https://avatars.discourse-cdn.com/v4/letter/d/ecae2f/32.png) [@danielps1818](https://forum.inductiveautomation.com/u/danielps1818)\
**Post date:** [June 20, 2024, 8:44am UTC](https://forum.inductiveautomation.com/t/json-schema-ref-to-file/92073/9 "2024-06-20T08:44:28Z")

</div>

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 `$re`f.

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