turkerdev / fastify-type-provider-zod

MIT License
386 stars 24 forks source link

Incompatible with Mercurius + Next evolution of the library #52

Open qlaffont opened 1 year ago

qlaffont commented 1 year ago

Hello, I'm currently trying to use your lib, but it doesn't work when I add these lines :

  fastify.setValidatorCompiler(validatorCompiler);
  fastify.setSerializerCompiler(serializerCompiler);

I have this error :

{"errors":[{"message":"Graphql validation error","extensions":{"exception":{"name":"FastifyError","code":"MER_ERR_GQL_VALIDATION","message":"Graphql validation error","statusCode":400,"errors":[{"statusCode":400,"validationContext":"querystring"}]}}}]}

Regards Quentin

qlaffont commented 1 year ago

As it seems, we need to detect if schema is a Zod or JSON Schema / AJV. And handle validation accordingly.

https://github.com/mercurius-js/mercurius/blob/master/lib/routes.js#L71

qlaffont commented 1 year ago

to fix it i do it like this @turkerdev :


  fastify.setValidatorCompiler(({ schema }) => {
    const ajv = new Ajv({
      removeAdditional: 'all',
      useDefaults: true,
      coerceTypes: 'array',
      // any other options
      // ...
    });

    //@ts-ignore
    if (schema.safeParse) {
      return (data) => {
        try {
          //@ts-ignore
          schema.parse(data);
          return { value: data };
        } catch (e) {
          const validationError = fromZodError(e as ZodError);
          //@ts-ignore
          return { error: [{ message: validationError }] };
        }
      };
    }

    return ajv.compile(schema);
  });

AND

await fastify.register(require('@fastify/swagger'), {
      mode: 'dynamic',
      exposeRoute: true,
      openapi: {
        info: {
          title: softName,
          version: pkg?.version,
        },
        host: 'localhost:' + port,
        schemes: ['http'],
        consumes: ['application/json'],
        produces: ['application/json'],
        components: {
          securitySchemes: {
            bearerAuth: {
              type: 'http',
              scheme: 'bearer',
              bearerFormat: 'JWT',
            },
          },
        },
      },
      transform: ({ schema, url }) => {
        const newSchema = { ...schema };

        return createJsonSchemaTransform({
          skipList: [
            '/documentation/',
            '/documentation/initOAuth',
            '/documentation/json',
            '/documentation/uiConfig',
            '/documentation/yaml',
            '/documentation/*',
            '/documentation/static/*',
            '/graphql',
            '/graphiql',
            '/graphiql/main.js',
            '/graphiql/sw.js',
            '/graphiql/config.js',
          ],
        })({ schema: newSchema, url });
      },
    });
qlaffont commented 1 year ago

Because this lib is not maintained anymore, I have created a fork who fix all these issues : https://github.com/qlaffont/fastify-type-provider-zod2

kibertoad commented 1 year ago

@qlaffont This library is supported. Could you please send your changes as a PR with a test, so that we could properly review it?

qlaffont commented 1 year ago

You have everything on the repo if you want the src/index.ts is the same as the current library you just need to do a diff. And i have include some customs tests to test if everything is working on test folder.

kibertoad commented 1 year ago

@qlaffont I'm sorry but I honestly don't have time or energy to pull everything by hand and create a PR. Happy to review the PR if it's there.

qlaffont commented 9 months ago

@kibertoad or @turkerdev any update ?

ariefsn commented 3 months ago

any update?