mud_server.admin_gradio.ui.validators
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
See also
mud_server.api.password_policy: Comprehensive password policy enforcement.
Functions
|
Validate username meets requirements. |
|
Validate password meets basic length requirements. |
|
Validate password against comprehensive security policy. |
|
Get human-readable password requirements for display to users. |
|
Validate that password and confirmation match. |
|
Validate that new password is different from old password. |
|
Validate that a required field has a value. |
|
Validate that user has an active session. |
|
Validate that user has admin or superuser role. |
|
Validate that a command has been entered. |
Module Contents
- mud_server.admin_gradio.ui.validators.validate_username(username)[source]
Validate username meets requirements.
Requirements: - Must not be None or empty - Must be at least 2 characters after stripping whitespace
- Parameters:
username (str | None) – Username to validate
- Returns:
Tuple of (is_valid, error_message) - (True, “”) if valid - (False, error_message) if invalid
- Return type:
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.")
- mud_server.admin_gradio.ui.validators.validate_password(password, min_length=8)[source]
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)
- Parameters:
- Returns:
Tuple of (is_valid, error_message)
- Return type:
Examples
>>> validate_password("password123") (True, "") >>> validate_password("short") (False, "Password must be at least 8 characters.") >>> validate_password(None) (False, "Password is required.")
See also
validate_password_with_policy: Comprehensive password validation.
- mud_server.admin_gradio.ui.validators.validate_password_with_policy(password, level=PolicyLevel.STANDARD)[source]
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.
- Parameters:
password (str | None) – Password to validate. Must not be None or empty.
level (mud_server.api.password_policy.PolicyLevel) – 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.
- Return type:
Tuple of (is_valid, error_message, validation_result)
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
See also
mud_server.api.password_policy: Full policy configuration options. get_password_requirements_text: Get human-readable requirements.
- mud_server.admin_gradio.ui.validators.get_password_requirements_text(level=PolicyLevel.STANDARD)[source]
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.
- Parameters:
level (mud_server.api.password_policy.PolicyLevel) – 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.
- Return type:
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
- mud_server.admin_gradio.ui.validators.validate_password_confirmation(password, password_confirm)[source]
Validate that password and confirmation match.
This should be called AFTER validate_password() has confirmed the password meets basic requirements.
- Parameters:
- Returns:
Tuple of (is_valid, error_message)
- Return type:
Examples
>>> validate_password_confirmation("password123", "password123") (True, "") >>> validate_password_confirmation("password123", "different") (False, "Passwords do not match.")
- mud_server.admin_gradio.ui.validators.validate_password_different(old_password, new_password)[source]
Validate that new password is different from old password.
- Parameters:
- Returns:
Tuple of (is_valid, error_message)
- Return type:
Examples
>>> validate_password_different("old123", "new456") (True, "") >>> validate_password_different("same123", "same123") (False, "New password must be different from current password.")
- mud_server.admin_gradio.ui.validators.validate_required_field(value, field_name)[source]
Validate that a required field has a value.
- Parameters:
- Returns:
Tuple of (is_valid, error_message)
- Return type:
Examples
>>> validate_required_field("something", "username") (True, "") >>> validate_required_field("", "password") (False, "Password is required.") >>> validate_required_field(None, "email") (False, "Email is required.")
- mud_server.admin_gradio.ui.validators.validate_session_state(session_state)[source]
Validate that user has an active session.
- Parameters:
session_state (dict) – Session state dictionary from Gradio
- Returns:
Tuple of (is_valid, error_message)
- Return type:
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.")
- mud_server.admin_gradio.ui.validators.validate_admin_role(session_state)[source]
Validate that user has admin or superuser role.
This should be called AFTER validate_session_state() has confirmed the user is logged in.
- Parameters:
session_state (dict) – Session state dictionary from Gradio
- Returns:
Tuple of (is_valid, error_message)
- Return type:
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.")
- mud_server.admin_gradio.ui.validators.validate_command_input(command)[source]
Validate that a command has been entered.
- Parameters:
command (str | None) – Command string to validate
- Returns:
Tuple of (is_valid, error_message)
- Return type:
Examples
>>> validate_command_input("look") (True, "") >>> validate_command_input(" ") (False, "Enter a command.") >>> validate_command_input("") (False, "Enter a command.")