AiBarber / config /validation.py
SyedBasitAbbas's picture
Upload 14 files
7c26280 verified
"""Validation utilities for the barber booking system."""
import re
from typing import Tuple, Dict, Optional
def validate_email(email: str) -> bool:
"""Validate email format."""
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return bool(re.match(pattern, email))
def validate_phone(phone: str) -> bool:
"""Validate phone number format."""
pattern = r'^\+?[1-9]\d{1,14}$'
return bool(re.match(pattern, phone))
def validate_name(name: str) -> bool:
"""Validate customer name."""
words = name.strip().split()
return len(words) >= 1 and all(len(word) >= 2 for word in words)
def format_phone(phone: str) -> str:
"""Format phone number to international format."""
if not phone.startswith('+'):
return f"+{phone.lstrip('0')}"
return phone
def validate_booking_info(current_node: str, booking_info: Dict) -> Tuple[bool, str]:
"""Validate required booking information for the current node."""
if current_node == "ShowTopSlots" and not booking_info.get("name"):
return False, "I need your name first. What's your name?"
elif current_node == "CollectEmail" and not booking_info.get("service"):
return False, "Please select a service first."
elif current_node == "CollectPhone" and not booking_info.get("email"):
return False, "I need your email for the confirmation. What's your email address?"
elif current_node == "Confirmation" and not booking_info.get("phone"):
return False, "What's your phone number for appointment reminders?"
return True, ""
def get_closest_service(query: str) -> Optional[str]:
"""Find the closest matching service based on the user's input."""
query = query.lower().strip()
# Direct matches
if query in ["haircut", "beard trim", "full service"]:
return query
# Handle variations and partial matches
if any(word in query for word in ["hair", "cut", "haircut"]):
return "haircut"
elif any(word in query for word in ["beard", "trim", "beardtrim"]):
return "beard trim"
elif any(word in query for word in ["full", "complete", "both", "all"]):
return "full service"
return None