From eecbcb143874873e173c151a9edb98995f5fb1fe Mon Sep 17 00:00:00 2001 From: mwgg Date: Tue, 27 Aug 2024 04:35:55 +0000 Subject: [PATCH] Property type validation --- .github/scripts/sort.py | 2 ++ .github/scripts/valid_json.py | 54 +++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/.github/scripts/sort.py b/.github/scripts/sort.py index da208ed..93769b2 100644 --- a/.github/scripts/sort.py +++ b/.github/scripts/sort.py @@ -5,6 +5,8 @@ def sort_json_by_keyword(input_file, output_file, keyword): with open(input_file, 'r') as file: data = json.load(file) + print(f"Found {len(data)} airport entries") + sorted_keys = sorted(data.keys(), key=lambda k: data[k][keyword]) sorted_data = {k: data[k] for k in sorted_keys} diff --git a/.github/scripts/valid_json.py b/.github/scripts/valid_json.py index 2c20549..b65a658 100644 --- a/.github/scripts/valid_json.py +++ b/.github/scripts/valid_json.py @@ -3,30 +3,54 @@ import sys def check_json_validity(file_path): + errors = [] + try: with open(file_path, 'r') as file: - json.load(file) - return True, "" + data = json.load(file) + + print(f"Found {len(data)} airport entries") + + for key, value in data.items(): + if "lat" in value: + if not isinstance(value["lat"], (float, int)): + errors.append(f'Invalid type for "lat" in entry "{key}". Expected float or int, got {type(value["lat"]).__name__}.') + elif not -90 <= value["lat"] <= 90: + errors.append(f'Invalid value for "lat" in entry "{key}". Expected between -90 and 90, got {value["lat"]}.') + + if "lon" in value: + if not isinstance(value["lon"], (float, int)): + errors.append(f'Invalid type for "lon" in entry "{key}". Expected float or int, got {type(value["lon"]).__name__}.') + elif not -180 <= value["lon"] <= 180: + errors.append(f'Invalid value for "lon" in entry "{key}". Expected between -180 and 180, got {value["lon"]}.') + + if "elevation" in value: + if not isinstance(value["elevation"], int): + errors.append(f'Invalid type for "elevation" in entry "{key}". Expected int, got {type(value["elevation"]).__name__}.') + + return errors + except json.JSONDecodeError as e: - return False, f"Invalid JSON: {e}" + errors.append(f"Invalid JSON: {e}") except Exception as e: - return False, f"An unexpected error occurred: {e}" + errors.append(f"An unexpected error occurred: {e}") + + return errors def main(): - parser = argparse.ArgumentParser(description="Check if a JSON file is valid.") - parser.add_argument("file", help="The path to the JSON file to check.") - + parser = argparse.ArgumentParser(description="Check if a JSON file is valid and meets specific criteria.") + parser.add_argument("file", help="The path to the JSON file to validate.") args = parser.parse_args() - file_path = args.file - - is_valid, message = check_json_validity(file_path) + + errors = check_json_validity(args.file) - if is_valid: - print(f"The JSON file '{file_path}' is valid.") - sys.exit(0) - else: - print(f"The JSON file '{file_path}' is invalid. Error: {message}") + if errors: + print("Validation errors found:") + for error in errors: + print(error) sys.exit(1) + else: + print("The JSON file is valid.") if __name__ == "__main__": main()