Skip to content

Commit 63f1b29

Browse files
committed
improv: catch invalid formats exception
1 parent 01fd4e9 commit 63f1b29

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

aws_lambda_powertools/utilities/validation/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ def validate_data_against_schema(data: Dict, schema: Dict, formats: Optional[Dic
3232
"""
3333
try:
3434
fastjsonschema.validate(definition=schema, data=data, formats=formats)
35+
except (TypeError, AttributeError, fastjsonschema.JsonSchemaDefinitionException) as e:
36+
raise InvalidSchemaFormatError(f"Schema received: {schema}, Formats: {formats}. Error: {e}")
3537
except fastjsonschema.JsonSchemaException as e:
3638
message = f"Failed schema validation. Error: {e.message}, Path: {e.path}, Data: {e.value}" # noqa: B306, E501
3739
raise SchemaValidationError(message)
38-
except (TypeError, AttributeError) as e:
39-
raise InvalidSchemaFormatError(f"Schema received: {schema}. Error: {e}")
4040

4141

4242
def unwrap_event_from_envelope(data: Dict, envelope: str, jmespath_options: Dict) -> Any:

tests/functional/validator/test_validator.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ def test_validate_accept_schema_custom_format(
4747
)
4848

4949

50+
@pytest.mark.parametrize("invalid_format", [None, bool(), {}, [], object])
51+
def test_validate_invalid_custom_format(
52+
eventbridge_schema_registry_cloudtrail_v2_s3, eventbridge_cloudtrail_s3_head_object_event, invalid_format
53+
):
54+
with pytest.raises(exceptions.InvalidSchemaFormatError):
55+
validate(
56+
event=eventbridge_cloudtrail_s3_head_object_event,
57+
schema=eventbridge_schema_registry_cloudtrail_v2_s3,
58+
formats=invalid_format,
59+
)
60+
61+
5062
def test_validate_invalid_envelope_expression(schema, wrapped_event):
5163
with pytest.raises(exceptions.InvalidEnvelopeExpressionError):
5264
validate(event=wrapped_event, schema=schema, envelope=True)

0 commit comments

Comments
 (0)