File size: 1,210 Bytes
1fce3ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Input validation utilities
"""

import re
from typing import Optional
from app.core.constants import EMAIL_REGEX, PHONE_REGEX, SAUDI_NATIONAL_ID_REGEX


def validate_email(email: str) -> bool:
    """Validate email format"""
    return bool(re.match(EMAIL_REGEX, email))


def validate_phone(phone: str) -> bool:
    """Validate phone number format"""
    return bool(re.match(PHONE_REGEX, phone))


def validate_saudi_id(national_id: str) -> bool:
    """Validate Saudi national ID format"""
    return bool(re.match(SAUDI_NATIONAL_ID_REGEX, national_id))


def validate_password_strength(password: str) -> tuple[bool, Optional[str]]:
    """
    Validate password strength
    
    Returns:
        (is_valid, error_message)
    """
    if len(password) < 8:
        return False, "Password must be at least 8 characters"
    
    if not re.search(r"[A-Z]", password):
        return False, "Password must contain at least one uppercase letter"
    
    if not re.search(r"[a-z]", password):
        return False, "Password must contain at least one lowercase letter"
    
    if not re.search(r"\d", password):
        return False, "Password must contain at least one digit"
    
    return True, None