middleware
middleware copied to clipboard
feat: add @hono/schema-validator middleware
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)
})
🦋 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
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.
@sebastianwessel
Until that, could you add the CI in .github/workflows
?
@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
Thanks.
From my side, there is no rush, and it's fine to focus on 4.0
Yeah. Please wait a moment.
What is the status of this PR?
Hi @sebastianwessel
v4 is available now. Can you update this PR?
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
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
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?
@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
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.