Skip to content

Feature request: add helper function to transform and pipe JSON strings to a custom schema #2893

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 6 tasks
am29d opened this issue Aug 7, 2024 · 2 comments · Fixed by #2901
Closed
1 of 6 tasks
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 Aug 7, 2024

Use case

As proposed by @dreamorosi in #2892 it'd be great to extract the helper function from the docs to a dedicated export, so users can utilize it from the IDE instead of consulting the docs.

  • add helper function
  • add exports
  • add tests all applicable built-in schemas that can have JSON stringified payload
  • change parser docs how to use the helper function

Solution/User Experience

import { z, type ZodSchema } from 'zod';

const JSONStringified = (schema: ZodSchema) =>
    z
      .string()
      .transform((str, ctx) => {
        try {
          return JSON.parse(str);
        } catch (err) {
          ctx.addIssue({
            code: 'custom',
            message: 'Invalid JSON',
          });
        }
      })
      .pipe(schema);

export { JSONStringified };

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 Aug 7, 2024
@am29d am29d removed the triage This item has not been triaged by a maintainer, please wait label Aug 7, 2024
@am29d am29d self-assigned this Aug 7, 2024
@am29d am29d added the parser This item relates to the Parser Utility label Aug 7, 2024
@am29d am29d moved this from Triage to Working on it in Powertools for AWS Lambda (TypeScript) Aug 7, 2024
@dreamorosi dreamorosi added the confirmed The scope is clear, ready for implementation label Aug 7, 2024
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (TypeScript) Aug 8, 2024
Copy link
Contributor

github-actions bot commented Aug 8, 2024

⚠️ 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 8, 2024
Copy link
Contributor

github-actions bot commented Aug 8, 2024

This is now released under v2.7.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 Aug 8, 2024
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Aug 9, 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 feature-request This item refers to a feature request for an existing or new utility parser This item relates to the Parser Utility
Projects
2 participants