class-validator
class-validator copied to clipboard
question: "classTransformer.plainToClass is not a function" when using simple validator in class (nestjs ValidationPipe)
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)
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...
Same issue as reported here: https://github.com/nestjs/nest/issues/8637
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
same issue. had to downgrade to 0.4.0
same issue. had to downgrade to 0.4.0
npm i [email protected] works, class-transformer: ^0.4.0 this not working.
yarn add [email protected]
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.
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.