Configuration Validation
Configuration validation checks that settings are present, parseable, and satisfy application constraints before the service handles requests. Validating at startup makes deployment errors predictable and easier to diagnose.
Configuration validation checks settings before the application depends on them. Typical checks include required values, data types, allowed ranges, and relationships between settings. For example, a port should parse as an integer and fall within the valid port range.
A common approach is to collect settings from environment variables or a configuration file, convert them to the intended types, and validate the resulting values during startup. Keep this work in one predictable path rather than scattering checks across request handlers.
When validation fails, report which setting is wrong and what constraint it must satisfy. Avoid including secrets such as passwords or API keys in error messages. Failing early prevents an invalid deployment from appearing healthy and breaking only when a particular code path is used.
For larger applications, a settings object or validation library can centralize parsing and constraints. Regardless of the mechanism, tests should cover missing values, malformed values, boundary values, and valid configuration.
Key points
- Validate configuration once, before the application relies on it.
- Parse raw strings into appropriate types and enforce domain constraints.
- Make failures clear and actionable while keeping secrets out of logs.
- Test missing, malformed, boundary, and valid values.
Interview questions
How do you validate configuration values that depend on each other?
I would identify invariants that involve multiple values and validate them after each individual value has been parsed. For example, a minimum timeout must not exceed its maximum, and mutually exclusive modes should not both be enabled. These checks belong in the same startup configuration boundary so invalid combinations fail consistently. Tests should include both valid and invalid combinations.
How would you validate a port supplied through an environment variable?
I would read the setting from its source, convert it to the intended type, and check its constraints during startup. For a port, that means handling a missing value or parse error and checking the accepted range. If it is invalid, startup should stop with a message naming the setting and expected rule, without exposing secrets. I would test valid input, malformed input, and boundary cases.
How should an application handle a missing configuration value?
A missing setting should be handled according to whether it is required or has a safe, documented default. Parsing should be explicit, and validation should distinguish malformed values from values that parse but violate a constraint. This makes diagnostics clearer and tests easier to write. Defaults should not hide a deployment mistake when the setting is genuinely required.
How would you organize validation when a service has many settings?
I would centralize configuration parsing and validation in a settings layer that produces a typed object consumed by the rest of the application. That avoids repeatedly reading raw strings and duplicating checks. The layer should define defaults, required fields, constraints, and safe error reporting. Tests can exercise it independently of application startup and external services.