From bc7cd033f265abe6505014495c8e6287784d84a1 Mon Sep 17 00:00:00 2001 From: Philipinho <16838612+Philipinho@users.noreply.github.com> Date: Thu, 27 Jun 2024 22:47:59 +0100 Subject: [PATCH] more env validations --- .env.example | 1 - .../environment/environment.validation.ts | 42 +++++++++++++++---- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index 80ce7c59..9665822e 100644 --- a/.env.example +++ b/.env.example @@ -19,7 +19,6 @@ AWS_S3_SECRET_ACCESS_KEY= AWS_S3_REGION= AWS_S3_BUCKET= AWS_S3_ENDPOINT= -AWS_S3_URL= # options: smtp | postmark MAIL_DRIVER=smtp diff --git a/apps/server/src/integrations/environment/environment.validation.ts b/apps/server/src/integrations/environment/environment.validation.ts index 8449d749..4f1bf563 100644 --- a/apps/server/src/integrations/environment/environment.validation.ts +++ b/apps/server/src/integrations/environment/environment.validation.ts @@ -1,4 +1,11 @@ -import { IsNotEmpty, IsNotIn, IsUrl, validateSync } from 'class-validator'; +import { + IsIn, + IsNotEmpty, + IsNotIn, + IsOptional, + IsUrl, + validateSync, +} from 'class-validator'; import { plainToInstance } from 'class-transformer'; export class EnvironmentVariables { @@ -9,9 +16,28 @@ export class EnvironmentVariables { ) DATABASE_URL: string; + @IsNotEmpty() + @IsUrl( + { protocols: ['redis', 'rediss'], require_tld: false }, + { message: 'REDIS_URL must be a valid redis connection string' }, + ) + REDIS_URL: string; + + @IsOptional() + @IsUrl({ protocols: ['http', 'https'], require_tld: false }) + APP_URL: string; + @IsNotEmpty() @IsNotIn(['REPLACE_WITH_LONG_SECRET']) APP_SECRET: string; + + @IsOptional() + @IsIn(['smtp', 'postmark']) + MAIL_DRIVER: string; + + @IsOptional() + @IsIn(['local', 's3']) + STORAGE_DRIVER: string; } export function validate(config: Record) { @@ -19,17 +45,17 @@ export function validate(config: Record) { const errors = validateSync(validatedConfig); - console.error( - 'The EnvironmentVariables has failed the following validations:', - ); - if (errors.length > 0) { + console.error( + 'The Environment variables has failed the following validations:', + ); + errors.map((error) => { - console.log(JSON.stringify(error.constraints)); + console.error(JSON.stringify(error.constraints)); }); - console.log( - 'Please fix the environment variables and try again. Shutting down...', + console.error( + 'Please fix the environment variables and try again. Exiting program...', ); process.exit(1); }