Skip to content

feat(core): Implement nestforge-config for Type-Safe, Modular Configuration #23

@vernonthedev

Description

@vernonthedev

Area

core

NestForge Version

1.6.0

Rust Version

1.87

Summary

Following the philosophy of NestJS (and inspired by libraries like Rikta), we need a standardized way to handle application configuration. Currently, developers often manually call env::var, which lacks type safety and validation. This feature introduces a centralized, injectable, and schema-validated configuration system that ensures the application "fails fast" at startup if the environment is misconfigured.

Core Objectives

  • Type Safety: Map configuration directly to Rust structs using serde.
  • Validation: Use the validator crate to ensure required variables (like DATABASE_URL) are malformed or missing before the app starts.
  • Modularity: Support ConfigModule::for_root() for global settings and for_feature() for module-specific scoped configurations.
  • Zero-Config/Autowiring: A "decorate and inject" workflow where developers simply define a struct and inject it where needed.

Technical Spec

  • Crate: Create a new member crate nestforge-config.
  • API Shape:
    • Definition: Use #[derive(Deserialize, Validate, Config)] on structs.
    • Registration: ConfigModule::for_root(options) within the AppModule macro.
    • Injection: pub config: Inject<DatabaseConfig> within #[injectable] services.
  • Crate Boundaries: * nestforge-macros: Add a Config derive macro to register structs into the DI inventory.
    • nestforge-core: Update the DI container to resolve these validated structs as singletons.
  • Dependencies: serde, dotenvy, validator, and inventory (for static registration/discovery).

Breaking Change Analysis

None to Low. This is a new, optional module. However, if we move the core framework to require nestforge-config for internal settings (like Port or Host), it will be a breaking change for existing main.rs setups.

Performance Impact

  • Startup: Slight increase in startup time due to .env parsing and struct validation (runs once).
  • Runtime: Zero overhead. Once validated and injected, configuration access is as fast as accessing a standard Arc<T>.
  • Binary Size: Minimal increase due to serde and validator dependencies.

Alternatives Considered

  • Manual envy or config-rs: Reason for Rejection: These don't integrate natively with the NestForge Dependency Injection (DI) system. Developers would still have to manually pass the config around, breaking the "Nest-like" ergonomics.
  • Plain lazy_static or OnceCell: Reason for Rejection: Harder to test and doesn't support the "Modular/Scoped" configuration pattern needed for larger microservices.

Implementation Checklist

  • Create nestforge-config crate.
  • Implement EnvSchema and EnvValidationIssue for detailed startup error reporting.
  • Develop the #[derive(Config)] procedural macro.
  • Update nestforge-cli to scaffold a default app_config.rs on nestforge new.

Metadata

Metadata

Assignees

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions