E
E
Egor Babintsev2020-11-13 10:46:27
typescript
Egor Babintsev, 2020-11-13 10:46:27

How to validate class dto fields using decorators?

Started learning nest.js. I got to the validation of dto classes using the class-validator, connected the ValidationPipe in the controller, but when I try to use decorators from the class-validator, it gives an error, the value parameter is not passed the error text:
5fae37998c018569400358.png
Controller code:

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

 // тут простой get метод для вывода пользователей из сервиса

  @Post()
  @UsePipes(new ValidationPipe({ transform: true }))
  create(@Body() createUserDto: CreateUserDto) {
    this.appService.createUser(createUserDto);
  }
}

DTO class:
import { isNotEmpty } from 'class-validator';

export class CreateUserDto {
  @isNotEmpty()
  name: string;
}

@isNotEmpty is underlined in red and an error is thrown from the screen. In the console, in the running npm script, the same thing. I would be very grateful for the help, since I could not google this error. Nest specially set the read one using nest-cli, added only the dto file and the User interface

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Babintsev, 2020-11-13
@egor_babintsev

Problem solved. I mixed up the uppercase and lowercase letters and used a regular function instead of a decorator. You should have written @IsNotEmpty, not @isNotEmpty.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question