angular/components · error · Error

Formats array must not be empty.

Error message

Formats array must not be empty.

What it means

The Luxon adapter's parse method needs at least one format to try against the input string. It normalizes parseFormat to an array but then checks `!parseFormat.length` on the raw input, throwing when an empty array is supplied. Like the date-fns adapter, it fails fast rather than silently returning null.

Source

Thrown at src/material-luxon-adapter/adapter/luxon-date-adapter.ts:185

    const options = this._getOptions();

    return this._useUTC ? LuxonDateTime.utc(options) : LuxonDateTime.local(options);
  }

  parse(value: unknown, parseFormat: string | string[]): LuxonDateTime | null {
    const options: LuxonDateTimeOptions = this._getOptions();

    if (typeof value == 'string' && value.length > 0) {
      const iso8601Date = LuxonDateTime.fromISO(value, options);

      if (this.isValid(iso8601Date)) {
        return iso8601Date;
      }

      const formats = Array.isArray(parseFormat) ? parseFormat : [parseFormat];

      if (!parseFormat.length) {
        throw Error('Formats array must not be empty.');
      }

      for (const format of formats) {
        const fromFormat = LuxonDateTime.fromFormat(value, format, options);

        if (this.isValid(fromFormat)) {
          return fromFormat;
        }
      }

      return this.invalid();
    } else if (typeof value === 'number') {
      return LuxonDateTime.fromMillis(value, options);
    } else if (value instanceof Date) {
      return LuxonDateTime.fromJSDate(value, options);
    } else if (value instanceof LuxonDateTime) {
      return LuxonDateTime.fromMillis(value.toMillis(), options);
    }

View on GitHub (pinned to 0411926e7d)

Solutions

  1. Pass a non-empty format array or string: `adapter.parse(value, ['yyyy-MM-dd'])`.
  2. To rely on ISO-8601 fallback, pass undefined/null rather than an empty array.
  3. Audit MAT_DATE_FORMATS parse settings for empty arrays.
  4. Guard dynamic lists: `const fmts = formats.length ? formats : undefined;` before parsing.

Example fix

// before
adapter.parse('2024-01-15', []); // throws

// after
adapter.parse('2024-01-15', ['yyyy-MM-dd']);
// or use ISO fallback:
adapter.parse('2024-01-15', formats.length ? formats : undefined);
Defensive patterns

Strategy: validation

Validate before calling

const formats: string | string[] = getConfiguredFormats();
if (Array.isArray(formats) && formats.length === 0) {
  // fall back to ISO-8601 handling by passing undefined
  return adapter.parse(value, undefined);
}
return adapter.parse(value, formats);

Type guard

function isNonEmptyFormatList(f: string | string[]): boolean {
  return typeof f === 'string' ? f.length > 0 : f.length > 0;
}

Try / catch

try {
  return adapter.parse(value, formats);
} catch (e) {
  if (String(e.message).includes('Formats array must not be empty')) {
    return adapter.parse(value, undefined); // ISO fallback
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling `adapter.parse(value, [])` (empty formats array) or via parseTime/result helpers with an empty format list configured in MAT_DATE_FORMATS parse.dateInput.

Common situations: Empty parse formats from locale-driven config that failed to load; passing `[]` instead of omitting the argument (ISO-8601 fallback path only triggers for falsy/undefined parseFormat); dynamic format lists filtered to empty.

Related errors


AI-assisted analysis of angular/components@0411926e7d (2026-08-31). Data as JSON: /api/errors/6d55b2dd990e7094. Report an issue: GitHub.