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.
Both grossG and tareG are nonnegative safe whole grams. Net mass is gross minus tare; reject tare greater than gross. Retain both source observations beside netG. Equal values legitimately produce zero net mass.
| Case | Input record (JSON) | Expected output (JSON) |
|---|---|---|
| Derive net and retain observations | {"grossG":10,"tareG":2} | {"ok":true,"value":{"grossG":10,"tareG":2,"netG":8}} |
| Reject negative net | {"grossG":2,"tareG":3} | {"ok":false,"error":"NEGATIVE_NET"} |
| Preserve zero net | {"grossG":5,"tareG":5} | {"ok":true,"value":{"grossG":5,"tareG":5,"netG":0}} |
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 netMass(i){
const fail=error=>({ok:false,error});if(!i||![i.grossG,i.tareG].every(n=>Number.isSafeInteger(n)&&n>=0))return fail('WEIGHING_VALUE');
if(i.tareG>i.grossG)return fail('NEGATIVE_NET');
return {ok:true,value:{grossG:i.grossG,tareG:i.tareG,netG:i.grossG-i.tareG}};
}
// Original synthetic cases; expected outputs are literal records.
const cases = [
{
"label": "Derive net and retain observations",
"input": {
"grossG": 10,
"tareG": 2
},
"expected": {
"ok": true,
"value": {
"grossG": 10,
"tareG": 2,
"netG": 8
}
}
},
{
"label": "Reject negative net",
"input": {
"grossG": 2,
"tareG": 3
},
"expected": {
"ok": false,
"error": "NEGATIVE_NET"
}
},
{
"label": "Preserve zero net",
"input": {
"grossG": 5,
"tareG": 5
},
"expected": {
"ok": true,
"value": {
"grossG": 5,
"tareG": 5,
"netG": 0
}
}
}
];
for (const sample of cases) {
const actual = netMass(sample.input);
assert.deepEqual(actual, sample.expected);
console.log(sample.label, JSON.stringify(actual));
}
Work through the boundary
What distinguishes gross 5/tare 5 from gross 2/tare 3?
Show the worked answer
5−5=0 is a valid retained measurement. 2−3=−1 violates the declared nonnegative net condition and returns NEGATIVE_NET; it is not converted into a zero measurement.