Skip to content

NestJS #594

Answered by TatyOko28
houstonTaleubou asked this question in Q&A
Discussion options

You must be logged in to vote

A DTO (Data Transfer Object) is a TypeScript class used to validate and transform incoming data. NestJS uses class-validatorand class-transformerfor validation.

Example of a DTO to create a user :

import { IsString, IsEmail, Length } from 'class-validator';

export class CreateUserDto {
  @IsString()
  @Length(3, 20)
  username: string;

  @IsEmail()
  email: string;
}

It is used in the controller with @Body():

import { Controller, Post, Body } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';

@Controller('users')
export class UsersController {
  @Post()
  create(@Body() createUserDto: CreateUserDto) {
    return `User ${createUserDto.username} with email ${cr…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by houstonTaleubou
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants