I have a rest call, which might receive body of type classA or classB. I need to keep it as 2 different classes. Example -
// classes -
class ClassA {
@IsString()
@Length(1, 128)
public readonly name: string;
@IsString()
@Length(1, 128)
public readonly address: string;
}
class ClassB {
@IsString()
@Length(1, 10)
public readonly id: string;
}
// my request controller -
@Post('/somecall')
public async doSomething(
@Body(new ValidationPipe({transform: true})) bodyDto: (ClassA | ClassB) // < not validating any of them..
): Promise<any> {
// do something
}
The issue is, that when having more than one class, body is not validated.
How can I use 2 or more classes and validate them using class-validator? I don't want to use same class..
Thank you all :)