57 lines
1.0 KiB
TypeScript
57 lines
1.0 KiB
TypeScript
import { plainToInstance } from 'class-transformer';
|
|
import { IsNotEmpty, IsNumberString, IsOptional, IsString, validateSync } from 'class-validator';
|
|
|
|
class EnvironmentVariables {
|
|
@IsOptional()
|
|
@IsNumberString()
|
|
PORT?: string;
|
|
|
|
@IsOptional()
|
|
@IsString()
|
|
NODE_ENV?: string;
|
|
|
|
@IsNotEmpty()
|
|
@IsString()
|
|
DB_URL!: string;
|
|
|
|
@IsOptional()
|
|
@IsString()
|
|
REDIS_URL?: string;
|
|
|
|
@IsNotEmpty()
|
|
@IsString()
|
|
JWT_SECRET!: string;
|
|
|
|
@IsOptional()
|
|
@IsString()
|
|
JWT_ACCESS_TTL?: string;
|
|
|
|
@IsOptional()
|
|
@IsString()
|
|
JWT_REFRESH_TTL?: string;
|
|
|
|
@IsNotEmpty()
|
|
@IsString()
|
|
SMS_API_KEY!: string;
|
|
|
|
@IsOptional()
|
|
@IsNumberString()
|
|
OTP_TTL_SECONDS?: string;
|
|
}
|
|
|
|
export function validateEnv(config: Record<string, unknown>) {
|
|
const validatedConfig = plainToInstance(EnvironmentVariables, config, {
|
|
enableImplicitConversion: true,
|
|
});
|
|
|
|
const errors = validateSync(validatedConfig, {
|
|
skipMissingProperties: false,
|
|
});
|
|
|
|
if (errors.length > 0) {
|
|
throw new Error(errors.toString());
|
|
}
|
|
|
|
return validatedConfig;
|
|
}
|