Skip to content

docs: add parser example for built-in extension using transform and pipe #2892

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
wants to merge 2 commits into from

Conversation

am29d
Copy link
Contributor

@am29d am29d commented Aug 7, 2024

Summary

This PR add an example how to extend a built-in schema with transform and pipe zod functions. This allows more complex transformations and schema chaining.

I have removed the missing examples in api-gateway.md, otherwise you can't build docs.

Changes

Please provide a summary of what's being changed

Please add the issue number below, if no issue is present the PR might get blocked and not be reviewed

Issue number: closes #2891


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@am29d am29d self-assigned this Aug 7, 2024
@am29d am29d requested a review from a team August 7, 2024 08:27
@am29d am29d requested a review from a team as a code owner August 7, 2024 08:27
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Aug 7, 2024
@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Aug 7, 2024
dreamorosi
dreamorosi previously approved these changes Aug 7, 2024
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great addition to the docs!

I think we should also add this helper to our utility and export it, so that customers can just use it right away and have less boilerplate code.

For example, we could create a new file under packages/parser/src/helpers.ts and add a new schema helper like:

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 };

so that later, customers could import it in their code and use it like:

import { JSONStringified } from '@aws-lambda-powertools/parser/helpers';
import { APIGatewayProxyEventSchema } from '@aws-lambda-powertools/parser/schemas/api-gateway';

const orderSchema = z.object({
  orderId: z.number(),
  order: z.object({
    amount: z.number()
  })
});

const orderEventSchema = APIGatewayProxyEventSchema.extend({
  body: JSONStringified(orderSchema)
});

Putting myself into this situation, I would find it very useful to do this rather than going back to the docs every time, also in the future we could add more of these helpers if they come up. But for now at least we have it documented!

I leave it to you if you want to do it now, or open an issue so that someone can pick it up before the next release.

Copy link

sonarqubecloud bot commented Aug 7, 2024

@am29d
Copy link
Contributor Author

am29d commented Aug 7, 2024

You are right, having a small helper function is much better than pointing to the docs. I will create a separate issue for that

@am29d
Copy link
Contributor Author

am29d commented Aug 7, 2024

Closing in favour of new helper function and docs.

@am29d am29d closed this Aug 7, 2024
@dreamorosi dreamorosi deleted the docs/parser-extend-schema branch August 20, 2024 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation size/L PRs between 100-499 LOC
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Docs: add parser example to extend built-in schema with JSON parse
2 participants