{
  "slug": "NestJS--Data-Validation-and-Transformation-ce99115c760d",
  "title": "NestJS: Data Validation and Transformation",
  "subtitle": "This report summarizes data validation and transformation techniques in NestJS, a framework for building Node.js server-side applications.",
  "excerpt": "This report summarizes data validation and transformation techniques in NestJS, a framework for building Node.js server-side applications.",
  "date": "2024-06-26",
  "tags": [
    "Node.js",
    "Machine Learning"
  ],
  "readingTime": "1 min",
  "url": "https://medium.com/@mobinshaterian/nestjs-data-validation-and-transformation-ce99115c760d",
  "hero": "https://cdn-images-1.medium.com/max/800/1*gwMTI2OlRutMoFGDYh7m4g.jpeg",
  "content": [
    {
      "type": "paragraph",
      "html": "This report summarizes data validation and transformation techniques in NestJS, a framework for building Node.js server-side applications."
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Importance of Data Validation"
    },
    {
      "type": "paragraph",
      "html": "Validating data ensures it adheres to predefined rules before reaching the controller layer. This prevents issues like:"
    },
    {
      "type": "paragraph",
      "html": "<strong>Incorrect or Malicious Data</strong>: Protects against attacks like SQL injection and unexpected data types.<br><strong>Errors and Exceptions</strong>: Prevents errors caused by invalid data formats."
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Benefits"
    },
    {
      "type": "paragraph",
      "html": "<strong>Improved Data Integrity</strong>: Guarantees data adheres to defined rules.<br><strong>Reduced Errors</strong>: Prevents issues caused by invalid data.<br><strong>Cleaner Code</strong>: Separates data validation logic from controllers."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*gwMTI2OlRutMoFGDYh7m4g.jpeg",
      "alt": "NestJS: Data Validation and Transformation",
      "caption": "",
      "width": 1024,
      "height": 1024
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Check validation of DTO and input data before controller."
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://github.com/typestack/class-validator\" target=\"_blank\" rel=\"noreferrer noopener\">https://github.com/typestack/class-validator</a>"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*ILH0LlIscaC9_SQ8VI3cMw.png",
      "alt": "NestJS: Data Validation and Transformation",
      "caption": "",
      "width": 1419,
      "height": 715
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Install packages"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "yarn add class-validator class-transformer"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Document validation"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Run in developer mode"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "yarn start:dev"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Add Pipe controller"
    },
    {
      "type": "paragraph",
      "html": "main.ts"
    },
    {
      "type": "code",
      "lang": "typescript",
      "code": "import {\n  NestFactory\n}\nfrom '@nestjs/core';\nimport {\n  AppModule\n}\nfrom './app.module';\nimport {\n  ValidationPipe\n}\nfrom '@nestjs/common';\nasync function bootstrap() {\n  const app = await NestFactory.create(AppModule);\n  app.useGlobalPipes(new ValidationPipe());\n  await app.listen(3000);\n}\nbootstrap();"
    },
    {
      "type": "paragraph",
      "html": "DTO folder"
    },
    {
      "type": "code",
      "lang": "typescript",
      "code": "import {\n  IsNotEmpty\n}\nfrom 'class-validator';\nexport class CreateTaskDto {\n  @IsNotEmpty() title: string;\n  @IsNotEmpty() description: string;\n}"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Check Id exists"
    },
    {
      "type": "paragraph",
      "html": "service"
    },
    {
      "type": "code",
      "lang": "typescript",
      "code": "getTaskById(id: string): Task {    const found = this.tasks.find((task) => task.id === id);    if\n(!found) {      throw new NotFoundException(`Task with ID \"${id}\" not found`);    }    return found;\n}"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Check ID exists when the element was deleted"
    },
    {
      "type": "code",
      "lang": "typescript",
      "code": "deleteTask(id: string): void {    const found = this.getTaskById(id);    this.tasks =\nthis.tasks.filter((task) => task.id !== found.id);  }"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Check ID exists when the element was updated"
    },
    {
      "type": "code",
      "lang": "typescript",
      "code": "updateTaskStatus(id: string, status: TaskStatus): Task {    const task = this.getTaskById(id);\ntask.status = status;    return task;  }"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Check Update Status"
    },
    {
      "type": "paragraph",
      "html": "add update-task-status.dto in dto folder"
    },
    {
      "type": "code",
      "lang": "typescript",
      "code": "import {\n  IsEnum\n}\nfrom 'class-validator';\nimport {\n  TaskStatus\n}\nfrom '../tasks.model';\nexport class UpdateTaskStatus {\n  @IsEnum(TaskStatus) status: TaskStatus;\n}"
    },
    {
      "type": "paragraph",
      "html": "controller"
    },
    {
      "type": "code",
      "lang": "typescript",
      "code": "@Patch('/:id/status') updateTaskStatus(@Param('id') id: string, @Body() updateTaskStatusDto:\nUpdateTaskStatusDto,): Task {\n  const {\n    status\n  }\n  = updateTaskStatusDto;\n  return this.tasksService.updateTaskStatus(id, status);\n}"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "validation for Search"
    },
    {
      "type": "paragraph",
      "html": "dto"
    },
    {
      "type": "code",
      "lang": "typescript",
      "code": "import {\n  IsEnum, IsOptional, IsString\n}\nfrom 'class-validator';\nimport {\n  TaskStatus\n}\nfrom '../tasks.model';\nexport class GetTasksFilterDto {\n  @IsOptional() @IsEnum(TaskStatus) status?: TaskStatus;\n  @IsOptional() @IsString() search?: string;\n}"
    }
  ]
}
