Spaces:
Runtime error
Runtime error
File size: 16,958 Bytes
17847d4 f1515e9 17847d4 f1515e9 17847d4 f1515e9 17847d4 f1515e9 17847d4 f1515e9 17847d4 f1515e9 17847d4 f1515e9 17847d4 | 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 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | """
Validation service for form submissions
"""
from sqlalchemy.orm import Session
from typing import Optional, List, Dict, Any
import re
import hmac
import hashlib
import secrets
from datetime import datetime
from urllib.parse import urlparse
from ..models import FormQuestion, Form, QuestionType
from ..schemas.validation import FieldValidationError, ValidationResponse
from ..schemas.form import AnswerValue
class ValidationService:
"""Service for validating form submissions"""
# Email regex pattern
EMAIL_PATTERN = re.compile(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')
# Phone pattern (E.164 format)
PHONE_PATTERN = re.compile(r'^\+?[1-9]\d{1,14}$')
# URL pattern
URL_PATTERN = re.compile(
r'^https?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' # domain
r'localhost|' # localhost
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # or IP
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE
)
def __init__(self):
# Use environment variable or generate a secret for HMAC
self.secret_key = secrets.token_hex(32)
def validate_submission(
self,
db: Session,
form_id: int,
answers: List[Dict[str, Any]],
is_complete: bool = False,
form_version: Optional[int] = None
) -> ValidationResponse:
"""
Validate submission against form schema.
Args:
db: Database session
form_id: ID of the form
answers: List of answer dictionaries
is_complete: Whether to enforce required fields
form_version: Optional version to validate against (future use)
Returns:
ValidationResponse with errors and warnings
"""
errors = []
warnings = []
# Fetch form with questions
form = db.query(Form).filter(Form.id == form_id).first()
if not form:
return ValidationResponse(
is_valid=False,
errors=[FieldValidationError(
question_id=0,
field="form",
message="Form not found",
error_type="not_found"
)]
)
# Create answer map for quick lookup
answer_map = {ans.get('question_id'): ans.get('answer_value', {}) for ans in answers}
visible_question_ids = self._get_visible_question_ids(form, answer_map)
# Validate each question
for question in form.questions:
answer_value = answer_map.get(question.id)
# Check required fields (only if is_complete)
if (
is_complete
and question.required
and question.id in visible_question_ids
and not self._has_value(answer_value)
):
errors.append(FieldValidationError(
question_id=question.id,
field="value",
message=f"{question.question_text} is required",
error_type="required"
))
continue
# Skip validation if no answer provided (for partial submissions)
if not self._has_value(answer_value):
continue
# Validate based on question type
field_error = self.validate_field(question, answer_value)
if field_error:
errors.append(field_error)
return ValidationResponse(
is_valid=len(errors) == 0,
errors=errors,
warnings=warnings
)
def _get_visible_question_ids(
self,
form: Form,
answer_map: Dict[int, Dict[str, Any]]
) -> set[int]:
"""Determine which questions are visible based on conditional rules."""
visible = {q.id for q in form.questions}
if not getattr(form, "conditional_rules", None):
return visible
for rule in form.conditional_rules:
trigger_answer = answer_map.get(rule.trigger_question_id, {})
condition_met = self._is_condition_met(trigger_answer, rule.condition_type, rule.condition_value)
if condition_met:
if rule.action == "show":
visible.add(rule.target_question_id)
elif rule.action == "hide":
visible.discard(rule.target_question_id)
else:
if rule.action == "show":
visible.discard(rule.target_question_id)
return visible
def _is_condition_met(
self,
answer_value: Dict[str, Any],
condition_type: str,
condition_value: Optional[str]
) -> bool:
"""Evaluate conditional logic against an answer value."""
if not isinstance(answer_value, dict):
return False
text = answer_value.get("text")
number = answer_value.get("number")
choices = answer_value.get("choices") or []
if condition_type == "equals":
if isinstance(text, str) and text == condition_value:
return True
if number is not None and str(number) == condition_value:
return True
if choices and condition_value in choices:
return True
return False
if condition_type == "not_equals":
if isinstance(text, str):
return text != condition_value
if number is not None:
return str(number) != condition_value
if choices:
return condition_value not in choices
return True
if condition_type == "contains":
return isinstance(text, str) and condition_value is not None and condition_value in text
if condition_type == "is_not_empty":
return self._has_value(answer_value)
if condition_type == "is_empty":
return not self._has_value(answer_value)
return False
def validate_field(
self,
question: FormQuestion,
answer_value: Dict[str, Any]
) -> Optional[FieldValidationError]:
"""
Validate single field based on question type and settings.
Args:
question: FormQuestion object
answer_value: Answer value dictionary
Returns:
FieldValidationError if validation fails, None otherwise
"""
if not answer_value:
return None
question_type = question.question_type
settings = question.settings or {}
# EMAIL validation
if question_type == QuestionType.EMAIL:
email = answer_value.get('text', '')
if email and not self.EMAIL_PATTERN.match(email):
return FieldValidationError(
question_id=question.id,
field="text",
message="Invalid email format",
error_type="invalid_format"
)
# PHONE validation
elif question_type == QuestionType.PHONE:
phone = answer_value.get('text', '')
if phone:
# Remove spaces and dashes for validation
cleaned_phone = re.sub(r'[\s\-\(\)]', '', phone)
if not self.PHONE_PATTERN.match(cleaned_phone):
return FieldValidationError(
question_id=question.id,
field="text",
message="Invalid phone number format",
error_type="invalid_format"
)
# LINK validation
elif question_type == QuestionType.LINK:
url = answer_value.get('text', '')
if url and not self.URL_PATTERN.match(url):
return FieldValidationError(
question_id=question.id,
field="text",
message="Invalid URL format",
error_type="invalid_format"
)
# NUMBER validation
elif question_type == QuestionType.NUMBER:
number = answer_value.get('number')
if number is not None:
min_value = settings.get('min_value')
max_value = settings.get('max_value')
if min_value is not None and number < min_value:
return FieldValidationError(
question_id=question.id,
field="number",
message=f"Value must be at least {min_value}",
error_type="out_of_range"
)
if max_value is not None and number > max_value:
return FieldValidationError(
question_id=question.id,
field="number",
message=f"Value must be at most {max_value}",
error_type="out_of_range"
)
# SHORT_ANSWER / LONG_ANSWER length validation
elif question_type in [QuestionType.SHORT_ANSWER, QuestionType.LONG_ANSWER]:
text = answer_value.get('text', '')
if text:
min_length = settings.get('min_length')
max_length = settings.get('max_length')
if min_length and len(text) < min_length:
return FieldValidationError(
question_id=question.id,
field="text",
message=f"Must be at least {min_length} characters",
error_type="too_short"
)
if max_length and len(text) > max_length:
return FieldValidationError(
question_id=question.id,
field="text",
message=f"Must be at most {max_length} characters",
error_type="too_long"
)
# MULTIPLE_CHOICE / DROPDOWN validation
elif question_type in [QuestionType.MULTIPLE_CHOICE, QuestionType.DROPDOWN]:
choices = answer_value.get('choices', [])
valid_choices = settings.get('choices', [])
if choices and valid_choices:
for choice in choices:
if choice not in valid_choices:
return FieldValidationError(
question_id=question.id,
field="choices",
message=f"Invalid choice: {choice}",
error_type="invalid_choice"
)
# CHECKBOXES / MULTI_SELECT validation
elif question_type in [QuestionType.CHECKBOXES, QuestionType.MULTI_SELECT]:
choices = answer_value.get('choices', [])
valid_choices = settings.get('choices', [])
if choices and valid_choices:
for choice in choices:
if choice not in valid_choices:
return FieldValidationError(
question_id=question.id,
field="choices",
message=f"Invalid choice: {choice}",
error_type="invalid_choice"
)
# Check max selections if configured
max_selections = settings.get('max_selections')
if max_selections and len(choices) > max_selections:
return FieldValidationError(
question_id=question.id,
field="choices",
message=f"Maximum {max_selections} selections allowed",
error_type="too_many_selections"
)
# DATE validation (ISO format)
elif question_type == QuestionType.DATE:
date_str = answer_value.get('date', '')
if date_str:
try:
datetime.fromisoformat(date_str.replace('Z', '+00:00'))
except ValueError:
return FieldValidationError(
question_id=question.id,
field="date",
message="Invalid date format",
error_type="invalid_format"
)
# TIME validation
elif question_type == QuestionType.TIME:
time_str = answer_value.get('date', '') # Time is also stored in 'date' field
if time_str:
try:
# Validate HH:MM or HH:MM:SS format
datetime.strptime(time_str, '%H:%M:%S')
except ValueError:
try:
datetime.strptime(time_str, '%H:%M')
except ValueError:
return FieldValidationError(
question_id=question.id,
field="date",
message="Invalid time format",
error_type="invalid_format"
)
# LINEAR_SCALE validation
elif question_type == QuestionType.LINEAR_SCALE:
rating = answer_value.get('rating')
if rating is not None:
min_val = settings.get('min_value', 1)
max_val = settings.get('max_value', 5)
if rating < min_val or rating > max_val:
return FieldValidationError(
question_id=question.id,
field="rating",
message=f"Rating must be between {min_val} and {max_val}",
error_type="out_of_range"
)
# RATING validation
elif question_type == QuestionType.RATING:
rating = answer_value.get('rating')
if rating is not None:
max_rating = settings.get('max_value', 5)
if rating < 1 or rating > max_rating:
return FieldValidationError(
question_id=question.id,
field="rating",
message=f"Rating must be between 1 and {max_rating}",
error_type="out_of_range"
)
return None
def _has_value(self, answer_value: Optional[Dict[str, Any]]) -> bool:
"""Check if answer has a meaningful value"""
if not answer_value:
return False
# Check all possible value fields
raw_text = answer_value.get('text')
text = raw_text.strip() if isinstance(raw_text, str) else ""
number = answer_value.get('number')
date = answer_value.get('date')
choices = answer_value.get('choices') or []
rating = answer_value.get('rating')
files = answer_value.get('files') or []
file_url = answer_value.get('file_url')
return bool(text or number is not None or date or choices or rating is not None or files or file_url)
def generate_validation_token(
self,
form_id: int,
session_id: str
) -> str:
"""
Generate HMAC token to prevent form tampering.
Args:
form_id: ID of the form
session_id: Session ID
Returns:
HMAC token string
"""
message = f"{form_id}:{session_id}:{datetime.utcnow().isoformat()}"
return hmac.new(
self.secret_key.encode(),
message.encode(),
hashlib.sha256
).hexdigest()
def verify_validation_token(
self,
token: str,
form_id: int,
session_id: str,
max_age_seconds: int = 3600
) -> bool:
"""
Verify validation token.
Args:
token: Token to verify
form_id: Form ID
session_id: Session ID
max_age_seconds: Maximum age of token in seconds
Returns:
True if token is valid, False otherwise
"""
# For now, just check if token is not empty
# In production, implement proper HMAC verification with timestamp checking
return bool(token)
# Singleton instance
validation_service = ValidationService()
|