fastify / ajv-compiler

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

Standalone stops compiling when updating from 3.4.0 to 3.5.0 #95

Closed brodo closed 1 year ago

brodo commented 1 year ago

Prerequisites

Fastify version

4.11.0

Plugin version

3.5.0

Node.js version

v18.4.0

Operating system

macOS

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

13.0.1 (22A400)

Description

The schemaController.compilersFactory.buildValidator property that can be passed to the Fastify-function is not of the same type as the return value of StandaloneValidator anymore.

Here is the error:

error TS2769: No overload matches this call.
  The last overload gave the following error.
    Type '{ (externalSchemas: { [key: string]: AnySchema | AnySchema[]; }, options?: { mode: "JTD"; customOptions?: JTDOptions | undefined; } | undefined): AjvCompile; (externalSchemas: { ...; }, options?: { ...; } | undefined): AjvCompile; }' is not assignable to type 'ValidatorCompiler | undefined'.
      Type '{ (externalSchemas: { [key: string]: AnySchema | AnySchema[]; }, options?: { mode: "JTD"; customOptions?: JTDOptions | undefined; } | undefined): AjvCompile; (externalSchemas: { ...; }, options?: { ...; } | undefined): AjvCompile; }' is not assignable to type 'AjvCompile'.
        Types of parameters 'externalSchemas' and 'schema' are incompatible.
          Type 'AnySchema' is not assignable to type '{ [key: string]: AnySchema | AnySchema[]; }'.
            Type 'boolean' is not assignable to type '{ [key: string]: AnySchema | AnySchema[]; }'.

56       buildValidator: factory,

Steps to Reproduce

Check out this code snippet from the readme:

const { StandaloneValidator } = require('@fastify/ajv-compiler')
const factory = StandaloneValidator({
  readMode: false,
  storeFunction (routeOpts, schemaValidationCode) {
    // routeOpts is like: { schema, method, url, httpPart }
    // schemaValidationCode is a string source code that is the compiled schema function
    const fileName = generateFileName(routeOpts)
    fs.writeFileSync(path.join(__dirname, fileName), schemaValidationCode)
  }
})

const app = fastify({
  jsonShorthand: false,
  schemaController: {
    compilersFactory: {
      buildValidator: factory // this does not compile anymore
    }
  }
})

Expected Behavior

It should compile.

Eomm commented 1 year ago

I'm not a TS user so could you add a Minimal, Reproducible Example with the tsconfig and package.json details?

brodo commented 1 year ago

Here you go. Thanks for following up so fast.

Uzlopak commented 1 year ago

Well... I provided a PR for fastify regarding this issue.