Skip to content

Add input validation to POST /tasks - #5

Closed
aj-enns wants to merge 1 commit into
mainfrom
aj-enns-add-input-validation-post-tasks
Closed

Add input validation to POST /tasks#5
aj-enns wants to merge 1 commit into
mainfrom
aj-enns-add-input-validation-post-tasks

Conversation

@aj-enns

@aj-enns aj-enns commented Jun 24, 2026

Copy link
Copy Markdown
Owner

Why

POST /tasks passed req.body.title straight into the store with no validation, so a request with a missing, empty, or non-string title still returned 201 and created a task with a bad (or undefined) title. This adds proper input validation so malformed requests are rejected before anything is stored.

What changed

  • Validation in the route handler (sample-app/src/tasks/routes.ts): the POST / handler now rejects a title that is missing, not a string, or empty/whitespace-only. Invalid requests get a 400 with 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.
  • Tests (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 asserting 400 plus an empty task list afterward. The missing-title test asserts the exact error body to lock in the contract.

Notes

  • The error shape { error: string } matches the existing 404 responses in the same router, so the response format stays consistent.
  • Returning the trimmed title (rather than the raw input) is intentional to avoid storing accidental leading/trailing whitespace.
  • Out of scope for this small demo API: max-length limits and zero-width/invisible-unicode blanks.

All 16 tests pass and npm run typecheck is clean.

Fixes: #1

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>
@aj-enns

aj-enns commented Jun 24, 2026

Copy link
Copy Markdown
Owner Author

Closed by reset-demo cleanup

@aj-enns aj-enns closed this Jun 24, 2026
@aj-enns
aj-enns deleted the aj-enns-add-input-validation-post-tasks branch June 24, 2026 04:43
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.

Add input validation to POST /tasks

1 participant