Skip to content

Commit 4d9b2a9

Browse files
committed
docs: initial sketch of parser docs
1 parent 585f708 commit 4d9b2a9

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

docs/content/utilities/parser.mdx

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: Parser
3+
description: Utility
4+
---
5+
6+
7+
import Note from "../../src/components/Note"
8+
9+
<Note type="warning">
10+
It requires an extra dependency before using it.
11+
</Note><br/>
12+
13+
This utility provides data parsing and deep validation using [Pydantic](https://pydantic-docs.helpmanual.io/).
14+
15+
**Key features**
16+
17+
* Defines data in pure Python classes, then parse, validate and extract only what you want
18+
* Built-in envelopes to unwrap, extend, and validate popular event sources payloads
19+
* Enforces type hints at runtime with user friendly errors
20+
21+
**Extra dependency**
22+
23+
<Note type="info">
24+
This will install <a href="https://github.com/samuelcolvin/pydantic">pydantic</a> and <a href="https://github.com/python/typing/tree/master/typing_extensions).">typing_extensions</a>
25+
</Note><br/>
26+
27+
Install parser's extra dependencies using **`pip install aws-lambda-powertools[pydantic]`**.
28+
29+
## Defining models
30+
31+
You can define models to parse incoming events by inheriting from `BaseModel`.
32+
33+
```python:title=hello_world_model.py
34+
from aws_lambda_powertools.utilities.parser import BaseModel
35+
36+
class HelloWorldModel(BaseModel):
37+
message: str
38+
39+
payload = {"message": "hello world"}
40+
parsed_payload = HelloWorldModel(**payload)
41+
42+
assert parsed_payload.message == payload["message"]
43+
```
44+
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/).
46+
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+
49+
## Parsing events
50+
51+
You can parse inbound events using **parser** decorator.
52+
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.

docs/gatsby-config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ module.exports = {
3535
'utilities/batch',
3636
'utilities/typing',
3737
'utilities/validation',
38-
'utilities/data_classes'
38+
'utilities/data_classes',
39+
'utilities/parser'
3940
],
4041
},
4142
navConfig: {

0 commit comments

Comments
 (0)