diff --git a/packages/zod-validator/src/index.ts b/packages/zod-validator/src/index.ts index 8a96e9d3..a630869e 100644 --- a/packages/zod-validator/src/index.ts +++ b/packages/zod-validator/src/index.ts @@ -1,7 +1,7 @@ import type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono' import { validator } from 'hono/validator' import { ZodObject } from 'zod' -import type { ZodError, ZodSchema, z } from 'zod' +import { ZodError, ZodSchema, z } from 'zod' export type Hook< T, @@ -27,23 +27,24 @@ export const zValidator = < Out = z.output, I extends Input = { in: HasUndefined extends true - ? { - [K in Target]?: In extends ValidationTargets[K] - ? In - : { [K2 in keyof In]?: ValidationTargets[K][K2] } - } - : { - [K in Target]: In extends ValidationTargets[K] - ? In - : { [K2 in keyof In]: ValidationTargets[K][K2] } - } + ? { + [K in Target]?: In extends ValidationTargets[K] + ? In + : { [K2 in keyof In]?: ValidationTargets[K][K2] } + } + : { + [K in Target]: In extends ValidationTargets[K] + ? In + : { [K2 in keyof In]: ValidationTargets[K][K2] } + } out: { [K in Target]: Out } }, V extends I = I >( target: Target, schema: T, - hook?: Hook, E, P, Target> + hook?: Hook, E, P, Target>, + opt?: { passthroughObject?: boolean } ): MiddlewareHandler => // @ts-expect-error not typed well validator(target, async (value, c) => { @@ -63,7 +64,12 @@ export const zValidator = < ) } - const result = await schema.safeParseAsync(validatorValue) + let result: z.infer; + if (opt && "passthrough" in opt) { + result = await schema.passthrough().safeParseAsync(validatorValue) + } else { + result = await schema.safeParseAsync(validatorValue) + } if (hook) { const hookResult = await hook({ data: validatorValue, ...result, target }, c)