Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
GHkrishna opened this issue May 23, 2024 · 0 comments
Open

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

GHkrishna opened this issue May 23, 2024 · 0 comments
Labels
type: question Questions about the usage of the library.

Comments

@GHkrishna
Copy link

GHkrishna commented May 23, 2024

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.

@GHkrishna GHkrishna added the type: question Questions about the usage of the library. label May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Questions about the usage of the library.
Development

No branches or pull requests

1 participant