diff --git a/dictation_server/.devcontainer/docker-compose.yml b/dictation_server/.devcontainer/docker-compose.yml index 47cf396..c3de580 100644 --- a/dictation_server/.devcontainer/docker-compose.yml +++ b/dictation_server/.devcontainer/docker-compose.yml @@ -2,7 +2,6 @@ version: '3' services: dictation_server: - env_file: ../.env build: . working_dir: /app/dictation_server ports: diff --git a/dictation_server/src/app.module.ts b/dictation_server/src/app.module.ts index 3deb54c..49b1d97 100644 --- a/dictation_server/src/app.module.ts +++ b/dictation_server/src/app.module.ts @@ -44,6 +44,7 @@ import { SortCriteriaRepositoryModule } from './repositories/sort_criteria/sort_ import { TemplateFilesRepositoryModule } from './repositories/template_files/template_files.repository.module'; import { WorktypesRepositoryModule } from './repositories/worktypes/worktypes.repository.module'; import { TemplatesService } from './features/templates/templates.service'; +import { validate } from './common/validators/env.validator'; @Module({ imports: [ @@ -60,6 +61,7 @@ import { TemplatesService } from './features/templates/templates.service'; ConfigModule.forRoot({ envFilePath: ['.env.local', '.env'], isGlobal: true, + validate, }), AuthModule, AdB2cModule, diff --git a/dictation_server/src/common/validators/env.validator.ts b/dictation_server/src/common/validators/env.validator.ts new file mode 100644 index 0000000..9ceea2a --- /dev/null +++ b/dictation_server/src/common/validators/env.validator.ts @@ -0,0 +1,198 @@ +import { plainToClass } from 'class-transformer'; +import { + IsNotEmpty, + IsNumber, + IsOptional, + IsString, + validateSync, +} from 'class-validator'; + +/** + * 環境変数の型定義 + */ +export class EnvValidator { + // .env + @IsNotEmpty() + @IsString() + DB_HOST: string; + + @IsNotEmpty() + @IsNumber() + DB_PORT: number; + + @IsNotEmpty() + @IsNumber() + DB_EXTERNAL_PORT: number; + + @IsNotEmpty() + @IsString() + DB_NAME: string; + + @IsNotEmpty() + @IsString() + DB_ROOT_PASS: string; + + @IsNotEmpty() + @IsString() + DB_USERNAME: string; + + @IsNotEmpty() + @IsString() + DB_PASSWORD: string; + + @IsOptional() + @IsString() + NO_COLOR: string; + + @IsNotEmpty() + @IsNumber() + ACCESS_TOKEN_LIFETIME_WEB: number; + + @IsNotEmpty() + @IsNumber() + REFRESH_TOKEN_LIFETIME_WEB: number; + + @IsNotEmpty() + @IsNumber() + REFRESH_TOKEN_LIFETIME_DEFAULT: number; + + @IsNotEmpty() + @IsString() + TENANT_NAME: string; + + @IsNotEmpty() + @IsString() + SIGNIN_FLOW_NAME: string; + + @IsNotEmpty() + @IsNumber() + EMAIL_CONFIRM_LIFETIME: number; + + @IsNotEmpty() + @IsString() + APP_DOMAIN: string; + + @IsNotEmpty() + @IsNumber() + STORAGE_TOKEN_EXPIRE_TIME: number; + + // .env.local + @IsOptional() + @IsString() + STAGE: string; + + @IsOptional() + @IsString() + CORS: string; + + @IsNotEmpty() + @IsNumber() + PORT: number; + + @IsNotEmpty() + @IsString() + AZURE_TENANT_ID: string; + + @IsNotEmpty() + @IsString() + AZURE_CLIENT_ID: string; + + @IsNotEmpty() + @IsString() + AZURE_CLIENT_SECRET: string; + + @IsNotEmpty() + @IsString() + ADB2C_TENANT_ID: string; + + @IsNotEmpty() + @IsString() + ADB2C_CLIENT_ID: string; + + @IsNotEmpty() + @IsString() + ADB2C_CLIENT_SECRET: string; + + @IsNotEmpty() + @IsString() + ADB2C_ORIGIN: string; + + @IsNotEmpty() + @IsString() + KEY_VAULT_NAME: string; + + @IsNotEmpty() + @IsString() + JWT_PRIVATE_KEY: string; + + @IsNotEmpty() + @IsString() + JWT_PUBLIC_KEY: string; + + @IsNotEmpty() + @IsString() + SENDGRID_API_KEY: string; + + @IsNotEmpty() + @IsString() + MAIL_FROM: string; + + @IsNotEmpty() + @IsString() + NOTIFICATION_HUB_NAME: string; + + @IsNotEmpty() + @IsString() + NOTIFICATION_HUB_CONNECT_STRING: string; + + @IsNotEmpty() + @IsString() + STORAGE_ACCOUNT_NAME_US: string; + + @IsNotEmpty() + @IsString() + STORAGE_ACCOUNT_NAME_AU: string; + + @IsNotEmpty() + @IsString() + STORAGE_ACCOUNT_NAME_EU: string; + + @IsNotEmpty() + @IsString() + STORAGE_ACCOUNT_KEY_US: string; + + @IsNotEmpty() + @IsString() + STORAGE_ACCOUNT_KEY_AU: string; + + @IsNotEmpty() + @IsString() + STORAGE_ACCOUNT_KEY_EU: string; + + @IsNotEmpty() + @IsString() + STORAGE_ACCOUNT_ENDPOINT_US: string; + + @IsNotEmpty() + @IsString() + STORAGE_ACCOUNT_ENDPOINT_AU: string; + + @IsNotEmpty() + @IsString() + STORAGE_ACCOUNT_ENDPOINT_EU: string; +} + +export function validate(config: Record) { + const validatedConfig = plainToClass(EnvValidator, config, { + enableImplicitConversion: true, + }); + + const errors = validateSync(validatedConfig, { + skipMissingProperties: false, + }); + + if (errors.length > 0) { + throw new Error(errors.toString()); + } + return validatedConfig; +}