Product

With product it's the same process as category, we should copy and paste the CreateDTO and Service, it may be from the user again for example. However, we should make some changes:

  • In the CreateDTO:

    • description should receive @IsOptional()

    • price is a positive number with two decimal places, therefore we should use @IsNumber() with the option maxDecimalPlaces, and also @IsPositive()

    • A product is created with at least one category, so we must use @ArrayNotEmpty(), also categories should not repeat so we can use @ArrayUnique()

  • In the Service:

    • In findOne(), also fetch its categories

You can create a decorator for validating the price if you want, for example @IsCurrency().

However, you may have noticed that something was still not discussed. How do we validate the categories? How should they be shaped? In the next section, we'll learn two approaches for validating incoming data that represent entities.

Last updated