Repository navigation
feat: add warning spec attribute - #257
Open
vergiLgood1 wants to merge 1 commit into
Open
vergiLgood1 wants to merge 1 commit into
vergiLgood1 wants to merge 1 commit into
Conversation
Logs an optional warning message (without failing) whenever an env var is not set and a fallback value (default/devDefault/testDefault) is used. Closes af#220
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an optional
warningmessage to validator specs. When an env var is not set and a fallback value (default,devDefault, ortestDefault) is used, the message is surfaced by the reporter without failing the program.Closes #220
Design decision
The issue's example uses
str({ warning: "..." })with nodefaultand implies the app should keep running ("App will run but it will not send any email"). However, makingwarningimplicitly turn a required var into an optional one would conflict with the library's philosophy (a program should only run when its environment dependencies are met) and could silently weaken validation — e.g. someone adding a warning message to a required database password var.So this PR keeps optionality explicit, consistent with how
default: undefinedalready works:With a concrete fallback, the warning fires when that fallback is used:
I'm happy to adjust if a different behavior (e.g.
warningimplying optionality) is preferred.Changes
src/types.ts— newwarning?: stringonSpec<T>; new optionalwarningsfield onReporterOptions<T>src/core.ts—getSanitizedEnvrecords a warning whenever the env var was not set and a fallback (testDefault/devDefault/default) was used, and passeswarningsto the reporter. Withreporter: nullthe caller opts out of all reporting, including warnings (errors are still thrown, as before)src/reporter.ts—defaultReporterprints aWarnings:section (same visual style as the error sections). Warnings never affect the exit behavior: with only warnings the process exits normally; with errors the existing error/exit flow is unchangedREADME.md— documents the new attributetests/warning.test.ts— new: warning recorded fordefault/devDefault/testDefaultfallbacks, no warning when the var is set, missing var without any fallback still errors,reporter: nullbehavior, and type-level assertions (optionality still driven bydefault: undefined, not bywarning)tests/reporter.test.ts— new: warnings printed without exiting, warnings + errors printed together with exit preserved, warnings sortedtests/requiredWhen.test.ts— updated to match the new reporter call signature (warnings: {})Verification
bun run ts✅bun run lint✅bun run test:ci✅ (82 tests, 8 files)bun run coverage✅ (functions 100%, lines 98.89% — above the 98% thresholds)Example output: