Trim object

This commit is contained in:
Adria Navarro 2024-10-10 12:39:14 +02:00
parent 76f155f311
commit b6731494e7
1 changed files with 12 additions and 5 deletions

View File

@ -10,18 +10,25 @@ function validate(schema: AnyZodObject, property: "body" | "params") {
return next()
}
let params = null
let setClean: ((data: any) => void) | undefined
if (ctx[property] != null) {
params = ctx[property]
setClean = data => (ctx[property] = data)
} else if (property === "body" && ctx.request[property] != null) {
params = ctx.request[property]
} else if (ctx.request.get(property) != null) {
params = ctx.request.get(property)
setClean = data => (ctx.request[property] = data)
} else if (property === "params") {
params = ctx.request.query
setClean = data => (ctx.request.query = data)
}
const { error } = schema.safeParse(params)
if (error) {
ctx.throw(400, fromZodError(error))
const result = schema.safeParse(params)
if (!result.success) {
ctx.throw(400, fromZodError(result.error))
} else {
setClean?.(result.data)
}
return next()
}
}