{"record":{"id":"b8b5578611b8f302","repo":"yikart/AiToEarn","slug":"error-b8b557","errorCode":null,"errorMessage":"参数验证失败： ","messagePattern":"参数验证失败： ","errorType":"validation","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"project/aitoearn-electron/server/src/validation.pipe.ts","lineNumber":40,"sourceCode":"    return !types.includes(metatype);\n  }\n\n  async transform(value: any, { metatype }: ArgumentMetadata) {\n    if (!metatype || !this.toValidate(metatype)) return value;\n\n    // 数据转换成类\n    const inData: any = plainToClass(metatype, value, {\n      excludeExtraneousValues: true,\n    });\n\n    const errors = validateSync(inData, {\n      whitelist: true,\n    });\n\n    if (errors.length <= 0) return inData;\n    console.log('------ 参数验证失败：', _.values(errors[0].constraints)[0]);\n\n    throw new BadRequestException(\n      '参数验证失败： ' + _.values(errors[0].constraints)[0],\n    );\n  }\n}\n","sourceCodeStart":22,"sourceCodeEnd":45,"githubUrl":"https://github.com/yikart/AiToEarn/blob/d3aa8bea5b146a8675607cf0144d891aad3e9683/project/aitoearn-electron/server/src/validation.pipe.ts#L22-L45","documentation":"NestJS ValidationPipe built on class-validator. transform() validates the incoming DTO; when class-validator reports constraint violations it logs the first error and throws a BadRequestException with the message '参数验证失败： <first constraint message>'. Thrown whenever request payload fails DTO validation.","triggerScenarios":"Any HTTP request whose body/query/params violate the route's DTO decorators (@IsString, @IsNotEmpty, @Min, etc.), e.g. missing required field, wrong type, or string exceeding @MaxLength.","commonSituations":"Frontend sending undefined/null fields not marked @IsOptional; sending numbers as strings; API contract drift after a DTO change; clients omitting fields after backend added new validation rules.","solutions":["Read the appended constraint message after '参数验证失败： ' to see the first failing rule and fix the client payload accordingly.","Ensure the frontend sends exactly the fields/types declared in the DTO; mark truly optional fields with @IsOptional.","Check for recently changed DTOs and update client code to match the new contract.","If nested objects are validated incorrectly, verify @ValidateNested and @Type() decorators are present."],"exampleFix":"// before\naxios.post('/api/x', { title: 123 })\n// after\naxios.post('/api/x', { title: 'valid string' }) // or add @IsOptional to the DTO field","handlingStrategy":"validation","validationCode":"// client-side mirror of the DTO before sending\nfunction validatePayload(p: Record<string, unknown>, required: string[]): string | null {\n  const missing = required.filter(k => p[k] === undefined || p[k] === null || p[k] === '')\n  return missing.length ? `缺少必填字段: ${missing.join(', ')}` : null\n}","typeGuard":"function isPlainObject(v: unknown): v is Record<string, unknown> {\n  return typeof v === 'object' && v !== null && !Array.isArray(v)\n}","tryCatchPattern":"try {\n  await axios.post(url, payload)\n}\ncatch (e) {\n  if (e?.response?.status === 400 && String(e.response.data?.message || '').startsWith('参数验证失败')) {\n    console.warn('字段校验失败:', e.response.data.message)\n  }\n  else throw e\n}","preventionTips":["Keep frontend types in sync with backend DTOs (share schemas or generate from OpenAPI).","Always send strings as strings and numbers as numbers per the DTO.","Mark truly optional fields with @IsOptional server-side.","Log the full message after '参数验证失败： ' — it names the first failing constraint."],"tags":["validation","nestjs","bad-request","dto"],"backgroundTag":"schema-validation-failed","analyzedSha":"d3aa8bea5b146a8675607cf0144d891aad3e9683","analyzedAt":"2026-08-31T14:19:24.185Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}