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
`root_validator` can help when you have a complex validation mechanism. For example finding whether data has been omitted, comparing field values, etc.
221
-
222
-
```python:title=validate_all_field_values.py
223
-
from aws_lambda_powertools.utilities.parser import parse, BaseModel, validator
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>.
248
-
</Note><br/>
249
-
250
156
## Extending built-in models
251
157
252
158
Parser comes with the following built-in models:
@@ -324,12 +230,22 @@ for order_item in ret.detail.items:
324
230
325
231
## Envelopes
326
232
327
-
Envelope parameter is useful when your actual payload is wrapped around a known structure, for example Lambda Event Sources like EventBridge.
233
+
When trying to parse your payloads wrapped in a known structure, you might encounter the following situations:
234
+
235
+
* Your actual payload is wrapped around a known structure, for example Lambda Event Sources like EventBridge
236
+
* You're only interested in a portion of the payload, for example parsing the `detail` of custom events in EventBridge, or `body` of SQS records
237
+
238
+
You can either solve these situations by creating a model of these known structures, parsing them, then extracting and parsing a key where your payload is.
328
239
329
-
Example of parsing a model found in an event coming from EventBridge, where all you want is what's inside the `detail` key.
240
+
This can become difficult quite quickly. Parser makes this problem easier through a feature named `Envelope`.
241
+
242
+
Envelopes can be used via `envelope` parameter available in both `parse` function and `event_parser` decorator.
243
+
244
+
Here's an example of parsing a model found in an event coming from EventBridge, where all you want is what's inside the `detail` key.
330
245
331
246
```python:title=parse_eventbridge_payload.py
332
-
from aws_lambda_powertools.utilities.parser import parse, BaseModel, envelopes
247
+
from aws_lambda_powertools.utilities.parser import event_parser, parse, BaseModel, envelopes
248
+
from aws_lambda_powertools.utilities.typing import LambdaContext
333
249
334
250
classUserModel(BaseModel):
335
251
username: str
@@ -358,6 +274,11 @@ ret = parse(model=UserModel, envelope=envelopes.EventBridgeModel, event=payload)
358
274
359
275
# Parsed model only contains our actual model, not the entire EventBridge + Payload parsed
**DynamoDBStreamEnvelope** | 1. Parses data using `DynamoDBStreamModel`. <br/> 2. Parses records in `NewImage` and `OldImage` keys using your model. <br/> 3. Returns a list with a dictionary containing `NewImage` and `OldImage` keys | `List[Dict[Literal["NewImage", "OldImage"], BaseModel]]`
377
-
**EventBridgeEnvelope** | 1. Parses data using `EventBridgeModel`. <br/> 2. Parses `detail` key using your model and returns it. | `BaseModel`
378
-
**SqsEnvelope** | 1. Parses data using `SqsModel`. <br/> 2. Parses records in `body` key using your model and return them in a list. | `List[BaseModel]`
298
+
**DynamoDBStreamEnvelope** | 1. Parses data using `DynamoDBStreamModel`. <br/> 2. Parses records in `NewImage` and `OldImage` keys using your model. <br/> 3. Returns a list with a dictionary containing `NewImage` and `OldImage` keys | `List[Dict[str, Optional[Model]]]`
299
+
**EventBridgeEnvelope** | 1. Parses data using `EventBridgeModel`. <br/> 2. Parses `detail` key using your model and returns it. | `Model`
300
+
**SqsEnvelope** | 1. Parses data using `SqsModel`. <br/> 2. Parses records in `body` key using your model and return them in a list. | `List[Model]`
379
301
380
-
### Bringing your own envelope model
302
+
### Bringing your own envelope
381
303
382
304
You can create your own Envelope model and logic by inheriting from `BaseEnvelope`, and implementing the `parse` method.
383
305
@@ -407,28 +329,31 @@ class EventBridgeModel(BaseModel):
407
329
**EventBridge Envelope**
408
330
409
331
```python:title=eventbridge_envelope.py
410
-
from aws_lambda_powertools.utilities.parser import BaseEnvelope, BaseModel
411
-
from typing import Any, Dict
412
-
from ..models import EventBridgeModel
332
+
from aws_lambda_powertools.utilities.parser import BaseEnvelope, models
333
+
from aws_lambda_powertools.utilities.parser.models import EventBridgeModel
`root_validator` can help when you have a complex validation mechanism. For example finding whether data has been omitted, comparing field values, etc.
433
+
434
+
```python:title=validate_all_field_values.py
435
+
from aws_lambda_powertools.utilities.parser import parse, BaseModel, validator
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>.
460
+
</Note><br/>
461
+
462
+
442
463
## FAQ
443
464
444
465
**When should I use parser vs data_classes utility?**
0 commit comments