Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validating all parts of the request #433

Open
ryanleecode opened this issue Mar 24, 2024 · 1 comment
Open

validating all parts of the request #433

ryanleecode opened this issue Mar 24, 2024 · 1 comment

Comments

@ryanleecode
Copy link

zod validation is sequential right now. Meaning if the headers are invalid it will fail-first and not return any information about other parts of the request that might be invalid. Also the zod error has no context about the origin. you cant tell if its from header, query, body, etc by default.

@yusukebe
Copy link
Member

yusukebe commented Mar 28, 2024

Hi @ryanleecode

zod validation is sequential right now.

Hono's Validator validates the specified content, param, header, query, etc. By sequential, do you mean when use multiple validators?

In that case, how about using a custom Hook?

app.get(
  '/posts/:id',
  zValidator(
    'param',
    z.object({
      id: z.coerce.number()
    }),
    (result, c) => {
      if (!result.success) {
        return c.json(
          {
            'error in': 'param'
          },
          400
        )
      }
    }
  ),
  zValidator(
    'query',
    z.object({
      page: z.coerce.number()
    }),
    (result, c) => {
      if (!result.success) {
        return c.json(
          {
            'error in': 'query'
          },
          400
        )
      }
    }
  ),
  (c) => {
    const { page } = c.req.valid('query')
    // ...
    return c.json({
    })
  }
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants