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): pass target from zod-validator to a hook #695

Merged
merged 4 commits into from
Sep 26, 2024

Conversation

bartekbp
Copy link
Contributor

Today, when we use zod-openapi and pass schema for body and params, we can pass a hook which is called with the result of the validation.

The function doesn't get information about the target being validated. This is problematic when we want to return custom error responses depending on the target being validated. For example, one may want to:

  • return status 415 Unsupported Media Type when body parsing failed due to missing content-type. Such behaviour doesn't make sense for params
  • include in the error message the place where the validation failed. For json we may write in the response body 'body validation failed', for params 'path validation failed' and for query 'search params validation failed' .

This pull request adds this functionality.

Copy link

changeset-bot bot commented Aug 17, 2024

🦋 Changeset detected

Latest commit: 7fc598c

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

Copy link
Contributor

@lucaschultz lucaschultz left a comment

Choose a reason for hiding this comment

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

I just made the same change to the middleware. I'm so glad I double-checked and found your PR! I went a slightly different route by adding another generic to the hook (see comments), which creates a narrower type for the target passed to the hook. Would you consider including that change in your PR, @bartekbp?

packages/zod-validator/src/index.ts Outdated Show resolved Hide resolved
@bartekbp
Copy link
Contributor Author

Thanks for the suggestion @lucaschultz! I applied it!

@yusukebe yusukebe changed the title Proposal: pass target from zod-validator to a hook feat(zod-validator): pass target from zod-validator to a hook Sep 26, 2024
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

Hi @bartekbp

Sorry for the super late reply. Looks good to me. I'll merge this and release a new version now. Thank you!

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.

3 participants