* feat: add `@hono/conform-validator` to packages * docs: Add the conform-validtor middleware usage to README.md * docs: fix README * refactor: Fix tests to use HTTPException * fix: update devDependencies in conform-validator * chore: add github workflows for conform-validator * feat: add changesets * fix: Init conform-validator version to 0.0.0 for changesets * feat: Add a hook option to `conformValidator()` * feat: Fixed the conformValidator to return an error response when a validation error occurs * fix: Fixed node version used in CI from 18.x to 20.x * fix: Fix to use tsup in build command * chore: delete `.skip` from `it` in test files. * chore: fix title in test files. * fix: Fixed to return 400 response when the request body is not FormData * chore: fixed to change patch to major in changeset. * chore: Removed unused libraries |
||
---|---|---|
.. | ||
src | ||
test | ||
README.md | ||
package.json | ||
tsconfig.cjs.json | ||
tsconfig.esm.json | ||
tsconfig.json | ||
vitest.config.ts |
README.md
Conform validator middleware for Hono
The validator middleware using conform for Hono applications. This middleware allows you to validate submitted FormValue and making better use of Hono RPC.
Usage
Zod:
import { z } from 'zod'
import { parseWithZod } from '@conform-to/zod'
import { conformValidator } from '@hono/conform-validator'
import { HTTPException } from 'hono/http-exception'
const schema = z.object({
name: z.string(),
age: z.string(),
})
app.post(
'/author',
conformValidator((formData) => parseWithZod(formData, { schema })),
(c) => {
const submission = c.req.valid('form')
const data = submission.value
return c.json({ success: true, message: `${data.name} is ${data.age}` })
}
)
Yup:
import { object, string } from 'yup'
import { parseWithYup } from '@conform-to/yup'
import { conformValidator } from '@hono/conform-validator'
import { HTTPException } from 'hono/http-exception'
const schema = object({
name: string(),
age: string(),
})
app.post(
'/author',
conformValidator((formData) => parseWithYup(formData, { schema })),
(c) => {
const submission = c.req.valid('form')
const data = submission.value
return c.json({ success: true, message: `${data.name} is ${data.age}` })
}
)
Valibot:
import { object, string } from 'valibot'
import { parseWithValibot } from 'conform-to-valibot'
import { conformValidator } from '@hono/conform-validator'
import { HTTPException } from 'hono/http-exception'
const schema = object({
name: string(),
age: string(),
})
app.post(
'/author',
conformValidator((formData) => parseWithYup(formData, { schema })),
(c) => {
const submission = c.req.valid('form')
const data = submission.value
return c.json({ success: true, message: `${data.name} is ${data.age}` })
}
)
Custom Hook Option
By default, conformValidator()
returns a SubmissionResult
when a validation error occurs. If you wish to change this behavior, or if you wish to perform common processing, you can modify the response by passing a function as the second argument.
app.post(
'/author',
conformValidator(
(formData) => parseWithYup(formData, { schema })
(submission, c) => {
if(submission.status !== 'success') {
return c.json({ success: false, message: 'Bad Request' }, 400)
}
}
),
(c) => {
const submission = c.req.valid('form')
const data = submission.value
return c.json({ success: true, message: `${data.name} is ${data.age}` })
}
)
[!NOTE] if a response is returned by the Hook function, subsequent middleware or handler functions will not be executed. see more.
Author
License
MIT