Reviewer · Configurations
Validate config bounds and dependencies
Configuration `Validate()` functions must check more than individual fields in isolation: they should also enforce cross-field dependencies and document why specific bounds or sentinel values exist.
Configuration Validate() functions must check more than individual fields in isolation: they should also enforce cross-field dependencies and document why specific bounds or sentinel values exist.
- Check both lower and upper bounds for numeric options, not just negative values. For example, if a flag caps a version or count, reject values above the known maximum as well as below the minimum, so the validated range matches what the code can actually handle.
- Enforce dependent/interdependent settings together: if enabling one option (e.g. a deletion sweep) requires another resource or option to be configured (e.g. compaction enabled, a data bucket set), fail validation when the dependency is missing instead of only checking each flag separately.
- When a config value has a special “sentinel” meaning (e.g.
0means “use the library default” or “disabled”), state this explicitly in both the flag help text and the validation error message, since any numeric value, including0, may otherwise be a legitimate value and this creates ambiguous behavior. - Keep validation scoped correctly: if a setting is only read by a subset of components (e.g. only the server that registers to a ring, not every client), validate it only where the constraint actually applies, and add a comment explaining the split so another engineer does not “fix” it by merging the checks.
- When one validation function already guarantees an invariant (e.g. “config list is non-empty”), add a comment at the call site noting this, or better, consolidate the check once near where configs are combined, rather than re-checking (or silently relying on) the same invariant in multiple unrelated functions.
- Document the rationale for arbitrary limits/constants (e.g. “max 32 buckets”) directly in the code comment, even if the number is just a reasonable starting point, so future readers know it can be raised but not safely lowered.
Example:
func (cfg *Config) Validate() error {
if cfg.APIVersionsMaxVersion < 0 || cfg.APIVersionsMaxVersion > DefaultAPIVersionsMaxVersion {
return fmt.Errorf("api-versions-max-version must be between 0 (uses the franz-go default) and %d", DefaultAPIVersionsMaxVersion)
}
if cfg.DeletionSweep.Enabled && !cfg.Compaction.Enabled {
return errors.New("deletion sweep requires compaction to be enabled")
}
return nil
}