Skip to content

feat(zod-validator): pass target from zod-validator to a hook#695

Merged
yusukebe merged 4 commits intohonojs:mainfrom
bartekbp:main
Sep 26, 2024
Merged

feat(zod-validator): pass target from zod-validator to a hook#695
yusukebe merged 4 commits intohonojs:mainfrom
bartekbp:main

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.

@changeset-bot
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?

@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