Skip to content

Commit f0a8a18

Browse files
committed
docs: add 101 parsing events content
1 parent 190703b commit f0a8a18

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

docs/content/utilities/parser.mdx

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,62 @@ from aws_lambda_powertools.utilities.parser import BaseModel
3636
class HelloWorldModel(BaseModel):
3737
message: str
3838

39-
payload = {"message": "hello world"}
40-
parsed_payload = HelloWorldModel(**payload)
41-
42-
assert parsed_payload.message == payload["message"]
39+
class NestedHelloWorldModel(BaseModel):
40+
payload: HelloWorldModel
4341
```
4442

45-
These are simply Python classes that inherit from BaseModel, and use type hints to instruct **parser** to enforce it at runtime. The advantage here is that they can be [recursive, dumped as JSON, JSON Schema, Dicts, have validation and more](https://pydantic-docs.helpmanual.io/usage/models/).
43+
These are simply Python classes that inherit from BaseModel. **Parser** enforces type hints declared in your model at runtime.
44+
45+
<!-- TODO: We need a dedicated section to expand on these great features from Pydantic -->
46+
<!-- The advantage here is that they can be [recursive, dumped as JSON, JSON Schema, Dicts, have validation and more](https://pydantic-docs.helpmanual.io/usage/models/). -->
4647

47-
You can also even use [a code generator tool](https://github.com/koxudaxi/datamodel-code-generator/) to create models from JSON Schemas, OpenAPI, etc.
48+
<Note type="info">
49+
<strong>Looking to auto-generate models from JSON, YAML, JSON Schemas, OpenApi, etc?</strong>
50+
<br/><br/>
51+
Use Koudai Aono's <a href="https://github.com/koxudaxi/datamodel-code-generator">data model code generator tool for Pydantic</a>
52+
</Note><br/>
4853

4954
## Parsing events
5055

51-
You can parse inbound events using **event_parser** decorator.
56+
You can parse inbound events using **event_parser** decorator, or the standalone `parse` function.
5257

53-
You can also use the standalone **parse** function, if you want more control over data validation process such as handling data that doesn't conform with your model.
58+
### event_parser decorator
5459

55-
**TBW**
60+
Use the decorator for fail fast scenarios where you want your Lambda function to raise an exception in the event of a malformed payload.
5661

57-
### event_parser decorator
62+
`event_parser` decorator will throw a `ModelValidationError` if your event cannot be parsed according to the model.
5863

59-
**TBW**
64+
```python=:title=event_parser_decorator.py
65+
from aws_lambda_powertools.utilities.parser import parse, ModelValidationError
66+
from aws_lambda_powertools.utilities.typing import LambdaContext
6067

61-
### Parse function
68+
@event_parser(model=HelloWorldModel)
69+
def handler(event, context: LambdaContext):
70+
pass
6271

63-
**TBW**
72+
handler(event=payload, context=LambdaContext()
73+
```
74+
75+
### parse function
76+
77+
Use this standalone function when you want more control over the data validation process, for example returning a 400 error for malformed payloads.
78+
79+
```python:title=parse_standalone_example.py
80+
from aws_lambda_powertools.utilities.parser import parse, ModelValidationError
81+
82+
payload = {"message": "hello world"}
83+
84+
def my_function():
85+
try:
86+
parsed_payload = parse(event=payload, model=HelloWorldModel) # highlight-line
87+
# payload dict is now parsed into our model
88+
return assert parsed_payload.message == payload["message"]
89+
except ModelValidationError:
90+
return {
91+
"status_code": 400,
92+
"message": "Invalid input"
93+
}
94+
```
6495

6596
## Built-in envelopes
6697

0 commit comments

Comments
 (0)