Skip to content

Support for passing Standard Schema into hono/validator #4199

Open
@nakasyou

Description

@nakasyou

What is the feature you are proposing?

Related: honojs/middleware#887

Standard Schema is a spec to make APIs of JavaScript validators unified. I'd like to propose adding Standard Schema support to hono/validator.

import { validator } from 'hono/validator'

import z from 'zod'
import * as v from 'valibot'
import { type } from 'arktype'

app.post('/zod', validator('json', z.number()), ...)
app.post('/valibot', validator('json', v.number()), ...)
app.post('/arktype', validator('json', type('number')), ...)

Currently, we can use this with @hono/standard-validator as a 3rd party middleware, but using it needs to install another package. It can be a little troublesome.

Some frameworks are using Standard Schema natively1 and they are increasing (Astro 6.0 may support Standard Schema2). I think this will be a moderate incentive to use the frameworks supporting Standard Schema.

Pros

  • We will be able to use our favorite validators without installing other dependencies.
  • The amount of code to implement it is very short so we don't need to worry about bundled size.

Cons

  • Currently hono/validator provides very simple APIs and uses minimal algorithm. This can be broken with implementing this.
  • Standard Schema is not a Web Standard.

Footnotes

  1. https://standardschema.dev/#:~:text=What%20tools%20/%20frameworks%20accept%20spec%2Dcompliant%20schemas%3F

  2. https://github.com/standard-schema/standard-schema/issues/21#issuecomment-2931018088

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions