middleware icon indicating copy to clipboard operation
middleware copied to clipboard

feat: add @hono/schema-validator middleware

Open sebastianwessel opened this issue 1 year ago • 5 comments

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)
})

sebastianwessel avatar Jan 30 '24 14:01 sebastianwessel

🦋 Changeset detected

Latest commit: d9b4896d62d733910b1cd314df8b1fc0fd0d4b73

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

changeset-bot[bot] avatar Jan 30 '24 14:01 changeset-bot[bot]

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 avatar Feb 02 '24 13:02 yusukebe

@sebastianwessel

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

yusukebe avatar Feb 02 '24 14:02 yusukebe

@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

sebastianwessel avatar Feb 02 '24 15:02 sebastianwessel

@sebastianwessel

Thanks.

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

Yeah. Please wait a moment.

yusukebe avatar Feb 02 '24 22:02 yusukebe

What is the status of this PR?

inetol avatar Oct 17 '24 21:10 inetol

Hi @sebastianwessel

v4 is available now. Can you update this PR?

yusukebe avatar Nov 05 '24 02:11 yusukebe

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

sebastianwessel avatar Nov 06 '24 09:11 sebastianwessel

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.

yusukebe avatar Nov 07 '24 06:11 yusukebe

@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 avatar Nov 07 '24 08:11 sebastianwessel

@yusukebe renamed it.

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

sebastianwessel avatar Nov 11 '24 11:11 sebastianwessel

@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.

yusukebe avatar Nov 12 '24 20:11 yusukebe