{"record":{"id":"9b2a9b07b11f5b59","repo":"yikart/AiToEarn","slug":"zod-validation-error-issues-input","errorCode":null,"errorMessage":"Zod validation error (issues + input)","messagePattern":"Zod validation error \\(issues \\+ input\\)","errorType":"validation","errorClass":"ZodErrorWithInput","httpStatus":400,"severity":"error","filePath":"project/aitoearn-backend/libs/common/src/utils/zod-dto.util.ts","lineNumber":29,"sourceCode":"  create: (input: TInput) => TOutput\n}\n\nexport function createZodDto<\n  TOutput = unknown,\n  TInput = TOutput,\n>(schema: ZodType<TOutput, TInput>, id?: string) {\n  if (id)\n    z.globalRegistry.add(schema, { id })\n\n  class AugmentedZodDto {\n    public static isZodDto = true\n    public static schema = schema\n\n    public static create(input: TInput) {\n      const result = this.schema.safeParse(input)\n      if (result.success)\n        return result.data\n      throw new ZodErrorWithInput(result.error.issues, input)\n    }\n  }\n\n  return AugmentedZodDto as unknown as ZodDto<TOutput, TInput>\n}\n\nexport function isZodDto(metatype: unknown): metatype is ZodDto {\n  return typeof metatype === 'function'\n    && 'isZodDto' in metatype\n    && metatype.isZodDto === true\n}\n","sourceCodeStart":11,"sourceCodeEnd":41,"githubUrl":"https://github.com/yikart/AiToEarn/blob/d3aa8bea5b146a8675607cf0144d891aad3e9683/project/aitoearn-backend/libs/common/src/utils/zod-dto.util.ts#L11-L41","documentation":"createZodDto's AugmentedZodDto.create runs schema.safeParse(input) and, on failure, throws a ZodErrorWithInput carrying the Zod issues plus the offending input. This surfaces exactly which fields failed validation and what was received, typically when a DTO is instantiated manually or in tests/E2E helpers.","triggerScenarios":"Calling SomeZodDto.create(input) with a payload that fails the Zod schema: missing required fields, wrong types, failed refinements, unexpected enum values.","commonSituations":"Hand-constructing DTOs in unit tests with incomplete fixtures; API response shapes changed and no longer match the DTO schema; optional/nullable mismatches (null vs undefined); version drift between client payload and server schema.","solutions":["Inspect error.issues to see the exact failing paths and fix the input fields","Update the input to satisfy the schema (correct types, required keys present)","If the schema itself is outdated, update the Zod schema to the new contract","In tests, build fixtures from a schema-derived factory instead of hand-written objects"],"exampleFix":"// before\nCreateAssetDto.create({ name: 123 }) // name expects string\n// after\nCreateAssetDto.create({ name: 'my-asset' })\n// or catch and inspect:\ntry { CreateAssetDto.create(input) } catch (e) { console.error(e.issues) }","handlingStrategy":"try-catch","validationCode":"const parsed = CreateAssetDto.schema.safeParse(input)\nif (!parsed.success) throw new Error(parsed.error.issues.map(i => `${i.path.join('.')}: ${i.message}`).join('; '))","typeGuard":null,"tryCatchPattern":"try {\n  const dto = CreateAssetDto.create(input)\n} catch (e) {\n  if (e instanceof ZodErrorWithInput) {\n    console.error('DTO validation failed:', e.issues, 'input:', e.input)\n  } else throw e\n}","preventionTips":["Inspect e.issues paths to fix the exact offending fields","Keep client payloads and Zod schemas in sync (shared contracts)","Use schema-derived factories for test fixtures","Distinguish null vs undefined for optional fields"],"tags":["zod","validation","dto","typescript"],"backgroundTag":"schema-validation-failed","analyzedSha":"d3aa8bea5b146a8675607cf0144d891aad3e9683","analyzedAt":"2026-08-31T14:19:24.185Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}