Parent Dto:
export class ContactDto {
contact_id : number,
contact_type_id : ENUM [I,C]
name : string,
}
export class ContactPersonDto {
name : true,
mobile_no : string,
email_id : string
}
export class ContactCompanyDto {
company_name : true,
brand : true,
mobile_no:true,
email_id : true,
}
Parent Request Dto
export class ContactRequestDto extends IntersectionType (ContactDto) {
@IsValidContactData({ message: 'Invalid contact request data' })
@IsArray()
@ArrayNotEmpty({ message: 'Person array should not be empty' })
@ValidateNested({ each: true })
@Type(() => ContactPersonDto) // Tells class-transformer how to transform
person: ContactPersonDto[];
@ValidateIf((obj) => obj.contact_type_id === 'C') // Only validate if contact_type_id is "C"
@IsArray()
@ArrayNotEmpty({ message: 'Company array should not be empty' })
@ValidateNested({ each: true })
@Type(() => ContactCompanytDto)
company: ContactCompanyDto[];
}
Now , The issues I am facing in ContactPersonDto
Optional Value: email_id But it is mandatory when the
contact_type_id = "I" but when contact_type_id = "C" is should take value null and keep it as optional.
How Can I achieve this ?
@validate((obj) => obj.contact_type_id === "I" )
email_id? : string
Here , I am receiving the value obj Null and cannot make this validation proper .
Top comments (1)
@avantar Please check this