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

unneeded require("ajv/dist/runtime/equal") in generated code for enums with primitive types #1852

Open
cscheid opened this issue Dec 16, 2021 · 1 comment

Comments

@cscheid
Copy link

cscheid commented Dec 16, 2021

The code generation module appears to emit unnecessary calls to require("ajv/dist/runtime/equal") in cases where enums containing only primitive types are declared.

A minimal repro is available here: https://runkit.com/cscheid/ajv-enum-unneeded-require

What version of Ajv you are you using?
8.8.2

What problem do you want to solve?
I want to make it so that the equal declaration is only emitted in the cases where the enum schema requires it.

The reason we need this is that for our use case, we need a completely self-contained validating function. We're willing to restrict ourselves to a subset of the possible schemas, and in this case it seems like this declaration is unneeded. We can currently work around that at some expense by manipulating the generated code after the fact, but this would be a much cleaner solution.

What do you think is the correct solution to problem?
The solution here would be to perform the type checks that happen in equalCode ahead of time once, and only declare the equal function in case some of those checks fail.

Will you be able to implement it?
I believe I would, but I would really appreciate a thorough code review.

@cscheid
Copy link
Author

cscheid commented Dec 16, 2021

The fix was easier and simpler than I expected, so I went ahead and opened a PR; I hope that is ok.

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

No branches or pull requests

1 participant