Get typing for c.req.valid when seperate zValidator in another place #1050
Answered
by
yusukebe
harrytran998
asked this question in
Q&A
-
Thank for Hono team, I currently implement our services base on Hono, but the documents seem not have the typescript way to pass Zod Schema Validation to Handler. Here is my code
import { z } from "zod";
export class AuthValidation {
static oauthToken = z.object({
code: z.string(),
originUrl: z.string().url().optional()
});
}
export const authRoute = new Hono<{ Bindings: EnvBindings }>();
authRoute.post("/oauth/token", zValidator("query", AuthValidation.oauthToken), auth0Handler);
Hope anyone can help me, thank you! |
Beta Was this translation helpful? Give feedback.
Answered by
yusukebe
May 1, 2023
Replies: 1 comment 1 reply
-
Hi @harrytran998! You can write the code like the following: export const auth0Handler: Handler<{ Bindings: EnvBindings }, '/oauth/token', { out :{ query: {code: string, originUrl: string}}}> = async c => {
const { code, originUrl } = c.req.valid('query')
return c.text('res')
} However, this is quite verbose. I recommend writing handlers with routing instead of keeping them separate. export const authRoute = new Hono<{ Bindings: EnvBindings}>().basePath('/oauth')
authRoute.post('/token', zValidator('query', AuthValidation.oauthToken), (c)=> {
const { code, originUrl } = c.req.valid('query')
// ...
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
harrytran998
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @harrytran998!
You can write the code like the following:
However, this is quite verbose. I recommend writing handlers with routing instead of keeping them separate.