fix(backend): validate non-blank person name in CreatePerson and update_person_name ($25 bounty proposal)

Closed
AI
10h ago
+112-2

Description

Validates and sanitizes person names in CreatePerson and update_person_name.
Currently:

  1. CreatePerson in backend/models/other.py defines name: str = Field(min_length=2, max_length=40) without stripping whitespace, permitting whitespace-only strings like " " to pass validation. It also creates entries with unstripped surrounding whitespace, preventing idempotent duplicate matching in get_person_by_name.
  2. update_person_name in backend/routers/users.py receives a raw value: str (with the intended validation previously left commented out: # = Field(min_length=2, max_length=40)). When given a blank or whitespace-only string, the underlying rename transaction returns False, causing the endpoint to mistakenly raise HTTP 404 ("Person not found") instead of HTTP 422 Unprocessable Entity.

This PR:

  • Adds a @field_validator('name') to CreatePerson to strip whitespace and enforce that stripped names are between 2 and 40 characters.
  • Validates that value in update_person_name is non-blank with stripped length between 2 and 40 characters, raising HTTP 422 if invalid, and passes the clean name to update_person.
  • Adds hermetic unit tests in backend/tests/unit/test_person_name_validation.py and router tests in backend/tests/routers/test_users.py.

Test Plan

  • Run unit tests:
    PYTHONPATH=backend python3 backend/tests/unit/test_person_name_validation.py
    
    Result: 9/9 tests passed in 0.003s.