Review a generated job normalizer

from Data types
Node 24 advanced 7 min 4 issues to find

Review this generated normalizer before it handles untrusted job records.

Accept an own-property record with a Boolean or exact "true"/"false" enabled field, a complete decimal retries value from 0 to 10, a string-array labels field, and an optional name where an empty string is valid.

JavaScript
function normalizeJob(input) {
  const result = {};
  for (const key in input) {
    result[key] = input[key];
  }

  const enabled = Boolean(input.enabled);
  const retries = parseInt(input.retries, 10);
  if (!Number.isFinite(retries) || retries > 10) {
    throw new TypeError("invalid retries");
  }

  const labels = typeof input.labels === "object"
    ? [...input.labels]
    : [];

  result.enabled = enabled;
  result.retries = retries;
  result.labels = labels;
  result.name = input.name || "worker";
  return result;
}

generated code is illustrative, not from any one model

Open in playground
Report an error