-
-
Notifications
You must be signed in to change notification settings - Fork 887
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
standalone code doesn't work when ajv-formats and user code use different ajvs (solution here) #1390
Comments
As I investigated further this issue is characteristic to setup where dependencies are linked. In my case it's:
And If I install the dependencies normally via |
Does it mean we can close it? To make it work in your case it may be enough to do |
There's no issue with installation on my side. Each package sees its each dependency at expected version. It's a definitive issue on AJV (or bundler it uses) side. Maybe it's simply lack of support for symlinked dependencies (of which resultion works perfectly in scope of Node.js process). This blocks us from shipping, as due to dynamic nature of our schema we generate it on user side, and now if user relies on some installer that favors symlinking (as e.g. pnpm) it may simply not work |
Ok, it would help to have a reproducible small example to investigate/fix |
Sure, I've published reproduction case here: https://github.com/medikoo/ajv-compilation-issue |
@medikoo the reason for the problem is that ajv that is used by ajv-formats and ajv that is used by your code are different ajv's (you can check in node_modules), and because of that There are two solutions possible here:
I will think whether it is something that should be solved in ajv (e.g. replacing instanceof with some other check). |
From my experience In Node.js there are a lot of ways to introduce a setup where two different installations of same package are installed (even if not linking it can easily be introduced when practicing fixed versioning in package.json). In browsers there can be different realms (frames) and e.g. IMO best is to rely on some kind of duck typing (all types detection in native layer works like that), with symbols it can be done really cleanly. Check |
A possible fix is to pass |
This is the case, where standalone will be built on user side. Any installation that works for Node.js should be considered valid
We can explore, still can you point exactly the code we should use? Our standalone generation code can be seen here: https://github.com/serverless/serverless/blob/acd3b5b331c19fd70f620cc0b204992f6c21819a/lib/classes/ConfigSchemaHandler/resolveAjvValidate.js#L23-L34 With what it should be replaced, to be bulletproof? |
as I wrote above you need to do:
|
As long as your compiled code can require "ajv-formats/dist/formats" it would work - if not, you can change require path to simply point to this file location on your filesystem. |
Now that ajv-formats has ajv as peer dependency (starting from v1.6 and in v2) it should not happen. |
Just as a note I still find it happening with the latest version of However, the patch proposed in the comment above works. |
I followed the advise at #1386 to generate standalone instance of
validate
. Still after having that done, occasionally we observe the following error when we try to run a generatedvalidate
I looked into (prettified version) of generated code, and indeed something doesn't look right there.
At https://gist.github.com/medikoo/1d5f509c8d705b835c207fe1d3b37ef8#file-ajv-compiled-js-L9437-L9439 it's obviously the
undefined
that's assigned toformats4
variableThen at L84237 (not shown in gist as file is too large), there's a
if (!formats4(data331)) {
which obviously crashes with above error, and I don't see in generated script any logic that will recoverformats4
to be a functionOur standalone generator code can be inspected here:
https://github.com/serverless/serverless/blob/acd3b5b331c19fd70f620cc0b204992f6c21819a/lib/classes/ConfigSchemaHandler/resolveAjvValidate.js#L23-L34
What version of Ajv are you using? Does the issue happen if you use the latest version? v7.0.3
Ajv options object
JSON Schema
It's a large Schema as generated by Serverless Framework (we can provide a gist if needed)
The text was updated successfully, but these errors were encountered: