feat: add environment configuration validation using zod and dotenv#9
Merged
feat: add environment configuration validation using zod and dotenv#9
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This pull request introduces environment variable validation using the zod library, streamlining the configuration initialization process. Key changes include:
- Introducing a new module (src/config/environment.ts) that validates environment variables using zod and dotenv.
- Refactoring src/index.ts to remove direct dotenv usage and employ the loadEnvironment function for validation.
- Moving the PORT assignment into the conditional block to ensure the server only starts outside of test environments.
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/index.ts | Replaces dotenv usage with loadEnvironment and adjusts PORT assignment. |
| src/config/environment.ts | Implements environment variable validation using zod, throwing errors when invalid. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces significant updates to the environment configuration and initialization process. The changes mainly focus on validating environment variables using the
zodlibrary and improving the structure of the environment loading process.Environment configuration and validation:
src/config/environment.ts: Added a new module that useszodto validate environment variables and ensure they have the correct types and default values. This module also loads the environment variables usingdotenv.Initialization process improvements:
src/index.ts: Removed the direct use ofdotenvand replaced it with the newloadEnvironmentfunction from the environment configuration module. This change ensures environment variables are validated before being used.src/index.ts: Moved thePORTvariable assignment inside the conditional block that checks if the environment is not 'test', ensuring the server only starts in non-test environments.