feat: implement error handling and validation middleware, enhance result handling

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
Jose Selesan
2026-04-23 16:35:57 -03:00
parent 7bfa3b390e
commit 20d6018836
14 changed files with 306 additions and 8 deletions

View File

@@ -0,0 +1,52 @@
import { zValidator } from '@hono/zod-validator'
import type { ZodSchema } from 'zod'
import { validationProblem } from './problem-builders'
import { zodIssuesToRecord } from './zod-issues'
type ValidationTarget = 'json' | 'query' | 'param' | 'header' | 'form'
function makeValidator<TSchema extends ZodSchema>(
target: ValidationTarget,
schema: TSchema
) {
return zValidator(target, schema, (result, c) => {
if (result.success) {
return
}
const requestId = c.get('requestId') as string | undefined
const instance = requestId ? `/requests/${requestId}` : undefined
return c.json(
validationProblem({
detail: `Invalid ${target} data`,
instance,
errors: zodIssuesToRecord(result.error.issues)
}),
400
)
})
}
export const validate = {
json<TSchema extends ZodSchema>(schema: TSchema) {
return makeValidator('json', schema)
},
query<TSchema extends ZodSchema>(schema: TSchema) {
return makeValidator('query', schema)
},
param<TSchema extends ZodSchema>(schema: TSchema) {
return makeValidator('param', schema)
},
header<TSchema extends ZodSchema>(schema: TSchema) {
return makeValidator('header', schema)
},
form<TSchema extends ZodSchema>(schema: TSchema) {
return makeValidator('form', schema)
}
}