linshenkx/prompt-optimizer · error · FavoriteValidationError

Invalid import data format

Error message

Invalid import data format

What it means

Thrown by FavoriteManager.importFavorites when the JSON payload parses successfully but does not contain a favorites array. The import format requires a top-level object with a favorites property that is a non-empty array (categories is optional).

Source

Thrown at packages/core/src/services/favorite/manager.ts:1215

  async importFavorites(data: string, options?: {
    mergeStrategy?: 'skip' | 'overwrite' | 'merge';
    categoryMapping?: Record<string, string>;
  }): Promise<{
    imported: number;
    skipped: number;
    errors: string[];
  }> {
    const mergeStrategy = options?.mergeStrategy || 'skip';
    const categoryMapping = options?.categoryMapping || {};
    const result = { imported: 0, skipped: 0, errors: [] as string[] };

    try {
      await this.ensureInitialized();
      const importData = JSON.parse(data);

      if (!importData.favorites || !Array.isArray(importData.favorites)) {
        throw new FavoriteValidationError('Invalid import data format');
      }
      // 预处理分类:避免重复获取
      if (importData.categories && Array.isArray(importData.categories)) {
        const existingCategories = await this.getCategories();
        const existingCategoryIds = new Set(existingCategories.map(c => c.id));
        const existingCategoryNames = new Set(existingCategories.map(c => c.name));

        for (const category of importData.categories) {
          if (!category || typeof category.name !== 'string') continue;
          try {
            const exists =
              (category.id && existingCategoryIds.has(category.id)) ||
              existingCategoryNames.has(category.name);

            if (!exists) {
              await this.addCategory({
                name: category.name,
                description: category.description,

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Validate the payload before importing: JSON.parse(data) must yield an object with an Array favorites property
  2. Re-export favorites with exportFavorites from the same library version and import that file
  3. If converting foreign data, build { favorites: [...], categories: [...] } before calling importFavorites

Example fix

// before
await manager.importFavorites(rawJson);

// after
const parsed = JSON.parse(rawJson);
if (!parsed || !Array.isArray(parsed.favorites)) throw new TypeError('not a favorites export');
await manager.importFavorites(rawJson);
Defensive patterns

Strategy: validation

Validate before calling

const parsed = JSON.parse(raw);
if (parsed == null || typeof parsed !== 'object' || !Array.isArray((parsed as any).favorites)) {
  throw new TypeError('Not a favorites export file');
}

Type guard

const isImportPayload = (v: unknown): v is { favorites: unknown[] } =>
  typeof v === 'object' && v !== null && Array.isArray((v as any).favorites);

Try / catch

try {
  await manager.importFavorites(raw);
} catch (e) {
  if (e instanceof FavoriteValidationError && e.message.includes('Invalid import data format')) {
    // ask the user for a valid export file produced by exportFavorites
  }
}

Prevention

When it happens

Trigger: Calling importFavorites(data) where JSON.parse succeeds but importData.favorites is missing, null, or not an array — e.g. importing a categories-only export, arbitrary JSON, or a file from an incompatible tool.

Common situations: Hand-edited export files that dropped the favorites field, schema drift between library versions, passing an export from a different product, or passing a JSONL/CSV string.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/b0f36bd68a890b5b. Report an issue: GitHub.