Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 699
Inu Jung Avatar asked Dec 23 '22 15:12

Inu Jung


1 Answers

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

like image 66
Inu Jung Avatar answered Dec 31 '22 09:12

Inu Jung