-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.py
More file actions
98 lines (76 loc) · 3.09 KB
/
validators.py
File metadata and controls
98 lines (76 loc) · 3.09 KB
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
"""
Input Validators using Pydantic
Ensures data integrity and security
"""
from pydantic import BaseModel, validator, Field
from datetime import datetime
import re
class PhoneNumber(BaseModel):
"""Validate phone numbers"""
number: str = Field(..., description="Phone number to validate")
@validator('number')
def validate_phone(cls, v):
if not v:
raise ValueError('Phone number cannot be empty')
# Remove all non-digits
digits = re.sub(r'\D', '', v)
# Check length (7-15 digits is standard international range)
if len(digits) < 7 or len(digits) > 15:
raise ValueError(f'Invalid phone number length: {len(digits)} digits')
return digits
@property
def formatted(self):
"""Return formatted phone number"""
return self.number
class AppointmentTime(BaseModel):
"""Validate appointment times"""
time: str = Field(..., description="ISO 8601 datetime string")
@validator('time')
def validate_time(cls, v):
try:
# Parse ISO 8601 datetime
dt = datetime.fromisoformat(v.replace('Z', '+00:00'))
# Check if in the future
if dt < datetime.now():
raise ValueError('Appointment time must be in the future')
return v
except ValueError as e:
raise ValueError(f'Invalid datetime format: {e}')
class AppointmentPurpose(BaseModel):
"""Validate appointment purpose"""
purpose: str = Field(..., min_length=3, max_length=200)
@validator('purpose')
def validate_purpose(cls, v):
# Remove potentially dangerous characters
cleaned = re.sub(r'[<>{}]', '', v)
if len(cleaned.strip()) < 3:
raise ValueError('Purpose must be at least 3 characters')
return cleaned.strip()
class AppointmentId(BaseModel):
"""Validate appointment ID"""
id: str = Field(..., description="Appointment ID")
@validator('id')
def validate_id(cls, v):
# Allow alphanumeric, hyphens, and underscores only
if not re.match(r'^[a-zA-Z0-9_-]+$', v):
raise ValueError('Invalid appointment ID format')
if len(v) > 100:
raise ValueError('Appointment ID too long')
return v
# Helper functions for easy validation
def validate_phone_number(number: str) -> str:
"""Validate and return cleaned phone number"""
validated = PhoneNumber(number=number)
return validated.formatted
def validate_appointment_time(time: str) -> str:
"""Validate appointment time"""
validated = AppointmentTime(time=time)
return validated.time
def validate_purpose(purpose: str) -> str:
"""Validate appointment purpose"""
validated = AppointmentPurpose(purpose=purpose)
return validated.purpose
def validate_appointment_id(id: str) -> str:
"""Validate appointment ID"""
validated = AppointmentId(id=id)
return validated.id