AcadeDoc/apps/server/src/integrations/environment/environment.validation.ts
2024-06-27 22:47:59 +01:00

64 lines
1.4 KiB
TypeScript

import {
IsIn,
IsNotEmpty,
IsNotIn,
IsOptional,
IsUrl,
validateSync,
} from 'class-validator';
import { plainToInstance } from 'class-transformer';
export class EnvironmentVariables {
@IsNotEmpty()
@IsUrl(
{ protocols: ['postgres', 'postgresql'], require_tld: false },
{ message: 'DATABASE_URL must be a valid postgres connection string' },
)
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>) {
const validatedConfig = plainToInstance(EnvironmentVariables, config);
const errors = validateSync(validatedConfig);
if (errors.length > 0) {
console.error(
'The Environment variables has failed the following validations:',
);
errors.map((error) => {
console.error(JSON.stringify(error.constraints));
});
console.error(
'Please fix the environment variables and try again. Exiting program...',
);
process.exit(1);
}
return validatedConfig;
}