'using validation pipe in nestjs gives me an classTransformer.plainToclass is not a function error

i am using nestjs/graphql, and i made a dto for a graphql mutation where i used class-validator options like @IsString() and @IsBoolean(). for this i installed class-validator and class-transformer. But when i do the mutation, it gives me an unheard error. i googled it, but nothing comes out. the error is like this:

[Nest] 5872  - 2021. 11. 21. 오후 7:56:09   ERROR [ExceptionsHandler] classTransformer.plainToClass is not a function
TypeError: classTransformer.plainToClass is not a function
    at ValidationPipe.transform (/home/inust33/ubereats-backend/node_modules/@nestjs/common/pipes/validation.pipe.js:51:39)
    at /home/inust33/ubereats-backend/node_modules/@nestjs/core/pipes/pipes-consumer.js:16:33
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

in playground, it shows me like this: graphql playground error

my dto looks like this:

@ArgsType()
export class createRestaurantDto {
  @Field((type) => String)
  @IsString()
  @Length(5, 10)
  name: string;

  @Field((type) => Boolean)
  @IsBoolean()
  isVegan: boolean;

  @Field((type) => String)
  @IsString()
  address: string;

  @Field((type) => String)
  @IsString()
  ownersName: string;

  @Field(() => String)
  @IsString()
  categoryName: string;
}

the mutation i used this dto is this:

 @Mutation(() => Boolean)
  async createRestaurant(
    @Args() createRestaurantDto: createRestaurantDto,
  ): Promise<boolean> {
    try {
      await this.restaurantService.createRestaurant(createRestaurantDto);
      return true;
    } catch (e) {
      console.log(e);
      return false;
    }
  }

i did the validation pipe setting in main.ts like this:

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

All I can get is without setting the useGlobalPipes option which is not what i want to do here, the mutation works out well. could you please help me with this?



Solution 1:[1]

problem solved. due to recent update, [email protected] makes an error when used in validationPipe of nestJS.

you should downgrade to [email protected]

https://github.com/nestjs/nest/issues/8637

Solution 2:[2]

Had the same error with [email protected]

Updated to [email protected] and it was solved.

I'm using Nest v8.0.0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Inu Jung
Solution 2 Marcus Vinicius N. Bondezan