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.
The source has three parallel arrays: metrics, values and units. They must have equal length; each label is nonempty and each value is a finite number. Pair entries at the same position into observation objects. Unit labels pass through as supplied; this mapping performs no unit conversion. Three empty arrays yield an empty observations list.
| Case | Input record (JSON) | Expected output (JSON) |
|---|---|---|
| Zip corresponding columns | {"metrics":["mass","length"],"values":[12,0],"units":["kg","mm"]} | {"ok":true,"value":{"observations":[{"metric":"mass","value":12,"unit":"kg"},{"metric":"length","value":0,"unit":"mm"}]}} |
| Reject unequal arrays | {"metrics":["mass"],"values":[12],"units":[]} | {"ok":false,"error":"COLUMN_LENGTH"} |
| Preserve no observations | {"metrics":[],"values":[],"units":[]} | {"ok":true,"value":{"observations":[]}} |
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 observationColumns(i){
const fail=error=>({ok:false,error});if(!i||![i.metrics,i.values,i.units].every(Array.isArray))return fail('OBSERVATION_COLUMNS');
if(i.metrics.length!==i.values.length||i.metrics.length!==i.units.length)return fail('COLUMN_LENGTH');
if(i.metrics.some(x=>typeof x!=='string'||!x)||i.units.some(x=>typeof x!=='string'||!x)||i.values.some(x=>typeof x!=='number'||!Number.isFinite(x)))return fail('OBSERVATION_VALUE');
return {ok:true,value:{observations:i.metrics.map((metric,n)=>({metric,value:i.values[n],unit:i.units[n]}))}};
}
// Original synthetic cases; expected outputs are literal records.
const cases = [
{
"label": "Zip corresponding columns",
"input": {
"metrics": [
"mass",
"length"
],
"values": [
12,
0
],
"units": [
"kg",
"mm"
]
},
"expected": {
"ok": true,
"value": {
"observations": [
{
"metric": "mass",
"value": 12,
"unit": "kg"
},
{
"metric": "length",
"value": 0,
"unit": "mm"
}
]
}
}
},
{
"label": "Reject unequal arrays",
"input": {
"metrics": [
"mass"
],
"values": [
12
],
"units": []
},
"expected": {
"ok": false,
"error": "COLUMN_LENGTH"
}
},
{
"label": "Preserve no observations",
"input": {
"metrics": [],
"values": [],
"units": []
},
"expected": {
"ok": true,
"value": {
"observations": []
}
}
}
];
for (const sample of cases) {
const actual = observationColumns(sample.input);
assert.deepEqual(actual, sample.expected);
console.log(sample.label, JSON.stringify(actual));
}
Work through the boundary
Why must a missing unit reject the pair rather than shift the next unit left?
Show the worked answer
The missing cell breaks positional evidence. COLUMN_LENGTH rejects the input before pairing; length=0 with unit mm is still a recorded zero observation in the complete case.