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
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
serde.validatorcrate to ensure required variables (likeDATABASE_URL) are malformed or missing before the app starts.ConfigModule::for_root()for global settings andfor_feature()for module-specific scoped configurations.Technical Spec
nestforge-config.#[derive(Deserialize, Validate, Config)]on structs.ConfigModule::for_root(options)within theAppModulemacro.pub config: Inject<DatabaseConfig>within#[injectable]services.nestforge-macros: Add aConfigderive macro to register structs into the DI inventory.nestforge-core: Update the DI container to resolve these validated structs as singletons.serde,dotenvy,validator, andinventory(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-configfor internal settings (like Port or Host), it will be a breaking change for existingmain.rssetups.Performance Impact
.envparsing and struct validation (runs once).Arc<T>.serdeandvalidatordependencies.Alternatives Considered
envyorconfig-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.lazy_staticorOnceCell: Reason for Rejection: Harder to test and doesn't support the "Modular/Scoped" configuration pattern needed for larger microservices.Implementation Checklist
nestforge-configcrate.EnvSchemaandEnvValidationIssuefor detailed startup error reporting.#[derive(Config)]procedural macro.nestforge-clito scaffold a defaultapp_config.rsonnestforge new.