For polymorphic nested objects where the correct class depends on a discriminator field, @Type() accepts a discriminator option. class-transformer reads the discriminator property from the incoming JSON and instantiates the matching subtype class. class-validator then runs on that specific subtype's constraints.
The discriminator.property field must exist on the incoming JSON to select the correct subtype.
keepDiscriminatorProperty: true retains the type field in the instantiated object.
@ValidateNested() must be paired with the discriminator @Type() for nested validation to run.
Each subtype class (CatDto, DogDto) has its own class-validator decorators validated independently.
If the discriminator value does not match any subType, class-transformer returns a plain object.