Skip to content

Commit d434f48

Browse files
committed
docs: ensure examples can be copied/pasted as-is
Signed-off-by: heitorlessa <[email protected]>
1 parent 4b6ecbf commit d434f48

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

docs/content/utilities/parser.mdx

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ ret = parse(model=OrderEventModel, event=payload) # highlight-line
118118

119119
assert ret.source == "OrderService"
120120
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
122122

123123
for order_item in ret.detail.items:
124124
...
@@ -142,11 +142,21 @@ Use the decorator for fail fast scenarios where you want your Lambda function to
142142
`event_parser` decorator will throw a `ModelValidationError` if your event cannot be parsed according to the model.
143143

144144
```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
146146
from aws_lambda_powertools.utilities.typing import LambdaContext
147147
import json
148148

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+
150160
payload = {
151161
"id": 10876546789,
152162
"description": "My order",
@@ -177,15 +187,27 @@ handler(event=json.dumps(payload), context=LambdaContext()) # also works if even
177187
Use this standalone function when you want more control over the data validation process, for example returning a 400 error for malformed payloads.
178188

179189
```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+
181203

182-
# Raw event for the Order model we've defined earlier
183204
payload = {
184205
"id": 10876546789,
185206
"description": "My order",
186207
"items": [
187208
{
188-
"id": 1015938732,
209+
# this will cause a validation error
210+
"id": [1015938732], # highlight-line
189211
"quantity": 1,
190212
"description": "item xpto"
191213
}
@@ -194,7 +216,7 @@ payload = {
194216

195217
def my_function():
196218
try:
197-
parsed_payload: Order = parse(event=payload, model=HelloWorldModel) # highlight-line
219+
parsed_payload: Order = parse(event=payload, model=Order) # highlight-line
198220
# payload dict is now parsed into our model
199221
return parsed_payload.items
200222
except ModelValidationError:

0 commit comments

Comments
 (0)