Using the dto class and @Body decorator does not work as expected.
See original GitHub issueThe code:
DTOs:
import {
ApiModelProperty,
} from '@nestjs/swagger';
import { UserDto } from './user.dto';
export class CreateUserDto extends UserDto {
@ApiModelProperty()
readonly password: string;
@ApiModelProperty()
readonly confirmPassword: string;
}
import {
ApiModelProperty,
ApiModelPropertyOptional,
} from '@nestjs/swagger';
export class UserDto {
@ApiModelPropertyOptional()
readonly firstName?: string;
@ApiModelPropertyOptional()
readonly lastName?: string;
@ApiModelProperty()
readonly email: string;
}
Controller:
@ApiUseTags('users')
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}
/**
* Creates a new User (register)
*
* @param createUserDto
* @return Promise<void>
*/
@Post()
@ApiOperation({ title: 'Create a new User' })
@ApiResponse({
status: HttpStatus.CREATED,
description: 'The record has been successfully created.',
})
@ApiResponse({
status: HttpStatus.BAD_REQUEST,
description: 'confirm password doesn\'t match the password, or the payload is invalid',
})
@ApiResponse({
status: HttpStatus.CONFLICT,
description: 'Email already in use',
})
@ApiResponse({ status: HttpStatus.FORBIDDEN, description: 'Forbidden.' })
@ApiResponse({ status: HttpStatus.UNAUTHORIZED, description: 'Unauthorized.' })
async createUser(@Body() createUserDto: CreateUserDto) {
await this.usersService
.create(createUserDto)
.catch(this._handleError);
}
/ * <...> */
}
The result:
However, using the following is fixing the Swagger UI:
@ApiImplicitBody({
name: 'role data',
type: CreateUserDto,
})
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:13 (3 by maintainers)
Top Results From Across the Web
node.js - DTO not working with custom decorator in NestJS
Show activity on this post. i'm trying to use DTO near a custom decorator inside a controller in NestJS to validate the body....
Read more >Validation | NestJS - A progressive Node.js framework
Now we can add a few validation rules in our CreateUserDto . We do this using decorators provided by the class-validator package, described...
Read more >How To Use DTO For Validation in NestJS (2022)
Today I'll talk about Data Transfer Objects (DTO) in NestJS and how to use them in order to validate your incoming requests. What...
Read more >Understanding controllers and routes in NestJS
A controller is a class defined with methods for handling one or more requests. The controller provides the handler function for a route....
Read more >NestJS — The Hero We Didn't Know We Needed - SingleStone
Class validator — A decorator validator that uses validator.js under the hood. Class Transformer — This allows you to transform a plain object ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The same is for the
@Query
decorator@alexandr2110pro track this issue here https://github.com/nestjs/swagger/issues/83
The rest should be fixed in
2.0.1
which I just published.