{"id":"0933f1f5a9cad040","repo":"nestjs/nest","slug":"validation-failed-0933f1","errorCode":null,"errorMessage":"Validation failed","messagePattern":"Validation failed","errorType":"validation","errorClass":"BadRequestException","httpStatus":400,"severity":"error","filePath":"sample/36-hmr-esm/src/common/pipes/parse-int.pipe.ts","lineNumber":13,"sourceCode":"import {\n  ArgumentMetadata,\n  BadRequestException,\n  Injectable,\n  PipeTransform,\n} from '@nestjs/common';\n\n@Injectable()\nexport class ParseIntPipe implements PipeTransform<string> {\n  async transform(value: string, metadata: ArgumentMetadata) {\n    const val = parseInt(value, 10);\n    if (isNaN(val)) {\n      throw new BadRequestException('Validation failed');\n    }\n    return val;\n  }\n}\n","sourceCodeStart":1,"sourceCodeEnd":18,"githubUrl":"https://github.com/nestjs/nest/blob/6ec0e2783d15290732447f304d8549b591b9749e/sample/36-hmr-esm/src/common/pipes/parse-int.pipe.ts#L1-L18","documentation":"A hand-rolled ParseIntPipe (sample/36-hmr-esm/src/common/pipes/parse-int.pipe.ts) that calls parseInt(value, 10) on an inbound route argument and throws BadRequestException('Validation failed') when the result is NaN. It is a minimal reimplementation of @nestjs/common's built-in ParseIntPipe, here to demonstrate the HMR/ESM sample's custom pipe plumbing. NaN happens when parseInt cannot find any leading digits (empty string, letters, symbols).","triggerScenarios":"A request whose piped parameter is not a valid integer: e.g., GET /items/abc where 'abc' flows through ParseIntPipe, or ?page= (empty). parseInt('abc',10) and parseInt('',10) both yield NaN → 400.","commonSituations":"Route registered without the pipe so a non-numeric segment reaches the handler; legacy/SEO URLs with slug-style ids hitting a numeric route; empty query params not stripped; client sending floating or padded strings the pipe rejects.","solutions":["Send a valid integer in the parameter (fix the client URL/query).","Prefer NestJS's built-in ParseIntPipe, which produces a clearer message, unless the sample specifically needs a custom one.","Tighten the check to reject non-integer strings like '12px' that parseInt silently accepts: compare String(val) !== value.","Return a more descriptive message instead of the bare 'Validation failed'."],"exampleFix":"// before\nconst val = parseInt(value, 10);\nif (isNaN(val)) {\n  throw new BadRequestException('Validation failed');\n}\n\n// after\nconst val = Number(value);\nif (!Number.isInteger(val)) {\n  throw new BadRequestException(`Validation failed: \"${value}\" is not an integer`);\n}","handlingStrategy":"validation","validationCode":"// Client-side: only emit numeric segments.\nfunction toIntParam(v: string | undefined): number {\n  if (v == null || !/^-?\\d+$/.test(v)) {\n    throw new Error(`\"${v}\" is not an integer`);\n  }\n  return Number(v);\n}","typeGuard":"function isIntegerString(v: unknown): v is string {\n  return typeof v === 'string' && /^-?\\d+$/.test(v);\n}","tryCatchPattern":"// In an exception filter or controller layer\ntry {\n  const id = parseIntPipe.transform(req.params.id, { type: 'param' });\n} catch (e) {\n  if (e instanceof BadRequestException) {\n    return reply.code(400).send({ message: 'id must be an integer' });\n  }\n  throw e;\n}","preventionTips":["Prefer NestJS's built-in ParseIntPipe for clearer messages unless you need a custom one.","Tighten the check: reject '12px' by comparing String(val) === value.","Document which route params are numeric so clients send the right shape.","Return the offending value in the error message for faster debugging."],"tags":["nestjs","validation","pipe","http-400","hmr"],"analyzedSha":"6ec0e2783d15290732447f304d8549b591b9749e","analyzedAt":"2026-08-03T17:42:23.673Z","schemaVersion":2}