All quizzesHard
Validation & Shutdown
Preview — 3 of 10 questions
Using a custom validate function with class-transformer and class-validator, why is plainToInstance(EnvVars, config, { enableImplicitConversion: true }) important?
javascript
import { plainToInstance } from 'class-transformer';
import { IsInt, IsBoolean, validateSync } from 'class-validator';
class EnvVars {
@IsInt() PORT!: number;
@IsBoolean() FEATURE_X!: boolean;
}
export function validate(config: Record<string, unknown>): EnvVars {
const v = plainToInstance(EnvVars, config, { enableImplicitConversion: true });
const errors = validateSync(v, { skipMissingProperties: false });
if (errors.length) throw new Error(errors.toString());
return v;
}AIt encrypts the env values before validation
BEnv vars are always strings, so implicit conversion lets decorators like @IsInt()/@IsBoolean() coerce "3000"/"true" to their real types before validation
CIt disables validation for missing properties
DIt makes validation asynchronous
What is the main benefit of enabling webpack Hot Module Replacement (HMR) in NestJS during development?
javascript
// main.ts (HMR setup)
declare const module: any;
async function bootstrap(): Promise<void> {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
if (module.hot) {
module.hot.accept();
module.hot.dispose(() => app.close());
}
}
void bootstrap();AIt enables zero-downtime deploys in production
BIt reloads only changed modules without a full process restart, dramatically reducing rebuild/restart time for large apps
CIt automatically writes migrations when entities change
DIt replaces the DI container with a faster runtime
With expandVariables: true, what does .env containing BASE_URL=http://localhost\nAPI_URL=${BASE_URL}/api produce for API_URL?
javascript
ConfigModule.forRoot({ expandVariables: true });
// .env:
// BASE_URL=http://localhost
// API_URL=${BASE_URL}/api -> resolves to http://localhost/apiA${BASE_URL}/api (literal, unexpanded)
Bundefined, because expansion only works for process.env
Chttp://localhost/api (the reference is expanded)
DAn error, because variable references aren't allowed in .env
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.