Skip to content

Add PUT /users/:id endpoint with input hardening - #9

Open
pppmppp wants to merge 6 commits into
mate-academy:mainfrom
pppmppp:feat/put-update-users
Open

Add PUT /users/:id endpoint with input hardening#9
pppmppp wants to merge 6 commits into
mate-academy:mainfrom
pppmppp:feat/put-update-users

Conversation

@pppmppp

@pppmppp pppmppp commented Jun 23, 2026

Copy link
Copy Markdown

Summary

  • Adds PUT /users/:id to update an existing user's name and email (200 on success, 404 for unknown id, 400 for missing or blank fields)
  • Adds updateUser helper to db/store.js, returning a shallow copy so callers cannot mutate the stored record
  • Hardens body parsing in both PUT and POST: req.body || {} guard prevents a crash when Content-Type is absent; typeof check before .trim() prevents a TypeError crash from null or non-string fields
  • Validates that :id is a positive integer before hitting the store, so malformed ids return 400 instead of a misleading 404
  • Adds NOTES.md write-up (required by the grading suite)

Commit breakdown

  1. Add updateUser helper to the in-memory store — store layer only
  2. Add PUT /users/:id route to update an existing user — route wired up; all 9 tests green
  3. Add NOTES.md submission write-up — grading write-up
  4. Remove dead validation throw from updateUser — store guard was unreachable via the route and diverged semantically from the route's .trim() check
  5. Guard against null and non-string body fields in PUT and POST handlersnull fields and non-string values now return 400 instead of an unhandled 500

Test plan

  • npm test — all 9 tests pass
  • PUT /users/:id with valid body → 200 + updated user
  • PUT /users/:id with unknown id → 404
  • PUT /users/:id with missing field → 400
  • PUT /users/abc (non-numeric id) → 400
  • PUT /users/1 with { name: null } → 400 (not 500)
  • POST /users without Content-Type header → 400 (not 500)

🤖 Generated with Claude Code

Jeremy Skirrow and others added 6 commits June 23, 2026 13:06
Finds a user by id and mutates name/email in place, returning the
updated object — or undefined when the id does not exist. Exported
alongside the existing getAllUsers, getUserById, and createUser helpers.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Validates that both name and email are present (400 if either is
missing) before looking up the user, so a bad body is rejected even
when the id exists. Returns the updated user on success (200) or 404
when the id is not found.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Covers the implementation plan, model used (Claude Sonnet 4.6), commit
split, and the key review finding: input validation must run before the
store lookup so a missing field always returns 400 regardless of whether
the id exists.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The route layer trims and validates name/email before calling the store,
so the falsy guard in updateUser could never fire through the normal
request path. Worse, its falsy check diverged from the route's .trim()
semantics: a whitespace-only string would pass the store guard but be
rejected by the route. Removed the throw; validation belongs in the
route, not the store.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The previous code used destructuring defaults and .trim() directly, but
defaults only apply when a field is undefined — not null. Sending
{ name: null } or { name: 123 } bypassed the default and threw a
TypeError on .trim(), producing a 500 instead of a 400. Both handlers
now coerce through a typeof check (non-strings become "") and trim once
before validation, so any malformed field gets a clean 400. POST also
gains the req.body || {} guard that PUT already had, fixing the same
crash when Content-Type is absent.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replaces the stub write-up with full answers to the four submission
questions: what the approved plan contained and whether it was edited,
why Sonnet 4.6 was chosen, how the five commits were split and why,
and what two rounds of code review caught (and what was already correct).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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