-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(parser): add JSON string field extension example (#1526)
Co-authored-by: Heitor Lessa <[email protected]>
- Loading branch information
1 parent
a74fda3
commit 1ebb019
Showing
3 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
examples/parser/src/extending_built_in_models_with_json_mypy.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from pydantic import BaseModel, Json | ||
|
||
from aws_lambda_powertools.utilities.parser import event_parser | ||
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventV2Model | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
|
||
class CancelOrder(BaseModel): | ||
order_id: int | ||
reason: str | ||
|
||
|
||
class CancelOrderModel(APIGatewayProxyEventV2Model): | ||
body: Json[CancelOrder] # type: ignore[assignment] | ||
|
||
|
||
@event_parser(model=CancelOrderModel) | ||
def handler(event: CancelOrderModel, context: LambdaContext): | ||
cancel_order: CancelOrder = event.body # type: ignore[assignment] | ||
|
||
assert cancel_order.order_id is not None |
27 changes: 27 additions & 0 deletions
27
examples/parser/src/extending_built_in_models_with_json_validator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import json | ||
|
||
from pydantic import BaseModel, validator | ||
|
||
from aws_lambda_powertools.utilities.parser import event_parser | ||
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventV2Model | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
|
||
class CancelOrder(BaseModel): | ||
order_id: int | ||
reason: str | ||
|
||
|
||
class CancelOrderModel(APIGatewayProxyEventV2Model): | ||
body: CancelOrder # type: ignore[assignment] | ||
|
||
@validator("body", pre=True) | ||
def transform_body_to_dict(cls, value: str): | ||
return json.loads(value) | ||
|
||
|
||
@event_parser(model=CancelOrderModel) | ||
def handler(event: CancelOrderModel, context: LambdaContext): | ||
cancel_order: CancelOrder = event.body | ||
|
||
assert cancel_order.order_id is not None |