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: add @hono/schema-validator middleware #370

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sebastianwessel
Copy link

This middleware leverages TypeSchema, offering an abstraction layer that facilitates interaction with a variety of validation libraries through a unified interface.

Consequently, there is no immediate requirement to develop a dedicated middleware for each validation library.
This not only reduces maintenance efforts but also extends support to validation libraries that may currently lack compatibility.

List of possible validation libs: https://typeschema.com/

import { z } from 'zod'
import { schemaValidator, type ValidationError } from '@hono/schema-validator'

// schema could also some other validation lib
// see https://typeschema.com/#coverage
const schema = z.object({
  name: z.string(),
  age: z.number(),
})

app.post('/author', schemaValidator('json', schema), (c) => {
  const data = c.req.valid('json')
  return c.json({
    success: true,
    message: `${data.name} is ${data.age}`,
  })
})

app.onError(async (err, c) => {
  if (err instanceof ValidationError) {
    return c.json(err, err.status)
  }
  return c.text('Internal Server Error', 500)
})

Copy link

changeset-bot bot commented Jan 30, 2024

🦋 Changeset detected

Latest commit: 54bf5c9

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

This PR includes changesets to release 1 package
Name Type
@hono/schema-validator Major

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
Copy link
Member

yusukebe commented Feb 2, 2024

Hi @sebastianwessel !

This is super interesting. I'd like to merge this, but please wait a bit. We'll release the v4 of Hono core soon. There will be some breaking changes for the validator. So it is better to wait until v4 is released than to merge now.

@yusukebe
Copy link
Member

yusukebe commented Feb 2, 2024

@sebastianwessel

Until that, could you add the CI in .github/workflows?

@sebastianwessel
Copy link
Author

@yusukebe

ci-schema-validator.yml is added to github workflows
From my side, there is no rush, and it's fine to focus on 4.0

@yusukebe
Copy link
Member

yusukebe commented Feb 2, 2024

@sebastianwessel

Thanks.

From my side, there is no rush, and it's fine to focus on 4.0

Yeah. Please wait a moment.

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.

None yet

2 participants