{"record":{"id":"9b5ee2da5d0b1494","repo":"yikart/AiToEarn","slug":"imagesize-must-be-one-of-1k-2k-or-4k","errorCode":null,"errorMessage":"imageSize must be one of 1K, 2K, or 4K","messagePattern":"imageSize must be one of 1K, 2K, or 4K","errorType":"validation","errorClass":"BadRequestException","httpStatus":400,"severity":"warning","filePath":"project/aitoearn-backend/apps/aitoearn-ai/src/core/draft-generation/draft-generation.service.ts","lineNumber":94,"sourceCode":"  None = 'none',\n  Reference = 'reference',\n  Edit = 'edit',\n  Ignored = 'ignored',\n}\n\nconst OPENAI_IMAGE_DEFAULT_ASPECT_RATIO = '2:3'\nconst OPENAI_IMAGE_DEFAULT_RESOLUTION = '1K'\n\nfunction getOpenAIImageSizeProfile(imageSize: string): { maxEdge: number, maxPixels: number, squareSize?: string } {\n  switch (imageSize) {\n    case '1K':\n      return { maxEdge: 1536, maxPixels: 1536 * 1024, squareSize: '1024x1024' }\n    case '2K':\n      return { maxEdge: 2560, maxPixels: 2560 * 1440 }\n    case '4K':\n      return { maxEdge: 3840, maxPixels: 3840 * 2160 }\n    default:\n      throw new BadRequestException('imageSize must be one of 1K, 2K, or 4K')\n  }\n}\nconst OPENAI_IMAGE_SIZE_MULTIPLE = 16\nconst OPENAI_IMAGE_MIN_ASPECT_RATIO = 1 / 3\nconst OPENAI_IMAGE_MAX_ASPECT_RATIO = 3\n\nfunction getGreatestCommonDivisor(left: number, right: number): number {\n  let a = Math.abs(left)\n  let b = Math.abs(right)\n\n  while (b !== 0) {\n    const next = a % b\n    a = b\n    b = next\n  }\n\n  return a\n}","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/yikart/AiToEarn/blob/d3aa8bea5b146a8675607cf0144d891aad3e9683/project/aitoearn-backend/apps/aitoearn-ai/src/core/draft-generation/draft-generation.service.ts#L76-L112","documentation":"getOpenAIImageSizeProfile maps an imageSize tier ('1K'|'2K'|'4K') to pixel budgets used to compute OpenAI image dimensions. Any other value hits the default branch and throws a Nest BadRequestException with message 'imageSize must be one of 1K, 2K, or 4K' — a 400-class client input error, not an AppException.","triggerScenarios":"A draft-generation image request supplies imageSize (or a derived size profile) with a value like '1024', '1k' (lowercase), 'HD', or undefined-coerced garbage instead of exactly '1K', '2K', or '4K'.","commonSituations":"Client sends lowercase '1k' or a numeric pixel size; an older API consumer uses the legacy size vocabulary ('auto', '1024x1024'); DTO lacks an enum validation so bad values reach the service.","solutions":["Validate imageSize in the DTO with a Zod enum ['1K','2K','4K'] so invalid values are rejected with a field-level 400 before the service","Normalize case (toUpperCase) at the boundary to tolerate '1k'","Update callers to send only the canonical tier strings","Map legacy size vocabulary to tiers in the controller/DTO transform"],"exampleFix":"// before\nimageSize: '1k'\n// after\nimageSize: z.enum(['1K', '2K', '4K']).default('2K') // DTO level\n// caller sends: imageSize: '1K'","handlingStrategy":"validation","validationCode":"const IMAGE_SIZES = ['1K', '2K', '4K'] as const\nconst normalized = typeof input.imageSize === 'string' ? input.imageSize.toUpperCase() : input.imageSize\nif (!IMAGE_SIZES.includes(normalized)) {\n  throw new Error(`imageSize must be one of 1K, 2K, or 4K, got: ${input.imageSize}`)\n}","typeGuard":"type ImageSize = '1K' | '2K' | '4K'\nfunction isImageSize(v: unknown): v is ImageSize {\n  return v === '1K' || v === '2K' || v === '4K'\n}","tryCatchPattern":"try {\n  await draftGenerationService.generateImageText({ ...dto, imageSize })\n} catch (e) {\n  if (e instanceof BadRequestException && e.message.includes('imageSize must be one of')) {\n    return badRequest('imageSize must be 1K, 2K, or 4K')\n  }\n  throw e\n}","preventionTips":["Declare imageSize with a Zod enum in the DTO so bad values fail with a field-level message","Normalize case and legacy size vocabulary at the API boundary","Never send raw pixel dimensions where a tier string is expected","Share the ImageSize type between client and server to catch drift at compile time"],"tags":["validation","bad-request","dto","image-generation"],"backgroundTag":"invalid-enum-value","analyzedSha":"d3aa8bea5b146a8675607cf0144d891aad3e9683","analyzedAt":"2026-08-31T14:19:24.185Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}