Right now data['username'] raises KeyError on a bad payload; the client gets a 500. Different routes return {result}, {message}, or free-form bodies. One consistent contract plus one pydantic model per endpoint eliminates a class of bugs and is a trivially defensible API-design bullet.
Current state:
request.get_json()['field'] everywhere; jsonify({'result': ...}) on some routes, {'message': ...} on others.
Proposed implementation:
- Add
pydantic>=2 to requirements.txt.
- Create
backend/schemas.py with one BaseModel per route: LoginRequest, SignupRequest, CreateFolderRequest, ShareRequest, DeleteRequest, DownloadRequest, ChatRequest, DeleteUserRequest.
- Add a
@validate_body(Model) decorator that parses the JSON, returns 400 with a consistent error envelope on ValidationError, and passes the model to the handler.
- Standard response envelope:
{ok: bool, data: {...} | null, error: {code: str, message: str} | null, request_id: str}.
- Rewrite all route handlers to return
ok_response(data) / error_response(code, message, status).
Files likely affected:
app.py (all routes)
backend/schemas.py (new)
backend/responses.py (new)
- Tests:
tests/test_schema_validation.py
Acceptance criteria:
- Every 4xx response matches the new envelope.
- Missing/malformed fields on any route return 400 with a list of field errors, not 500.
- Frontend
api.js is updated to read data.data and data.error.message.
Right now
data['username']raisesKeyErroron a bad payload; the client gets a 500. Different routes return{result},{message}, or free-form bodies. One consistent contract plus one pydantic model per endpoint eliminates a class of bugs and is a trivially defensible API-design bullet.Current state:
request.get_json()['field']everywhere;jsonify({'result': ...})on some routes,{'message': ...}on others.Proposed implementation:
pydantic>=2torequirements.txt.backend/schemas.pywith oneBaseModelper route:LoginRequest,SignupRequest,CreateFolderRequest,ShareRequest,DeleteRequest,DownloadRequest,ChatRequest,DeleteUserRequest.@validate_body(Model)decorator that parses the JSON, returns 400 with a consistent error envelope onValidationError, and passes the model to the handler.{ok: bool, data: {...} | null, error: {code: str, message: str} | null, request_id: str}.ok_response(data)/error_response(code, message, status).Files likely affected:
app.py(all routes)backend/schemas.py(new)backend/responses.py(new)tests/test_schema_validation.pyAcceptance criteria:
api.jsis updated to readdata.dataanddata.error.message.