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

fix(validate-on-module-load): move validation timing to module load phase #1938

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kim-sung-jee
Copy link

@kim-sung-jee kim-sung-jee commented Jan 27, 2025

Resolved an issue where validation was executed during file import, causing unintended side effects. Validation now occurs when the module is loaded, ensuring proper lifecycle alignment.


Example Scenario

The following scenario describes the environment:
There are two entry modules: ApiModule and ConsumerModule.

@Module({
  imports: [
    ConfigModule.forRoot({
      validationSchema: Joi.object({ ...ApiEnvs }),
    }),
  ],
})
export class ApiModule {}
@Module({
  imports: [
    ConfigModule.forRoot({
      validationSchema: Joi.object({ ...ConsumerEnvs }),
    }),
  ],
})
export class ConsumerModule {}

In the project's root main.ts, these two modules are imported:

if (serverType === 'API') {
  await NestFactory.create(ApiModule);
} else if (serverType === 'CONSUMER') {
  await NestFactory.create(ConsumerModule);
} ...

In the current version, all validation functions are executed regardless of the serverType.

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our guidelines: https://github.com/nestjs/nest/blob/master/CONTRIBUTING.md
  • Tests for the changes have been added (for bug fixes / features): Tests were not added as they were considered unnecessary for this change.
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

Validation functions in ConfigModule.forRoot are executed regardless of module loading.

What is the new behavior?

  • Validation is now included in the factory function of the VALIDATED_ENV_LOADER provider. This ensures it runs during module initialization via NestJS DI and centralizes the responsibility of validating and loading environment variables.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

…hase

Resolved an issue where validation was executed during file import, causing unintended side effects. Validation now occurs when the module is loaded, ensuring proper lifecycle alignment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant