DEV Community

Faisal Shaikh
Faisal Shaikh

Posted on

Class Validator Parent Dto To Child Dto Validation based on the conditions

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 .

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video πŸ“ΉοΈ

Top comments (1)

Collapse
 
faisy25 profile image
Faisal Shaikh β€’

@avantar Please check this

πŸ‘‹ Kindness is contagious

Please drop a ❀️ or a friendly comment on this post if it resonated with you!

Okay