-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidators.py
More file actions
408 lines (313 loc) · 10.4 KB
/
validators.py
File metadata and controls
408 lines (313 loc) · 10.4 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""
Input validation functions for the Agentic Interview System.
This module provides reusable validators for form inputs, API payloads,
and configuration data. All validators return (is_valid, error_message) tuples.
"""
import re
import html
from typing import Tuple, List, Any, Optional
from constants import (
MAX_NAME_LENGTH,
MAX_EMAIL_LENGTH,
MAX_ROLE_LENGTH,
MAX_DEPARTMENT_LENGTH,
MAX_DESCRIPTION_LENGTH,
MAX_QUESTION_TEXT_LENGTH,
MAX_KEYPOINT_LENGTH,
MAX_TAG_LENGTH,
MAX_ANSWER_LENGTH,
MAX_CRITERION_NAME_LENGTH,
MAX_CRITERION_DEFINITION_LENGTH,
MAX_CRITERIA_COUNT,
)
# ==============================================================================
# Text Validation
# ==============================================================================
def validate_required(value: Any, field_name: str = "Field") -> Tuple[bool, str]:
"""
Validate that a value is not None or empty.
Args:
value: The value to check
field_name: Name of the field for error message
Returns:
Tuple of (is_valid, error_message)
"""
if value is None:
return False, f"{field_name} is required"
if isinstance(value, str) and not value.strip():
return False, f"{field_name} is required"
return True, ""
def validate_max_length(
value: str,
max_length: int,
field_name: str = "Field"
) -> Tuple[bool, str]:
"""
Validate that a string doesn't exceed maximum length.
Args:
value: The string to check
max_length: Maximum allowed length
field_name: Name of the field for error message
Returns:
Tuple of (is_valid, error_message)
"""
if value and len(value) > max_length:
return False, f"{field_name} must be {max_length} characters or less"
return True, ""
def validate_name(name: str, field_name: str = "Name") -> Tuple[bool, str]:
"""
Validate a name field (person name, template name, etc.).
Args:
name: The name to validate
field_name: Name of the field for error message
Returns:
Tuple of (is_valid, error_message)
"""
is_valid, error = validate_required(name, field_name)
if not is_valid:
return is_valid, error
is_valid, error = validate_max_length(name, MAX_NAME_LENGTH, field_name)
if not is_valid:
return is_valid, error
return True, ""
# ==============================================================================
# Email Validation
# ==============================================================================
# Basic email regex pattern (not exhaustive but catches most issues)
EMAIL_PATTERN = re.compile(
r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
)
def validate_email(email: str, required: bool = True) -> Tuple[bool, str]:
"""
Validate an email address.
Args:
email: The email to validate
required: Whether the email is required
Returns:
Tuple of (is_valid, error_message)
"""
if not email or not email.strip():
if required:
return False, "Email is required"
return True, "" # Empty is OK if not required
email = email.strip()
if len(email) > MAX_EMAIL_LENGTH:
return False, f"Email must be {MAX_EMAIL_LENGTH} characters or less"
if not EMAIL_PATTERN.match(email):
return False, "Please enter a valid email address"
return True, ""
# ==============================================================================
# Text Sanitization
# ==============================================================================
def sanitize_text(text: str) -> str:
"""
Sanitize text input to prevent XSS and clean up whitespace.
Args:
text: The text to sanitize
Returns:
Sanitized text string
"""
if not text:
return ""
# Strip leading/trailing whitespace
text = text.strip()
# HTML escape special characters to prevent XSS
text = html.escape(text)
# Normalize internal whitespace (collapse multiple spaces)
text = ' '.join(text.split())
return text
def sanitize_multiline_text(text: str) -> str:
"""
Sanitize multiline text (preserves newlines but escapes HTML).
Args:
text: The multiline text to sanitize
Returns:
Sanitized text string
"""
if not text:
return ""
# Strip leading/trailing whitespace
text = text.strip()
# HTML escape special characters
text = html.escape(text)
return text
# ==============================================================================
# List/Array Validation
# ==============================================================================
def validate_string_list(
items: List[str],
max_item_length: int,
max_items: int = 100,
field_name: str = "List"
) -> Tuple[bool, str]:
"""
Validate a list of strings.
Args:
items: List of strings to validate
max_item_length: Maximum length for each item
max_items: Maximum number of items allowed
field_name: Name of the field for error message
Returns:
Tuple of (is_valid, error_message)
"""
if not isinstance(items, list):
return False, f"{field_name} must be a list"
if len(items) > max_items:
return False, f"{field_name} cannot have more than {max_items} items"
for i, item in enumerate(items):
if not isinstance(item, str):
return False, f"{field_name} item {i+1} must be a string"
if len(item) > max_item_length:
return False, f"{field_name} item {i+1} exceeds maximum length of {max_item_length}"
return True, ""
def validate_tags(tags: List[str]) -> Tuple[bool, str]:
"""
Validate a list of tags.
Args:
tags: List of tag strings
Returns:
Tuple of (is_valid, error_message)
"""
return validate_string_list(tags, MAX_TAG_LENGTH, max_items=20, field_name="Tags")
def validate_keypoints(keypoints: List[str]) -> Tuple[bool, str]:
"""
Validate a list of keypoints for a question.
Args:
keypoints: List of keypoint strings
Returns:
Tuple of (is_valid, error_message)
"""
return validate_string_list(keypoints, MAX_KEYPOINT_LENGTH, max_items=20, field_name="Keypoints")
# ==============================================================================
# Form Field Validators (for Admin forms)
# ==============================================================================
def validate_person_form(
name: str,
email: str,
role: Optional[str] = None,
department: Optional[str] = None,
tags: Optional[List[str]] = None
) -> Tuple[bool, str]:
"""
Validate all fields for a Person form.
Args:
name: Person's name
email: Person's email
role: Optional role
department: Optional department
tags: Optional list of tags
Returns:
Tuple of (is_valid, error_message)
"""
# Validate required fields
is_valid, error = validate_name(name, "Name")
if not is_valid:
return is_valid, error
is_valid, error = validate_email(email)
if not is_valid:
return is_valid, error
# Validate optional fields
if role:
is_valid, error = validate_max_length(role, MAX_ROLE_LENGTH, "Role")
if not is_valid:
return is_valid, error
if department:
is_valid, error = validate_max_length(department, MAX_DEPARTMENT_LENGTH, "Department")
if not is_valid:
return is_valid, error
if tags:
is_valid, error = validate_tags(tags)
if not is_valid:
return is_valid, error
return True, ""
def validate_template_form(
name: str,
description: Optional[str] = None
) -> Tuple[bool, str]:
"""
Validate fields for an InterviewTemplate form.
Args:
name: Template name
description: Optional description
Returns:
Tuple of (is_valid, error_message)
"""
is_valid, error = validate_name(name, "Template name")
if not is_valid:
return is_valid, error
if description:
is_valid, error = validate_max_length(description, MAX_DESCRIPTION_LENGTH, "Description")
if not is_valid:
return is_valid, error
return True, ""
def validate_question_form(
question_text: str,
competency: str,
difficulty: str,
keypoints: List[str]
) -> Tuple[bool, str]:
"""
Validate fields for a TemplateQuestion form.
Args:
question_text: The question text
competency: Competency area
difficulty: Difficulty level
keypoints: List of keypoints
Returns:
Tuple of (is_valid, error_message)
"""
is_valid, error = validate_required(question_text, "Question text")
if not is_valid:
return is_valid, error
is_valid, error = validate_max_length(question_text, MAX_QUESTION_TEXT_LENGTH, "Question text")
if not is_valid:
return is_valid, error
is_valid, error = validate_required(competency, "Competency")
if not is_valid:
return is_valid, error
is_valid, error = validate_required(difficulty, "Difficulty")
if not is_valid:
return is_valid, error
# Keypoints can be empty but if provided must be valid
if keypoints:
is_valid, error = validate_keypoints(keypoints)
if not is_valid:
return is_valid, error
return True, ""
def validate_answer(answer: str) -> Tuple[bool, str]:
"""
Validate a candidate's answer.
Args:
answer: The answer text
Returns:
Tuple of (is_valid, error_message)
"""
is_valid, error = validate_required(answer, "Answer")
if not is_valid:
return is_valid, error
is_valid, error = validate_max_length(answer, MAX_ANSWER_LENGTH, "Answer")
if not is_valid:
return is_valid, error
return True, ""
# ==============================================================================
# Public API
# ==============================================================================
__all__ = [
# Basic validators
"validate_required",
"validate_max_length",
"validate_name",
"validate_email",
# Sanitization
"sanitize_text",
"sanitize_multiline_text",
# List validators
"validate_string_list",
"validate_tags",
"validate_keypoints",
# Form validators
"validate_person_form",
"validate_template_form",
"validate_question_form",
"validate_answer",
]