Skip to content
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

Feature request: add safeParse option #2204

Closed
1 of 2 tasks
am29d opened this issue Mar 11, 2024 · 4 comments · Fixed by #2244
Closed
1 of 2 tasks

Feature request: add safeParse option #2204

am29d opened this issue Mar 11, 2024 · 4 comments · Fixed by #2244
Assignees
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility parser This item relates to the Parser Utility

Comments

@am29d
Copy link
Contributor

am29d commented Mar 11, 2024

Use case

If you don't want Zod to throw errors when validation fails, use .safeParse. This method returns an object containing either the successfully parsed data or a ZodError instance containing detailed information about the validation problems.

There are cases when customers used safeParse in their previous implementation and expect to have this option in the powertools parser utility. Thus, we should add an option to that will determine wether we call parse or safeParse internally in built-in schemas and envelopes

Solution/User Experience

We can expand the ParserOptions by adding safeParse argument, and call it directly on decorator

import { SqsSchema } from '@aws-lambda-powertools/parser/schemas';
import { SqsEvent } from '@aws-lambda-powertools/parser/types';

export type ParserOptions<S extends ZodSchema> = {
  schema: S;
  envelope?: Envelope;
  safeParse?: boolean;
};


class Lambda implements LambdaInterface {
  @parser({ schema: SqsSchema, safeParse: true })
  public async handler(
    event: SqsEvent,
    _context: Context
  ): Promise<unknown> {
    return event;
  }

This would also require an expansion of the envelopes to support safeParse option.

Alternative solutions

No response

Acknowledgment

Future readers

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

@am29d am29d added triage This item has not been triaged by a maintainer, please wait feature-request This item refers to a feature request for an existing or new utility labels Mar 11, 2024
@am29d am29d added this to the Parser - Beta Release milestone Mar 11, 2024
@am29d am29d self-assigned this Mar 11, 2024
@am29d am29d added confirmed The scope is clear, ready for implementation parser This item relates to the Parser Utility and removed triage This item has not been triaged by a maintainer, please wait labels Mar 11, 2024
@dreamorosi dreamorosi moved this from Triage to Working on it in Powertools for AWS Lambda (TypeScript) Mar 11, 2024
@dreamorosi
Copy link
Contributor

Will this value be enabled by default?

@am29d
Copy link
Contributor Author

am29d commented Mar 19, 2024

Will this value be enabled by default?

No.

@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 Mar 21, 2024
@dreamorosi dreamorosi linked a pull request Mar 21, 2024 that will close this issue
9 tasks
@dreamorosi dreamorosi moved this from Working on it to Shipped in Powertools for AWS Lambda (TypeScript) Mar 21, 2024
@dreamorosi dreamorosi 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 Mar 21, 2024
@github-project-automation github-project-automation bot moved this from Shipped to Coming soon in Powertools for AWS Lambda (TypeScript) Mar 21, 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.

@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Apr 18, 2024
@dreamorosi
Copy link
Contributor

This is now released under v2.1.0 version!

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 feature-request This item refers to a feature request for an existing or new utility parser This item relates to the Parser Utility
Projects
Development

Successfully merging a pull request may close this issue.

2 participants