You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**DynamoDBStreamModel** | Lambda Event Source payload for Amazon DynamoDB Streams
67
-
**EventBridgeModel** | Lambda Event Source payload for Amazon EventBridge
68
-
**SqsModel** | Lambda Event Source payload for Amazon SQS
69
-
70
-
You can extend them to include your own models, and yet have all other known fields parsed along the way.
71
-
72
-
**EventBridge example**
73
-
74
-
```python:title=extending_builtin_models.py
75
-
from aws_lambda_powertools.utilities.parser import parse, BaseModel
76
-
from aws_lambda_powertools.utilities.parser.models import EventBridgeModel
77
-
78
-
from typing import List, Optional
79
-
80
-
classOrderItem(BaseModel):
81
-
id: int
82
-
quantity: int
83
-
description: str
84
-
85
-
classOrder(BaseModel):
86
-
id: int
87
-
description: str
88
-
items: List[OrderItem]
89
-
90
-
# highlight-start
91
-
classOrderEventModel(EventBridgeModel):
92
-
detail: Order
93
-
# highlight-end
94
-
95
-
payload = {
96
-
"version": "0",
97
-
"id": "6a7e8feb-b491-4cf7-a9f1-bf3703467718",
98
-
"detail-type": "OrderPurchased",
99
-
"source": "OrderService",
100
-
"account": "111122223333",
101
-
"time": "2020-10-22T18:43:48Z",
102
-
"region": "us-west-1",
103
-
"resources": ["some_additional"],
104
-
"detail": { # highlight-line
105
-
"id": 10876546789,
106
-
"description": "My order",
107
-
"items": [
108
-
{
109
-
"id": 1015938732,
110
-
"quantity": 1,
111
-
"description": "item xpto"
112
-
}
113
-
]
114
-
}
115
-
}
116
-
117
-
ret = parse(model=OrderEventModel, event=payload) # highlight-line
118
-
119
-
assert ret.source =="OrderService"
120
-
assert ret.detail.description =="My order"
121
-
assert ret.detail_type =="OrderPurchased"# we rename it to snake_case since detail-type is an invalid name
122
-
123
-
for order_item in ret.detail.items:
124
-
...
125
-
```
126
-
127
-
**What's going on here, you might ask**:
128
-
129
-
1. We imported our built-in model `EventBridgeModel` from the parser utility
130
-
2. Defined how our `Order` should look like
131
-
3. Defined how part of our EventBridge event should look like by overriding `detail` key within our `OrderEventModel`
132
-
4. Parser parsed the original event against `OrderEventModel`
133
-
134
60
## Parsing events
135
61
136
62
You can parse inbound events using **event_parser** decorator, or the standalone `parse` function. Both are also able to parse either dictionary or JSON string as an input.
@@ -141,7 +67,7 @@ Use the decorator for fail fast scenarios where you want your Lambda function to
141
67
142
68
`event_parser` decorator will throw a `ValidationError` if your event cannot be parsed according to the model.
143
69
144
-
```python=:title=event_parser_decorator.py
70
+
```python:title=event_parser_decorator.py
145
71
from aws_lambda_powertools.utilities.parser import event_parser, BaseModel, ValidationError
146
72
from aws_lambda_powertools.utilities.typing import LambdaContext
You can read more about validating list items, reusing validators, validating raw inputs, and a lot more in <ahref="https://pydantic-docs.helpmanual.io/usage/validators/">Pydantic's documentation</a>.
0 commit comments