I need to check if some datas (which can be optional in db(typeorm entity) level) is not empty using class-validator dto for user activation after reading these data from user db .
Controller level validations for body, query and all using class-validator is working fine for me
DTO file
export class UserActiveDTO {
@IsNotEmpty()
@IsString()
readonly id: string;
@IsNotEmpty()
@IsString()
readonly name: string;
@IsNotEmpty()
@IsString()
readonly role: string;
//.. other data validations
}
Service File
// after reading user form findOne (typeorm)
let userCanBeActive: boolean = false;
try {
const canActiveUser: UserActiveDTO = user;
// tried to validate user using validate form class-validator
const vErrors = await validate(canActiveUser, {});
if (vErrors.length > 0) throw vErrors;
userCanBeActive = true;
} catch (error) {
userCanBeActive = false;
}
My above code always gives userCanBeActive true, even if the user is missing some of the property I mensioned in DTO file. What is best practice of validating these type of scenarios in nest js ?