Skip to content

Commit ba7cd29

Browse files
committed
docs: use non-hello world model to better exemplify parsing
Signed-off-by: heitorlessa <[email protected]>
1 parent 96fc444 commit ba7cd29

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

docs/content/utilities/parser.mdx

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ You can define models to parse incoming events by inheriting from `BaseModel`.
3232

3333
```python:title=hello_world_model.py
3434
from aws_lambda_powertools.utilities.parser import BaseModel
35-
36-
class HelloWorldModel(BaseModel):
37-
message: str
38-
39-
class NestedHelloWorldModel(BaseModel):
40-
payload: HelloWorldModel
35+
from typing import List, Optional
36+
37+
class OrderItem(BaseModel):
38+
id: int
39+
quantity: int
40+
description: str
41+
42+
class Order(BaseModel):
43+
id: int
44+
description: str
45+
items: List[OrderItem] # nesting models are supported
46+
optional_field: Optional[str] # this field may or may not be available when parsing
4147
```
4248

4349
These are simply Python classes that inherit from BaseModel. **Parser** enforces type hints declared in your model at runtime.
@@ -65,11 +71,14 @@ Use the decorator for fail fast scenarios where you want your Lambda function to
6571
from aws_lambda_powertools.utilities.parser import parse, ModelValidationError
6672
from aws_lambda_powertools.utilities.typing import LambdaContext
6773

68-
@event_parser(model=HelloWorldModel)
69-
def handler(event, context: LambdaContext):
70-
pass
74+
@event_parser(model=Order) # highlight-line
75+
def handler(event: Order, context: LambdaContext):
76+
print(event.id)
77+
print(event.description)
78+
print(event.items)
7179

72-
handler(event=payload, context=LambdaContext()
80+
order_items = [items for item in event.items]
81+
...
7382
```
7483

7584
### parse function
@@ -79,17 +88,28 @@ Use this standalone function when you want more control over the data validation
7988
```python:title=parse_standalone_example.py
8089
from aws_lambda_powertools.utilities.parser import parse, ModelValidationError
8190

82-
payload = {"message": "hello world"}
91+
# Raw event for the Order model we've defined earlier
92+
payload = {
93+
"id": 10876546789,
94+
"description": "My order",
95+
"items": [
96+
{
97+
"id": 1015938732,
98+
"quantity": 1,
99+
"description": "item xpto"
100+
}
101+
]
102+
}
83103

84104
def my_function():
85105
try:
86-
parsed_payload = parse(event=payload, model=HelloWorldModel) # highlight-line
106+
parsed_payload: Order = parse(event=payload, model=HelloWorldModel) # highlight-line
87107
# payload dict is now parsed into our model
88-
return assert parsed_payload.message == payload["message"]
108+
return parsed_payload.items
89109
except ModelValidationError:
90110
return {
91111
"status_code": 400,
92-
"message": "Invalid input"
112+
"message": "Invalid order"
93113
}
94114
```
95115

0 commit comments

Comments
 (0)