A warehouse import can receive a quantity as text. If its contract requires a non-negative whole number, parsing a numeric prefix is too permissive: parseInt("12pcs", 10) returns 12 rather than rejecting the field.
This example deliberately accepts only canonical ASCII decimal strings: "0", or a nonzero digit followed by digits. It rejects whitespace, leading zeros, signs, fractions and exponent notation. Those are application choices, not universal rules for every inventory system.
const assert = require('node:assert/strict');
function quantity(text) {
if (typeof text !== 'string' || !/^(0|[1-9][0-9]*)$/.test(text)) {
throw new TypeError('Expected a canonical quantity string');
}
const value = Number(text);
if (!Number.isSafeInteger(value)) throw new RangeError('Unsafe quantity');
return value;
}
assert.equal(parseInt('12pcs', 10), 12);
assert.equal(quantity('0'), 0);
assert.equal(quantity('12'), 12);
assert.equal(quantity('9007199254740991'), Number.MAX_SAFE_INTEGER);
for (const bad of ['', '12pcs', ' 12', '12 ', '01', '-1', '+1', '1.5', '1e2', 12, null]) {
assert.throws(() => quantity(bad), TypeError);
}
assert.throws(() => quantity('9007199254740992'), RangeError);
console.log('quantity boundaries passed');
The lexical check runs before conversion, so invalid suffixes cannot disappear. The safe-integer check then prevents accepting an integer that JavaScript Number cannot represent safely. If a business contract permits formatting such as leading zeros, change that contract explicitly and add corresponding tests rather than silently trimming every field.
The ECMAScript parseInt specification describes its prefix-oriented parsing. This local executable example demonstrates a stricter application boundary; it is not a live Shopify connector.
In a Shopify 3PL import, the accepted quantity still needs an agreed unit and SKU mapping. A drop shipping feed can be syntactically valid and refer to the wrong packing unit. For shopify fulfillment, keep validation errors attached to the source row instead of replacing them with zero.
I write warehouse data examples for fulfillmentyx. The same boundary matters in china fulfillment files sent to a china 3PL. Even a specification labeled “International Fulfillment for Folligen” would need its own quantity contract; that phrase is illustrative here, not a client claim. Conversion success proves neither stock availability nor permission to dispatch.
Photo: A warehouse work area with cartons, supplied by fulfillmentyx. The image does not depict execution of this code.

Top comments (0)