- Already had correct validation:
- Price validation:
isNaN(price) || price <= 0→ Error: "Price must be a positive number" - Quantity validation:
isNaN(quantity) || quantity < 1→ Error: "Quantity must be a positive integer"
- Price validation:
-
BEFORE:
- Price: Error message was "Price must be positive" (imprecise)
- Quantity allowed 0:
quantity < 0(incorrect, zero should be invalid)
-
AFTER:
- Price error message updated to: "Price must be a positive number" (consistent with POST)
- Quantity validation changed from
< 0to<= 0: "Quantity must be a positive integer"
-
Form Errors State
- Added
formErrorsstate to track validation errors per field
- Added
-
Client-Side Validation
- Validates price > 0 (positive number)
- Validates quantity > 0 (positive integer)
- Validates name is not empty
- Shows error messages below each field
- Highlights input with red border on error
-
Real-Time Error Clearing
- Errors clear when user modifies the field
-
Submit Button Behavior
- Button disabled if there are validation errors
- Prevents invalid submission to backend
-
Form Input Enhancements
- Price field:
type="number",step="0.01",min="0" - Quantity field:
type="number",step="1",min="0"
- Price field:
- ✅ Price = -5 → 400, "Price must be a positive number"
- ✅ Price = 0 → 400, "Price must be a positive number"
- ✅ Price = 0.01 → 200, Success
- ✅ Price = "abc" → 400, "Price must be a positive number"
- ✅ Quantity = 0 → 400, "Quantity must be a positive integer"
- ✅ Quantity = -10 → 400, "Quantity must be a positive integer"
- ✅ Quantity = 1 → 200, Success
- ✅ Quantity = "xyz" → 400, "Quantity must be a positive integer"
- ✅ Price = -5 → 400, "Price must be a positive number"
- ✅ Price = 0 → 400, "Price must be a positive number"
- ✅ Price = 0.01 → 200, Success
- ✅ Quantity = 0 → 400, "Quantity must be a positive integer" (was allowed before)
- ✅ Quantity = -10 → 400, "Quantity must be a positive integer"
- ✅ Quantity = 1 → 200, Success
- ✅ User enters price < 0 → Error shows: "Price must be a positive number"
- ✅ User enters price = 0 → Error shows: "Price must be a positive number"
- ✅ User enters price = 0.01 → No error, submit enabled
- ✅ User enters quantity = 0 → Error shows: "Quantity must be a positive integer"
- ✅ User enters quantity < 0 → Error shows: "Quantity must be a positive integer"
- ✅ User enters quantity = 1 → No error, submit enabled
- ✅ User clears price field → Error shows: "Price must be a positive number"
- ✅ User modifies field → Error clears in real-time
- ✅ Submit button disabled when errors exist
- ✅ Submit button enabled when all validations pass
✅ POST /api/products with price ≤ 0 returns 400
✅ POST /api/products with quantity ≤ 0 returns 400
✅ POST /api/products with non-numeric price returns 400
✅ Dashboard form shows validation errors before submitting
✅ Valid products are still created successfully
✅ Same validation applies to product edit endpoint (PATCH)
// POST and PATCH both now use:
if (isNaN(price) || price <= 0)
return err(res, 400, 'Price must be a positive number', 'validation_error');
if (isNaN(quantity) || quantity <= 0)
return err(res, 400, 'Quantity must be a positive integer', 'validation_error');const errors = {};
const price = parseFloat(form.price);
const quantity = parseInt(form.quantity, 10);
if (!form.price || isNaN(price) || price <= 0) {
errors.price = 'Price must be a positive number';
}
if (!form.quantity || isNaN(quantity) || quantity <= 0) {
errors.quantity = 'Quantity must be a positive integer';
}
if (Object.keys(errors).length > 0) {
setFormErrors(errors);
return; // Prevent submission
}backend/src/routes/products.js- Backend validation fixesfrontend/src/pages/Dashboard.jsx- Frontend validation implementation