Skip to content

Commit

Permalink
Exiti non-zero if validation failed
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-clematide committed Apr 7, 2024
1 parent fd68779 commit 080b39c
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions scripts/jsonlschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
schema. This script can handle files from various sources (local, cloud storage) through
smart_open, and logs validation errors while allowing for command-line configured
verbosity.
In cases of validation errors, the offending JSON object is logged and skipped. The
script exits with a non-zero status if any validation errors are encountered.
"""

__author__ = "[email protected]"
Expand Down Expand Up @@ -56,6 +59,7 @@ def __init__(

self.input_files = input_files
self.file_format = file_format
self.validation_errors = 0
self.output_writer = (
smart_open.open(output_file, mode="w", encoding="utf-8")
if output_file
Expand All @@ -68,6 +72,9 @@ def run(self) -> None:
"""Starts the validation process."""

self.process()
if self.validation_errors > 0:
log.error("Validation errors encountered: %d", self.validation_errors)
sys.exit(1)

def process(self) -> None:
"""
Expand All @@ -83,6 +90,7 @@ def process(self) -> None:
except jsonschema.exceptions.ValidationError as e:
log.error("Validation error: %s", e.message)
log.info("Offending JSON object ignored: %s", jo)
self.validation_errors += 1
continue
print(
json.dumps(jo, ensure_ascii=False, separators=(",", ":")),
Expand Down

0 comments on commit 080b39c

Please sign in to comment.