Creating a new user will ignore non-specified objects from create-user.dto.ts
However when I update the user it will add unwanted fields like this:
// update-user.dto.ts
import { IsEmail } from 'class-validator';
import { Address } from '../model/address';
export class UpdateUserDto {
firstName: string;
lastName: string;
@IsEmail(undefined, { message: 'Not a valid e-mail' })
email: string;
username: string;
password: string;
addresses: Address[];
}
This is the update action from the user service
// user.service.ts
async update(data: UpdateUserDto) {
try {
this.logger.log(data);
const id = '5c6dd9852d4f441638c2df86';
const user = await this.userRepository.update(id, data);
return { message: 'Updated your information' };
} catch (error) {
this.logger.log(error);
throw new HttpException('', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
And here is the user.controller.ts
@Patch()
@UsePipes(CustomValidationPipe)
async update(@Body() data: UpdateUserDto) {
return this.userService.update(data);
}
The client patch data:
// Unwanted junk from client
{
"email": "newemail@gmail.com",
"junk": "junk"
}
The email
will update properly but the row will have a new unwanted property junk
with the value junk