-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.py
More file actions
166 lines (134 loc) · 5.55 KB
/
Copy pathvalidators.py
File metadata and controls
166 lines (134 loc) · 5.55 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import re
from typing import Dict, Any, List
from datetime import datetime
def validate_patient_data(health_data: Dict[str, Any]) -> bool:
"""Validate patient health data"""
try:
# Check required fields
if not health_data.get('patient_id'):
return False
# Validate vital signs ranges
if health_data.get('heart_rate'):
hr = health_data['heart_rate']
if not isinstance(hr, int) or hr < 30 or hr > 200:
return False
if health_data.get('blood_pressure_systolic'):
systolic = health_data['blood_pressure_systolic']
if not isinstance(systolic, int) or systolic < 70 or systolic > 250:
return False
if health_data.get('blood_pressure_diastolic'):
diastolic = health_data['blood_pressure_diastolic']
if not isinstance(diastolic, int) or diastolic < 40 or diastolic > 150:
return False
if health_data.get('temperature'):
temp = float(health_data['temperature'])
if temp < 90.0 or temp > 110.0:
return False
if health_data.get('weight'):
weight = float(health_data['weight'])
if weight < 10.0 or weight > 500.0:
return False
if health_data.get('height'):
height = float(health_data['height'])
if height < 50.0 or height > 250.0:
return False
return True
except (ValueError, TypeError):
return False
def validate_medical_record(record_data: Dict[str, Any]) -> bool:
"""Validate medical record data"""
try:
# Check required fields
required_fields = ['patient_id', 'record_type', 'title']
if not all(field in record_data for field in required_fields):
return False
# Validate record type
valid_types = [
'lab_report', 'imaging', 'prescription', 'consultation_note',
'discharge_summary', 'vaccination_record', 'allergy_record'
]
if record_data['record_type'] not in valid_types:
return False
# Validate file size if present
if record_data.get('file_size'):
file_size = record_data['file_size']
max_size = 50 * 1024 * 1024 # 50MB
if file_size > max_size:
return False
return True
except (ValueError, TypeError):
return False
def validate_email(email: str) -> bool:
"""Validate email format"""
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return re.match(pattern, email) is not None
def validate_phone_number(phone: str) -> bool:
"""Validate phone number format"""
# Remove all non-digit characters
digits_only = re.sub(r'\D', '', phone)
# Check if it's between 10-15 digits
return 10 <= len(digits_only) <= 15
def validate_wallet_address(address: str) -> bool:
"""Validate crypto wallet address format"""
# Basic validation for Ethereum-style addresses
if address.startswith('0x') and len(address) == 42:
return re.match(r'^0x[a-fA-F0-9]{40}$', address) is not None
return False
def validate_consultation_data(consultation_data: Dict[str, Any]) -> bool:
"""Validate consultation data"""
try:
# Check required fields
required_fields = ['patient_id', 'doctor_id']
if not all(field in consultation_data for field in required_fields):
return False
# Validate consultation type
valid_types = ['regular', 'emergency', 'follow_up', 'telemedicine']
if consultation_data.get('consultation_type') not in valid_types:
return False
# Validate scheduled time if present
if consultation_data.get('scheduled_at'):
try:
scheduled_time = datetime.fromisoformat(consultation_data['scheduled_at'].replace('Z', '+00:00'))
# Check if scheduled time is in the future
if scheduled_time <= datetime.utcnow():
return False
except ValueError:
return False
# Validate duration
if consultation_data.get('duration'):
duration = consultation_data['duration']
if not isinstance(duration, int) or duration < 5 or duration > 180:
return False
return True
except (ValueError, TypeError):
return False
def sanitize_input(input_string: str) -> str:
"""Sanitize user input to prevent injection attacks"""
if not isinstance(input_string, str):
return ""
# Remove potentially dangerous characters
dangerous_chars = ['<', '>', '"', "'", '&', ';', '(', ')', '|', '`']
sanitized = input_string
for char in dangerous_chars:
sanitized = sanitized.replace(char, '')
# Limit length
return sanitized[:1000]
def validate_symptoms_input(symptoms: str) -> bool:
"""Validate symptoms input"""
if not isinstance(symptoms, str):
return False
# Check length
if len(symptoms.strip()) < 5 or len(symptoms) > 2000:
return False
# Check for potentially malicious content
malicious_patterns = [
r'<script.*?>.*?</script>',
r'javascript:',
r'on\w+\s*=',
r'<iframe.*?>',
r'<object.*?>'
]
for pattern in malicious_patterns:
if re.search(pattern, symptoms, re.IGNORECASE):
return False
return True