{"record":{"id":"4a5501a0b3f56aed","repo":"linshenkx/prompt-optimizer","slug":"validation-error-4a5501","errorCode":"VALIDATION_ERROR","errorMessage":"Invalid data format: expected array of ImageModelConfig","messagePattern":"Invalid data format: expected array of ImageModelConfig","errorType":"validation","errorClass":"ImportExportError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/image-model/manager.ts","lineNumber":327,"sourceCode":"\n  // === 导入导出 ===\n\n  async exportData(): Promise<ImageModelConfig[]> {\n    try {\n      return await this.getAllConfigs()\n    } catch (error) {\n      throw new ImportExportError(\n        'Failed to export image model configurations',\n        await this.getDataType(),\n        error as Error,\n        IMPORT_EXPORT_ERROR_CODES.EXPORT_FAILED,\n      )\n    }\n  }\n\n  async importData(data: any): Promise<void> {\n    if (!Array.isArray(data)) {\n      throw new ImportExportError(\n        'Invalid data format: expected array of ImageModelConfig',\n        await this.getDataType(),\n        undefined,\n        IMPORT_EXPORT_ERROR_CODES.VALIDATION_ERROR,\n      )\n    }\n\n    const configs = data as ImageModelConfigInput[]\n    const failed: { config: ImageModelConfigInput, error: Error }[] = []\n\n    for (const config of configs) {\n      try {\n        const completeConfig = this.ensureSelfContained(config)\n        this.validateConfig(completeConfig)\n\n        // 检查是否已存在\n        const existing = await this.getConfig(completeConfig.id)\n        if (existing) {","sourceCodeStart":309,"sourceCodeEnd":345,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/image-model/manager.ts#L309-L345","documentation":"Thrown by ImageModelManager.importData when the imported payload is not a JSON array. The import/export facility serializes image model configs as an array of ImageModelConfig objects, and this validation is the first sanity check before any item is processed. Anything non-array (an object, a string, null) is rejected with VALIDATION_ERROR.","triggerScenarios":"Calling importData(data) where data is a single config object instead of an array, a JSON string that was never parsed (JSON.parse omitted), or a payload wrapped like { configs: [...] } instead of the bare array.","commonSituations":"Hand-editing an export file and accidentally wrapping it in an object; reading the file with fs.readFile and forgetting JSON.parse; passing exportData() output that was stringified twice.","solutions":["Ensure the value passed to importData is an array: JSON.parse the file contents first and pass the resulting array directly.","If your file stores { configs: [...] }, unwrap it: await manager.importData(parsed.configs).","Verify the payload came from exportData() (which produces the array shape) and was not transformed in transit."],"exampleFix":"// before\nconst raw = await fs.readFile(path, 'utf-8')\nawait manager.importData(raw) // string -> throws\n\n// after\nconst parsed = JSON.parse(raw)\nawait manager.importData(Array.isArray(parsed) ? parsed : parsed.configs)","handlingStrategy":"validation","validationCode":"if (!Array.isArray(data)) {\n  const parsed = typeof data === 'string' ? JSON.parse(data) : data\n  if (!Array.isArray(parsed)) throw new TypeError('expected array of ImageModelConfig')\n}\nawait manager.importData(data)","typeGuard":"const isConfigArray = (v: unknown): v is ImageModelConfig[] =>\n  Array.isArray(v) && v.every(i => typeof i === 'object' && i !== null && 'providerId' in i)","tryCatchPattern":"try { await manager.importData(data) } catch (e) { if (e instanceof ImportExportError && e.code === 'VALIDATION_ERROR') { /* re-shape payload */ } throw e }","preventionTips":["Always JSON.parse file contents before importData","Import only payloads produced by exportData()"],"tags":["validation","import-export","json"],"backgroundTag":"invalid-json-payload","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}