feat(12factor-env): fallback-class wrappers + house NODE_ENV semantics (+ graphql-env dedup)#1385
Merged
Merged
Conversation
…mantics Add withDefault/devDefault/required wrappers over any envalid validator to make a var's fallback intent explicit at the declaration site: withDefault(v, x) honest fallback in every environment devDefault(v, x) fallback in dev/test, required (throws) in production required(v) no fallback; throws in every environment when absent devDefault relies on house NODE_ENV semantics: envalid natively treats an unset NODE_ENV as production (making devDefault throw in local dev). This normalizes NODE_ENV before validation without mutating the caller's env: unset => development, test/testing/GITHUB_ACTIONS => test, production => production. Exported as getNodeEnv/isProduction/isTest/isDevelopment. Also add lenient coercion: boolish validator (accepts TRUE/yes/1 that envalid bool rejects) plus parseEnvBoolean/parseEnvNumber helpers.
Contributor
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
graphql-env duplicated the '[true,1,yes]' boolean coercion locally. Drop the copy and import parseEnvBoolean from 12factor-env (workspace dep), the canonical home for repeated env-coercion helpers. Behavior is unchanged: every call site is already guarded by a conditional spread, so the value is never '' or undefined.
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
Makes
12factor-envexpress the distinction that actually matters for config — "is there an honest fallback?" — instead of only "optional vs required", and starts consolidating the repeated env-coercion helpers into it. First consumer migrated:graphql-env.12factor-env: fallback-class wrappersThree thin wrappers over any envalid validator so the fallback class is legible at the declaration site:
withDefault(v, x)xxdevDefault(v, x)xrequired(v)This is the class that eliminates prod-lie defaults (e.g. an empty-string URL or a
localhostbase domain silently shipping to production).Key papercut fixed:
NODE_ENVdetectionenvalid natively treats an unset
NODE_ENVas production, sodevDefaultwould throw in ordinary local dev. This normalizesNODE_ENVbefore validation — without mutating the caller's env — using house semantics mirroring@pgpmjs/env:Exported as
getNodeEnv/isProduction/isTest/isDevelopment, and injected into the input passed to envalid'scleanEnvso its nativedevDefault/testDefaultresolve correctly.Lenient coercion helpers
boolishacceptstrue/1/yescase-insensitively (envalid'sboolrejectsTRUE/yes) and guards the typed-default case. PlusparseEnvBoolean/parseEnvNumbermatching the@pgpmjs/envconvention — the shared home for the coercion we keep re-implementing.Consumer:
graphql-envgraphql-envhad its own copy of the[true,1,yes]boolean coercion. Dropped it and importsparseEnvBooleanfrom12factor-env(workspace dep). Behavior unchanged — every call site is guarded by a conditional spread, so the value is never''/undefined.Notes
12factor-env— existingenv(inputEnv, secrets, vars)API, secret-file (_FILE,/run/secrets) behavior, and validator re-exports unchanged.12factor-envtests pass (20 new);graphql-envtests pass.compute-env(inconstructive-db) is the next consumer but needs12factor-envpublished first; deferred. pgpm reclassification deferred to a later phase.Link to Devin session: https://app.devin.ai/sessions/b4f03ceb2daf4231b0d30dbee3ae89da
Requested by: @pyramation