Skip to content

Add Zod input validation to Product API POST endpoint#9

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/add-input-validation-product-api
Draft

Add Zod input validation to Product API POST endpoint#9
Copilot wants to merge 2 commits into
mainfrom
copilot/add-input-validation-product-api

Conversation

Copy link
Copy Markdown

Copilot AI commented May 27, 2026

POST /api/products accepted any payload without validation, allowing products to be created with missing or invalid required fields.

Changes

api/src/routes/product.ts

  • Added a productSchema using Zod that enforces:
    • name — non-empty string
    • price — positive number
    • supplierId — positive integer
  • POST / now returns 400 with structured error details on validation failure; 201 on success
  • Exported resetProducts() for test isolation (mirrors branch.ts pattern)

api/src/routes/product.test.ts (new)

  • Tests covering valid creation and each missing/invalid required field case

api/package.json

  • Added zod v4 dependency

Example

// Missing supplierId → 400
POST /api/products  { name: "Widget", price: 9.99 }
// → 400 { error: "Validation failed", details: [...] }

// All required fields present → 201
POST /api/products  { name: "Widget", price: 9.99, supplierId: 3, ... }
// → 201 { ...product }

Co-authored-by: thomasiverson <12767513+thomasiverson@users.noreply.github.com>
Copilot AI changed the title [WIP] Add input validation to Product API POST endpoint Add Zod input validation to Product API POST endpoint May 27, 2026
Copilot AI requested a review from thomasiverson May 27, 2026 17:58
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 the Product API POST endpoint

2 participants