honojs-middleware/packages/conform-validator
Jonathan Haines 4d67af162f
test(workspace): upgrade to vitest v3 (#1009)
* test(workspace): upgrade to vitest v3

Fixes #1007

* chore(standard-validator): add vitest type to `tsconfig.json`

* chore: update `yarn.lock`

* chore(zod-openapi): bump `typescript`

* chore(typia-validator): make it ESM

* ci(bun-transpiler): fix Bun to v1.1.32

---------

Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
2025-03-12 12:52:15 +09:00
..
src chore: use the latest eslint and `@hono/eslint-config` (#904) 2024-12-25 18:08:43 +09:00
test chore: use the latest eslint and `@hono/eslint-config` (#904) 2024-12-25 18:08:43 +09:00
CHANGELOG.md Version Packages (#675) 2024-07-31 23:04:52 +09:00
README.md feat: Conform Validator Middleware (#666) 2024-07-31 22:55:02 +09:00
package.json test(workspace): upgrade to vitest v3 (#1009) 2025-03-12 12:52:15 +09:00
tsconfig.cjs.json feat: Conform Validator Middleware (#666) 2024-07-31 22:55:02 +09:00
tsconfig.esm.json feat: Conform Validator Middleware (#666) 2024-07-31 22:55:02 +09:00
tsconfig.json feat: Conform Validator Middleware (#666) 2024-07-31 22:55:02 +09:00
vitest.config.ts test(workspace): upgrade to vitest v3 (#1009) 2025-03-12 12:52:15 +09:00

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

uttk https://github.com/uttk

License

MIT