Skip to content

Maintenance: JSONStringified helper does not infer return type #2945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 of 2 tasks
dreamorosi opened this issue Aug 17, 2024 · 6 comments · Fixed by #2946
Closed
1 of 2 tasks

Maintenance: JSONStringified helper does not infer return type #2945

dreamorosi opened this issue Aug 17, 2024 · 6 comments · Fixed by #2946
Assignees
Labels
completed This item is complete and has been merged/shipped internal PRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.) parser This item relates to the Parser Utility

Comments

@dreamorosi
Copy link
Contributor

Summary

In 2.7.0 we introduced a new helper in the Parser utility, which helps customers to extend built-in Zod schemas that have a stringified body.

The helper, while working at runtime, currently sets the return type of the parsed object to any, which defeats the purpose of the DX improvement that the helper was supposed to bring.

We should

Why is this needed?

So that customers using the helper can have the correct type on the parsed field.

Which area does this relate to?

Parser

Solution

Follow Zod best practices around writing generic functions.

Acknowledgment

Future readers

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

@dreamorosi dreamorosi added internal PRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.) confirmed The scope is clear, ready for implementation parser This item relates to the Parser Utility labels Aug 17, 2024
@dreamorosi dreamorosi self-assigned this Aug 17, 2024
@dreamorosi dreamorosi moved this from Triage to Pending review in Powertools for AWS Lambda (TypeScript) Aug 17, 2024
@aripalo
Copy link

aripalo commented Aug 17, 2024

@dreamorosi "opened this issue 13 minutes ago" … I basically hit this problem like 15 minutes ago 😅

But yeah, this would definitely be an improvement to have JSONStringified return correct types for the handler.

I'm currently working on a personal project with this, but we also use PowerTools and the parser in our company.

We have multiple use cases with APIGatewayProxyEventSchema, APIGatewayProxyEventV2Schema and LambdaFunctionUrlSchema where we can't use an Envelope as we are very much interested about the request headers too; Therefore we're using the XEventSchemas and calling .extend on it, like this:

const MySchema = LambdaFunctionUrlSchema.extend({
  headers: MyCustomHeaderSchema,
  body: JSONStringified(MyCustomBodySchema),
});

And it'd be super useful to have the correct types in event.body. Of couse we can workaround it:

// inside the handler
const body = event.body as z.output<typeof MyCustomBodySchema>;

... but with a team(s) of multiple people developing things, that workaround is easy for someone to forget this workaround and spend time on debugging the incorrect type.

@aripalo
Copy link

aripalo commented Aug 17, 2024

… and of course another workaround is to use Middy's body JSON parser, then just assume the body is already JSON, and finally using MyCustomBodySchema directly on body… but that too is problematic because there's no "direct connection" between the Middy JSON body parser and the zod schema.

@dreamorosi
Copy link
Contributor Author

Hi @aripalo, I already have a PR up that fixes this.

Should be merged on Monday and released mid next week 😊

@aripalo
Copy link

aripalo commented Aug 17, 2024

Fantastic! 😍

(… also the timing so I can use this as an excuse to put my coding project on hold until next week and actually enjoy the weekend 😄)

@github-project-automation github-project-automation bot moved this from Pending review to Coming soon in Powertools for AWS Lambda (TypeScript) Aug 19, 2024
Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Aug 19, 2024
Copy link
Contributor

This is now released under v2.8.0 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Sep 16, 2024
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed This item is complete and has been merged/shipped internal PRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.) parser This item relates to the Parser Utility
Projects
Development

Successfully merging a pull request may close this issue.

2 participants