y_nkY
tRPC7mo ago
1 reply
y_nk

sanitizing output based on output schema

As i understand, the
.output
will only ensure the schema is respected, but will not sanitize output on the go (even tho it could be possible with a
z.strictObject()
):

.output(someSchema) // <- does not sanitize data
// ---
.use(output(someSchema)) // <- should sanatize data

export function output<T extends ZodType>(schema: T) {
  return middleware(async ({ next }) => {
    const result = await next()
    return schema.parse(result)
  })
}


but for some reason the typing does not work. could somebody point out how to achieve similar but in a working way? is there no way to do what i want?
Was this page helpful?