import {zValidator} from '@hono/zod-validator' import type {ValidationTargets} from 'hono' import {HTTPException} from 'hono/http-exception' import {z, type ZodType} from 'zod' export const validate = < Target extends keyof ValidationTargets, T extends ZodType, >( target: Target, schema: T, code = 'invalid_request', ) => zValidator(target, schema, (result) => { if (!result.success) throw new HTTPException(400, {message: code}) }) // the shared uuid path-param validator, e.g. uuidParams('id', 'crumbId') export const uuidParams = (...keys: K[]) => validate( 'param', z.object( Object.fromEntries(keys.map((key) => [key, z.uuid()])) as { [P in K]: ReturnType }, ), )