Skip to content

Add Support for Optional Environment Variables - #8

Merged
yasserfedsi merged 3 commits into
mainfrom
feature/optional-environment-variable
Jun 26, 2026
Merged

Add Support for Optional Environment Variables#8
yasserfedsi merged 3 commits into
mainfrom
feature/optional-environment-variable

Conversation

@yasserfedsi

@yasserfedsi yasserfedsi commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Pull Request

Description

This PR adds support for optional environment variables in the validation schema. Currently, all variables defined in the schema are treated as required, which forces users to provide values for every configuration key even when they're genuinely optional in their application.

Purpose of this PR:

  • Add an optional property to variable definitions that allows variables to be omitted from the environment without causing validation errors

  • When a variable is marked as optional and is missing, validation passes without any error

  • When a variable is marked as optional but is present, it is still validated according to its type definition

Which issues does it resolve?

Relevant context or dependencies:

  • This is a non-breaking change that maintains backward compatibility

  • Existing schemas will continue to work as expected (default behavior remains "required")

  • No external dependencies are introduced

Type of Change

  • Bug fix
  • New feature
  • Refactor
  • Documentation update
  • Performance improvement

How Has This Been Tested?

Unit tests

Added comprehensive unit tests covering:

  • Optional string variable missing → validation passes
  • Optional number variable missing → validation passes
  • Optional boolean variable missing → validation passes
  • Optional variable present with valid value → validation passes
  • Optional variable present with invalid value → validation fails
  • Required variable missing → validation fails (unchanged behavior)
  • Required variable present → validation passes (unchanged behavior)
  • Required variable present with invalid value → validation fails (unchanged behavior)
  • Mixed required and optional variables in same schema → correct behavior for each

Integration tests

  • Tested with actual environment variable loading and validation
  • Verified that optional variables that are undefined don't trigger validation errors
  • Confirmed that type validation still applies when optional variables are provided

Manual testing

Tested the following scenarios locally:

// Schema with mixed required and optional
const schema = {
  PORT: { type: "number" },
  API_KEY: { type: "string", optional: true },
  LOG_LEVEL: { type: "string", optional: true },
  DEBUG: { type: "boolean", optional: true }
};

// All combinations validated successfully

Test coverage results

  • All existing tests pass ✓
  • New tests achieve 100% coverage for the optional feature
  • Test coverage remains at 95%+ overall

Checklist

  • My code follows the project’s coding standards
  • I have added or updated tests
  • I have updated documentation
  • All tests pass
  • I have reviewed my code before submitting

Additional Notes

Implementation Details

The implementation extends the existing validation logic with a check for the optional property:

// In the validation function
if (definition.optional && value === undefined) {
  return; // Skip validation, variable is optional and missing
}
// Continue with normal validation

Documentation Updates

Updated the README with:

  • Documentation of the optional property
  • Usage examples showing required vs optional variables
  • Updated API reference
  • Migration guide note (not required as this is backward compatible)

Backward Compatibility

This change is fully backward compatible:

  • Existing schemas without the optional property behave exactly as before (required by default)
  • No changes required to existing code
  • No breaking changes introduced

Example Usage

import { validateEnv } from '@yasserfedsi/envfy';

const env = validateEnv({
  // Required variables
  NODE_ENV: {
    type: "string",
    required: true // default behavior
  },
  PORT: {
    type: "number"
    // required by default
  },
  
  // Optional variables
  API_KEY: {
    type: "string",
    optional: true
  },
  LOG_LEVEL: {
    type: "string",
    optional: true
  },
  DEBUG_MODE: {
    type: "boolean",
    optional: true
  },
  REDIS_HOST: {
    type: "string",
    optional: true
  }
});

// All these will work:
// - PORT: 3000, API_KEY: missing ✓
// - PORT: 3000, API_KEY: "abc", DEBUG_MODE: missing ✓
// - PORT: 3000, API_KEY: "abc", DEBUG_MODE: true ✓

Future Considerations

Potential future enhancements:

  • Add support for default values when optional variables are missing
  • Support for conditional optionality based on other variables
  • Validation of optional variables against a set of allowed values only when present

@yasserfedsi yasserfedsi added this to the v0.4.0 milestone Jun 26, 2026
@yasserfedsi
yasserfedsi requested a review from Copilot June 26, 2026 11:54
@yasserfedsi yasserfedsi self-assigned this Jun 26, 2026
@yasserfedsi yasserfedsi added the enhancement New feature or request label Jun 26, 2026
@yasserfedsi yasserfedsi linked an issue Jun 26, 2026 that may be closed by this pull request
@github-project-automation github-project-automation Bot moved this to Backlog in envfy Jun 26, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@yasserfedsi yasserfedsi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good

@yasserfedsi yasserfedsi moved this from Backlog to In review in envfy Jun 26, 2026
@yasserfedsi
yasserfedsi merged commit dee1875 into main Jun 26, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from In review to Done in envfy Jun 26, 2026
@yasserfedsi
yasserfedsi deleted the feature/optional-environment-variable branch July 9, 2026 02:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Optional Environment Variables

2 participants