Skip to content

Feature request: export Records schema building blog for customizations #2811

Closed
@am29d

Description

@am29d

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

const SqsRecordSchema = z.object({
messageId: z.string(),
receiptHandle: z.string(),
body: z.string(),
attributes: SqsAttributesSchema,
messageAttributes: z.record(z.string(), SqsMsgAttributeSchema),
md5OfBody: z.string(),
md5OfMessageAttributes: z.string().optional().nullable(),
eventSource: z.literal('aws:sqs'),
eventSourceARN: z.string(),
awsRegion: z.string(),
});

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:

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

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

Labels

completedThis item is complete and has been merged/shippedfeature-requestThis item refers to a feature request for an existing or new utilitygood-first-issueSomething that is suitable for those who want to start contributingparserThis item relates to the Parser Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions