Skip to content

Commit

Permalink
Merge pull request #142 from mwgg/2024_08_28_type_validation
Browse files Browse the repository at this point in the history
Property type validation with Actions
  • Loading branch information
mwgg authored Aug 27, 2024
2 parents b33335c + eecbcb1 commit 8527538
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .github/scripts/sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
54 changes: 39 additions & 15 deletions .github/scripts/valid_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 8527538

Please sign in to comment.