The “0% Discount” Bug: Why Truthy Checks Break PATCH Updates.
I recently fixed a production bug on an e-commerce platform where admins couldn’t remove a product discount. Setting the discount to 0% should have cleared it — but the update kept failing silently. V
The “0% Discount” Bug: Why Truthy Checks Break PATCH Updates.
I recently fixed a production bug on an e-commerce platform where admins couldn’t remove a product discount. Setting the discount to 0% should have cleared it — but the update kept failing silently. Validation passed, requests were correct, yet the value never changed.
The issue came from a very common JavaScript pattern:
// ❌ Buggy logic
if (dto.discount) {
product.discount = dto.discount;
}
Because 0 is falsy in JavaScript, the condition never ran. The system interpreted 0 as “no value provided” instead of “update discount to zero”. Ironically, validation already ensured only valid numbers were allowed, but the service logic blocked a legitimate update.
The fix was to check for presence, not truthiness:
// ✅ Correct PATCH handling
if (dto.discount != null) {
product.discount = dto.discount;
}
Now the API correctly accepts 0 while still ignoring null or undefined.
Lesson learned: truthy checks answer “should this logic run?”, but PATCH operations need to answer “was a value supplied?”.