Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesnt work with ajv@8.8+ #46

Open
LukaszGrela opened this issue Dec 1, 2021 · 13 comments
Open

Doesnt work with ajv@8.8+ #46

LukaszGrela opened this issue Dec 1, 2021 · 13 comments

Comments

@LukaszGrela
Copy link

Currently the highest ajv version we can use is 8.6.3 any higher version breaks the addFormats that do not accept the Ajv instance anymore.

Do you plan an upgrade to support newest versions of AJV?

@epoberezkin
Copy link
Member

Could you please post the code sample?

ajv-formats is tested with the latest Ajv version, so it must be something specific that fails in the newer version, not a general lack of support.

@LukaszGrela
Copy link
Author

well, it is pretty straightforward:

import Ajv from 'ajv';
import addFormats from 'ajv-formats';

export const extendAjv = (ajv:Ajv): void => {
   addFormats(ajv);
   ^^^ Error hilights this line as addFormats can't accept the Ajv type
   // ...
}

@epoberezkin
Copy link
Member

epoberezkin commented Dec 1, 2021

I cannot really debug VSCode warnings, sorry.

Please check the installed versions - both ajv and ajv-formats should be the latest - and post tsc output here, if it generates any errors.

VSCode warnings can be happening for various reasons, and are not always a reliable indication that there is something wrong with the types.

@epoberezkin
Copy link
Member

This works here: https://runkit.com/esp/61a74fca5dd82f000846a30f

Tests use typescript, so the latest version of ajv-formats does work with the latest version of ajv. You can just try re-installing all dependencies and restarting vscode.

@LukaszGrela
Copy link
Author

we're using ajv in our project and from npm ls ajv I can see many other modules that is also using it (having in it's deps) with varous versions. The error appears at the addFormats as the Ajv type comes from different places:
the import Ajv from 'ajv' is imported from node_modules/ajv/dist/ajv.d.ts but the Ajv type in the addFormats from node_modules/ajv-formats/node_modules/ajv/dist/core.d.ts

The ajv-formats@2.1.1 reports the ajv@8.6.3 version installed in its dependency (the package.json in devDependencies has ajv set as ^8.0.0

Regarding the uninstalling/restarting it was first thing that I did to no avail:)

@epoberezkin
Copy link
Member

the import Ajv from 'ajv' is imported from node_modules/ajv/dist/ajv.d.ts but the Ajv type in the addFormats from node_modules/ajv-formats/node_modules/ajv/dist/core.d.ts

that should not be a problem as the former is the subclass of the latter, as ajv-formats can be used with different Ajv classes (one for each JSON Schema version), all being subclasses of the parent class defined in core.ts.

I would like to see tsc output, as I said VSCode extension you use may have some issues with it, but it’s not something I can support.

@unematiii
Copy link

In my case, I had ajv 8.8.* under ajv-formats/node_modules and 8.6.* in node_modules, fixing this version mismatch also fixed TS compilation issues.

@gadicc
Copy link

gadicc commented Jan 29, 2022

Me too, seems it was an issue with my yarn.lock.

yarn remove ajv-formats and yarn add ajv-formats fixed the issue for me.

@connorjs
Copy link

connorjs commented Feb 6, 2022

I had the same issue (upgrade AJV broke TypeScript compilation). It seems that this matches #13, which #15 fixed a couple years ago. The above solutions worked: uninstall ajv-formats and reinstall.

However, I noticed that the package.json has ajv as a dependency and a peerDependency. Therefore, in the “broken” state, I had ajv-8.9.0 in the node_modules, but ajv-8.6.3 (previous version) under ajv-formatsnode_modules.

I do not have that intimate familiarity with peer dependencies, but I would have expected only peerDependency entry and NOT dependency entry. (The package may also have devDependency for its internal testing.) Can others help shed some light here?

Also, if it should only have peerDependency, the upcoming 3.0 release could ease the (breaking?) change for folks. Let me know if I should create a new issue, too. Thanks!

@max-b
Copy link

max-b commented Feb 17, 2022

I'm currently encountering this issue. Here's the output of tsc for me:

$ project-root/node_modules/.bin/tsc
src/broadcast/middleware/validators.ts:12:12 - error TS2345: Argument of type 'import("project-root/node_modules/ajv/dist/ajv").default' is not assignable to parameter of type 'import("project-root/node_modules/ajv-formats/node_modules/ajv/dist/core").default'.
  Types of property 'opts' are incompatible.
    Type 'import("project-root/node_modules/ajv/dist/core").InstanceOptions' is not assignable to type 'import("project-root/node_modules/ajv-formats/node_modules/ajv/dist/core").InstanceOptions'.
      Type 'InstanceOptions' is not assignable to type 'CurrentOptions'.
        Types of property 'keywords' are incompatible.
          Type 'import("project-root/node_modules/ajv/dist/types/index").Vocabulary | undefined' is not assignable to type 'import("project-root/node_modules/ajv-formats/node_modules/ajv/dist/types/index").Vocabulary | undefined'.
            Type 'Vocabulary' is not assignable to type '(string | KeywordDefinition)[]'.
              Type 'string | import("project-root/node_modules/ajv/dist/types/index").KeywordDefinition' is not assignable to type 'string | import("project-root/node_modules/ajv-formats/node_modules/ajv/dist/types/index").KeywordDefinition'.
                Type 'CodeKeywordDefinition' is not assignable to type 'string | KeywordDefinition'.
                  Type 'import("project-root/node_modules/ajv/dist/types/index").CodeKeywordDefinition' is not assignable to type 'import("project-root/node_modules/ajv-formats/node_modules/ajv/dist/types/index").CodeKeywordDefinition'.
                    Types of property 'code' are incompatible.
                      Type '(cxt: import("project-root/node_modules/ajv/dist/compile/validate/index").KeywordCxt, ruleType?: string | undefined) => void' is not assignable to type '(cxt: import("project-root/node_modules/ajv-formats/node_modules/ajv/dist/compile/validate/index").KeywordCxt, ruleType?: string | undefined) => void'.
                        Types of parameters 'cxt' and 'cxt' are incompatible.
                          Type 'import("project-root/node_modules/ajv-formats/node_modules/ajv/dist/compile/validate/index").KeywordCxt' is not assignable to type 'import("project-root/node_modules/ajv/dist/compile/validate/index").KeywordCxt'.
                            The types of 'gen._scope' are incompatible between these types.
                              Type 'import("project-root/node_modules/ajv-formats/node_modules/ajv/dist/compile/codegen/scope").Scope' is not assignable to type 'import("project-root/node_modules/ajv/dist/compile/codegen/scope").Scope'.
                                Property '_names' is protected but type 'Scope' is not a class derived from 'Scope'.

12 addFormats(ajv);
              ~~~


Found 1 error.

error Command failed with exit code 2.

Here's a bit of additional potentially useful debug info:

❯ head -n 10 project-root/node_modules/ajv/package.json 
{
  "name": "ajv",
  "version": "8.8.2",
  "description": "Another JSON Schema Validator",
  "main": "dist/ajv.js",
  "types": "dist/ajv.d.ts",
  "files": [
    "lib/",
    "dist/",
    ".runkit_example.js"

❯ head -n 10 project-root/node_modules/ajv-formats/package.json
{
  "name": "ajv-formats",
  "version": "2.1.1",
  "description": "Format validation for Ajv v7+",
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  "files": [
    "src/",
    "dist/"
  ],

@connorjs
Copy link

connorjs commented Apr 2, 2022

My current workaround whenever I updated ajv

npm un ajv-formats && npm i ajv-formats

This often installs the same version of ajv-formats (because its version did not change), but its underlying node_modules now gets the latest ajv as a “fresh” install.


Alternatively, you could manually update the lock file’s two (for me) locations of ajv under ajv-formats (update version and integrity).

@chris-armstrong
Copy link

I've tried to use 3.0.0-rc.0 above with yarn and typescript, but I still get the incompatibility error below. From what I can tell, yarn will still install the version of ajv specified in its package.json because it is listed as a dependency.

src/compile_schemas.ts:64:16 - error TS2345: Argument of type 'import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv/dist/ajv").default' is not assignable to parameter of type 'import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv-formats/node_modules/ajv/dist/core").default'.
  Types of property 'opts' are incompatible.
    Type 'import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv/dist/core").InstanceOptions' is not assignable to type 'import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv-formats/node_modules/ajv/dist/core").InstanceOptions'.
      Type 'InstanceOptions' is not assignable to type 'CurrentOptions'.
        Types of property 'keywords' are incompatible.
          Type 'import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv/dist/types/index").Vocabulary | undefined' is not assignable to type 'import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv-formats/node_modules/ajv/dist/types/index").Vocabulary | undefined'.
            Type 'Vocabulary' is not assignable to type '(string | KeywordDefinition)[]'.
              Type 'string | import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv/dist/types/index").KeywordDefinition' is not assignable to type 'string | import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv-formats/node_modules/ajv/dist/types/index").KeywordDefinition'.
                Type 'CodeKeywordDefinition' is not assignable to type 'string | KeywordDefinition'.
                  Type 'import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv/dist/types/index").CodeKeywordDefinition' is not assignable to type 'import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv-formats/node_modules/ajv/dist/types/index").CodeKeywordDefinition'.
                    Types of property 'code' are incompatible.
                      Type '(cxt: import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv/dist/compile/validate/index").KeywordCxt, ruleType?: string | undefined) => void' is not assignable to type '(cxt: import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv-formats/node_modules/ajv/dist/compile/validate/index").KeywordCxt, ruleType?: string | undefined) => void'.
                        Types of parameters 'cxt' and 'cxt' are incompatible.
                          Type 'import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv-formats/node_modules/ajv/dist/compile/validate/index").KeywordCxt' is not assignable to type 'import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv/dist/compile/validate/index").KeywordCxt'.
                            The types of 'gen._scope' are incompatible between these types.
                              Type 'import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv-formats/node_modules/ajv/dist/compile/codegen/scope").Scope' is not assignable to type 'import("/Users/chris/dev/honeycomb/packages/backend-scripts/node_modules/ajv/dist/compile/codegen/scope").Scope'.
                                Property '_names' is protected but type 'Scope' is not a class derived from 'Scope'.

64     addFormats(ajv);

The correct solution is to remove ajv as a direct dependency of ajv-formats and only retain it as a peer dependency and dev dependency - there is no reason for ajv-formats to specify ajv directly as a dependency as the downstream user will need to have it in their package.json and referenced from their code anyway.

@lorenzogrv
Copy link

Under non-typescript proyects, having both ajv on peerDependencies and dependencies it's also causing troubles. Please see #12 #47 #69 or search for 'undefined' within issues...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

8 participants