🔍 Code Extractor

function validate_config

Maturity: 41

Validates the presence of required Microsoft 365 configuration settings and raises an error if any are missing.

File:
/tf/active/vicechatdev/email-forwarder/src/config/settings.py
Lines:
59 - 76
Complexity:
simple

Purpose

This function performs configuration validation for Microsoft 365 integration by checking that all required environment variables or configuration values (client ID, client secret, tenant ID, and sender email) are present and non-empty. It's typically called during application startup or before attempting MS365 operations to fail fast if configuration is incomplete.

Source Code

def validate_config():
    """Validate that all required configuration is present"""
    required_settings = [
        ('MS365_CLIENT_ID', MS365_CLIENT_ID),
        ('MS365_CLIENT_SECRET', MS365_CLIENT_SECRET),
        ('MS365_TENANT_ID', MS365_TENANT_ID),
        ('MS365_SENDER_EMAIL', MS365_SENDER_EMAIL),
    ]
    
    missing = []
    for name, value in required_settings:
        if not value:
            missing.append(name)
    
    if missing:
        raise ValueError(f"Missing required configuration: {', '.join(missing)}")
    
    return True

Return Value

Returns True (boolean) if all required configuration settings are present and non-empty. If any required settings are missing, raises a ValueError with a message listing all missing configuration items instead of returning.

Usage Example

import os

# Set up required configuration
MS365_CLIENT_ID = os.getenv('MS365_CLIENT_ID', '')
MS365_CLIENT_SECRET = os.getenv('MS365_CLIENT_SECRET', '')
MS365_TENANT_ID = os.getenv('MS365_TENANT_ID', '')
MS365_SENDER_EMAIL = os.getenv('MS365_SENDER_EMAIL', '')

# Validate configuration before proceeding
try:
    validate_config()
    print("Configuration is valid")
    # Proceed with MS365 operations
except ValueError as e:
    print(f"Configuration error: {e}")
    # Handle missing configuration

Best Practices

  • Call this function early in your application startup or initialization process to fail fast if configuration is incomplete
  • Ensure the required configuration variables (MS365_CLIENT_ID, MS365_CLIENT_SECRET, MS365_TENANT_ID, MS365_SENDER_EMAIL) are defined in the same scope before calling this function
  • Handle the ValueError exception appropriately - log the error and prevent the application from attempting MS365 operations with incomplete configuration
  • Consider loading configuration from environment variables using os.getenv() to keep sensitive credentials out of source code
  • The function checks for empty strings and None values, so ensure configuration values are properly set and not just defined as empty strings

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_configuration_v3 76.8% similar

    A test function that validates the presence and loading of required Microsoft 365 and SMTP configuration settings from a settings module.

    From: /tf/active/vicechatdev/email-forwarder/test_imports.py
  • function check_configuration_v1 72.1% similar

    Validates the presence of a .env configuration file and checks that all required environment variables (TENANT_ID, CLIENT_ID, CLIENT_SECRET, FROM_EMAIL) are defined.

    From: /tf/active/vicechatdev/email-forwarder/run_service.py
  • function test_configuration_v1 70.2% similar

    Validates that all required configuration variables (Azure AD credentials, OpenAI API key, and domain) are properly set and not using placeholder values.

    From: /tf/active/vicechatdev/find_email/test_vendor_extractor.py
  • function main_v34 68.8% similar

    A validation function that checks SharePoint configuration settings from environment variables and provides diagnostic feedback on their validity.

    From: /tf/active/vicechatdev/SPFCsync/validate_config.py
  • function validate_azure_client_secret 64.1% similar

    Validates an Azure client secret by checking for placeholder values, minimum length requirements, and common invalid patterns.

    From: /tf/active/vicechatdev/SPFCsync/validate_config.py
← Back to Browse