{"record":{"id":"431eef6875b06d82","repo":"koala73/worldmonitor","slug":"use-at-most-30-deliveries-per-list","errorCode":null,"errorMessage":"Use at most 30 deliveries per list.","messagePattern":"Use at most 30 deliveries per list\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/utils/operational-balance.ts","lineNumber":38,"sourceCode":"}\nfunction date(value: unknown): string {\n  if (typeof value !== 'string' || !/^\\d{4}-\\d{2}-\\d{2}$/.test(value) || !Number.isFinite(Date.parse(value)) || new Date(value).toISOString().slice(0, 10) !== value || value < '1900-01-01' || value > '9998-12-31') {\n    throw new Error('Use a valid date from 1900 through 9998.');\n  }\n  return value;\n}\n\nexport function parseOperationalInput(value: unknown): OperationalInput {\n  const input = record(value);\n  if (typeof input.operation !== 'string' || !input.operation.trim() || input.operation.length > 100) throw new Error('Enter an operation name of 1-100 characters.');\n  if (typeof input.unit !== 'string' || !/^[\\p{L}\\p{N} %./-]{1,24}$/u.test(input.unit) || !input.unit.trim()) throw new Error('Enter one quantity unit of 1-24 characters.');\n  const unit = input.unit.trim();\n  const startDate = date(input.startDate);\n  if (!Number.isInteger(input.horizonDays) || (input.horizonDays as number) < 1 || (input.horizonDays as number) > MAX_OPERATIONAL_DAYS) throw new Error('Horizon must be 1-90 whole days.');\n  const horizonDays = input.horizonDays as number;\n  if (input.basis !== 'example' && input.basis !== 'user') throw new Error('Input basis must be example or user.');\n  const deliveries = (value: unknown): OperationalDelivery[] => {\n    if (!Array.isArray(value) || value.length > MAX_OPERATIONAL_DELIVERIES) throw new Error('Use at most 30 deliveries per list.');\n    return value.map(value => {\n      const row = record(value);\n      const deliveryDate = date(row.date);\n      if (deliveryDate < startDate || deliveryDate > dateAt(startDate, horizonDays - 1)) throw new Error('Delivery dates must be inside the selected horizon.');\n      if (row.unit !== unit) throw new Error(`Every delivery must use ${unit}; mixed units are not converted.`);\n      return { date: deliveryDate, unit, quantity: quantity(row.quantity, 'Delivery quantity'), costUsd: row.costUsd === null || row.costUsd === undefined ? null : quantity(row.costUsd, 'Delivery cost') };\n    });\n  };\n  return { operation: input.operation.trim(), basis: input.basis, unit, startDate, horizonDays,\n    startingStock: quantity(input.startingStock, 'Starting stock'), dailyDemand: quantity(input.dailyDemand, 'Daily demand'),\n    deliveries: deliveries(input.deliveries), alternativeDeliveries: deliveries(input.alternativeDeliveries),\n    alternativeDailyDemand: input.alternativeDailyDemand === null ? null : quantity(input.alternativeDailyDemand, 'Alternative daily demand') };\n}\n\nexport function calculateOperationalBalance(value: unknown): OperationalSnapshot {\n  const input = parseOperationalInput(value);\n  const balance = (deliveries: OperationalDelivery[], demand: number): OperationalBalance => {\n    let stock = input.startingStock;","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/koala73/worldmonitor/blob/7d06c8633d256c18e38133030bc3613976a96ec9/src/utils/operational-balance.ts#L20-L56","documentation":"The inner deliveries() validator in parseOperationalInput caps each delivery list at MAX_OPERATIONAL_DELIVERIES (30) rows. A longer array is rejected rather than silently truncated, so users get an explicit message instead of a quietly incomplete plan.","triggerScenarios":"Calling parseOperationalInput/calculateOperationalBalance with input.deliveries or input.alternativeDeliveries as an array of 31+ delivery objects; bulk-importing a CSV/log of deliveries without chunking.","commonSituations":"Importing a season-long supply schedule; a scripted generator producing hundreds of rows; merging multiple worksheets' delivery arrays into one.","solutions":["Reduce the deliveries array to 30 or fewer entries before calling the parser.","Split long schedules across multiple worksheets or aggregate deliveries that share a date.","If the data legitimately needs more rows, request a MAX_OPERATIONAL_DELIVERIES bump upstream instead of bypassing validation.","Validate array length in the import UI and surface a user-facing warning before parse."],"exampleFix":"// before\ncalculateOperationalBalance({ ...input, deliveries: allDeliveries });\n// after\nconst deliveries = allDeliveries.slice(0, 30);\nif (allDeliveries.length > 30) console.warn('Truncated deliveries to the 30-row worksheet limit.');\ncalculateOperationalBalance({ ...input, deliveries });","handlingStrategy":"validation","validationCode":"for (const key of ['deliveries', 'alternativeDeliveries'] as const) {\n  if (Array.isArray(input[key]) && input[key].length > 30) {\n    throw new Error(`${key} has ${input[key].length} rows; the worksheet allows at most 30.`);\n  }\n}","typeGuard":"const withinDeliveryCap = (v: unknown): v is unknown[] =>\n  Array.isArray(v) && v.length <= 30;","tryCatchPattern":"try {\n  const snapshot = calculateOperationalBalance(input);\n} catch (err) {\n  if (err instanceof Error && err.message === 'Use at most 30 deliveries per list.') {\n    input.deliveries = input.deliveries.slice(0, 30);\n  } else throw err;\n}","preventionTips":["Enforce a hard maxLength of 30 rows in the delivery table UI.","Chunk or aggregate bulk imports before parsing.","Add a row counter next to the add-delivery button."],"tags":["validation","limit","payload-size"],"backgroundTag":"value-out-of-range","analyzedSha":"7d06c8633d256c18e38133030bc3613976a96ec9","analyzedAt":"2026-09-15T16:44:39.439Z","contentChangedAt":"2026-09-15T16:44:39.439Z","schemaVersion":2},"datasetVersion":"2026-09-15T18:17:12.389Z"}