Skip to content

Add input validation, error handling, and proper HTTP status codes#1

Open
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1776332077-add-input-validation
Open

Add input validation, error handling, and proper HTTP status codes#1
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1776332077-add-input-validation

Conversation

@devin-ai-integration
Copy link
Copy Markdown

Summary

Hardens the user CRUD routes with missing input validation, duplicate-entry handling, and correct HTTP semantics.

Schema validation (app/schema.py):

  • email fields now use Pydantic EmailStr (leverages email-validator already in requirements) instead of bare str
  • name fields are stripped of surrounding whitespace and rejected if empty
  • password on UserCreate requires a minimum length of 6 characters

Route-level error handling (main.py):

  • POST /users/ returns 201 Created instead of 200
  • Both POST /users/ and PUT /users/{user_id} check for duplicate emails before committing, returning 409 Conflict if the email is already taken
  • IntegrityError is caught with a db.rollback() as a safety net against race conditions on the unique email constraint
  • Renamed misleading handler functions (get_user_by_emailget_user, etc.) since they operate on user_id, not email

Review & Testing Checklist for Human

  • Duplicate email on create/update: Manually POST a user, then POST again with the same email — confirm you get 409. Do the same with PUT targeting a different user's ID.
  • Password minimum length: Confirm POST /users/ with a 5-char password returns a 422 validation error, and a 6-char password succeeds. Decide if 6 is the right threshold for your use case.
  • Whitespace-only name: POST or PUT with "name": " " — confirm it's rejected. Also confirm leading/trailing whitespace is stripped from valid names (e.g. " Alice ""Alice").
  • Email format validation: Confirm malformed emails like "not-an-email" return 422.
  • End-to-end smoke test: Run the app with uvicorn main:app --reload (using Python 3.10 — pydantic v1 is incompatible with 3.12) and exercise all five CRUD endpoints to confirm nothing regressed.

Notes

  • No automated test suite exists in this repo, so all verification is manual.
  • The pre-existing Python 3.12 + pydantic v1 incompatibility is unrelated to this PR (the project targets Python 3.10 per the Dockerfile).

Link to Devin session: https://app.devin.ai/sessions/384a73385f864e4396f60815e484395a
Requested by: @SoySoy4444

- Add email format validation using Pydantic EmailStr on create/update schemas
- Add name non-empty validation (strips whitespace) on create/update schemas
- Add minimum password length (6 chars) validation on create schema
- Add duplicate email check (409 Conflict) on POST /users/ and PUT /users/{user_id}
- Handle SQLAlchemy IntegrityError with rollback on create/update
- Return 201 Created on POST /users/ instead of 200
- Fix misleading route handler function names

Co-Authored-By: soya.shinkura22 <sshoinkyuraa.08.04@hotmail.co.jp>
@devin-ai-integration
Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant