Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: Updates to jsonschema dereference script #235

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:
hooks:
- id: jsonschema-dereference
name: jsonschema-dereference
entry: python .pre-commit/jsonschema-dereference.py
entry: python .pre-commit/jsonschema_dereference.py
additional_dependencies: [jsonref]
language: python
types_or: [yaml, json]
34 changes: 0 additions & 34 deletions .pre-commit/jsonschema-dereference.py

This file was deleted.

54 changes: 54 additions & 0 deletions .pre-commit/jsonschema_dereference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
This Python module:
- Searches for JSON Schema templates with the name values.schema.tmpl.json
- Dereferences any $refs contained in those files
- Outputs the new Schema to a values.schema.json file in the same directory
"""

import sys
import json
from typing import List, Dict, Any
from pathlib import Path

# External library dependency
# Install with 'pip install jsonref'
import jsonref

# File to write the dereferenced JSON Schema to
JSONSCHEMA_NAME = "values.schema.json"
# File that contains the JSON Schema that needs dereferencing
JSONSCHEMA_TEMPLATE_NAME = "values.schema.tmpl.json"

def load_template_schema(schema_dir: Path) -> Dict[str, Any]:
"""Load the schema template values.schema.tmpl.json"""
with open(schema_dir / JSONSCHEMA_TEMPLATE_NAME, "r", encoding="utf-8") as f:
return json.loads(f.read())

def save(schema_dir: Path, schema_data: Any):
"""Save the dereferenced schema to values.schema.json"""
with open(schema_dir / JSONSCHEMA_NAME, "w", encoding="utf-8") as f:
json.dump(schema_data, f, indent=4, sort_keys=True)

if __name__ == '__main__':
# Search for all values.schema.tmpl.json files
schema_templates = [p.parent for p in Path(".").rglob(JSONSCHEMA_TEMPLATE_NAME)]

# Create a list to hold any exceptions
errors: List[BaseException] = []
# Iterate over the List of found schema templates
for schema_template in schema_templates:
try:
# Load the schema into a variable as JSON
st = load_template_schema(schema_template)
# Dereference all of the $refs
s = jsonref.replace_refs(st)
# Save the dereferenced JSON
save(schema_template, s)
except BaseException as e:
# Print any errors to the screen
print(f"Could not process schema for '{schema_template}': {e}")
# Append any exceptions to the errors List
errors.append(e)
if errors:
# Exit with status 1 if any exceptions were thrown
sys.exit(1)