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-validator): support coerce #411

Merged
merged 4 commits into from
Mar 6, 2024
Merged

feat(zod-validator): support coerce #411

merged 4 commits into from
Mar 6, 2024

Conversation

yusukebe
Copy link
Member

@yusukebe yusukebe commented Mar 6, 2024

Fixes #368

Now you can use coerce.

const app = new Hono()

const schema = z.object({
  page: z.coerce.number(),
})

const route = app.get('/page', zValidator('query', schema), (c) => {
  const { page } = c.req.valid('query') // pages is `number`
  return c.json({ page })
})

const client = hc<typeof route>('/')
const res = await client.page.$get({
  query: {
    page: '123', // you should pass `string`
  },
})

const { page } = await res.json() // page is `number`

Copy link

changeset-bot bot commented Mar 6, 2024

🦋 Changeset detected

Latest commit: e9aa13f

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

This PR includes changesets to release 2 packages
Name Type
@hono/zod-validator Minor
@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

@yusukebe yusukebe changed the title fix(zod-validator): support coerce feat(zod-validator): support coerce Mar 6, 2024
@yusukebe yusukebe merged commit 4875e1c into main Mar 6, 2024
1 check passed
@yusukebe yusukebe deleted the fix/support-coerce branch March 6, 2024 10:35
@github-actions github-actions bot mentioned this pull request Mar 6, 2024
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.

Cannot use preprocess and coerce in zValidator.
1 participant