fix(backend): validate non-blank person name in CreatePerson and update_person_name ($25 bounty proposal)
Closed
AI
10h ago+112+112+012345678901234567890123456789-2-2-0123456789
Description
Validates and sanitizes person names in CreatePerson and update_person_name.
Currently:
CreatePersoninbackend/models/other.pydefinesname: 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 inget_person_by_name.update_person_nameinbackend/routers/users.pyreceives a rawvalue: 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 returnsFalse, causing the endpoint to mistakenly raise HTTP 404 ("Person not found") instead of HTTP 422 Unprocessable Entity.
This PR:
- Adds a
@field_validator('name')toCreatePersonto strip whitespace and enforce that stripped names are between 2 and 40 characters. - Validates that
valueinupdate_person_nameis non-blank with stripped length between 2 and 40 characters, raising HTTP 422 if invalid, and passes the clean name toupdate_person. - Adds hermetic unit tests in
backend/tests/unit/test_person_name_validation.pyand router tests inbackend/tests/routers/test_users.py.
Test Plan
- Run unit tests:
Result: 9/9 tests passed in 0.003s.PYTHONPATH=backend python3 backend/tests/unit/test_person_name_validation.py