I am using pipes for request validation. If the request fails, I want to redirect to a page but don't want to throw error. The question is, how can I access the response object in validation?
This is my validation pipe.
@Injectable()
export class ValidationPipe implements PipeTransform<any> {
async transform(value: any, { metatype }: ArgumentMetadata) {
if (!metatype || !this.toValidate(metatype)) {
return value;
}
const object = plainToClass(metatype, value);
const errors = await validate(object);
if (errors.length > 0) {
// in here i need to response with res.redirect('') function
throw new BadRequestException('Validation failed');
}
return value;
}
private toValidate(metatype: Function): boolean {
const types: Function[] = [String, Boolean, Number, Array, Object];
return !types.includes(metatype);
}
}
Instead of throwing exception i need to access res.redirect() function