chrishoermann / zod-prisma-types

Generator creates zod types for your prisma models with advanced validation
Other
579 stars 43 forks source link

Swagger resolve errors - Could not resolve pointer #197

Open clabnet opened 8 months ago

clabnet commented 8 months ago

This is a POST to get all drawings using where, select and others parameters.

export async function getAllDrawings(
  request: FastifyRequest<{ Body: z.infer<typeof drawingFindManyArgsSchema> }>,
  reply: FastifyReply
): Promise<drawing[]> {
  try {
    const { skip, take, select, orderBy, where } = request.body
    const data = await prisma.drawing.findMany({
      select: select,
      where: where,
      orderBy: orderBy,
      take: take ? Number(take) : 10,
      skip: skip ? Number(skip) : undefined,
    })
    if (!data || data.length === 0) {
      return reply.status(404).send({ message: 'No drawings found' })
    }
    return reply.status(200).send(data)
  } catch (error) {
    console.error(error)
    return reply.status(500).send({
      message: 'Error fetching drawings',
    })
  }
}

Using these packages

    "@fastify/swagger": "^8.12.0",
    "@fastify/swagger-ui": "^1.10.1",

when click on Swagger page, POST, I have a lot of errors :

Resolver error at paths./drawings/.post.requestBody.content.application/json.schema.properties.where.properties.AND.anyOf.0.$ref
Could not resolve reference: Could not resolve pointer: /properties/where does not exist in document
Resolver error at paths./drawings/.post.requestBody.content.application/json.schema.properties.where.properties.AND.anyOf.1.items.$ref
Could not resolve reference: Could not resolve pointer: /properties/where does not exist in document
Resolver error at paths./drawings/.post.requestBody.content.application/json.schema.properties.where.properties.OR.items.$ref
Could not resolve reference: Could not resolve pointer: /properties/where does not exist in document
Resolver error at paths./drawings/.post.requestBody.content.application/json.schema.properties.where.properties.NOT.anyOf.0.$ref
Could not resolve reference: Could not resolve pointer: /properties/where does not exist in document
.....

Are correct to use these schemas for CRUD operations ??

Thanks for your work.

chrishoermann commented 7 months ago

@clabnet yes the schemas you mentioned can be used for CRUD operations. I personally use it with trpc but have not used it with swagger or fastify

In your example you also just used the type of the FindMAnyArgsSchema in

  request: FastifyRequest<{ Body: z.infer<typeof drawingFindManyArgsSchema> }>,

but did not validate at runtime if the body actually has the expeced data promised by the type like

const { skip, take, select, orderBy, where } = drawingFindManyArgsSchema.parse(request.body) // or use .safeParse() to not throw any errors directly

maybe this helps to catch any errors in your request body and gives some more traceable error messages