node.js - 如果字段可以为空,则验证期间的装饰器仍然有效
问题描述
我使用类验证器。但是在阅读了这个包的文档之后graphql
,总的来说,我仍然没有找到如何解决这个问题
有这个突变:
@Mutation(() => UserEntity)
async updateUserById(
@Args('id') id: string,
@Args('inputs') inputs: UpdateUserDto
)
: Promise<UserEntity> {
return await this.userService.updateUserById(id, inputs)
}
我的 Dto 看起来像:
@InputType()
export class UpdateUserDto {
@Field({nullable: true})
@IsString()
fullName?: string;
@Field({nullable: true})
@IsEmail()
@IsString()
email?: string;
@Field({nullable: true, description: 'Password must be longer than or equal to 5 characters'})
@MinLength(5)
@IsString()
password?: string;
@Field({nullable: true})
@IsString()
phoneNumber?: string
}
当我想更新一个字段时:
mutation{
updateUserById(id:"847025d8-0e6f-4456-88f7-710b104db6ca",
inputs:{
fullName:"New name"
})
{
fullName
}
}
即使该字段可以为空,仍然会从工作装饰器中收到错误:
"errors": [
{
"message": {
"statusCode": 400,
"error": "Bad Request",
"message": [
{
"target": {
"fullName": "New name"
},
"constraints": {
"isString": "email must be a string",
"isEmail": "email must be an email"
}
},
如何解决?
解决方案
通过添加@IsOptional()
装饰器解决
推荐阅读
- swagger - Swashbuckle - 如何只省略 REST api 方法,而不是实体
- javascript - 在功能组件中存储非状态变量
- scipy - 对象之间的成对距离(Xarray)
- c# - StackExchange.Redis 如何订阅多个频道
- javascript - 在 Firefox 中使用 JavaScript 设置线性渐变
- excel - 将工作簿中具有相同列范围的工作表拆分为使用 VBA 代码分隔 Excel 文件
- arrays - 调用函数时出现不兼容的类型错误
- c - 显示本地时间和 UTC 时间
- java - 如何将数组初始化为空?
- elixir - 有没有更简单的方法来获取与函数匹配的列表中的第一个项目,并返回该项目以及没有该项目的列表?