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 4 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: d9b4896

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.

@inetol
Copy link

inetol commented Oct 17, 2024

What is the status of this PR?

@yusukebe
Copy link
Member

yusukebe commented Nov 5, 2024

Hi @sebastianwessel

v4 is available now. Can you update this PR?

@sebastianwessel sebastianwessel force-pushed the feat/add_schema_validator_package branch from 54bf5c9 to 434539e Compare November 6, 2024 09:33
@sebastianwessel
Copy link
Author

Updated the pr

Also changed the typeschema packages to their new nameings - here I'm not sure if this package should use the @typeschema/main or @typeschema/all (currently used)

see: https://typeschema.com/#quickstart

@yusukebe
Copy link
Member

yusukebe commented Nov 7, 2024

Hi @sebastianwessel

Why not change the name to typeschema-validator or type-schema-validator instead of schema-validator? I think schema is too generic, and that library is called TypeSchema.

@sebastianwessel
Copy link
Author

@yusukebe
makes sense. what do you think about the included packages.
should it use all which is larger, but without the need to add peer-dependencies, or should it use main which is smaller, but requires to in install peer deps?

@sebastianwessel
Copy link
Author

@yusukebe renamed it.

I also switched to the main instead of all. Here, the user needs to add peer dependencies, depending on the requirements

],
"scripts": {
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest",
"build:cjs": "tsc -p tsconfig.cjs.json",
Copy link
Member

Choose a reason for hiding this comment

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

Can you use tsup to build and vitest for testing, referring to the hello middleware?

https://github.com/honojs/middleware/tree/main/packages/hello

Copy link
Member

Choose a reason for hiding this comment

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

This file's change is not needed.

@yusukebe
Copy link
Member

@sebastianwessel

Thanks.

I also switched to the main instead of all. Here, the user needs to add peer dependencies, depending on the requirements

I also think it's good. It's better to write the instructions on README.

I've left some comments. Please check them.

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