Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property type validation with Actions #142

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading