2025-01-08 18:48:44 +08:00
|
|
|
import type { Context, Env, Input, MiddlewareHandler, TypedResponse, ValidationTargets } from 'hono'
|
2023-07-30 07:07:44 +08:00
|
|
|
import { validator } from 'hono/validator'
|
2024-12-25 17:08:43 +08:00
|
|
|
import type {
|
|
|
|
GenericSchema,
|
|
|
|
GenericSchemaAsync,
|
|
|
|
InferInput,
|
|
|
|
InferOutput,
|
|
|
|
SafeParseResult,
|
|
|
|
} from 'valibot'
|
2024-05-13 05:56:42 +08:00
|
|
|
import { safeParseAsync } from 'valibot'
|
2023-07-30 07:07:44 +08:00
|
|
|
|
2025-01-08 18:48:44 +08:00
|
|
|
export type Hook<
|
|
|
|
T extends GenericSchema | GenericSchemaAsync,
|
|
|
|
E extends Env,
|
|
|
|
P extends string,
|
|
|
|
Target extends keyof ValidationTargets = keyof ValidationTargets,
|
2025-04-27 18:28:24 +08:00
|
|
|
O = {},
|
2025-01-08 18:48:44 +08:00
|
|
|
> = (
|
|
|
|
result: SafeParseResult<T> & {
|
|
|
|
target: Target
|
|
|
|
},
|
2023-07-30 07:07:44 +08:00
|
|
|
c: Context<E, P>
|
2025-01-08 18:48:44 +08:00
|
|
|
) => Response | void | TypedResponse<O> | Promise<Response | void | TypedResponse<O>>
|
2023-07-30 07:07:44 +08:00
|
|
|
|
2024-02-15 04:38:43 +08:00
|
|
|
type HasUndefined<T> = undefined extends T ? true : false
|
|
|
|
|
2023-07-30 07:07:44 +08:00
|
|
|
export const vValidator = <
|
2024-06-09 12:15:41 +08:00
|
|
|
T extends GenericSchema | GenericSchemaAsync,
|
2023-07-30 07:07:44 +08:00
|
|
|
Target extends keyof ValidationTargets,
|
|
|
|
E extends Env,
|
|
|
|
P extends string,
|
2024-06-09 12:15:41 +08:00
|
|
|
In = InferInput<T>,
|
|
|
|
Out = InferOutput<T>,
|
2025-01-08 18:48:44 +08:00
|
|
|
I extends Input = {
|
2024-05-06 09:10:56 +08:00
|
|
|
in: HasUndefined<In> extends true
|
|
|
|
? {
|
2025-01-08 18:48:44 +08:00
|
|
|
[K in Target]?: In extends ValidationTargets[K]
|
2024-05-06 09:10:56 +08:00
|
|
|
? In
|
2025-01-08 18:48:44 +08:00
|
|
|
: { [K2 in keyof In]?: ValidationTargets[K][K2] }
|
2024-05-06 09:10:56 +08:00
|
|
|
}
|
|
|
|
: {
|
2025-01-08 18:48:44 +08:00
|
|
|
[K in Target]: In extends ValidationTargets[K]
|
2024-05-06 09:10:56 +08:00
|
|
|
? In
|
|
|
|
: { [K2 in keyof In]: ValidationTargets[K][K2] }
|
|
|
|
}
|
|
|
|
out: { [K in Target]: Out }
|
|
|
|
},
|
2025-04-27 18:28:24 +08:00
|
|
|
V extends I = I,
|
2023-07-30 07:07:44 +08:00
|
|
|
>(
|
|
|
|
target: Target,
|
|
|
|
schema: T,
|
2025-01-08 18:48:44 +08:00
|
|
|
hook?: Hook<T, E, P, Target>
|
2023-07-30 07:07:44 +08:00
|
|
|
): MiddlewareHandler<E, P, V> =>
|
2024-05-06 09:10:56 +08:00
|
|
|
// @ts-expect-error not typed well
|
2024-05-13 05:56:42 +08:00
|
|
|
validator(target, async (value, c) => {
|
|
|
|
const result = await safeParseAsync(schema, value)
|
2023-07-30 07:07:44 +08:00
|
|
|
|
|
|
|
if (hook) {
|
2025-01-08 18:48:44 +08:00
|
|
|
const hookResult = await hook({ ...result, target }, c)
|
|
|
|
if (hookResult) {
|
|
|
|
if (hookResult instanceof Response) {
|
|
|
|
return hookResult
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('response' in hookResult) {
|
|
|
|
return hookResult.response
|
|
|
|
}
|
2023-07-30 07:07:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result.success) {
|
|
|
|
return c.json(result, 400)
|
|
|
|
}
|
|
|
|
|
2025-01-08 18:48:44 +08:00
|
|
|
return result.output
|
2023-07-30 07:07:44 +08:00
|
|
|
})
|