mud_server.admin_gradio.ui.validators ===================================== .. py:module:: mud_server.admin_gradio.ui.validators .. autoapi-nested-parse:: Input validation utilities for MUD client. This module provides validation functions for user input across all client operations. All validators return a tuple of (is_valid: bool, error_message: str). Validation is separated from API logic to enable: - Reusability across different UI contexts - Easy testing of validation rules - Consistent error messages - Client-side validation before API calls Password Validation: This module integrates with the password_policy module for comprehensive password strength validation. The validate_password_strength() function provides detailed feedback including: - Policy compliance checking (length, character classes) - Common password rejection - Sequential/repeated character detection - Strength scoring and entropy estimation Common Patterns: All validators return (bool, str) tuples: - (True, "") for valid input - (False, "Error message") for invalid input .. seealso:: mud_server.api.password_policy: Comprehensive password policy enforcement. Functions --------- .. autoapisummary:: mud_server.admin_gradio.ui.validators.validate_username mud_server.admin_gradio.ui.validators.validate_password mud_server.admin_gradio.ui.validators.validate_password_with_policy mud_server.admin_gradio.ui.validators.get_password_requirements_text mud_server.admin_gradio.ui.validators.validate_password_confirmation mud_server.admin_gradio.ui.validators.validate_password_different mud_server.admin_gradio.ui.validators.validate_required_field mud_server.admin_gradio.ui.validators.validate_session_state mud_server.admin_gradio.ui.validators.validate_admin_role mud_server.admin_gradio.ui.validators.validate_command_input Module Contents --------------- .. py:function:: validate_username(username) Validate username meets requirements. Requirements: - Must not be None or empty - Must be at least 2 characters after stripping whitespace :param username: Username to validate :returns: Tuple of (is_valid, error_message) - (True, "") if valid - (False, error_message) if invalid .. admonition:: Examples >>> validate_username("alice") (True, "") >>> validate_username("a") (False, "Username must be at least 2 characters.") >>> validate_username(" ") (False, "Username must be at least 2 characters.") >>> validate_username(None) (False, "Username must be at least 2 characters.") .. py:function:: validate_password(password, min_length = 8) Validate password meets basic length requirements. This is a simple length-only validator for backward compatibility. For comprehensive password strength validation including common password checking, sequential/repeated character detection, and strength scoring, use validate_password_with_policy() instead. Requirements: - Must not be None or empty - Must be at least min_length characters (default: 8) :param password: Password to validate :param min_length: Minimum required length (default: 8) :returns: Tuple of (is_valid, error_message) .. admonition:: Examples >>> validate_password("password123") (True, "") >>> validate_password("short") (False, "Password must be at least 8 characters.") >>> validate_password(None) (False, "Password is required.") .. seealso:: validate_password_with_policy: Comprehensive password validation. .. py:function:: validate_password_with_policy(password, level = PolicyLevel.STANDARD) Validate password against comprehensive security policy. This function performs thorough password validation using the password policy module. It checks for: - Minimum length requirements (12 chars for STANDARD policy) - Common/compromised password rejection - Sequential character patterns (abc, 123) - Repeated character patterns (aaa, 111) - Character class diversity (uppercase, lowercase, digits, special) The function returns detailed feedback including a strength score and specific error messages to help users create stronger passwords. :param password: Password to validate. Must not be None or empty. :param level: Security policy level to enforce. Options: - PolicyLevel.BASIC: 8 char minimum, basic checks - PolicyLevel.STANDARD: 12 char minimum, comprehensive checks (default) - PolicyLevel.STRICT: 16 char minimum, all character classes required :returns: - is_valid: True if password meets all policy requirements - error_message: Combined error messages or empty string if valid - validation_result: Full ValidationResult object with score, warnings, etc. None if password was None/empty. :rtype: Tuple of (is_valid, error_message, validation_result) .. admonition:: Examples >>> is_valid, msg, result = validate_password_with_policy("MyStr0ng!Pass#2024") >>> is_valid True >>> result.score > 70 True >>> is_valid, msg, result = validate_password_with_policy("password123") >>> is_valid False >>> "common" in msg.lower() True >>> is_valid, msg, result = validate_password_with_policy("short") >>> is_valid False >>> "12 characters" in msg True .. seealso:: mud_server.api.password_policy: Full policy configuration options. get_password_requirements_text: Get human-readable requirements. .. py:function:: get_password_requirements_text(level = PolicyLevel.STANDARD) Get human-readable password requirements for display to users. This function returns a formatted string describing all password requirements for the specified policy level. Useful for displaying in registration forms, password change dialogs, or help text. :param level: Policy level to describe. Options: - PolicyLevel.BASIC: Minimal requirements - PolicyLevel.STANDARD: Recommended requirements (default) - PolicyLevel.STRICT: Maximum security requirements :returns: Multi-line string describing all password requirements. .. admonition:: Examples >>> requirements = get_password_requirements_text() >>> "12 characters" in requirements True >>> "common" in requirements.lower() True >>> strict_req = get_password_requirements_text(PolicyLevel.STRICT) >>> "16 characters" in strict_req True >>> "uppercase" in strict_req.lower() True .. py:function:: validate_password_confirmation(password, password_confirm) Validate that password and confirmation match. This should be called AFTER validate_password() has confirmed the password meets basic requirements. :param password: Original password :param password_confirm: Confirmation password :returns: Tuple of (is_valid, error_message) .. admonition:: Examples >>> validate_password_confirmation("password123", "password123") (True, "") >>> validate_password_confirmation("password123", "different") (False, "Passwords do not match.") .. py:function:: validate_password_different(old_password, new_password) Validate that new password is different from old password. :param old_password: Current/old password :param new_password: New password to set :returns: Tuple of (is_valid, error_message) .. admonition:: Examples >>> validate_password_different("old123", "new456") (True, "") >>> validate_password_different("same123", "same123") (False, "New password must be different from current password.") .. py:function:: validate_required_field(value, field_name) Validate that a required field has a value. :param value: Field value to check :param field_name: Name of field (for error message) :returns: Tuple of (is_valid, error_message) .. admonition:: Examples >>> validate_required_field("something", "username") (True, "") >>> validate_required_field("", "password") (False, "Password is required.") >>> validate_required_field(None, "email") (False, "Email is required.") .. py:function:: validate_session_state(session_state) Validate that user has an active session. :param session_state: Session state dictionary from Gradio :returns: Tuple of (is_valid, error_message) .. admonition:: Examples >>> validate_session_state({"logged_in": True}) (True, "") >>> validate_session_state({"logged_in": False}) (False, "You are not logged in.") >>> validate_session_state({}) (False, "You are not logged in.") .. py:function:: validate_admin_role(session_state) Validate that user has admin or superuser role. This should be called AFTER validate_session_state() has confirmed the user is logged in. :param session_state: Session state dictionary from Gradio :returns: Tuple of (is_valid, error_message) .. admonition:: Examples >>> validate_admin_role({"role": "admin"}) (True, "") >>> validate_admin_role({"role": "superuser"}) (True, "") >>> validate_admin_role({"role": "player"}) (False, "Access Denied: Admin or Superuser role required.") .. py:function:: validate_command_input(command) Validate that a command has been entered. :param command: Command string to validate :returns: Tuple of (is_valid, error_message) .. admonition:: Examples >>> validate_command_input("look") (True, "") >>> validate_command_input(" ") (False, "Enter a command.") >>> validate_command_input("") (False, "Enter a command.")