{"record":{"id":"b0f36bd68a890b5b","repo":"linshenkx/prompt-optimizer","slug":"invalid-import-data-format","errorCode":null,"errorMessage":"Invalid import data format","messagePattern":"Invalid import data format","errorType":"validation","errorClass":"FavoriteValidationError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/favorite/manager.ts","lineNumber":1215,"sourceCode":"\n  async importFavorites(data: string, options?: {\n    mergeStrategy?: 'skip' | 'overwrite' | 'merge';\n    categoryMapping?: Record<string, string>;\n  }): Promise<{\n    imported: number;\n    skipped: number;\n    errors: string[];\n  }> {\n    const mergeStrategy = options?.mergeStrategy || 'skip';\n    const categoryMapping = options?.categoryMapping || {};\n    const result = { imported: 0, skipped: 0, errors: [] as string[] };\n\n    try {\n      await this.ensureInitialized();\n      const importData = JSON.parse(data);\n\n      if (!importData.favorites || !Array.isArray(importData.favorites)) {\n        throw new FavoriteValidationError('Invalid import data format');\n      }\n      // 预处理分类：避免重复获取\n      if (importData.categories && Array.isArray(importData.categories)) {\n        const existingCategories = await this.getCategories();\n        const existingCategoryIds = new Set(existingCategories.map(c => c.id));\n        const existingCategoryNames = new Set(existingCategories.map(c => c.name));\n\n        for (const category of importData.categories) {\n          if (!category || typeof category.name !== 'string') continue;\n          try {\n            const exists =\n              (category.id && existingCategoryIds.has(category.id)) ||\n              existingCategoryNames.has(category.name);\n\n            if (!exists) {\n              await this.addCategory({\n                name: category.name,\n                description: category.description,","sourceCodeStart":1197,"sourceCodeEnd":1233,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/favorite/manager.ts#L1197-L1233","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Validate the payload before importing: JSON.parse(data) must yield an object with an Array favorites property","Re-export favorites with exportFavorites from the same library version and import that file","If converting foreign data, build { favorites: [...], categories: [...] } before calling importFavorites"],"exampleFix":"// before\nawait manager.importFavorites(rawJson);\n\n// after\nconst parsed = JSON.parse(rawJson);\nif (!parsed || !Array.isArray(parsed.favorites)) throw new TypeError('not a favorites export');\nawait manager.importFavorites(rawJson);","handlingStrategy":"validation","validationCode":"const parsed = JSON.parse(raw);\nif (parsed == null || typeof parsed !== 'object' || !Array.isArray((parsed as any).favorites)) {\n  throw new TypeError('Not a favorites export file');\n}","typeGuard":"const isImportPayload = (v: unknown): v is { favorites: unknown[] } =>\n  typeof v === 'object' && v !== null && Array.isArray((v as any).favorites);","tryCatchPattern":"try {\n  await manager.importFavorites(raw);\n} catch (e) {\n  if (e instanceof FavoriteValidationError && e.message.includes('Invalid import data format')) {\n    // ask the user for a valid export file produced by exportFavorites\n  }\n}","preventionTips":["Only import files produced by exportFavorites from a compatible version","Validate JSON.parse result has a favorites array before importing","Reject mismatched files in the upload/file-picker layer"],"tags":["import","favorites","json","validation","schema"],"backgroundTag":"payload-schema-validation-failed","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}