Skip to content

Commit eeabc0f

Browse files
committed
docs: address Ran's feedback
Signed-off-by: heitorlessa <lessa@amazon.co.uk>
1 parent 57681a2 commit eeabc0f

File tree

1 file changed

+50
-9
lines changed

1 file changed

+50
-9
lines changed

docs/content/utilities/parser.mdx

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,6 @@ class Order(BaseModel):
4848

4949
These are simply Python classes that inherit from BaseModel. **Parser** enforces type hints declared in your model at runtime.
5050

51-
<!-- TODO: We need a dedicated section to expand on these great features from Pydantic -->
52-
<!-- 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/). -->
53-
54-
<Note type="info">
55-
<strong>Looking to auto-generate models from JSON, YAML, JSON Schemas, OpenApi, etc?</strong>
56-
<br/><br/>
57-
Use Koudai Aono's <a href="https://github.com/koxudaxi/datamodel-code-generator">data model code generation tool for Pydantic</a>
58-
</Note><br/>
59-
6051
## Parsing events
6152

6253
You can parse inbound events using **event_parser** decorator, or the standalone `parse` function. Both are also able to parse either dictionary or JSON string as an input.
@@ -460,6 +451,56 @@ parse(model=UserModel, event=payload)
460451
</Note><br/>
461452

462453

454+
## Advanced use cases
455+
456+
<Note type="info">
457+
<strong>Looking to auto-generate models from JSON, YAML, JSON Schemas, OpenApi, etc?</strong>
458+
<br/><br/>
459+
Use Koudai Aono's <a href="https://github.com/koxudaxi/datamodel-code-generator">data model code generation tool for Pydantic</a>
460+
</Note><br/>
461+
462+
There are number of advanced use cases well documented in Pydantic's doc such as creating [immutable models](https://pydantic-docs.helpmanual.io/usage/models/#faux-immutability), [declaring fields with dynamic values]((https://pydantic-docs.helpmanual.io/usage/models/#field-with-dynamic-default-value)) e.g. UUID, and [helper functions to parse models from files, str](https://pydantic-docs.helpmanual.io/usage/models/#helper-functions), etc.
463+
464+
Two possible unknown use cases are Models and exception' serialization. Models have methods to [export them](https://pydantic-docs.helpmanual.io/usage/exporting_models/) as `dict`, `JSON`, `JSON Schema`, and Validation exceptions can be exported as JSON.
465+
466+
```python:title=serializing_models_exceptions.py
467+
from aws_lambda_powertools.utilities import Logger
468+
from aws_lambda_powertools.utilities.parser import parse, BaseModel, ValidationError, validator
469+
470+
logger = Logger(service="user")
471+
472+
class UserModel(BaseModel):
473+
username: str
474+
password1: str
475+
password2: str
476+
477+
payload = {
478+
"username": "universe",
479+
"password1": "myp@ssword",
480+
"password2": "repeat password"
481+
}
482+
483+
def my_function():
484+
try:
485+
return parse(model=UserModel, event=payload)
486+
except ValidationError as e:
487+
logger.exception(e.json()) # highlight-line
488+
return {
489+
"status_code": 400,
490+
"message": "Invalid username"
491+
}
492+
493+
User: UserModel = my_function()
494+
# highlight-start
495+
user_dict = User.dict()
496+
user_json = User.json()
497+
user_json_schema_as_dict = User.schema()
498+
user_json_schema_as_json = User.schema_json(indent=2)
499+
# highlight-end
500+
```
501+
502+
These can be quite useful when manipulating models that later need to be serialized as inputs for services like DynamoDB, EventBridge, etc.
503+
463504
## FAQ
464505

465506
**When should I use parser vs data_classes utility?**

0 commit comments

Comments
 (0)