Skip to content

Commit 6091b35

Browse files
fix(governance): improve artifact validation and error handling
- Move manual structural checks before jsonschema validation in scripts/validate_gsifi_governance_assets.py to ensure error message consistency with existing tests. - Add comprehensive error handling for jsonschema exceptions during validation. - Ensure all tests in tests/test_validate_gsifi_governance_assets.py pass. - Clean up __pycache__ directories. Co-authored-by: OneFineStarstuff <87420139+OneFineStarstuff@users.noreply.github.com>
1 parent 4d80000 commit 6091b35

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

scripts/validate_gsifi_governance_assets.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,21 @@ def _validate_with_jsonschema(schema: dict, sample: dict) -> None:
9292
if validator_type is None:
9393
return
9494

95-
validator = validator_type(schema)
96-
errors = sorted(validator.iter_errors(sample), key=lambda e: e.path)
97-
if errors:
98-
first = errors[0]
99-
path = ".".join(str(p) for p in first.path) or "<root>"
100-
raise ValidationError(f"JSON Schema validation failed at {path}: {first.message}")
95+
try:
96+
validator = validator_type(schema)
97+
errors = list(validator.iter_errors(sample))
98+
if errors:
99+
errors = sorted(errors, key=lambda e: e.path)
100+
first = errors[0]
101+
path = ".".join(str(p) for p in first.path) or "<root>"
102+
raise ValidationError(f"JSON Schema validation failed at {path}: {first.message}")
103+
except ValidationError:
104+
raise
105+
except Exception as exc:
106+
# Wrap any jsonschema-related exceptions that might occur during validation or initialization
107+
if "jsonschema" in str(type(exc)):
108+
raise ValidationError(f"JSON Schema validation failed: {exc}") from exc
109+
raise
101110

102111
def validate_event_schema_and_sample(
103112
schema_path: Path = SCHEMA_PATH,
@@ -110,8 +119,8 @@ def validate_event_schema_and_sample(
110119
if not isinstance(sample, dict):
111120
raise ValidationError("Sample event root must be a JSON object")
112121

113-
_validate_with_jsonschema(schema, sample)
114-
122+
# Perform basic structure validation before letting jsonschema take over,
123+
# as existing tests expect these specific error messages.
115124
required = schema.get("required", [])
116125
if not isinstance(required, list):
117126
raise ValidationError("Schema field 'required' must be a list")
@@ -122,6 +131,7 @@ def validate_event_schema_and_sample(
122131
properties = schema.get("properties", {})
123132
if not isinstance(properties, dict):
124133
raise ValidationError("Schema field 'properties' must be an object")
134+
125135
additional_allowed = schema.get("additionalProperties", True)
126136
if additional_allowed is False:
127137
allowed = set(properties.keys())
@@ -155,6 +165,8 @@ def validate_event_schema_and_sample(
155165
if prop.get("format") == "date-time" and isinstance(value, str):
156166
_validate_date_time(value, key)
157167

168+
_validate_with_jsonschema(schema, sample)
169+
158170

159171
def validate_rego_policy(rego_path: Path = REGO_PATH) -> None:
160172
text = _read_text(rego_path)

0 commit comments

Comments
 (0)