I have a DTO class of a user in NestJS. I am using many validations using class-validator package in order to enforce my logic. If a field that doesn't exists on the DTO definition, I would like to ignore it and even throw an error. This is why I was trying to use the 'excludeExtraneousValues' flag. When I do use it, it ignores all the fields, even the ones that defined in the DTO.
import { ApiPropertyOptional } from '@nestjs/swagger';
import {
IsDefined,
IsEmail,
IsOptional,
IsPhoneNumber,
MaxLength,
ValidateIf,
} from 'class-validator';
export default class UserDTO {
@ApiPropertyOptional()
@MaxLength(254)
@IsEmail()
@IsDefined()
@ValidateIf((object) => object.email || !object.phone_number)
email?: string;
@ApiPropertyOptional()
@MaxLength(15)
@IsPhoneNumber()
@IsDefined()
@ValidateIf((object) => object.phone_number || !object.email)
phone_number?: string;
@ApiPropertyOptional()
@IsOptional()
@MaxLength(40)
name?: string;
}
As I mentioned, I am using NestJS. This is the ValidationPipe definition:
app.useGlobalPipes(
new ValidationPipe({
transform: true,
stopAtFirstError: true,
transformOptions: { excludeExtraneousValues: true },
}),
);
Following the addition of 'excludeExtraneousValues' flag, I cannot send any value, even the ones that is defined.
Is it a bug or am I missing something?