🔍 Code Extractor

function validate_sharepoint_url

Maturity: 42

Validates that a given URL string conforms to SharePoint site URL format requirements, checking for proper protocol, domain, and path structure.

File:
/tf/active/vicechatdev/SPFCsync/validate_config.py
Lines:
13 - 31
Complexity:
simple

Purpose

This function performs comprehensive validation of SharePoint URLs to ensure they meet Microsoft SharePoint's URL conventions. It checks for HTTPS protocol, proper SharePoint domain (.sharepoint.com), and the presence of site or team paths. It's designed to catch common configuration errors, including placeholder URLs that haven't been updated. The function returns both a boolean validation result and a descriptive error message, making it useful for configuration validation, user input verification, and setup wizards.

Source Code

def validate_sharepoint_url(url):
    """Validate SharePoint URL format."""
    if not url or url == "https://your-tenant.sharepoint.com/sites/your-site":
        return False, "Please update SHAREPOINT_SITE_URL with your actual SharePoint site URL"
    
    if not url.startswith("https://"):
        return False, "SharePoint URL must start with https://"
    
    parsed = urlparse(url)
    if not parsed.hostname:
        return False, "Invalid URL format"
    
    if not parsed.hostname.endswith(".sharepoint.com"):
        return False, "URL must be a SharePoint site (*.sharepoint.com)"
    
    if "/sites/" not in url and "/teams/" not in url:
        return False, "URL should contain /sites/ or /teams/ for a specific site"
    
    return True, "SharePoint URL format is valid"

Parameters

Name Type Default Kind
url - - positional_or_keyword

Parameter Details

url: A string representing the SharePoint site URL to validate. Expected format is 'https://<tenant>.sharepoint.com/sites/<site-name>' or 'https://<tenant>.sharepoint.com/teams/<team-name>'. Can be None or empty string, which will fail validation. Should not be the placeholder value 'https://your-tenant.sharepoint.com/sites/your-site'.

Return Value

Returns a tuple of (bool, str). The first element is True if the URL passes all validation checks, False otherwise. The second element is a string message describing either the validation success ('SharePoint URL format is valid') or the specific reason for failure (e.g., 'SharePoint URL must start with https://', 'URL must be a SharePoint site (*.sharepoint.com)', 'URL should contain /sites/ or /teams/ for a specific site', etc.).

Dependencies

  • urllib.parse

Required Imports

from urllib.parse import urlparse

Usage Example

from urllib.parse import urlparse

def validate_sharepoint_url(url):
    """Validate SharePoint URL format."""
    if not url or url == "https://your-tenant.sharepoint.com/sites/your-site":
        return False, "Please update SHAREPOINT_SITE_URL with your actual SharePoint site URL"
    
    if not url.startswith("https://"):
        return False, "SharePoint URL must start with https://"
    
    parsed = urlparse(url)
    if not parsed.hostname:
        return False, "Invalid URL format"
    
    if not parsed.hostname.endswith(".sharepoint.com"):
        return False, "URL must be a SharePoint site (*.sharepoint.com)"
    
    if "/sites/" not in url and "/teams/" not in url:
        return False, "URL should contain /sites/ or /teams/ for a specific site"
    
    return True, "SharePoint URL format is valid"

# Example usage
valid_url = "https://contoso.sharepoint.com/sites/marketing"
is_valid, message = validate_sharepoint_url(valid_url)
print(f"Valid: {is_valid}, Message: {message}")
# Output: Valid: True, Message: SharePoint URL format is valid

invalid_url = "http://contoso.sharepoint.com/sites/marketing"
is_valid, message = validate_sharepoint_url(invalid_url)
print(f"Valid: {is_valid}, Message: {message}")
# Output: Valid: False, Message: SharePoint URL must start with https://

placeholder_url = "https://your-tenant.sharepoint.com/sites/your-site"
is_valid, message = validate_sharepoint_url(placeholder_url)
print(f"Valid: {is_valid}, Message: {message}")
# Output: Valid: False, Message: Please update SHAREPOINT_SITE_URL with your actual SharePoint site URL

Best Practices

  • Always check both the boolean return value and the message string to provide meaningful feedback to users
  • Use this function during application startup or configuration loading to fail fast with clear error messages
  • The function validates format only, not whether the URL actually exists or is accessible - additional authentication and connectivity checks may be needed
  • Consider logging the validation message for debugging purposes when validation fails
  • The function explicitly checks for the placeholder URL 'https://your-tenant.sharepoint.com/sites/your-site' to catch unconfigured applications
  • Both '/sites/' and '/teams/' paths are accepted as valid SharePoint site patterns

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function main_v33 70.1% 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 test_sharepoint_connection 63.6% similar

    Tests the connection to a SharePoint site by attempting to instantiate a SharePointClient with Azure credentials and configuration settings.

    From: /tf/active/vicechatdev/SPFCsync/test_connections.py
  • function test_sharepoint_with_token 61.2% similar

    Tests SharePoint REST API connectivity and authentication by making a GET request to retrieve site information using a provided access token.

    From: /tf/active/vicechatdev/SPFCsync/diagnose_permissions.py
  • function test_sharepoint_api_call 61.1% similar

    Tests SharePoint REST API connectivity by making an authenticated GET request to retrieve basic site information and validates the access token and permissions.

    From: /tf/active/vicechatdev/SPFCsync/diagnose_sharepoint.py
  • function test_sharepoint_listing 60.2% similar

    Tests the SharePoint document listing functionality by connecting to a SharePoint site and retrieving all documents from a specified path.

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