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(validator): Introduce responseValidator #3843

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

EdamAme-x
Copy link
Contributor

@EdamAme-x EdamAme-x commented Jan 21, 2025

Resolve: #2439
Related: honojs/middleware#184

This is proposal.
It has not been tested, nor is it fully molded.

import { Hono } from '../preset/quick'
import { responseValidator } from '.'

const app = new Hono()

app
  .use(
    '*',
    responseValidator('text', (value, c) => {
      if (!/\d/.test(value)) {
        return c.text('Invalid!', 400)
      }
    })
  )
  .get('/', (c) => {
    return c.text(c.validate('hello world at ' + Date.now()))
  })
  .get('/invalid', (c) => {
    return c.text(c.validate('hello world at ' + NaN))
  })

export default app

The data is kept intact in the context.

The author should do the following, if applicable

No Prepared

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@EdamAme-x EdamAme-x marked this pull request as draft January 21, 2025 02:08
@EdamAme-x EdamAme-x changed the title feat: introduce responseValidator feat: Introduce responseValidator Jan 21, 2025
@EdamAme-x
Copy link
Contributor Author

EdamAme-x commented Jan 21, 2025

What do you think about this API? @yusukebe

@EdamAme-x
Copy link
Contributor Author

The only code added to context is the following, with no major changes to the bundle size.
this.validate=a=>(this.validateData=a,a);

Copy link

codecov bot commented Jan 21, 2025

Codecov Report

Attention: Patch coverage is 8.60215% with 85 lines in your changes missing coverage. Please review.

Project coverage is 90.57%. Comparing base (ac4d42c) to head (6b87b95).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/validator/response-validator.ts 7.81% 59 Missing ⚠️
src/validator/response-validator.sandbox.ts 0.00% 24 Missing ⚠️
src/context.ts 60.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3843      +/-   ##
==========================================
- Coverage   91.31%   90.57%   -0.75%     
==========================================
  Files         161      163       +2     
  Lines       10240    10333      +93     
  Branches     2999     2889     -110     
==========================================
+ Hits         9351     9359       +8     
- Misses        888      973      +85     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@EdamAme-x EdamAme-x changed the title feat: Introduce responseValidator feat(validator): Introduce responseValidator Jan 21, 2025
@EdamAme-x
Copy link
Contributor Author

In the future, it may be necessary to properly infer the output type for RPC.

@belgattitude
Copy link

@EdamAme-x just bringing to your attention that there's an initiative of a standard schema specification that seems to be implemented in latest zod, valibot beta, arktypes...

I don't know if it affects your PR but in case it might be good to have a look.

BTW I'm just starting some internal projects with hono and nextjs and it looks great !

@EdamAme-x
Copy link
Contributor Author

@belgattitude
Thanks for letting us know!

There is already an interesting discussion about standard schema in the honojs/middleware repository, if you want to check it out!

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.

Response serialization and validation
2 participants