Skip to content

Commit 0327666

Browse files
fix(parser): revert a regression in v3 when raising ValidationError (#5259)
Revert regression in v3 when raising ValidationError
1 parent 72b9546 commit 0327666

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Diff for: aws_lambda_powertools/utilities/parser/parser.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import typing
55
from typing import TYPE_CHECKING, Any, Callable, overload
66

7-
from pydantic import PydanticSchemaGenerationError, ValidationError
7+
from pydantic import PydanticSchemaGenerationError
88

99
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
1010
from aws_lambda_powertools.utilities.parser.exceptions import InvalidEnvelopeError, InvalidModelTypeError
@@ -108,7 +108,7 @@ def handler(event: Order, context: LambdaContext):
108108

109109
logger.debug(f"Calling handler {handler.__name__}")
110110
return handler(parsed_event, context, **kwargs)
111-
except (ValidationError, AttributeError) as exc:
111+
except AttributeError as exc:
112112
raise InvalidModelTypeError(f"Error: {str(exc)}. Please ensure the type you're trying to parse into is correct")
113113

114114

@@ -199,7 +199,7 @@ def handler(event: Order, context: LambdaContext):
199199
# when we pass a data structure that does not match the model (trying to parse a true/false/etc into a model)
200200
except PydanticSchemaGenerationError as exc:
201201
raise InvalidModelTypeError(f"The event supplied is unable to be validated into {type(model)}") from exc
202-
except ValidationError as exc:
202+
except AttributeError as exc:
203203
raise InvalidModelTypeError(
204204
f"Error: {str(exc)}. Please ensure the Input model inherits from BaseModel,\n"
205205
"and your payload adheres to the specified Input model structure.\n"

Diff for: tests/functional/parser/test_parser.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pydantic
55
import pytest
6+
from pydantic import ValidationError
67
from typing_extensions import Annotated
78

89
from aws_lambda_powertools.utilities.parser import (
@@ -18,7 +19,7 @@ def test_parser_unsupported_event(dummy_schema, invalid_value):
1819
def handle_no_envelope(event: Dict, _: LambdaContext):
1920
return event
2021

21-
with pytest.raises(exceptions.InvalidModelTypeError):
22+
with pytest.raises(ValidationError):
2223
handle_no_envelope(event=invalid_value, context=LambdaContext())
2324

2425

@@ -75,7 +76,7 @@ def validate_field(cls, value):
7576
assert event_parsed.version == int(event_raw["version"])
7677

7778

78-
@pytest.mark.parametrize("invalid_schema", [str, False, [], ()])
79+
@pytest.mark.parametrize("invalid_schema", [False, [], ()])
7980
def test_parser_with_invalid_schema_type(dummy_event, invalid_schema):
8081
@event_parser(model=invalid_schema)
8182
def handle_no_envelope(event: Dict, _: LambdaContext):
@@ -120,6 +121,15 @@ def handler(evt: dummy_schema, _: LambdaContext):
120121
handler(dummy_event["payload"], LambdaContext())
121122

122123

124+
def test_parser_event_with_payload_not_match_schema(dummy_event, dummy_schema):
125+
@event_parser(model=dummy_schema)
126+
def handler(event, _):
127+
assert event.message == "hello world"
128+
129+
with pytest.raises(ValidationError):
130+
handler({"project": "powertools"}, LambdaContext())
131+
132+
123133
@pytest.mark.parametrize(
124134
"test_input,expected",
125135
[

0 commit comments

Comments
 (0)