An inventory export screen sends skus: [] after the user clears a selection. An adapter interprets an empty list as “no filter” and requests all stock. The interface intended to select nothing; the downstream request selected everything.
Give those intentions separate representations. In this standalone example, { mode: 'all' } explicitly requests the whole agreed stock scope. { mode: 'selected', skus: [...] } requires a nonempty SKU selection. An empty selection is rejected before the transport test double receives a request.
Validate the mode and its required fields together
An array check establishes the container type. Array.isArray() distinguishes arrays from other values; it does not establish that an array contains any SKUs or that its elements are valid identifiers. Those are separate checks below.
'use strict';
const assert = require('node:assert/strict');
function stockSelection(input) {
if (input === null || typeof input !== 'object' ||
Array.isArray(input) || !Object.hasOwn(input, 'mode')) {
throw new Error('Explicit selection mode required');
}
if (input.mode === 'all') {
if (Object.hasOwn(input, 'skus')) {
throw new Error('All mode must not include a SKU selection');
}
return { scope: 'all' };
}
if (input.mode !== 'selected') {
throw new Error('Unknown selection mode');
}
if (!Object.hasOwn(input, 'skus') || !Array.isArray(input.skus) ||
input.skus.length === 0) {
throw new Error('Selected mode needs a nonempty SKU array');
}
const skus = [];
for (const sku of input.skus) {
if (typeof sku !== 'string' || sku.length === 0 ||
sku.trim() !== sku) {
throw new Error('Invalid SKU identifier');
}
skus.push(sku);
}
return { scope: 'selected', skus };
}
const requests = [];
function exportRequest(input) {
const selection = stockSelection(input);
requests.push(selection); // Local transport test double.
return selection;
}
assert.deepEqual(exportRequest({ mode: 'all' }), { scope: 'all' });
const original = { mode: 'selected', skus: ['0012', 'BLUE-M'] };
const selected = exportRequest(original);
assert.deepEqual(selected, {
scope: 'selected', skus: ['0012', 'BLUE-M']
});
assert.notStrictEqual(selected.skus, original.skus);
const invalid = [
{ mode: 'selected', skus: [] },
{ mode: 'selected' },
{ mode: 'selected', skus: 'BLUE-M' },
{ mode: 'selected', skus: [''] },
{ mode: 'selected', skus: [' BLUE-M'] },
{ mode: 'selected', skus: [12] },
{ mode: 'selected', skus: new Array(1) },
{ mode: 'all', skus: [] },
{ mode: 'unknown', skus: ['BLUE-M'] },
Object.create({ mode: 'all' }),
{}, null
];
for (const input of invalid) {
const before = requests.length;
assert.throws(() => exportRequest(input));
assert.equal(requests.length, before);
}
assert.equal(requests.length, 2);
assert.deepEqual(original.skus, ['0012', 'BLUE-M']);
console.log('Stock selection checks passed');
Save this as stock-selection.cjs and run node stock-selection.cjs. It was executed with Node.js 24 during preparation. The checks exercise explicit all-stock and selected-stock requests, preserve leading zeros, reject empty or malformed selections and confirm that rejection leaves the transport test double unchanged.
Preserve the distinction in the actual API adapter
The returned object is an internal contract, not an endpoint URL. Map each supported scope to the documented downstream API deliberately. Do not put a fallback such as “if the SKU list is empty, omit the filter” back into that mapping. If the remote API cannot represent the requested selection, stop the operation rather than broadening its scope.
This example checks request intent. It does not authorize access to every stock owner, apply account permissions, fetch all export pages or validate the returned inventory. A request for all stock must still be limited to the caller's permitted account scope.
I work with YX Fulfillment, whose warehouse work includes SKU and inventory management. This is an executable boundary example for an integration discussion, not a description of a deployed YX export connector. The operational benefit is a request whose scope remains the one the user actually chose.
Workstations at YX Fulfillment.

Top comments (0)