validation - ValidationPipe doesn't transform string to number - Stack Overflow

I use: @UsePipes(new ValidationPipe({ transform: true })) to transform query param criteria to specific

I use: @UsePipes(new ValidationPipe({ transform: true })) to transform query param criteria to specific types, but it doesn't work for numbers, can you explain me why?

Version of @bx/nestjs-commons - 9.4.0.

Not working version:

@Get()
@UsePipes(new ValidationPipe({ transform: true }))
@ApiPageableParams()
@ApiPagedResponse(DeliveryReadModel)
@ApiCriteriaParams(new DeliveryListCriteria().criteriaMapping)
async getDelivery(
  @AuthUser() userDetails: UserDetails,
  @Query() criteria: DeliveryListCriteria,
  @Query() pageable: Pageable
): Promise<PagedModel<DeliveryReadModel>> {
  return this.deliveryService.get(
    criteria,
    pageable
  );
}

export class DeliveryListCriteria {
  mode: number[];
  calculatedLength?: number;  
}

Working version:

@Get()
@UsePipes(
  new ValidationPipe({
    transform: true,
    transformOptions: {
      enableImplicitConversion: true,
    },
  })
)
@ApiPageableParams()
@ApiPagedResponse(DeliveryReadModel)
@ApiCriteriaParams(new DeliveryListCriteria().criteriaMapping)
async getDelivery(
  @AuthUser() userDetails: UserDetails,
  @Query() criteria: DeliveryListCriteria,
  @Query() pageable: Pageable
): Promise<PagedModel<DeliveryReadModel>> {
  return this.deliveryService.get(
    criteria,
    pageable
  );
}

export class DeliveryListCriteria {
  @ApiProperty({
    type: [Number],
  })
  @IsOptional()
  @Type(() => Number)
  mode: number[];
  @ApiProperty({
    format: 'float',
  })
  calculatedLength?: number;  
}

I use: @UsePipes(new ValidationPipe({ transform: true })) to transform query param criteria to specific types, but it doesn't work for numbers, can you explain me why?

Version of @bx/nestjs-commons - 9.4.0.

Not working version:

@Get()
@UsePipes(new ValidationPipe({ transform: true }))
@ApiPageableParams()
@ApiPagedResponse(DeliveryReadModel)
@ApiCriteriaParams(new DeliveryListCriteria().criteriaMapping)
async getDelivery(
  @AuthUser() userDetails: UserDetails,
  @Query() criteria: DeliveryListCriteria,
  @Query() pageable: Pageable
): Promise<PagedModel<DeliveryReadModel>> {
  return this.deliveryService.get(
    criteria,
    pageable
  );
}

export class DeliveryListCriteria {
  mode: number[];
  calculatedLength?: number;  
}

Working version:

@Get()
@UsePipes(
  new ValidationPipe({
    transform: true,
    transformOptions: {
      enableImplicitConversion: true,
    },
  })
)
@ApiPageableParams()
@ApiPagedResponse(DeliveryReadModel)
@ApiCriteriaParams(new DeliveryListCriteria().criteriaMapping)
async getDelivery(
  @AuthUser() userDetails: UserDetails,
  @Query() criteria: DeliveryListCriteria,
  @Query() pageable: Pageable
): Promise<PagedModel<DeliveryReadModel>> {
  return this.deliveryService.get(
    criteria,
    pageable
  );
}

export class DeliveryListCriteria {
  @ApiProperty({
    type: [Number],
  })
  @IsOptional()
  @Type(() => Number)
  mode: number[];
  @ApiProperty({
    format: 'float',
  })
  calculatedLength?: number;  
}
Share Improve this question asked Nov 19, 2024 at 8:22 Paweł GrabowskiPaweł Grabowski 1
Add a comment  | 

1 Answer 1

Reset to default 0

In the second version, the following changes ensure proper transformation:

transformOptions: { enableImplicitConversion: true }

This option allows ValidationPipe to implicitly convert types based on the destination type in the DTO or class. In this case, it converts strings to numbers or arrays of numbers without needing explicit @Type decorators. Documentation: ValidationPipe Options - transformOptions

Decorators like @Type(() => Number):

This explicitly tells class-transformer how to convert incoming query parameters to the intended type, making sure that arrays like ['1', '2'] is converted to [1, 2]. Documentation: @Type Decorator - class-transformer

But the first example by default the enableImplicitConversion is set to false and this prevent the convertion of the param criteria

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745576301a4633982.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信