koala73/worldmonitor · error

Enter one quantity unit of 1-24 characters.

Error message

Enter one quantity unit of 1-24 characters.

What it means

parseOperationalInput validates the unit field against the Unicode-aware regex /^[\p{L}\p{N} %./-]{1,24}$/u and requires it to be non-blank after trimming. It throws 'Enter one quantity unit of 1-24 characters.' when the unit is missing, not a string, contains characters outside letters/digits/space/%/.///- , exceeds 24 characters, or is only whitespace.

Solutions

  1. Use a short unit within letters, digits, space, %, ., /, or - (e.g. 'kg', 'l/day', 'rounds').
  2. Strip or replace disallowed characters and trim before submission.
  3. Enforce the same character class in the form input pattern: [\p{L}\p{N} %./-]{1,24}.

Example fix

// before
parseOperationalInput({ ..., unit: 'µg/m³' });
// after
parseOperationalInput({ ..., unit: 'kg' });
Defensive patterns

Strategy: validation

Validate before calling

const UNIT_RE = /^[\p{L}\p{N} %./-]{1,24}$/u;
const unit = (payload as any)?.unit;
if (typeof unit !== 'string' || !UNIT_RE.test(unit) || !unit.trim()) {
  throw new Error('Unit must be 1-24 chars: letters, digits, space, %, ., /, -');
}

Type guard

const hasValidUnit = (w: unknown): w is { unit: string } & Record<string, unknown> =>
  w !== null && typeof w === 'object' && typeof (w as any).unit === 'string' &&
  /^[\p{L}\p{N} %./-]{1,24}$/u.test((w as any).unit) && (w as any).unit.trim().length > 0;

Try / catch

try {
  const input = parseOperationalInput(raw);
} catch (e) {
  if (e instanceof Error && e.message.includes('quantity unit of 1-24')) {
    console.error('Unit missing, blank, has disallowed characters, or too long:', e.message);
  } else throw e;
}

Prevention

When it happens

Trigger: Passing units with symbols like 'kg/h²', 'µg', '°C', or quotes; empty or blank unit; a descriptive unit sentence longer than 24 characters; undefined unit from a mismatched field name.

Common situations: Users typing free-text units with special characters; localization introducing non-ASCII symbols outside the allowed Unicode letter/number classes; spreadsheet cells containing 'kilograms (metric)' as the unit.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of koala73/worldmonitor@7d06c8633d (2026-09-15). Data as JSON: /api/errors/c7bdcd54f5e0dffd. Report an issue: GitHub.

Appendix: source

Thrown at src/utils/operational-balance.ts:31

}
function quantity(value: unknown, label: string): number {
  if (value === '' || value === null || value === undefined) throw new Error(`${label} is required.`);
  if (typeof value !== 'number' || !Number.isFinite(value) || value < 0 || value > 1e6 || round(value) !== value) {
    throw new Error(`${label} must be between 0 and 1 million with at most 6 decimal places.`);
  }
  return value;
}
function date(value: unknown): string {
  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') {
    throw new Error('Use a valid date from 1900 through 9998.');
  }
  return value;
}

export function parseOperationalInput(value: unknown): OperationalInput {
  const input = record(value);
  if (typeof input.operation !== 'string' || !input.operation.trim() || input.operation.length > 100) throw new Error('Enter an operation name of 1-100 characters.');
  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.');
  const unit = input.unit.trim();
  const startDate = date(input.startDate);
  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.');
  const horizonDays = input.horizonDays as number;
  if (input.basis !== 'example' && input.basis !== 'user') throw new Error('Input basis must be example or user.');
  const deliveries = (value: unknown): OperationalDelivery[] => {
    if (!Array.isArray(value) || value.length > MAX_OPERATIONAL_DELIVERIES) throw new Error('Use at most 30 deliveries per list.');
    return value.map(value => {
      const row = record(value);
      const deliveryDate = date(row.date);
      if (deliveryDate < startDate || deliveryDate > dateAt(startDate, horizonDays - 1)) throw new Error('Delivery dates must be inside the selected horizon.');
      if (row.unit !== unit) throw new Error(`Every delivery must use ${unit}; mixed units are not converted.`);
      return { date: deliveryDate, unit, quantity: quantity(row.quantity, 'Delivery quantity'), costUsd: row.costUsd === null || row.costUsd === undefined ? null : quantity(row.costUsd, 'Delivery cost') };
    });
  };
  return { operation: input.operation.trim(), basis: input.basis, unit, startDate, horizonDays,
    startingStock: quantity(input.startingStock, 'Starting stock'), dailyDemand: quantity(input.dailyDemand, 'Daily demand'),
    deliveries: deliveries(input.deliveries), alternativeDeliveries: deliveries(input.alternativeDeliveries),

View on GitHub (pinned to 7d06c8633d)