linshenkx/prompt-optimizer · error · ImportExportError

VALIDATION_ERROR

VALIDATION_ERROR

Error message

Invalid data format: expected array of ImageModelConfig

What it means

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.

Source

Thrown at packages/core/src/services/image-model/manager.ts:327

  // === 导入导出 ===

  async exportData(): Promise<ImageModelConfig[]> {
    try {
      return await this.getAllConfigs()
    } catch (error) {
      throw new ImportExportError(
        'Failed to export image model configurations',
        await this.getDataType(),
        error as Error,
        IMPORT_EXPORT_ERROR_CODES.EXPORT_FAILED,
      )
    }
  }

  async importData(data: any): Promise<void> {
    if (!Array.isArray(data)) {
      throw new ImportExportError(
        'Invalid data format: expected array of ImageModelConfig',
        await this.getDataType(),
        undefined,
        IMPORT_EXPORT_ERROR_CODES.VALIDATION_ERROR,
      )
    }

    const configs = data as ImageModelConfigInput[]
    const failed: { config: ImageModelConfigInput, error: Error }[] = []

    for (const config of configs) {
      try {
        const completeConfig = this.ensureSelfContained(config)
        this.validateConfig(completeConfig)

        // 检查是否已存在
        const existing = await this.getConfig(completeConfig.id)
        if (existing) {

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Ensure the value passed to importData is an array: JSON.parse the file contents first and pass the resulting array directly.
  2. If your file stores { configs: [...] }, unwrap it: await manager.importData(parsed.configs).
  3. Verify the payload came from exportData() (which produces the array shape) and was not transformed in transit.

Example fix

// before
const raw = await fs.readFile(path, 'utf-8')
await manager.importData(raw) // string -> throws

// after
const parsed = JSON.parse(raw)
await manager.importData(Array.isArray(parsed) ? parsed : parsed.configs)
Defensive patterns

Strategy: validation

Validate before calling

if (!Array.isArray(data)) {
  const parsed = typeof data === 'string' ? JSON.parse(data) : data
  if (!Array.isArray(parsed)) throw new TypeError('expected array of ImageModelConfig')
}
await manager.importData(data)

Type guard

const isConfigArray = (v: unknown): v is ImageModelConfig[] =>
  Array.isArray(v) && v.every(i => typeof i === 'object' && i !== null && 'providerId' in i)

Try / catch

try { await manager.importData(data) } catch (e) { if (e instanceof ImportExportError && e.code === 'VALIDATION_ERROR') { /* re-shape payload */ } throw e }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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