feat: implement error handling and validation middleware, enhance result handling
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
52
apps/backend/src/lib/http/validate.ts
Normal file
52
apps/backend/src/lib/http/validate.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user