Skip to content

Commit

Permalink
#2300: scripts: clean up JSON schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
cz4rs authored and cwschilly committed Sep 20, 2024
1 parent f23f455 commit 1f0e3c7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
33 changes: 16 additions & 17 deletions scripts/JSON_data_files_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from collections import Counter
from collections.abc import Iterable
import json
import logging

import brotli
from schema import And, Optional, Schema
Expand Down Expand Up @@ -328,7 +329,7 @@ def __check_if_dir_exists(dir_path: str) -> bool:

@staticmethod
def __get_files_for_validation(dir_path: str, file_prefix: str, file_suffix: str) -> list:
""" Check for existence of a given directory. Returns True when file exists. """
""" Get a sorted list of files from directory. """
list_of_files = os.listdir(dir_path)

if not list_of_files:
Expand All @@ -354,32 +355,30 @@ def __get_files_for_validation(dir_path: str, file_prefix: str, file_suffix: str
@staticmethod
def __validate_file(file_path):
""" Validates the file against the schema. """
print(f"Validating file: {file_path}")
with open(file_path, "rb") as compr_json_file:
compr_bytes = compr_json_file.read()
try:
decompr_bytes = brotli.decompress(compr_bytes)
decompressed_dict = json.loads(decompr_bytes.decode("utf-8"))
except brotli.error:
decompressed_dict = json.loads(compr_bytes.decode("utf-8"))
logging.info(f"Validating file: {file_path}")
with open(file_path, "rb") as json_file:
content = json_file.read()
if file_path.endswith('.br'):
content = brotli.decompress(content)
json_data = json.loads(content.decode("utf-8"))

# Extracting type from JSON data
schema_type = None
if decompressed_dict.get("metadata") is not None:
schema_type = decompressed_dict.get("metadata").get("type")
if json_data.get("metadata") is not None:
schema_type = json_data.get("metadata").get("type")
else:
if decompressed_dict.get("type") is not None:
schema_type = decompressed_dict.get("type")
if json_data.get("type") is not None:
schema_type = json_data.get("type")

if schema_type is not None:
# Validate schema
if SchemaValidator(schema_type=schema_type).is_valid(schema_to_validate=decompressed_dict):
print(f"Valid JSON schema in {file_path}")
if SchemaValidator(schema_type=schema_type).is_valid(schema_to_validate=json_data):
logging.info(f"Valid JSON schema in {file_path}")
else:
print(f"Invalid JSON schema in {file_path}")
SchemaValidator(schema_type=schema_type).validate(schema_to_validate=decompressed_dict)
SchemaValidator(schema_type=schema_type).validate(schema_to_validate=json_data)
else:
print(f"Schema type not found in file: {file_path}. \nPassing by default when schema type not found.")
logging.warning(f"Schema type not found in file: {file_path}. \nPassing by default when schema type not found.")

def main(self):
if self.__file_path is not None:
Expand Down
2 changes: 2 additions & 0 deletions scripts/check_lb_data_files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ path_to_vt_build_dir=${1}
path_to_vt_src_dir=${2}
cd "$path_to_vt_build_dir" || exit 1

set +x

function run_schema_validator() {
file=$1
echo "Running schema validator on: $file"
Expand Down

0 comments on commit 1f0e3c7

Please sign in to comment.