-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_schema_auditor.py
executable file
·92 lines (73 loc) · 2.98 KB
/
json_schema_auditor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import json
import os
import click
def audit_schema(schema, keywords_set=None, types_set=None):
"""
Audits a JSON Schema to identify the keywords and types used.
Args:
schema (dict): The JSON Schema to audit.
keywords_set (set): Accumulated set of keywords used in the schema.
types_set (set): Accumulated set of types used in the schema.
Returns:
tuple: Two sets containing the keywords and types found in the schema.
"""
if keywords_set is None:
keywords_set = set()
if types_set is None:
types_set = set()
if isinstance(schema, dict):
for keyword in schema:
keywords_set.add(keyword)
if keyword in ["allOf", "anyOf", "oneOf"]:
for subschema in schema["keyword"]:
audit_schema(subschema, keywords_set, types_set)
if keyword in ["properties", "dependentSchemas"]:
for prop in schema[keyword].values():
for key, value in prop.items():
# Recursively audit nested properties
if key == "properties":
for nested_prop in value.values():
audit_schema(nested_prop, keywords_set, types_set)
else:
keywords_set.add(key)
if key == "type" and isinstance(value, str):
types_set.add(value)
elif key == "type" and isinstance(value, list):
types_set.update(value)
if keyword in ["definitions", "$defs"]:
for defn in schema[keyword].values():
audit_schema(defn, keywords_set, types_set)
if keyword in ["not", "if", "then", "else"]:
audit_schema(schema[keyword])
if keyword == "type":
types = schema["type"]
if isinstance(types, str):
types_set.add(types)
elif isinstance(types, list):
types_set.update(types)
return keywords_set, types_set
@click.command()
@click.argument("file_path", type=click.Path(exists=True, readable=True))
def main(file_path):
"""
CLI to audit a JSON Schema.
FILE_PATH is the path to the JSON Schema file to audit.
"""
try:
with open(file_path, "r", encoding="utf-8") as file:
json_schema = json.load(file)
except json.JSONDecodeError as e:
click.echo(f"Invalid JSON: {e}", err=True)
return
except Exception as e:
click.echo(f"Error reading the file: {e}", err=True)
return
# Audit the schema
keywords, types = audit_schema(json_schema)
# Report the results
click.echo("\nKeywords used in the schema:")
click.echo(", ".join(sorted(keywords)))
click.echo("\nTypes used in the schema:")
click.echo(", ".join(sorted(types)))
if __name__ == "__main__":
main()