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

feat(zod-openapi): use z.input to infer input types of the request #286

Merged

Conversation

fahchen
Copy link
Contributor

@fahchen fahchen commented Dec 4, 2023

Hi,

This PR change z.infer to z.input to infer input types of the request, for example in params, query, and body.

For most cases, z.infer and z.input are the same, but we can add transform phase to it, and these two values can diverge.

const Schema = z.string().transform(Number)
type Input = z.input<typeof Schema> // string
type Output = z.output<typeof Schema> // number
type Infer = z.infer<typeof Schema> // number

for the above example we can get a number value from inputs

  const ParamsSchema = z.object({
--    id: z.string().openapi({
++    id: z.string().transform(Number).openapi({
      param: {
        name: 'id',
        in: 'path',
      },
      example: 123,
    }),
  })

    app.openapi(route, (c) => {
      const { id } = c.req.valid('param')
--      typeof id // "string"
++      typeof id // "number"

      // ...
    })

Copy link

changeset-bot bot commented Dec 4, 2023

🦋 Changeset detected

Latest commit: 3d8cb4a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/zod-openapi Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@fahchen fahchen force-pushed the feat/fahchen/use-zod-input-to-infer-input-types branch from 85b8e10 to b168422 Compare December 4, 2023 10:32
@yusukebe
Copy link
Member

yusukebe commented Dec 5, 2023

Hi @fahchen

Looks good!

One thing, please resolve the conflict. You can retain undefined instead of using {}:

const res = await app.request('/doc', undefined, { TITLE: 'My API' })

@fahchen
Copy link
Contributor Author

fahchen commented Dec 5, 2023

Hi @fahchen

Looks good!

One thing, please resolve the conflict. You can retain undefined instead of using {}:

const res = await app.request('/doc', undefined, { TITLE: 'My API' })

Hi @yusukebe, thanks for your review, I have resolved the conflict.

By the way, is it necessary for me to rebase this branch prior to merging?

@yusukebe
Copy link
Member

yusukebe commented Dec 5, 2023

@fahchen

Thanks!

By the way, is it necessary for me to rebase this branch prior to merging?

No. I'll squash and merge this PR now.

@yusukebe yusukebe merged commit 8178ba0 into honojs:main Dec 5, 2023
1 check passed
@github-actions github-actions bot mentioned this pull request Dec 5, 2023
@yusukebe
Copy link
Member

yusukebe commented Dec 5, 2023

@fahchen

Just published the new release. Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants