Skip to content

Commit

Permalink
fix: print error details for 'json_template_lint' pre-commit hook (#1092
Browse files Browse the repository at this point in the history
)
  • Loading branch information
akocbek authored Jan 24, 2025
1 parent 110b38c commit 5e76b15
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions module-assets/ci/validateJsonTemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,21 @@ def create_temp_json(root, file):

# check if a file is valid JSON
def is_json(myjson):
error = None
with open(myjson) as json_file:
try:
json.load(json_file)
except ValueError:
return False
return True
except json.JSONDecodeError as e:
# read the JSON file content to get the lines for error printing
file = open(myjson)
content = file.readlines()

# construct error message
line_text = content[e.lineno - 1].replace("\n", "")
pointer_line = "-" * (e.colno - 1) + "^"
error = line_text + "\n" + pointer_line + "\n" + str(e)

return error


# create 'temp_tf_inputs.json' file using terraform-docs to get all tf inputs
Expand Down Expand Up @@ -133,9 +142,13 @@ def main():
temp_catalog_file = create_temp_json(root, file)

# if catalogValidationValues.json.template is not valid JSON format then save the error
if not is_json(temp_catalog_file):
error = is_json(temp_catalog_file)
if error is not None:
validation_errors.append(
original_catalog_file + " is not valid JSON format."
original_catalog_file
+ " is not valid JSON format."
+ "\n\n"
+ error
)
else:
validate_inputs(root, temp_catalog_file, original_catalog_file)
Expand Down

0 comments on commit 5e76b15

Please sign in to comment.