Description
Use case
When you want to extend the built-in schema with your custom schema you can extend the it and overwrite specific parts with of the built-in schema we provide.
const mySchema = z.object({
name: z.string(),
age: z.number()
});
const AlbCustomizedSchema = AlbSchema.extend({
body: mySchema
});
The body
value is z.string()
in the AlbSchema
and we can plug in modifications.
This works for schemas with first the modifiable attribute available in the main schema, such as AlbSchema
, EventBrdigeSchema
or others.
When working with schemas that contain a list of elements the body
part is not accessible, thus customers can't extend it without replicating the entire Record
. For example, the SqsEvent
is:
{
"Records": [
{
"messageId": "059f36b4-87a3-44ab-83d2-661975830a7d",
"receiptHandle": "AQEBwJnKyrHigUMZj6rYigCgxlaS3SLy0a...",
"body": "Test message.",
"attributes": {
"ApproximateReceiveCount": "1",
"SentTimestamp": "1545082649183",
"SenderId": "AIDAIENQZJOLO23YVJ4VO",
"ApproximateFirstReceiveTimestamp": "1545082649185"
},
"messageAttributes": {
"testAttr": {
"stringValue": "100",
"binaryValue": "base64Str",
"dataType": "Number"
}
},
"md5OfBody": "e4e68fb7bd0e697a0ae8f1bb342846b3",
"eventSource": "aws:sqs",
"eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:my-queue",
"awsRegion": "us-east-2"
}
]
}
To overwrite the body, we'd need:
const SqsCustomizedSchema = SqsSchema.extend({
Records: z.array(
z.object({
messageId...
...
body: mySchema,
// the rest
})
)
})
Solution/User Experience
Export the record elements that are nested parts of the built-in schema. For SQS it would be
powertools-lambda-typescript/packages/parser/src/schemas/sqs.ts
Lines 26 to 38 in 1e7175a
This would allow us change only the body field
const SqsMySchema = SqsSchema.extend({
Records: SqsRecordSchema.extend({
body: mySchema
})
})
The requirement for nested Records
exports applies to:
- KafkaRecordSchema
- KinesisFirehoseRecrod
- KinesisFirehoseSqsSchema
- KinesisDataStreamRecord
- SesRecordSchema
- SnsRecordSchema and SnsNotificationSchema
- SqsRecordSchema
While some of this objects are exported in their own files, they are not part of the barrel export and thus customers can't import them.
Alternative solutions
No response
Acknowledgment
- This feature request meets Powertools for AWS Lambda (TypeScript) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
Future readers
Please react with 👍 and your use case to help us understand customer demand.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status