class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

question: "classTransformer.plainToClass is not a function" when using simple validator in class (nestjs ValidationPipe)

Open tiagosiebler opened this issue 2 years ago • 7 comments

I was trying to... Apply simple validations on a data transfer object (simple class) used in nestjs:

import { ExchangeName, TransactionSide } from "../exchange.model";
import { IsNotEmpty, IsNumber, IsString } from 'class-validator';

export class CreateTransactionDto {
  @IsNotEmpty()
  @IsString()
  coin: string;

  @IsNotEmpty()
  @IsNumber()
  price: number;

  @IsNotEmpty()
  @IsNumber()
  qty: number;

  @IsNotEmpty()
  @IsNumber()
  size: number;

  @IsNotEmpty()
  @IsString()
  side: TransactionSide;

  @IsNotEmpty()
  @IsNumber()
  time: number;//timestamp

  @IsNotEmpty()
  @IsString()
  exchange: ExchangeName;
}

Simple POST handler:

  @Post()
  async newTransaction(@Body() createTranactionDto: CreateTransactionDto) {
    return await this.transactionService.newTransaction(createTranactionDto);
  }

Invoked useGlobalPipes:

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());

  await app.listen(ServerPort);
}
bootstrap();

The problem: Any request triggers a class transformer error...

[Nest] 84636  - 11/20/2021, 9:18:43 PM   ERROR [ExceptionsHandler] classTransformer.plainToClass is not a function
TypeError: classTransformer.plainToClass is not a function
    at ValidationPipe.transform (/projectroot/node_modules/@nestjs/common/pipes/validation.pipe.js:51:39)
    at /projectroot/node_modules/@nestjs/core/pipes/pipes-consumer.js:16:33
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

tiagosiebler avatar Nov 20 '21 21:11 tiagosiebler

So it looks like it's the latest version of class-transformer:

    "class-transformer": "^0.5.0",

downgrade to 0.4.0 (npm i [email protected]) and the problem solves itself...

tiagosiebler avatar Nov 20 '21 21:11 tiagosiebler

Same issue as reported here: https://github.com/nestjs/nest/issues/8637

tiagosiebler avatar Nov 20 '21 21:11 tiagosiebler

I opened a discussion on this in the class-transformer github repo.

You can view it here: https://github.com/typestack/class-transformer/issues/995

c43721 avatar Nov 20 '21 22:11 c43721

same issue. had to downgrade to 0.4.0

dammy001 avatar Nov 21 '21 15:11 dammy001

same issue. had to downgrade to 0.4.0

dammy001 avatar Nov 21 '21 15:11 dammy001

npm i [email protected] works, class-transformer: ^0.4.0 this not working.

codeyourwayup avatar May 04 '22 03:05 codeyourwayup

yarn add [email protected]

codeyourwayup avatar May 04 '22 03:05 codeyourwayup

Closing this as solved.

If the issue still persists, you may open a new Q&A in the discussions tab and someone from the community may be able to help.

NoNameProvided avatar Nov 16 '22 03:11 NoNameProvided

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Dec 17 '22 00:12 github-actions[bot]