46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
import re
|
|
|
|
def sanitize_phone(phone_str: str) -> str:
|
|
"""
|
|
Sanitiza um número de telefone para o formato E.164 brasileiro (ex: 5511999999999).
|
|
Remove caracteres não numéricos e adiciona DDI 55 se ausente.
|
|
"""
|
|
if not phone_str:
|
|
return ""
|
|
|
|
# Manter apenas digitos
|
|
digits = re.sub(r'\D', '', str(phone_str))
|
|
|
|
if not digits:
|
|
return ""
|
|
|
|
# Se já tiver o DDI 55 e tiver 12 ou 13 dígitos
|
|
if digits.startswith('55') and len(digits) in (12, 13):
|
|
return digits
|
|
|
|
# Se tiver 10 (fixo com DDD) ou 11 (celular com DDD) dígitos
|
|
if len(digits) in (10, 11):
|
|
return f"55{digits}"
|
|
|
|
return digits
|
|
|
|
def sanitize_cep(cep_str: str) -> str:
|
|
"""
|
|
Limpa o CEP retornando apenas 8 dígitos numéricos.
|
|
"""
|
|
if not cep_str:
|
|
return ""
|
|
digits = re.sub(r'\D', '', str(cep_str))
|
|
if len(digits) == 8:
|
|
return digits
|
|
return digits
|
|
|
|
def validate_email(email_str: str) -> bool:
|
|
"""
|
|
Valida formato basico de email.
|
|
"""
|
|
if not email_str:
|
|
return False
|
|
regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
|
|
return bool(re.match(regex, email_str.strip()))
|