如何在Nest.js中验证Optionnal参数?

时间:2019-04-24 08:47:17

标签: validation nestjs

我正在尝试验证我的PUT用户/端点的参数,但是我希望所有参数都是可选的,但是一旦api使用者填写了这些参数,我就想对其进行验证。

我尝试将@ApiModelProperty必需的false与验证管道装饰器组合在一起,但是验证管道接管了ApiModelProperty (发送400个错误请求HttpException非常正常)

这是我的DTO: usersUpdate.dto.ts

import { IsEmail, IsEnum, IsPhoneNumber, IsEmpty } from 'class-validator';
import { ApiModelProperty } from '@nestjs/swagger';
import { RoleType } from './role-type.enum';

class UserInfo {
    @ApiModelProperty({ description: 'User firstname', required: false  })
    readonly firstname: string;

    @ApiModelProperty({ description: 'User lastname', required: false  })
    readonly lastname: string;

    @ApiModelProperty({description: 'User postal address', required: false })
    readonly address: string;

    @ApiModelProperty({ description: 'User phone number', required: false  })
    @IsPhoneNumber('FR')
    readonly phone: string;

    @ApiModelProperty({ description: 'User siret number', required: false  })
    readonly siret: string;
}

export class UpdateUserDTO {
    @ApiModelProperty({ description: 'User email address', required: false })
    readonly email: string;

    @ApiModelProperty({ description: 'User password', required: false })
    readonly password: string;

    @ApiModelProperty({ description: 'User informations', required: false })
    readonly user_info: UserInfo;

    @ApiModelProperty({ enum: ['Admin', 'Runner', 'Dispatcher'], description:'User role',  required: false })
    readonly role: RoleType;

    @ApiModelProperty({ description: 'User activation token', required: false })
    readonly activationToken: string;
}

我想在电子邮件参数上使用和@IsEmail()装饰器,并且在角色中使用@Enum()装饰器,但是一旦使用这些装饰器,该参数就是必填项。

有人知道如何在参数为空的情况下跳过此验证器吗?

1 个答案:

答案 0 :(得分:1)

是的,您可以通过应用 class-validator 中的@IsOptional装饰器使字段中的验证器为可选。

您可以代替@ApiModelProperty({ description: 'User activation token', required: false })使用的另一种方法是@ApiModelPropertyOptional,从而删除声明的 必需的:false 部分。

最后,您甚至可以使用@IsEmail()装饰器来验证email字段是否为电子邮件,最后可以做的是将@IsNotEmpty()@IsOptional()组合在一起,以便当字段为前提是它不能为空。

提供的代码如下:

    import { IsEmail, IsEnum, IsPhoneNumber, IsEmpty, IsOptional } from 'class-validator';
    import { ApiModelPropertyOptional } from '@nestjs/swagger';
    import { RoleType } from './role-type.enum';

    class UserInfo {
        @ApiModelPropertyOptional({ description: 'User firstname' })
        @IsOptional()
        readonly firstname: string;

        @ApiModelPropertyOptional({ description: 'User lastname' })
        @IsOptional()
        readonly lastname: string;

        @ApiModelPropertyOptional({description: 'User postal address' })
        @IsOptional()
        readonly address: string;

        @ApiModelPropertyOptional({ description: 'User phone number'  })
        @IsOptional()
        @IsPhoneNumber('FR')
        readonly phone: string;

        @ApiModelPropertyOptional({ description: 'User siret number'  })
        @IsOptional()
        readonly siret: string;
    }

    export class UpdateUserDTO {
        @ApiModelPropertyOptional({ description: 'User email address' })
        @IsEmail()
        @IsOptional()
        readonly email: string;

        @ApiModelPropertyOptional({ description: 'User password' })
        @IsOptional()
        readonly password: string;

        @ApiModelPropertyOptional({ description: 'User informations' })
        @IsOptional()
        readonly user_info: UserInfo;

        @ApiModelPropertyOptional({ enum: ['Admin', 'Runner', 'Dispatcher'], description:'User role' })
        @IsEnum(RoleType)
        @IsOptional()
        readonly role: RoleType;

        @ApiModelPropertyOptional({ description: 'User activation token' })
        @IsOptional()
        readonly activationToken: string;
    }

让我知道是否有帮助;)