Skip to content

Add pydantic request-schema validation and a consistent JSON response envelope #5

@devangb3

Description

@devangb3

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:

  1. Add pydantic>=2 to requirements.txt.
  2. Create backend/schemas.py with one BaseModel per route: LoginRequest, SignupRequest, CreateFolderRequest, ShareRequest, DeleteRequest, DownloadRequest, ChatRequest, DeleteUserRequest.
  3. 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.
  4. Standard response envelope: {ok: bool, data: {...} | null, error: {code: str, message: str} | null, request_id: str}.
  5. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions