I just enabled the swagger CLI plugin in my project and its so good, now I'm wondering if we can't have the same thing for class-validator, to get all the types from typescript and use those to add validation rules instead of having to add a decorator for every property.
So, the following dto can just be written like a plain class
export class CreateUserDto {
@IsString()
email: string;
@IsString()
password: string;
@IsEnum(RoleEnum)
roles: RoleEnum[] = [];
@IsOptional()
@IsBoolean()
isEnabled?: boolean = true;
}
can be just this:
export class CreateUserDto {
email: string;
password: string;
roles: RoleEnum[] = [];
isEnabled?: boolean = true;
}
I imagine it should be possible since the swagger cli plugin does the same thing for swagger, and I imagine the code from the swagger plugin could be reused to build this ( I think... I haven't looked at the plugin's code ). But I just wanted to ask if something like this already exists? or is someone working on this? I would like to try and implement this if not.
similiar stackoverflow post: https://stackoverflow.com/questions/76944629/how-to-automatically-add-type-validation-decorators-to-nestjs-dto