turkerdev / fastify-type-provider-zod

MIT License
303 stars 19 forks source link

Cannot convert undefined or null to object #79

Open ollebergkvist opened 2 months ago

ollebergkvist commented 2 months ago

I get this error: Cannot convert undefined or null to object

Triggered by making query string a z.object:

querystring: z.object({
    query: z.string()
  }),
import { type FastifyInstance, type FastifyPluginAsync } from 'fastify'

import { ZodTypeProvider } from 'fastify-type-provider-zod'
import z from 'zod'

const scraperRoute: FastifyPluginAsync = async function (
  fastify: FastifyInstance
) {
  fastify.withTypeProvider<ZodTypeProvider>().route({
    method: 'GET',
    url: '/scraper',
    schema: {
      querystring: z.object({
        query: z.string().min(4)
      }),
      response: {
        200: z.object({
          url: z.string().min(2),
          hidden: z.boolean()
        })
      }
    },
    handler: async (request, reply) => {
      // @ts-ignore
      const { query } = request.query

      try {
        const data = await getScraper(query)

        return reply.code(200).send(data)
      } catch (error) {
        fastify.log.error(error)
        return reply.code(500).send()
      }
    }
  })
}

export default scraperRoute