Streaming import: row validation
Project milestone
Define a deliberately small tab-separated inventory format: an uppercase SKU of 1–20 characters, a tab, and a nonnegative integer quantity from 0 to 999999. Reject malformed rows before accepting them.
Run the examples
In projects/stream-import/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {parseLine}=require('./import.cjs');console.log(JSON.stringify(parseLine('PEN-1\t12')));Expected console output:
{"sku":"PEN-1","quantity":12}A valid decimal field becomes a number.
In projects/stream-import/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {parseLine}=require('./import.cjs');try { parseLine('PEN\t12x'); } catch(e) { console.log(e.name); }Expected console output:
TypeErrorThe full-field regex rejects the trailing letter.
Implementation and contract
parseLine() splits on a tab and requires exactly two fields. Regular expressions constrain the SKU and decimal spelling; Number then converts only a validated quantity. The format has no quoting, headers, embedded tabs or blank lines. This is TSV for a narrow application, not a general CSV parser.
Common mistakes
parseInt alone can accept a valid prefix of an invalid field. Validate the whole field first.
Acceptance task
Add tests for zero quantity, leading zero, an extra tab and a lowercase SKU.
Project source, setup and tests: projects/stream-import/README.md. Read the implementation before modifying it; the tests check the supplied baseline, not every possible implementation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Streaming import: row validation — Knowledge Check
Assess implementation decisions and project behavior.
What format does this importer accept?
Why reject a trailing letter in a quantity?
Which quantity spelling is accepted?
In the Streaming import: row validation lab, what does this independent example log? const {parseLine}=require('./import.cjs'); console.log(JSON.stringify(parseLine('PEN-1\t12')));
In the Streaming import: row validation lab, what does this independent example log? const {parseLine}=require('./import.cjs'); try { parseLine('PEN\t12x'); } catch(e) { console.log(e.name); }