Skip to content

Commit 1977faf

Browse files
authored
docs(batch): use newly supported Json model (#2100)
1 parent f2da40f commit 1977faf

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

Diff for: docs/utilities/batch.md

+7-18
Original file line numberDiff line numberDiff line change
@@ -693,27 +693,22 @@ Inheritance is importance because we need to access message IDs and sequence num
693693

694694
=== "SQS"
695695

696-
```python hl_lines="5 13 22 28"
696+
```python hl_lines="5 14 23 29"
697697
import json
698698

699699
from aws_lambda_powertools import Logger, Tracer
700700
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response
701701
from aws_lambda_powertools.utilities.parser.models import SqsRecordModel
702702
from aws_lambda_powertools.utilities.typing import LambdaContext
703-
from aws_lambda_powertools.utilities.parser import validator, BaseModel
703+
from aws_lambda_powertools.utilities.parser import BaseModel
704+
from aws_lambda_powertools.utilities.parser.types import Json
704705

705706

706707
class Order(BaseModel):
707708
item: dict
708709

709710
class OrderSqsRecord(SqsRecordModel):
710-
body: Order
711-
712-
# auto transform json string
713-
# so Pydantic can auto-initialize nested Order model
714-
@validator("body", pre=True)
715-
def transform_body_to_dict(cls, value: str):
716-
return json.loads(value)
711+
body: Json[Order] # deserialize order data from JSON string
717712

718713
processor = BatchProcessor(event_type=EventType.SQS, model=OrderSqsRecord)
719714
tracer = Tracer()
@@ -732,13 +727,14 @@ Inheritance is importance because we need to access message IDs and sequence num
732727

733728
=== "Kinesis Data Streams"
734729

735-
```python hl_lines="5 14 25 29 35"
730+
```python hl_lines="5 15 19 23 29 36"
736731
import json
737732

738733
from aws_lambda_powertools import Logger, Tracer
739734
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response
740735
from aws_lambda_powertools.utilities.parser.models import KinesisDataStreamRecordPayload, KinesisDataStreamRecord
741736
from aws_lambda_powertools.utilities.parser import BaseModel, validator
737+
from aws_lambda_powertools.utilities.parser.types import Json
742738
from aws_lambda_powertools.utilities.typing import LambdaContext
743739

744740

@@ -747,14 +743,7 @@ Inheritance is importance because we need to access message IDs and sequence num
747743

748744

749745
class OrderKinesisPayloadRecord(KinesisDataStreamRecordPayload):
750-
data: Order
751-
752-
# auto transform json string
753-
# so Pydantic can auto-initialize nested Order model
754-
@validator("data", pre=True)
755-
def transform_message_to_dict(cls, value: str):
756-
# Powertools KinesisDataStreamRecord already decodes b64 to str here
757-
return json.loads(value)
746+
data: Json[Order]
758747

759748

760749
class OrderKinesisRecord(KinesisDataStreamRecord):

0 commit comments

Comments
 (0)