Skip to content

Standalone stops compiling when updating from 3.4.0 to 3.5.0 #95

@brodo

Description

@brodo

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions