File size: 1,223 Bytes
e656014
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
from typing import Optional


EMAIL_RE = re.compile(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")
USERNAME_RE = re.compile(r"^[a-zA-Z0-9_-]{3,32}$")


def is_valid_email(email: str) -> bool:
    """Return True if the email address is syntactically valid."""
    return bool(EMAIL_RE.match(email))


def is_valid_username(username: str) -> bool:
    """Return True if username is 3-32 chars, alphanumeric/underscore/hyphen only."""
    return bool(USERNAME_RE.match(username))


def is_strong_password(password: str) -> tuple[bool, Optional[str]]:
    """

    Check password strength.



    Returns:

        (True, None) if valid, (False, reason) if invalid.

    """
    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"[0-9]", password):
        return False, "Password must contain at least one number"
    return True, None


def sanitize_string(value: str, max_length: int = 500) -> str:
    """Strip whitespace and truncate a string to max_length."""
    return value.strip()[:max_length]