Skip to content

refactor: conditional require ajv when using custom validators/serializers #6005

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

spaceemotion
Copy link

When using a custom validator or serializer for content, the AJV and fast-json-stringify libraries are still required by default. This makes excluding them via a build process (like esbuild) error out.

This PR moves the require() call into a function that only gets used as a fallback.

See #5507

Checklist

@climba03003 climba03003 changed the title Don't require AJV when using custom validators/serializers refactor: conditional require ajv when using custom validators/serializers Mar 4, 2025
* Inline require to avoid hard dependency on ajv when a custom validator is used
*/
function buildDefaultValidator () {
const ValidatorSelector = require('@fastify/ajv-compiler')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a test for it. It must be a new test file and then we can inspect the require.cache: https://nodejs.org/api/modules.html#requirecache

@Eomm Eomm added the performances Everything related to performances label Mar 8, 2025
@Eomm Eomm linked an issue Mar 16, 2025 that may be closed by this pull request
2 tasks
@Eomm
Copy link
Member

Eomm commented Mar 22, 2025

Are @spaceemotion planning to push forward this PR?
I think it deserves to be merged!

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

Successfully merging this pull request may close these issues.

import/require fastify module is incredibly slow > 300 ms
2 participants