Skip to content

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

Closed
spaceemotion wants to merge 1 commit intofastify:mainfrom
spaceemotion:feature/5507-optional-ajv-require
Closed

refactor: conditional require ajv when using custom validators/serializers#6005
spaceemotion wants to merge 1 commit intofastify:mainfrom
spaceemotion:feature/5507-optional-ajv-require

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!

@spaceemotion
Copy link
Author

@Eomm thanks for picking this up - I couldn't find the time to work on this so far.

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