Review generated payment normalizer

from JSON
Node 24 advanced 6 min 4 issues to find

Review this generated payment normalizer before it handles API input.

Accept a JSON object with a decimal-string id, a non-negative safe-integer amountCents, and an optional string note; return JSON containing only those three fields, preserve zero, reject malformed and invalid input distinctly, and never apply untrusted keys to an object prototype.

JavaScript
function normalizePayment(body) {
  try {
    const input = JSON.parse(body);
    const payment = Object.assign({}, input);

    payment.id = BigInt(Number(payment.id));
    payment.amountCents = payment.amountCents || 100;
    payment.note = payment.note || '';

    delete payment.password;
    return JSON.stringify(payment, null, 2);
  } catch {
    return JSON.stringify({});
  }
}

const body = JSON.stringify({
  id: '9007199254740993',
  amountCents: 0,
  note: '',
  token: 'internal-secret',
});
console.log(normalizePayment(body));

generated code is illustrative, not from any one model

Open in playground
Report an error