Source contract and expected records
This is an original, fully synthetic mapping example. Inputs are plain, acyclic JSON-shaped records with the fields shown; the canonical shape and rejection codes belong to this example. Adapt the contract before using a different source.
In this fictional stream, a header starts a new document and following line rows belong to it until another header appears. Header references must be nonempty and unique. Lines require a nonempty item and a nonnegative safe integer quantity. A line before any header is rejected; a header with no lines remains an empty document.
| Case | Input record (JSON) | Expected output (JSON) |
|---|---|---|
| Group under preceding header | {"records":[{"kind":"header","ref":"D1"},{"kind":"line","item":"A","units":2}]} | {"ok":true,"value":{"documents":[{"ref":"D1","lines":[{"item":"A","units":2}]}]}} |
| Reject orphan line | {"records":[{"kind":"line","item":"A","units":2}]} | {"ok":false,"error":"HEADER_REQUIRED"} |
| Preserve empty document | {"records":[{"kind":"header","ref":"D2"}]} | {"ok":true,"value":{"documents":[{"ref":"D2","lines":[]}]}} |
Run the local transformation
Run the complete example as a JavaScript module in Node.js. It contains the pure mapping function, all three inputs and literal expected outputs; assertions stop on a mismatch. A rejected case returns ok=false with an error code, rather than a partial canonical record.
import assert from 'node:assert/strict';
function headerDocuments(i){
const fail=error=>({ok:false,error});if(!i||!Array.isArray(i.records))return fail('DOCUMENT_ROWS');
const documents=[],refs=new Set();let current;
for(const r of i.records){
if(!r||typeof r.kind!=='string')return fail('DOCUMENT_KIND');
if(r.kind==='header'){
if(typeof r.ref!=='string'||!r.ref||refs.has(r.ref))return fail('DOCUMENT_HEADER');
refs.add(r.ref);current={ref:r.ref,lines:[]};documents.push(current);
}else if(r.kind==='line'){
if(!current)return fail('HEADER_REQUIRED');
if(typeof r.item!=='string'||!r.item||!Number.isSafeInteger(r.units)||r.units<0)return fail('DOCUMENT_LINE');
current.lines.push({item:r.item,units:r.units});
}else return fail('DOCUMENT_KIND');
}
return {ok:true,value:{documents}};
}
// Original synthetic cases; expected outputs are literal records.
const cases = [
{
"label": "Group under preceding header",
"input": {
"records": [
{
"kind": "header",
"ref": "D1"
},
{
"kind": "line",
"item": "A",
"units": 2
}
]
},
"expected": {
"ok": true,
"value": {
"documents": [
{
"ref": "D1",
"lines": [
{
"item": "A",
"units": 2
}
]
}
]
}
}
},
{
"label": "Reject orphan line",
"input": {
"records": [
{
"kind": "line",
"item": "A",
"units": 2
}
]
},
"expected": {
"ok": false,
"error": "HEADER_REQUIRED"
}
},
{
"label": "Preserve empty document",
"input": {
"records": [
{
"kind": "header",
"ref": "D2"
}
]
},
"expected": {
"ok": true,
"value": {
"documents": [
{
"ref": "D2",
"lines": []
}
]
}
}
}
];
for (const sample of cases) {
const actual = headerDocuments(sample.input);
assert.deepEqual(actual, sample.expected);
console.log(sample.label, JSON.stringify(actual));
}
Work through the boundary
Can the mapper attach an orphan line to a later header?
Show the worked answer
No. The declared ownership rule uses the preceding header, so that line returns HEADER_REQUIRED. D2 with no lines is instead valid and retains lines=[].