fix: validate config after PostLoad hooks

Re-run validator after PostLoad, return errors from RootDir, and add Config
alias for C(). Hooks receive *T instead of *Manager.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-06-29 18:00:31 -07:00
parent 444b2d4cf4
commit f2b6c07ff7
9 changed files with 421 additions and 230 deletions
+61 -1
View File
@@ -1,3 +1,63 @@
# config
config is a tiny library to streamline configuration loading and validation using [viper](https://github.com/spf13/viper) and [validator](https://github.com/go-playground/validator).
Typed configuration loading with [viper](https://github.com/spf13/viper) and [validator](https://github.com/go-playground/validator).
## Install
```bash
go get gitea.auvem.com/go-toolkit/config
```
## Quick start
```go
type Schema struct {
AppName string `mapstructure:"appName" validate:"required"`
}
dir, err := config.RootDir(".env.yml", 1)
if err != nil {
log.Fatal(err)
}
m := config.NewManager[Schema](".env", "yaml", dir).WithOpts(&config.ManagerOpts[Schema]{
EnvPrefix: "MYAPP",
PostLoad: func(cfg *Schema) error {
// derived fields — validated again after PostLoad
return nil
},
})
if err := m.Load(); err != nil {
log.Fatal(err)
}
cfg := m.C() // or m.Config()
_ = cfg.AppName
```
## Load vs C
- `Load()` returns errors explicitly — use in `main` during startup
- `C()` and `Config()` lazy-load and panic on failure — convenient after startup
## PostLoad hooks
PostLoad runs after the first validation pass. The schema is **re-validated** after PostLoad so derived fields cannot bypass constraints.
## RootDir
`RootDir(name, depth)` returns `(string, error)`. `MustRootDir` panics on failure (for package-level `var` initialization).
## Breaking changes (V1)
| Before | After |
|--------|-------|
| `PostLoad func(*Manager[T])` | `PostLoad func(*T)` |
| `PreLoad func(*Manager[T])` | `PreLoad func(*T)` |
| `Manager.R` field | use `C()` / hook `*T` parameter |
| `RootDir(...) string` (panic) | `RootDir(...) (string, error)`; use `MustRootDir` for panic |
## Companion types
Configuration structs in [dbx](https://gitea.auvem.com/go-toolkit/dbx) and [courier](https://gitea.auvem.com/go-toolkit/courier) include mapstructure/validate tags compatible with this package.