38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
import json
|
|
import sys
|
|
from jsonschema import validate, ValidationError
|
|
|
|
def validate_json(schema_file, json_file):
|
|
try:
|
|
# Wczytaj schemat JSON
|
|
with open(schema_file, 'r') as f:
|
|
schema = json.load(f)
|
|
|
|
# Wczytaj dane JSON
|
|
with open(json_file, 'r') as f:
|
|
data = json.load(f)
|
|
|
|
# Sprawdź, czy dane JSON są zgodne ze schematem
|
|
validate(instance=data, schema=schema)
|
|
print("JSON jest zgodny z schematem")
|
|
|
|
except ValidationError as e:
|
|
print("JSON nie jest zgodny z schematem:", e.message)
|
|
except json.JSONDecodeError as e:
|
|
print("Błąd wczytywania JSON:", e)
|
|
except FileNotFoundError as e:
|
|
print("Plik nie został znaleziony:", e)
|
|
except Exception as e:
|
|
print("Wystąpił błąd:", e)
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 3:
|
|
print("Użycie: python validate_json.py <schemat.json> <plik.json>")
|
|
sys.exit(1)
|
|
|
|
schema_file = sys.argv[1]
|
|
json_file = sys.argv[2]
|
|
|
|
validate_json(schema_file, json_file)
|
|
|