How can a field in a child object be validated based on properties of the parent object's structure? For example, in the given structure, only the field first.name is not required when the fields id and dateOfBirth (dob) are defined.
@InputType()
export class GetEligibilityArgs {
@Field((type) => NameInput)
@ValidateNested()
@Type(() => NameInput)
name: NameInput;
@Field({ nullable: true })
@ValidateIf((o: GetEligibilityArgs) => {
return !(o.name?.first && o.name?.last && o.dateOfBirth);
})
@IsNotEmpty({ message: 'id is required' })
id?: string;
@Field({ nullable: true })
@ValidateIf((o: GetEligibilityArgs) => {
return !(o.id && o.name?.first && o.name?.last);
})
@IsNotEmpty()
dateOfBirth?: string;
}
Nested Object
@InputType()
export class NameInput {
@Field({ nullable: true })
@IsNotEmpty()
first?: string;
@Field({ nullable: true })
@IsNotEmpty()
last?: string;
}
Valid Inputs
- id, name.first, name.last
- id, name.first, dob
- id, name.last, dob
- name.first, name.last, dob
all other inputs are considered invalid