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

@hono/zod-validator - Update README.md to include an example with a custom validator wrapper for improved type inference #922

Merged
merged 2 commits into from
Jan 5, 2025

Conversation

farhadham
Copy link
Contributor

Summary

This PR updates the README.md file to include a custom zValidator wrapper example that improves type inference for validated data.

Changes Made

  • Added a new section titled "Custom Wrapper for Improved Type Inference".
  • Included an example of a generic wrapper that leverages TypeScript generics to ensure correct type inference.

Related Issue

Closes #921.

Checklist

  • Documentation updated.
  • Ready for review.

Copy link

changeset-bot bot commented Jan 4, 2025

⚠️ No Changeset found

Latest commit: 56d9d64

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

})
export const zValidator = <
T extends ZodSchema,
Target extends keyof ValidationTargets = keyof ValidationTargets
Copy link
Member

Choose a reason for hiding this comment

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

I think you can write this line more simply because the default value of Target is not needed:

export const zValidator = <T extends ZodSchema, Target extends keyof ValidationTargets>(
  target: Target,
  schema: T
) =>
  zv(target, schema, (result, c) => {
    if (!result.success) {
      throw new HTTPException(400, { cause: result.error })
    }
  })

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yusukebe You are right!
Thank you for your help, I committed the change

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

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

LGTM!

@yusukebe
Copy link
Member

yusukebe commented Jan 5, 2025

@farhadham

Thank you! I'll merge it.

@yusukebe yusukebe merged commit 8a2d465 into honojs:main Jan 5, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants