创建新用户将忽略未指定的对象create-user.dto.ts
但是,当我更新用户时,它会添加不需要的字段,如下所示:
// 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[];
}
这是来自用户服务的更新操作
// 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);
}
}
这是 user.controller.ts
@Patch()
@UsePipes(CustomValidationPipe)
async update(@Body() data: UpdateUserDto) {
return this.userService.update(data);
}
客户端补丁数据:
// Unwanted junk from client
{
"email": "newemail@gmail.com",
"junk": "junk"
}
将email
正确更新,但该行将有一个新的不需要的属性junk
,其值为junk