elierotenberg / fastify-zod

Zod integration with Fastify
MIT License
212 stars 19 forks source link

Validation schema not working for params & querystring #17

Closed henrifardeau closed 2 years ago

henrifardeau commented 2 years ago

Hello !

I encounter an error when validating parameters or querystring

I register all my schema in my app file like so :

export const { schemas, $ref } = buildJsonSchemas({
  findByIdPartnerSchema,
  findByIdCustomerSchema,
  createPartnerSchema,
  createCustomerSchema,
});

schemas.forEach((schema) => app.addSchema(schema));

I try to generate documentation based on the "findByIdCustomerSchema" but this is what i'm getting; (It's working for body validation/documentation)

Maybe i'm doing something wrong 😅

Thank you for your help ! (From france 🇫🇷)

Capture d’écran 2022-05-20 à 10 08 20 Capture d’écran 2022-05-20 à 10 08 30 Capture d’écran 2022-05-20 à 10 09 02 Capture d’écran 2022-05-20 à 10 11 16
henrifardeau commented 2 years ago

EDIT : Sorry my bad, i was using @fastify/swagger instead of fastify-swagger

sourcec0de commented 2 years ago

@henrifardeau did you downgrade to get this working?

It looks like https://www.npmjs.com/package/fastify-swagger is actually deprecated. They recommend upgrading to @fastify/swagger@6.0.0

elierotenberg commented 2 years ago

Hello,

I'm sorry, as mentioned in #15 for some reason GitHub notifications got lost in my mailbox.

I added support for querystring in 1.0.0. Does it cover your needs?

fukouda commented 1 year ago

@sourcec0de did you ever find a workaround for this? @elierotenberg do we HAVE to use fastify-swagger to get queryStrings and params working? I am running into this same issue still

EDIT: NVM! my solution was to use @fastify/swagger in dynamic mode and change swagger (openAPIV2) in the plugin definition to openapi:

import { FastifyPluginAsync } from "fastify";
import fp from "fastify-plugin";
import fastifySwagger from "@fastify/swagger";
import fastifySwaggerUi from "@fastify/swagger-ui";

const schemas: FastifyPluginAsync = async (fastify) => {
  await fastify.register(fastifySwagger, {
    openapi: {
      info: {
        title: "API",
        description: "Documentation for our application APIs",
        version: "0.0.1",
      },
      host: "localhost:8080",
      schemes: ["http"],
      consumes: ["application/json"],
      produces: ["application/json"],
      securityDefinitions: {
        apiKey: {
          type: "apiKey",
          name: "Authorization",
          in: "header",
        },
      },
    },
  });

  await fastify.register(fastifySwaggerUi, {
    routePrefix: "/documentation",
  });
};

export default fp(schemas);