All quizzesMedium
Namespaced Config & Hooks
Preview — 3 of 10 questions
You pass a validationSchema (Joi) to ConfigModule.forRoot(). What is the effect at application startup?
javascript
import * as Joi from 'joi';
ConfigModule.forRoot({
validationSchema: Joi.object({
NODE_ENV: Joi.string().valid('development', 'production', 'test').required(),
PORT: Joi.number().default(3000),
DATABASE_URL: Joi.string().uri().required(),
}),
validationOptions: { abortEarly: false },
});AIt validates the loaded environment variables and throws (aborting boot) if any required value is missing or malformed
BIt silently coerces invalid values to defaults and continues
CIt only validates values the first time ConfigService.get() is called
DIt runs validation on every HTTP request before the route handler
You provide load: [() => ({ database: { host: process.env.DB_HOST } })]. How do you read the host?
javascript
ConfigModule.forRoot({
load: [() => ({ database: { host: process.env.DB_HOST } })],
});
// Later:
const host = this.config.get<string>('database.host');Aconfig.get('DB_HOST')
Bconfig.get('database/host')
Cconfig.get('database.host')
Dconfig.get('database').host()
What does the generic in config.get<string>('database.host') actually do?
javascript
// Compile-time type is string, but runtime value is whatever was stored.
const host = this.config.get<string>('database.host');
// If you need a number, parse/validate explicitly:
const port = Number(this.config.get('PORT'));AIt validates at runtime that the value is a string and throws otherwise
BIt registers the key as required in the validation schema
CIt converts the stored value into a string at runtime
DIt only affects the compile-time return type; no runtime conversion or check happens
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.