I have multiple large json-schema
files. There are properties which are common for many schema files.
To remove redundancy between the common schema definitions, separated those in separated schema files and using $ref
to reference to the individual JSON schema.
My directory structure for all schema files are like
/
|- schemas
|- components
|- page_layout.json
|- page.json
The content of page.json
is
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"examples": [],
"items": {
"type": "object",
"required": [],
"additionalProperties": false,
"properties": {
"type": {
"type": "string",
"enum": [
"page_layout", "banner_images"
]
},
"data": {
"type": "object"
}
},
"allOf": [
{
"if": {
"properties": {
"type": {
"const": "page_layout"
}
}
},
"then": {
"properties": {
"data": {
"$ref": "components/page_layout.json"
}
}
}
}
]
}
}
and the content of page_layout.json
file is
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {},
"type": "object",
"required": [],
"additionalProperties": false,
"properties": {
"backgroundColor": {
"type": "string"
},
"templateName": {
"type": "string",
"enum": ["linear", "square", "circle"]
}
}
}
Combining both the files, the example JSON data to be validated is
[
{
"type": "page_layout"
"data": {
"backgroundColor": "#ff00ff",
"templateName": "square"
}
},
{
"type": "banner_images"
"data": {
// Content for banner_images
}
}
]
Content of data
will change depending on the type
value.
I'm using python jsonchema to validate the JSON data and the code for validating is
import json
import jsonschema
import os
from json import JSONDecodeError
class SchemaValidator:
def __init__(self, category: str):
self.category = category
self.data = ''
def _schema(self):
schema_file = self.category.replace(' ', '_')
schema_path = os.path.join(os.path.dirname(__file__), 'json_schema', 'categories', '{}.json'.format(schema_file))
try:
with open(schema_path, 'r') as file:
return json.loads(file.read())
except FileNotFoundError:
raise Exception('Schema definition missing')
except JSONDecodeError:
raise Exception('Schema definition invalid')
def validate(self, data):
self.data = data
try:
status = jsonschema.validate(json.loads(data), self._schema())
except jsonschema.SchemaError as e:
raise Exception(e)
return status
But this is giving error for the $ref
files as
unknown url type: 'components/page_layout.json'