Skip to content

question: Validating based upon root object's property #2481

@GHkrishna

Description

@GHkrishna

I have used a DTO that has a 'property' of type another DTO. In the DTO of the 'property', I want to perform some validations based upon a property in the root DTO. Below mentioned is an example:

I have two DTOs, AnimalDto and AnimalFoodDto:

export enum PetAnimalsType {
    Cat = 'cat',
    Dog = 'dog',
}

class AnimalDto {
@Apiproperty({enum: PetAnimalsType})
type: PetAnimalsType

@Apiproperty({type: AnimalFood})
food: AnimalFood
}

class AnimalFoodDto {
@ApiProperty()
@Type(({ object }) => {
// _In here how do I get the property 'type' of AnimalDto and use it here in AnimalFoodDto_
      const type = object.type;
      switch (type) {
        case PetAnimalsType.Dog:
          return DogStarters;
        case PetAnimalsType.Cat:
          return CatStarters;
        default:
          throw new BadRequestException('Invalid Type');
      }
 })
starters: CatStarters | DogStarters
}

The problem:

How do I access property of a base DTO and do some validations based on that in a DTO of another property of a base DTO

Reference:

The closest Issue that I found was:
#2409

But this is for a property in root. Mine is in a previous layer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questionQuestions about the usage of the library.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions