fix: deduplicate validation logic and guard JSON parse #27
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "5-fix-duplicated-validation-unguarded-json"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
validateSignRequestfromvalidation.tsrequest.json()in try/catch so malformed JSON returns 400 instead of 500Changes
src/routes/contract/[token]/sign/+server.ts: Removed 12 lines of inline validation (required-field check, type guard, PNG prefix check, size check) and replaced with singlevalidateSignRequest()import from$lib/validation. Wrappedrequest.json()in try/catch to return 400 on malformed JSON.tests/validation.test.ts: Added test forundefinedbody (malformed JSON parse result) and non-object primitives (string, number, boolean). Total tests: 14.Test Plan
npm test-- 14 passed)validateSignRequest(undefined)returns{ valid: false, error: 'Invalid request body' }Review Checklist
Related Notes
project-westside-basketball-- the project this work belongs toQA Review -- PR #27
Scope Check
All 5 acceptance criteria from issue #5 are addressed:
validateSignRequestfrom validation.tsrequest.json())Code Review
+server.ts changes
validateSignRequestimport added correctly from$lib/validationrequest.json()is correct -- SvelteKit'serror()returnsnever, so TypeScript knowsbodyis always assigned after the try/catch blockvalidation.error ?? 'Invalid request'fallback is defensive but fine --validateSignRequestalways setserrorwhenvalidis falseTest additions
undefinedbody test correctly validates the malformed JSON parse pathJSON.parsesucceeds but returns a non-objectPotential Concerns (non-blocking)
SignRequesttype import is still used for thelet body: SignRequestdeclaration, which is fine -- it provides type narrowing after validation, even thoughvalidateSignRequestacceptsunknown@types/pg/@types/nodemissing) are unrelated to this PRTest Results
VERDICT: APPROVE