fastify / ajv-compiler

Build and manage the AJV instances for the fastify framework
Other
18 stars 9 forks source link

Incompatible FastifyServerOptions types when using this as the default schema validator #52

Closed robotastronaut closed 2 years ago

robotastronaut commented 2 years ago

Prerequisites

Fastify version

3.25.3

Plugin version

3.0.0

Node.js version

16.6.2

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

20.04

Description

When using this package as the default schema validator in fastify@3.25.3 (and others), FastifyServerOptions uses its own imported Ajv options type as the type for the ajv property, which is incompatible with those of Ajv 8. In general, this isn't a huge deal because you can just create an override of the fastify function that accepts something like this (pardon any ugliness, it's a five second hack and I didn't bother with the generics):

declare module 'fastify' {
  type FastifyAjv8Options = FastifyServerOptions & {
    ajv?: {
      customOptions?: Ajv8OptionsImportedAbove
      plugins?: Function[]
    }
  }

  export function fastify (opts?: FastifyAjv8Options): FastifyInstance
}

The current documentation for configuring Ajv 8 as the default validator isn't actually complete for typescript users.

Steps to Reproduce

Simply attempt to set the ajv.customOptions.validateFormats field on FastifyServerOptions when using typescript

Expected Behavior

Considering the tight coupling between this package and the main Fastify package, I would have expected this to include an override for the fastify function to support the new Ajv 8 options.

mcollina commented 2 years ago

cc @Eomm

Eomm commented 2 years ago

Considering the tight coupling between this package and the main Fastify package, I would have expected this to include an override for the fastify function to support the new Ajv 8 options.

Fastify should use these exported types (that are the ajv8). I wonder if the issue is on Fastify's side. Did you take a look there too?

robotastronaut commented 2 years ago

Considering the tight coupling between this package and the main Fastify package, I would have expected this to include an override for the fastify function to support the new Ajv 8 options.

Fastify should use these exported types (that are the ajv8). I wonder if the issue is on Fastify's side. Did you take a look there too?

Yeah. Unfortunately, they have a non-peer dependency on Ajv6 and use that import directly, and the options type definitions are incompatible between the two. It's hard to say whether or not moving the Ajv dependency to peer would make sense. I know there are compatibility issues that have been noted when ajv has been installed as a peer dependency in other projects (I recall there being eslint issues, for one), so there's some logic to keeping it a private dependency. I'm sure I'm also in a minority of developers who use typescript in such a strict fashion that this is an issue.

Eomm commented 2 years ago

Considering the tight coupling between this package and the main Fastify package, I would have expected this to include an override for the fastify function to support the new Ajv 8 options.

I think this is solved now in fastify v4: https://github.com/fastify/fastify/blob/main/fastify.d.ts#L11

It uses the exported option by this module.