5
5
6
6
from aws_lambda_powertools .middleware_factory import lambda_handler_decorator
7
7
8
- from .envelopes .base import BaseEnvelope , parse_envelope
9
- from .exceptions import InvalidSchemaTypeError , SchemaValidationError
8
+ from .envelopes .base import BaseEnvelope
9
+ from .exceptions import InvalidEnvelopeError , InvalidSchemaTypeError , SchemaValidationError
10
10
11
11
logger = logging .getLogger (__name__ )
12
12
@@ -19,53 +19,96 @@ def parser(
19
19
schema : BaseModel ,
20
20
envelope : Optional [BaseEnvelope ] = None ,
21
21
) -> Any :
22
- # noinspection SpellCheckingInspection,SpellCheckingInspection
23
22
"""Decorator to conduct advanced parsing & validation for lambda handlers events
24
23
25
- As Lambda follows (event, context) signature we can remove some of the boilerplate
26
- and also capture any exception any Lambda function throws as metadata.
27
- event will be the parsed and passed as a BaseModel pydantic class of the input type "schema"
28
- to the lambda handler.
29
- event will be extracted from the envelope in case envelope is not None.
30
- In case envelope is None, the complete event is parsed to match the schema parameter BaseModel definition.
31
- In case envelope is not None, first the event is parsed as the envelope's schema definition, and the user
32
- message is extracted and parsed again as the schema parameter's definition.
33
-
34
- Example
35
- -------
36
- **Lambda function using validation decorator**
37
-
38
- @parser(schema=MyBusiness, envelope=envelopes.EVENTBRIDGE)
39
- def handler(event: MyBusiness , context: LambdaContext):
40
- ...
41
-
42
- Parameters
43
- ----------
44
- handler: input for lambda_handler_decorator, wraps the handler lambda
45
- event: AWS event dictionary
46
- context: AWS lambda context
47
- schema: pydantic BaseModel class. This is the user data schema that will replace the event.
48
- event parameter will be parsed and a new schema object will be created from it.
49
- envelope: what envelope to extract the schema from, can be any AWS service that is currently
50
- supported in the envelopes module. Can be None.
51
-
52
- Raises
53
- ------
54
- SchemaValidationError
55
- When input event doesn't conform with schema provided
56
- InvalidSchemaTypeError
57
- When schema given does not implement BaseModel
58
- """
59
- if envelope is None :
60
- try :
61
- logger .debug ("Parsing and validating event schema; no envelope used" )
62
- parsed_event = schema .parse_obj (event )
63
- except (ValidationError , TypeError ) as e :
64
- raise SchemaValidationError ("Input event doesn't conform with schema" ) from e
65
- except AttributeError :
66
- raise InvalidSchemaTypeError ("Input schema must implement BaseModel" )
67
- else :
68
- parsed_event = parse_envelope (event = event , envelope = envelope , schema = schema )
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.
32
+
33
+ Example
34
+ -------
35
+ **Lambda function using validation decorator**
36
+
37
+ @parser(schema=MyBusiness, envelope=envelopes.EVENTBRIDGE)
38
+ def handler(event: MyBusiness , context: LambdaContext):
39
+ ...
40
+
41
+ Parameters
42
+ ----------
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.
69
50
51
+ Raises
52
+ ------
53
+ SchemaValidationError
54
+ When input event doesn't conform with schema provided
55
+ InvalidSchemaTypeError
56
+ When schema given does not implement BaseModel
57
+ """
58
+ parsed_event = parse (event = event , schema = schema , envelope = envelope )
70
59
logger .debug (f"Calling handler { handler .__name__ } " )
71
60
return handler (parsed_event , context )
61
+
62
+
63
+ def 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
66
+
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.
75
+
76
+ Example
77
+ -------
78
+ **Lambda function using standalone parse**
79
+
80
+ def handler(event: MyBusiness , context: LambdaContext):
81
+ parse(event=event, schema=MyBusiness, envelope=envelopes.EVENTBRIDGE)
82
+ ...
83
+
84
+ Parameters
85
+ ----------
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.
91
+
92
+ Raises
93
+ ------
94
+ SchemaValidationError
95
+ When input event doesn't conform with schema provided
96
+ InvalidSchemaTypeError
97
+ When schema given does not implement BaseModel
98
+
99
+ """
100
+ if envelope :
101
+ try :
102
+ logger .debug (f"Parsing and validating event schema, envelope={ envelope } " )
103
+ # noinspection PyCallingNonCallable
104
+ return envelope ().parse (event = event , schema = schema )
105
+ except (TypeError , AttributeError ):
106
+ raise InvalidEnvelopeError (f"envelope must be a callable and instance of BaseEnvelope, envelope={ envelope } " )
107
+
108
+ try :
109
+ logger .debug ("Parsing and validating event schema; no envelope used" )
110
+ return schema .parse_obj (event )
111
+ except (ValidationError , TypeError ) as e :
112
+ raise SchemaValidationError ("Input event doesn't conform with schema" ) from e
113
+ except AttributeError :
114
+ raise InvalidSchemaTypeError ("Input schema must implement BaseModel" )
0 commit comments