-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from OpenNBS/feature/env-validation
feat: implement environment variable validation with class-validator and class-transformer
- Loading branch information
Showing
2 changed files
with
104 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { plainToInstance } from 'class-transformer'; | ||
import { IsEnum, IsOptional, IsString, validateSync } from 'class-validator'; | ||
|
||
enum Environment { | ||
Development = 'development', | ||
Production = 'production', | ||
} | ||
|
||
export class EnvironmentVariables { | ||
@IsEnum(Environment) | ||
@IsOptional() | ||
NODE_ENV?: Environment; | ||
|
||
@IsString() | ||
GITHUB_CLIENT_ID: string; | ||
|
||
@IsString() | ||
GITHUB_CLIENT_SECRET: string; | ||
|
||
@IsString() | ||
GOOGLE_CLIENT_ID: string; | ||
|
||
@IsString() | ||
GOOGLE_CLIENT_SECRET: string; | ||
|
||
@IsString() | ||
DISCORD_CLIENT_ID: string; | ||
|
||
@IsString() | ||
DISCORD_CLIENT_SECRET: string; | ||
|
||
@IsString() | ||
JWT_SECRET: string; | ||
|
||
@IsString() | ||
JWT_EXPIRES_IN: string; | ||
|
||
@IsString() | ||
JWT_REFRESH_SECRET: string; | ||
|
||
@IsString() | ||
JWT_REFRESH_EXPIRES_IN: string; | ||
|
||
@IsString() | ||
MONGO_URL: string; | ||
|
||
@IsString() | ||
SERVER_URL: string; | ||
|
||
@IsString() | ||
FRONTEND_URL: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
APP_DOMAIN?: string; | ||
|
||
@IsString() | ||
RECAPTCHA_KEY: string; | ||
|
||
@IsString() | ||
S3_ENDPOINT: string; | ||
|
||
@IsString() | ||
S3_BUCKET_SONGS: string; | ||
|
||
@IsString() | ||
S3_BUCKET_THUMBS: string; | ||
|
||
@IsString() | ||
S3_KEY: string; | ||
|
||
@IsString() | ||
S3_SECRET: string; | ||
|
||
@IsString() | ||
S3_REGION: string; | ||
|
||
@IsString() | ||
@IsOptional() | ||
WHITELISTED_USERS?: string; | ||
|
||
@IsString() | ||
DISCORD_WEBHOOK_URL: string; | ||
} | ||
|
||
export function validate(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; | ||
} |