honojs / middleware

monorepo for Hono third-party middleware/helpers/wrappers

Home Page:https://hono.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Passing function as `zValidator` Middleware (accessing `c`)

movahhedi opened this issue · comments

I found the need to access the context object (c) in defining my zValidator.
Like this:

// ...
.post(
	"/login/otp",
	zValidator("json"
		z.object({
			otp: z.string().length(c.env.otpLength),
		}),
	),
// ...

I suggest supporting passing a function as the 2nd parameter of zValidator with a signature of somehting like (c: Context) => zValidatorType. Like this:

// ...
.post(
	"/login/otp",
	zValidator("json"
+		(c) => 
		z.object({
			otp: z.string().length(c.env.otpLength),
		}),
	),
// ...