more env validations

This commit is contained in:
Philipinho
2024-06-27 22:47:59 +01:00
parent a582d4786d
commit bc7cd033f2
2 changed files with 34 additions and 9 deletions

View File

@ -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

View File

@ -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<string, any>) {
@ -19,17 +45,17 @@ export function validate(config: Record<string, any>) {
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);
}