turkerdev / fastify-type-provider-zod

MIT License
290 stars 19 forks source link

Sending blank response body does not provide runtime type safety #88

Open tzezar opened 3 weeks ago

tzezar commented 3 weeks ago

having schema set like this

export const schema = {
    response: {
        201: z.object({
            status: z.literal('success'),
            data: z.object({
                id: z.number()
            })
        })
    }
}

sending blank response DOES NOT raise ts error

export const testCommandHandler = async (
    req: FastifyRequestZod<typeof schema >,
    reply: FastifyReplyZod<typeof schema >
) => {
    return reply.status(201).send()
}

changing it to object does raise ts err

return reply.status(201).send({})
JonRC commented 1 week ago

It's a behavior from Fastify. The send method param is optional, so you can pass whatever was inferred from the schema or undefined (nothing).

I've already opened an issue to discuss this point https://github.com/fastify/fastify/issues/5534