@@ -118,7 +118,7 @@ ret = parse(model=OrderEventModel, event=payload) # highlight-line
118
118
119
119
assert ret.source == " OrderService"
120
120
assert ret.detail.description == " My order"
121
- assert ret.detail_type == " OrderPurchased" # we rename it to snake_case
121
+ assert ret.detail_type == " OrderPurchased" # we rename it to snake_case since detail-type is an invalid name
122
122
123
123
for order_item in ret.detail.items:
124
124
...
@@ -142,11 +142,21 @@ Use the decorator for fail fast scenarios where you want your Lambda function to
142
142
` event_parser ` decorator will throw a ` ModelValidationError ` if your event cannot be parsed according to the model.
143
143
144
144
``` python=:title=event_parser_decorator.py
145
- from aws_lambda_powertools.utilities.parser import parse , ModelValidationError
145
+ from aws_lambda_powertools.utilities.parser import event_parser, BaseModel , ModelValidationError
146
146
from aws_lambda_powertools.utilities.typing import LambdaContext
147
147
import json
148
148
149
- # Raw event for the Order model we've defined earlier
149
+ class OrderItem (BaseModel ):
150
+ id : int
151
+ quantity: int
152
+ description: str
153
+
154
+ class Order (BaseModel ):
155
+ id : int
156
+ description: str
157
+ items: List[OrderItem] # nesting models are supported
158
+ optional_field: Optional[str ] # this field may or may not be available when parsing
159
+
150
160
payload = {
151
161
" id" : 10876546789 ,
152
162
" description" : " My order" ,
@@ -177,15 +187,27 @@ handler(event=json.dumps(payload), context=LambdaContext()) # also works if even
177
187
Use this standalone function when you want more control over the data validation process, for example returning a 400 error for malformed payloads.
178
188
179
189
``` python:title=parse_standalone_example.py
180
- from aws_lambda_powertools.utilities.parser import parse, ModelValidationError
190
+ from aws_lambda_powertools.utilities.parser import parse, BaseModel, ModelValidationError
191
+
192
+ class OrderItem (BaseModel ):
193
+ id : int
194
+ quantity: int
195
+ description: str
196
+
197
+ class Order (BaseModel ):
198
+ id : int
199
+ description: str
200
+ items: List[OrderItem] # nesting models are supported
201
+ optional_field: Optional[str ] # this field may or may not be available when parsing
202
+
181
203
182
- # Raw event for the Order model we've defined earlier
183
204
payload = {
184
205
" id" : 10876546789 ,
185
206
" description" : " My order" ,
186
207
" items" : [
187
208
{
188
- " id" : 1015938732 ,
209
+ # this will cause a validation error
210
+ " id" : [1015938732 ], # highlight-line
189
211
" quantity" : 1 ,
190
212
" description" : " item xpto"
191
213
}
@@ -194,7 +216,7 @@ payload = {
194
216
195
217
def my_function ():
196
218
try :
197
- parsed_payload: Order = parse(event = payload, model = HelloWorldModel ) # highlight-line
219
+ parsed_payload: Order = parse(event = payload, model = Order ) # highlight-line
198
220
# payload dict is now parsed into our model
199
221
return parsed_payload.items
200
222
except ModelValidationError:
0 commit comments