A stock value of zero can be valid data. Using a logical OR to choose a default replaces every falsy value, so an explicit zero can silently become the configured fallback. Nullish coalescing applies a different rule: it selects the fallback for null or undefined. MDN explains this distinction.
const assert = require('node:assert/strict');
function quantityOrDefault(value, fallback) {
const result = value ?? fallback;
if (!Number.isSafeInteger(result) || result < 0) {
throw new TypeError('Expected a non-negative safe integer');
}
return result;
}
assert.equal(0 || 8, 8);
assert.equal(quantityOrDefault(0, 8), 0);
assert.equal(quantityOrDefault(undefined, 8), 8);
assert.equal(quantityOrDefault(null, 8), 8);
assert.equal(quantityOrDefault(3, 8), 3);
for (const bad of ['', false, '0', -1, NaN, Infinity, 1.5]) {
assert.throws(() => quantityOrDefault(bad, 8), TypeError);
}
assert.throws(() => quantityOrDefault(undefined, -1), TypeError);
console.log('Zero, missing values and invalid types checked');
Defaulting and validation are different decisions
This illustrative function treats null and undefined as missing, then validates the result. A string containing "0" remains a string and is rejected. If null means "unknown, do not allocate" in the upstream contract, reject it instead of defaulting it. The operator cannot decide the business meaning of missing data.
For a Shopify 3PL import, establish that contract before mapping a supplier quantity. The same distinction matters to drop shipping and Shopify fulfillment: zero available units must not become an arbitrary positive allocation merely because the field is falsy.
Test the boundary that caused the error
Run the example with Node.js. The assertions cover zero, positive values, absent values, strings, booleans, fractions and non-finite numbers. They test a local conversion contract, not a live store, database transaction or production integration.
This AI-assisted learning example was prepared for YX Fulfillment. In China fulfillment or a China 3PL configuration review, keep configuration defaults separate from observed stock. “International Fulfillment for Folligen” is a search phrase here, not an actual customer or an input record; the same validation rule should be specified against the real account's data contract.
Warehouse worktables and marked work areas.

Top comments (0)