Add input validation to POST /tasks - #5
Closed
aj-enns wants to merge 1 commit into
Closed
Conversation
Reject missing, non-string, or empty/whitespace-only titles with a 400 and a clear JSON error before any task is created. Trim the title before storing on the happy path. Add tests covering the happy path, whitespace trimming, and the invalid cases (missing, empty/whitespace, and non-string types). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Owner
Author
|
Closed by reset-demo cleanup |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
POST /taskspassedreq.body.titlestraight into the store with no validation, so a request with a missing, empty, or non-string title still returned201and created a task with a bad (orundefined) title. This adds proper input validation so malformed requests are rejected before anything is stored.What changed
sample-app/src/tasks/routes.ts): thePOST /handler now rejects atitlethat is missing, not a string, or empty/whitespace-only. Invalid requests get a400with a clear JSON error ({ error: "title is required and must be a non-empty string" }) and return early, so no task is created. On the happy path the title is trimmed before being stored.sample-app/src/app.test.ts): added coverage for whitespace trimming and for the invalid cases. Invalid types are covered with a table-driven test (number,null,boolean,array,object), each asserting400plus an empty task list afterward. The missing-title test asserts the exact error body to lock in the contract.Notes
{ error: string }matches the existing404responses in the same router, so the response format stays consistent.All 16 tests pass and
npm run typecheckis clean.Fixes: #1