add optional passthrough to options
parent
abb260632f
commit
73c961310a
|
@ -1,7 +1,7 @@
|
||||||
import type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono'
|
import type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono'
|
||||||
import { validator } from 'hono/validator'
|
import { validator } from 'hono/validator'
|
||||||
import { ZodObject } from 'zod'
|
import { ZodObject } from 'zod'
|
||||||
import type { ZodError, ZodSchema, z } from 'zod'
|
import { ZodError, ZodSchema, z } from 'zod'
|
||||||
|
|
||||||
export type Hook<
|
export type Hook<
|
||||||
T,
|
T,
|
||||||
|
@ -43,7 +43,8 @@ export const zValidator = <
|
||||||
>(
|
>(
|
||||||
target: Target,
|
target: Target,
|
||||||
schema: T,
|
schema: T,
|
||||||
hook?: Hook<z.infer<T>, E, P, Target>
|
hook?: Hook<z.infer<T>, E, P, Target>,
|
||||||
|
opt?: { passthroughObject?: boolean }
|
||||||
): MiddlewareHandler<E, P, V> =>
|
): MiddlewareHandler<E, P, V> =>
|
||||||
// @ts-expect-error not typed well
|
// @ts-expect-error not typed well
|
||||||
validator(target, async (value, c) => {
|
validator(target, async (value, c) => {
|
||||||
|
@ -63,7 +64,12 @@ export const zValidator = <
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await schema.safeParseAsync(validatorValue)
|
let result: z.infer<T>;
|
||||||
|
if (opt && "passthrough" in opt) {
|
||||||
|
result = await schema.passthrough().safeParseAsync(validatorValue)
|
||||||
|
} else {
|
||||||
|
result = await schema.safeParseAsync(validatorValue)
|
||||||
|
}
|
||||||
|
|
||||||
if (hook) {
|
if (hook) {
|
||||||
const hookResult = await hook({ data: validatorValue, ...result, target }, c)
|
const hookResult = await hook({ data: validatorValue, ...result, target }, c)
|
||||||
|
|
Loading…
Reference in New Issue