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
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.
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
+
<Notetype="info">
49
+
<strong>Looking to auto-generate models from JSON, YAML, JSON Schemas, OpenApi, etc?</strong>
50
+
<br/><br/>
51
+
Use Koudai Aono's <ahref="https://github.com/koxudaxi/datamodel-code-generator">data model code generator tool for Pydantic</a>
52
+
</Note><br/>
48
53
49
54
## Parsing events
50
55
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.
52
57
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
54
59
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.
56
61
57
-
### event_parser decorator
62
+
`event_parser` decorator will throw a `ModelValidationError` if your event cannot be parsed according to the model.
58
63
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
60
67
61
-
### Parse function
68
+
@event_parser(model=HelloWorldModel)
69
+
defhandler(event, context: LambdaContext):
70
+
pass
62
71
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
0 commit comments