{"record":{"id":"b3c801313698d4b9","repo":"OtterMind/Chat2DB","slug":"data-validation-failed-error-message","errorCode":null,"errorMessage":"Data validation failed: ${error.message}","messagePattern":"Data validation failed: (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"chat2db-community-client/src/database/validation.ts","lineNumber":22,"sourceCode":"export const DataSourceTreeSchema = z.object({\n  datasourceId: z.number(),\n  hiddenTreeNodeIds: z.array(z.string()),\n});\n\n// Schema used to validate input parameters\nexport const FilterInputSchema = z.object({\n  datasourceId: z.number(),\n  hiddenTreeNodeIds: z.array(z.string()).optional(),\n});\n\nexport type DataSourceTreeSchemaInput = z.infer<typeof FilterInputSchema>;\n\nexport const validateData = <T>(data: T, schema: z.ZodType<T>): void => {\n  try {\n    schema.parse(data);\n  } catch (error) {\n    if (error instanceof z.ZodError) {\n      throw new Error(`Data validation failed: ${error.message}`);\n    }\n    throw error;\n  }\n};\n","sourceCodeStart":4,"sourceCodeEnd":27,"githubUrl":"https://github.com/OtterMind/Chat2DB/blob/5ee1e990e73fbcae1969dc554be254fedb3ab888/chat2db-community-client/src/database/validation.ts#L4-L27","documentation":"validateData throws when a Zod schema parse fails, wrapping the ZodError message into a generic Error. It is a thin wrapper around schema.parse that converts ZodError (which has its own structure with issues array) into a standard Error with a concatenated message string. The underlying schema (DataSourceTreeSchema / FilterInputSchema) expects datasourceId to be a number and hiddenTreeNodeIds to be an array of strings.","triggerScenarios":"Calling validateData(data, DataSourceTreeSchema) where data.datasourceId is missing, null, a string, or undefined. Or data.hiddenTreeNodeIds is not an array of strings (e.g., null, or array of numbers). Calling with FilterInputSchema and omitting datasourceId.","commonSituations":"Backend returns datasourceId as a string instead of number (JSON parsing). hiddenTreeNodeIds is null when the backend omits it for the non-optional schema. API contract change where a field type changed but the frontend schema was not updated.","solutions":["Inspect error.message — it contains the Zod issues with the path and expected type.","Coerce datasourceId to Number before validation if the backend sends a string.","Update the Zod schema to match the actual API contract (e.g., z.coerce.number(), .nullable(), .default([])).","Use safeParse instead if you want to handle failures without throwing."],"exampleFix":"// before\nexport const DataSourceTreeSchema = z.object({\n  datasourceId: z.number(),\n  hiddenTreeNodeIds: z.array(z.string()),\n});\n\n// after — coerce and default to handle backend variance\nexport const DataSourceTreeSchema = z.object({\n  datasourceId: z.coerce.number(),\n  hiddenTreeNodeIds: z.array(z.string()).default([]),\n});","handlingStrategy":"validation","validationCode":"function isDataSourceTreeInput(data: unknown): data is { datasourceId: number; hiddenTreeNodeIds?: string[] } {\n  return (\n    typeof data === 'object' && data !== null &&\n    typeof (data as any).datasourceId === 'number' &&\n    (Array.isArray((data as any).hiddenTreeNodeIds)\n      ? (data as any).hiddenTreeNodeIds.every((x: unknown) => typeof x === 'string')\n      : (data as any).hiddenTreeNodeIds === undefined)\n  );\n}\n\nif (!isDataSourceTreeInput(data)) {\n  throw new Error('Invalid datasource tree input');\n}","typeGuard":"function isDataSourceTreeInput(data: unknown): data is { datasourceId: number; hiddenTreeNodeIds?: string[] } {\n  return (\n    !!data && typeof data === 'object' &&\n    typeof (data as any).datasourceId === 'number'\n  );\n}","tryCatchPattern":"const result = FilterInputSchema.safeParse(data);\nif (!result.success) {\n  console.error('Validation issues:', result.error.issues);\n  // handle each issue path/message\n} else {\n  // use result.data\n}","preventionTips":["Use safeParse instead of parse when you want non-throwing validation.","Keep Zod schemas in sync with the backend API contract.","Use z.coerce for type-flexible parsing when the backend sends strings for numbers."],"tags":["zod","validation","data-validation","schema","frontend"],"backgroundTag":null,"analyzedSha":"5ee1e990e73fbcae1969dc554be254fedb3ab888","analyzedAt":"2026-08-14T07:05:03.077Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}