55
66from aws_lambda_powertools .middleware_factory import lambda_handler_decorator
77
8+ from ..typing import LambdaContext
89from .envelopes .base import BaseEnvelope
910from .exceptions import InvalidEnvelopeError , InvalidSchemaTypeError , SchemaValidationError
1011
1516def parser (
1617 handler : Callable [[Dict , Any ], Any ],
1718 event : Dict [str , Any ],
18- context : Dict [ str , Any ] ,
19+ context : LambdaContext ,
1920 schema : BaseModel ,
2021 envelope : Optional [BaseEnvelope ] = None ,
2122) -> Any :
22- """Decorator to conduct advanced parsing & validation for lambda handlers events
23+ """Lambda handler decorator to parse & validate events using Pydantic models
2324
24- As Lambda follows (event, context) signature we can remove some of the boilerplate
25- and also capture any exception any Lambda function throws as metadata.
26- event will be the parsed and passed as a BaseModel Pydantic class of the input type "schema"
27- to the lambda handler.
28- event will be extracted from the envelope in case envelope is not None.
29- In case envelope is None, the complete event is parsed to match the schema parameter BaseModel definition.
30- In case envelope is not None, first the event is parsed as the envelope's schema definition, and the user
31- message is extracted and parsed again as the schema parameter's definition.
25+ It requires a schema that implements Pydantic BaseModel to parse & validate the event.
26+
27+ When an envelope is given, it'll use the following logic:
28+
29+ 1. Parse the event against envelope schema first e.g. EnvelopeSchema(**event)
30+ 2. Envelope will extract a given key to be parsed against the schema e.g. event.detail
31+
32+ This is useful when you need to confirm event wrapper structure, and
33+ b) selectively extract a portion of your payload for parsing & validation.
34+
35+ NOTE: If envelope is omitted, the complete event is parsed to match the schema parameter BaseModel definition.
3236
3337 Example
3438 -------
35- **Lambda function using validation decorator**
39+ **Lambda handler decorator to parse & validate event**
40+
41+ class Order(BaseModel):
42+ id: int
43+ description: str
44+ ...
45+
46+ @parser(schema=Order)
47+ def handler(event: Order, context: LambdaContext):
48+ ...
49+
50+ **Lambda handler decorator to parse & validate event - using built-in envelope**
51+
52+ class Order(BaseModel):
53+ id: int
54+ description: str
55+ ...
3656
37- @parser(schema=MyBusiness , envelope=envelopes.EVENTBRIDGE)
38- def handler(event: MyBusiness , context: LambdaContext):
57+ @parser(schema=Order , envelope=envelopes.EVENTBRIDGE)
58+ def handler(event: Order , context: LambdaContext):
3959 ...
4060
4161 Parameters
4262 ----------
43- handler: input for lambda_handler_decorator, wraps the handler lambda
44- event: AWS event dictionary
45- context: AWS lambda context
46- schema: pydantic BaseModel class. This is the user data schema that will replace the event.
47- event parameter will be parsed and a new schema object will be created from it.
48- envelope: what envelope to extract the schema from, can be any AWS service that is currently
49- supported in the envelopes module. Can be None.
63+ handler: Callable
64+ Method to annotate on
65+ event: Dict
66+ Lambda event to be parsed & validated
67+ context: LambdaContext
68+ Lambda context object
69+ schema: BaseModel
70+ Your data schema that will replace the event.
71+ envelope: BaseEnvelope
72+ Optional envelope to extract the schema from
5073
5174 Raises
5275 ------
5376 SchemaValidationError
5477 When input event does not conform with schema provided
5578 InvalidSchemaTypeError
5679 When schema given does not implement BaseModel
80+ InvalidEnvelopeError
81+ When envelope given does not implement BaseEnvelope
5782 """
5883 parsed_event = parse (event = event , schema = schema , envelope = envelope )
5984 logger .debug (f"Calling handler { handler .__name__ } " )
6085 return handler (parsed_event , context )
6186
6287
6388def parse (event : Dict [str , Any ], schema : BaseModel , envelope : Optional [BaseEnvelope ] = None ) -> Any :
64- """
65- Standalone parse function to conduct advanced parsing & validation for lambda handlers events
89+ """Standalone function to parse & validate events using Pydantic models
6690
67- As Lambda follows (event, context) signature we can remove some of the boilerplate
68- and also capture any exception any Lambda function throws as metadata.
69- event will be the parsed and passed as a BaseModel Pydantic class of the input type "schema"
70- to the lambda handler.
71- event will be extracted from the envelope in case envelope is not None.
72- In case envelope is None, the complete event is parsed to match the schema parameter BaseModel definition.
73- In case envelope is not None, first the event is parsed as the envelope's schema definition, and the user
74- message is extracted and parsed again as the schema parameter's definition.
91+ Typically used when you need fine-grained control over error handling compared to parser decorator.
7592
7693 Example
7794 -------
78- **Lambda function using standalone parse**
7995
80- def handler(event: MyBusiness , context: LambdaContext):
81- parse(event=event, schema=MyBusiness, envelope=envelopes.EVENTBRIDGE)
96+ **Lambda handler decorator to parse & validate event**
97+
98+ from aws_lambda_powertools.utilities.parser.exceptions import SchemaValidationError
99+
100+ class Order(BaseModel):
101+ id: int
102+ description: str
82103 ...
83104
105+ def handler(event: Order, context: LambdaContext):
106+ try:
107+ parse(schema=Order)
108+ except SchemaValidationError:
109+ ...
110+
111+ **Lambda handler decorator to parse & validate event - using built-in envelope**
112+
113+ class Order(BaseModel):
114+ id: int
115+ description: str
116+ ...
117+
118+ def handler(event: Order, context: LambdaContext):
119+ try:
120+ parse(schema=Order, envelope=envelopes.EVENTBRIDGE)
121+ except SchemaValidationError:
122+ ...
123+
84124 Parameters
85125 ----------
86- event: AWS event dictionary
87- schema: pydantic BaseModel class. This is the user data schema that will replace the event.
88- event parameter will be parsed and a new schema object will be created from it.
89- envelope: what envelope to extract the schema from, can be any AWS service that is currently
90- supported in the envelopes module. Can be None.
126+ event: Dict
127+ Lambda event to be parsed & validated
128+ schema: BaseModel
129+ Your data schema that will replace the event.
130+ envelope: BaseEnvelope
131+ Optional envelope to extract the schema from
91132
92133 Raises
93134 ------
@@ -101,7 +142,7 @@ def handler(event: MyBusiness , context: LambdaContext):
101142 if envelope and callable (envelope ):
102143 try :
103144 logger .debug (f"Parsing and validating event schema with envelope={ envelope } " )
104- return envelope ().parse (event = event , schema = schema )
145+ return envelope ().parse (data = event , schema = schema )
105146 except AttributeError :
106147 raise InvalidEnvelopeError (f"Envelope must implement BaseEnvelope, envelope={ envelope } " )
107148 except (ValidationError , TypeError ) as e :
0 commit comments