As I delved into my NestJS project, I found the class-validation aspect to be quite bothersome. It felt like I was constantly repeating the same classes with identical decorators.
For example:
export class DTO1 {
@IsDefined()
@IsString()
name: string;
@IsDefined()
@Type(() => Number)
@IsInt()
client: number;
@IsDefined()
@IsIn(['value1', 'value2'])
role: 'value1' | 'value2';
}
... (other DTO classes) ...
export class Base {
@IsDefined()
@IsString()
elemEveruoneHas: string;
}
export class DTO1 extends Base {
@IsDefined()
@IsNotEmpty()
@Type(() => Number)
@IsInt()
id: number;
}
This repetition made me realize that some elements were common across multiple DTOs but not consistently enough to create a base class and extend it.
... (base class setup) ...
I wanted to streamline this by creating generic classes for recurring elements and then generating them into specific DTOs.
The desired structure would be:
... (new DTO classes approach) ...
However, the current implementation, as shown below, is functional but lacks elegance:
... (improved but still unwieldy setup) ...
I am seeking advice on how to enhance this process so that I can avoid reconstructing the entire setup for each element class. Is there a way to incorporate a function that generates inherited classes with chaining inheritance to simplify something like DTO(Test1, Test2, Test3)?