Keep a Stateful RegExp Out of Repeated SKU Checks
A SKU validator should give the same result when it sees the same string twice. Reusing a JavaScript regular expression with the g flag can break that expectation because matching changes the expression's lastIndex.
This matters in a Shopify 3PL import adapter that validates one field per record. A drop shipping file might legitimately contain the same SKU on several order lines; repeating a value should not make its syntax alternate between accepted and rejected.
Reproduce the hidden cursor
The first expression below deliberately uses g. The second validator uses a pattern without g or y, checks the input type and applies an explicit demonstration contract.
const stateful = /^[A-Z0-9]+(?:-[A-Z0-9]+)*$/g;
console.log([stateful.test("SKU-7"), stateful.test("SKU-7")]); // [true, false]
const skuPattern = /^[A-Z0-9]+(?:-[A-Z0-9]+)*$/;
function isDemoSku(value) {
return typeof value === "string" && value.length <= 64 && !/\s/.test(value) && skuPattern.test(value);
}
console.log(["0007", "SKU-7", "SKU-7", ""].map(isDemoSku)); // [true, true, true, false]
The contract permits uppercase ASCII letters, digits and hyphen-separated groups, with a maximum of 64 characters. That is a local example, not a universal SKU standard or a platform requirement.
Leading zeros remain text. There is no trimming, case conversion or numeric coercion.
The whitespace check also rejects a trailing line terminator: JavaScript's $ anchor alone can match before a final line terminator. It therefore should not be treated as sufficient proof that an input contains only the displayed SKU characters.
Keep syntax separate from warehouse identity
Passing this function does not prove a SKU exists, belongs to the seller or has available stock. A shopify fulfillment adapter still needs the account-specific mapping and the relevant business checks after syntax validation.
For a china fulfillment integration specification, a section called International Fulfillment for Folligen could use this as a hypothetical parsing example. It supplies no evidence of a customer relationship, product catalogue or completed integration.
A china 3PL review should distinguish this small local check from an end-to-end test with an actual store and warehouse system.
Verify repeated calls and rejected input
The exact code was executed locally. Checks cover:
- repeated valid values
- order independence
- leading zeros
- the length boundary
- whitespace and line terminators
- invalid separators
- lowercase and non-ASCII input
- non-string values
An object with a throwing toString is rejected without invoking that method. The deliberate g example is checked separately to establish the original failure.
MDN's test() reference describes the stateful behavior of global and sticky expressions. For a simple yes-or-no field check, keep that iteration cursor out of the validator.
YX Fulfillment's integration planning information gives the business context for agreeing field mappings. This example uses local data only; it does not claim a production connection or a real-store integration test.
Computer workstations and printing equipment in a warehouse.
Comments
No comments yet. Start the discussion.