【发布时间】:2020-04-24 23:46:11
【问题描述】:
所以我遵循了如何为我的 Nest 应用程序创建配置的指南
https://docs.nestjs.com/techniques/configuration
由于我有许多配置部分,我想将这些部分拆分为多个配置服务。所以我的 app.module.ts 导入了一个自定义配置模块
@Module({
imports: [CustomConfigModule]
})
export class AppModule {}
此自定义配置模块 (config.module.ts) 捆绑所有配置服务并加载 Nest 配置模块
@Module({
imports: [ConfigModule.forRoot()],
providers: [ServerConfigService],
exports: [ServerConfigService],
})
export class CustomConfigModule {}
最后,我有一个简单的配置服务 server.config.service.ts,它返回应用程序正在运行的端口
@Injectable()
export class ServerConfigService {
constructor(private readonly configService: ConfigService) {}
public get port(): number {
return this.configService.get<number>('SERVER_PORT');
}
}
我想在应用程序启动时验证这些服务。文档解释了如何为配置模块设置验证模式
https://docs.nestjs.com/techniques/configuration#schema-validation
在使用自定义配置模块时,如何将其用于我的服务验证? 我是否必须在每个服务构造函数中调用 joi 并验证那里的属性?
提前致谢
【问题讨论】: