'class validator not working with class transformer's type function

class Product {
  @Min(0, { message: 'should be positive number' })
  @IsNotEmpty({ message: 'should not be empty' })
  @Type(() => Number)
  price: number;
}

*In this case, price come from input value, so it should be string at first.

I want to show error message that following cases by class-validator, but only @IsNotEmpty not working.

  1. If field is empty, show 'should not be empty'
  2. If field is not empty && field value is negative, show 'should be positive number'

why @IsNotEmpty not working with @Type type function?



Solution 1:[1]

I think you have mixed up class-validator and class-transformer's decorators.

My understanding is that @Type is solely used by class-transformer to aid in transforming the type of a nested object. That issue aside, I believe the order in which you declare your validators matters. This should work:

class Product {
  @IsNotEmpty({ message: 'should not be empty' })
  @Min(0, { message: 'should be positive number' })
  price: number;
}

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 Daedalus