File size: 5,799 Bytes
31f0e50 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | """
Input Validation Module.
Provides validation utilities for:
- Message validation
- Session ID validation
- Language code validation
- Financial entity validation
"""
import re
import uuid
from typing import Tuple, Optional, List
def validate_message(message: str) -> Tuple[bool, Optional[str]]:
"""
Validate input message.
Checks:
- Not empty
- Not only whitespace
- Within length limits (1-5000 chars)
Args:
message: Input message to validate
Returns:
Tuple of (is_valid, error_message)
"""
if not message:
return False, "Message is required"
if not message.strip():
return False, "Message cannot be empty or whitespace only"
if len(message) > 5000:
return False, f"Message exceeds maximum length of 5000 characters (got {len(message)})"
return True, None
def validate_session_id(session_id: str) -> Tuple[bool, Optional[str]]:
"""
Validate session ID format.
Args:
session_id: Session ID to validate
Returns:
Tuple of (is_valid, error_message)
"""
if not session_id:
return True, None # Session ID is optional
try:
uuid.UUID(session_id, version=4)
return True, None
except ValueError:
return False, "Session ID must be a valid UUID v4 format"
def validate_language(language: str) -> Tuple[bool, Optional[str]]:
"""
Validate language code.
Args:
language: Language code to validate
Returns:
Tuple of (is_valid, error_message)
"""
allowed = {"auto", "en", "hi"}
if language not in allowed:
return False, f"Language must be one of: {', '.join(sorted(allowed))}"
return True, None
def validate_upi_id(upi_id: str) -> bool:
"""
Validate UPI ID format.
Format: username@provider (e.g., user@paytm)
Args:
upi_id: UPI ID to validate
Returns:
True if valid format
"""
pattern = r"^[a-zA-Z0-9._-]+@[a-zA-Z]+$"
return bool(re.match(pattern, upi_id))
def validate_bank_account(account: str) -> bool:
"""
Validate bank account number.
Valid: 9-18 digits (excluding 10 digits which are phone numbers)
Args:
account: Account number to validate
Returns:
True if valid format
"""
if not account.isdigit():
return False
length = len(account)
# Must be 9-18 digits
if length < 9 or length > 18:
return False
# Exclude phone numbers (exactly 10 digits)
if length == 10:
return False
return True
def validate_ifsc_code(ifsc: str) -> bool:
"""
Validate IFSC code format.
Format: XXXX0XXXXXX (11 characters, 5th is always 0)
Args:
ifsc: IFSC code to validate
Returns:
True if valid format
"""
pattern = r"^[A-Z]{4}0[A-Z0-9]{6}$"
return bool(re.match(pattern, ifsc.upper()))
def validate_phone_number(phone: str) -> bool:
"""
Validate Indian phone number format.
Valid formats:
- 10 digits starting with 6-9
- +91 followed by 10 digits
Args:
phone: Phone number to validate
Returns:
True if valid format
"""
# Remove common separators
cleaned = re.sub(r"[\s\-]", "", phone)
# Check with country code
if cleaned.startswith("+91"):
cleaned = cleaned[3:]
# Must be 10 digits starting with 6-9
if len(cleaned) != 10:
return False
if not cleaned.isdigit():
return False
if cleaned[0] not in "6789":
return False
return True
def validate_url(url: str) -> bool:
"""
Validate URL format.
Args:
url: URL to validate
Returns:
True if valid URL format
"""
pattern = r"^https?://[^\s<>\"{}|\\^`\[\]]+"
return bool(re.match(pattern, url))
def validate_all_intelligence(
intel: dict,
) -> Tuple[dict, List[str]]:
"""
Validate all extracted intelligence.
Args:
intel: Dictionary of extracted intelligence
Returns:
Tuple of (validated_intel, validation_errors)
"""
validated = {
"upi_ids": [],
"bank_accounts": [],
"ifsc_codes": [],
"phone_numbers": [],
"phishing_links": [],
}
errors = []
for upi in intel.get("upi_ids", []):
if validate_upi_id(upi):
validated["upi_ids"].append(upi)
else:
errors.append(f"Invalid UPI ID: {upi}")
for account in intel.get("bank_accounts", []):
if validate_bank_account(account):
validated["bank_accounts"].append(account)
else:
errors.append(f"Invalid bank account: {account}")
for ifsc in intel.get("ifsc_codes", []):
if validate_ifsc_code(ifsc):
validated["ifsc_codes"].append(ifsc.upper())
else:
errors.append(f"Invalid IFSC code: {ifsc}")
for phone in intel.get("phone_numbers", []):
if validate_phone_number(phone):
validated["phone_numbers"].append(phone)
else:
errors.append(f"Invalid phone number: {phone}")
for url in intel.get("phishing_links", []):
if validate_url(url):
validated["phishing_links"].append(url)
else:
errors.append(f"Invalid URL: {url}")
return validated, errors
|