Add input_validation_script/src/input_validation_script/cli.py
This commit is contained in:
parent
65be4c9edc
commit
04f23b90a4
1 changed files with 66 additions and 0 deletions
66
input_validation_script/src/input_validation_script/cli.py
Normal file
66
input_validation_script/src/input_validation_script/cli.py
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from input_validation_script import core
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""CLI entry point for validating a JSON report against a JSON schema."""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Validate a JSON file against a provided JSON schema."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--input",
|
||||
required=True,
|
||||
help="Path to the JSON file to be validated.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--schema",
|
||||
required=True,
|
||||
help="Path to the JSON schema file.",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
input_path = Path(args.input)
|
||||
schema_path = Path(args.schema)
|
||||
|
||||
# CI safety: ensure input files exist
|
||||
if not input_path.exists():
|
||||
result = {"valid": False, "errors": ["artefact_missing: input file not found"]}
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
sys.exit(1)
|
||||
|
||||
if not schema_path.exists():
|
||||
result = {"valid": False, "errors": ["artefact_missing: schema file not found"]}
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
sys.exit(1)
|
||||
|
||||
# Load schema and JSON
|
||||
try:
|
||||
with open(schema_path, "r", encoding="utf-8") as f:
|
||||
schema = json.load(f)
|
||||
except Exception as e:
|
||||
result = {"valid": False, "errors": [f"parse_error: failed to load schema: {e}"]}
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
sys.exit(1)
|
||||
|
||||
# Call validate_json
|
||||
try:
|
||||
validation_result = core.validate_json(str(input_path), schema)
|
||||
except Exception as e:
|
||||
validation_result = {
|
||||
"valid": False,
|
||||
"errors": [f"contract_violation: unexpected error during validation: {e}"]
|
||||
}
|
||||
|
||||
# Output result as JSON to stdout
|
||||
print(json.dumps(validation_result, ensure_ascii=False, indent=2))
|
||||
|
||||
if not validation_result.get("valid", False):
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in a new issue