Currently, ArkEnv preserves empty environment variable values (e.g., MY_ENV= in a .env file) as empty strings (""). While this mirrors how Node.js populates process.env, it prevents ArkType from applying defined default values and often results in unnecessary validation errors for numeric or boolean types.
Current Behavior Example
Given the following schema:
const env = arkenv({
PORT: "number = 3000",
DEBUG: "boolean = false"
});
If a user has an "empty" entry in their .env:
ArkEnv processes this as:
PORT is "". Validation fails: "PORT must be a number (was a string)".
DEBUG is "". Validation fails: "DEBUG must be a boolean (was a string)".
The user's intent when leaving a value blank is typically to fallback to the default or treat it as "not set."
Proposed Solution
Modify the coerce logic to optionally or by default transform empty strings ("") into undefined before passing the data to the validator.
Expected Behavior After Change
- Input:
VAL= (manifests as "")
- Coercion: Transforms
"" to undefined.
- Validation:
- If
VAL: "string = 'default'" → result is 'default'.
- If
VAL: "string?" → result is undefined.
- If
VAL: "string" → validation fails correctly with "VAL is missing".
Implementation Considerations
- Opt-in vs Default: Decide if this should be the default behavior or controlled via
ArkEnvConfig (e.g., emptyAsUndefined: boolean).
- String Intent: Evaluate if any users genuinely need
"" to be treated as a valid value. If so, a configuration flag is preferable.
- Array Consistency: Ensure this doesn't break the existing successful parsing of empty strings into empty arrays
[].
Recommended Test Case
it("should apply defaults to empty environment variables", () => {
const env = createEnv(
{ PORT: "number = 3000" },
{ env: { PORT: "" } }
);
expect(env.PORT).toBe(3000);
});
Currently, ArkEnv preserves empty environment variable values (e.g.,
MY_ENV=in a.envfile) as empty strings (""). While this mirrors how Node.js populatesprocess.env, it prevents ArkType from applying defined default values and often results in unnecessary validation errors for numeric or boolean types.Current Behavior Example
Given the following schema:
If a user has an "empty" entry in their
.env:ArkEnv processes this as:
PORTis"". Validation fails:"PORT must be a number (was a string)".DEBUGis"". Validation fails:"DEBUG must be a boolean (was a string)".The user's intent when leaving a value blank is typically to fallback to the default or treat it as "not set."
Proposed Solution
Modify the
coercelogic to optionally or by default transform empty strings ("") intoundefinedbefore passing the data to the validator.Expected Behavior After Change
VAL=(manifests as"")""toundefined.VAL: "string = 'default'"→ result is'default'.VAL: "string?"→ result isundefined.VAL: "string"→ validation fails correctly with"VAL is missing".Implementation Considerations
ArkEnvConfig(e.g.,emptyAsUndefined: boolean).""to be treated as a valid value. If so, a configuration flag is preferable.[].Recommended Test Case